api.c 1.8 MB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582
  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 testDevId = WOLFSSL_CAAM_DEVID;
  388. #else
  389. static int testDevId = 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. int ret = 0;
  1273. const byte* der;
  1274. int derSz = 0;
  1275. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  1276. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1277. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1278. fclose(file1);
  1279. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1280. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1281. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1282. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1283. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1284. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1285. * and full OpenSSL compatibility */
  1286. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1287. #else
  1288. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1289. #endif
  1290. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1291. "./certs/ca-cert.pem", NULL));
  1292. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1293. for (i = 0; i < sk_X509_num(sk); i++) {
  1294. x509 = sk_X509_value(sk, i);
  1295. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1296. #ifdef DEBUG_WOLFSSL_VERBOSE
  1297. bio = BIO_new(wolfSSL_BIO_s_file());
  1298. if (bio != NULL) {
  1299. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1300. X509_print(bio, x509);
  1301. BIO_free(bio);
  1302. }
  1303. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1304. }
  1305. wolfSSL_X509_free(cert1);
  1306. sk_X509_pop_free(sk, NULL);
  1307. wolfSSL_CertManagerFree(cm);
  1308. printf(resultFmt, passed);
  1309. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1310. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1311. defined(WOLFSSL_SIGNER_DER_CERT) */
  1312. return 0;
  1313. }
  1314. static int test_wolfSSL_CertManagerSetVerify(void)
  1315. {
  1316. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1317. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1318. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1319. int ret = 0;
  1320. WOLFSSL_CERT_MANAGER* cm;
  1321. int tmp = myVerifyAction;
  1322. const char* ca_cert = "./certs/ca-cert.pem";
  1323. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1324. cm = wolfSSL_CertManagerNew();
  1325. AssertNotNull(cm);
  1326. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1327. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1328. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1329. AssertIntEQ(ret, -1);
  1330. #else
  1331. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1332. #endif
  1333. /* Use the test CB that always accepts certs */
  1334. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1335. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1336. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1337. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1338. {
  1339. const char* verifyCert = "./certs/server-cert.pem";
  1340. /* Use the test CB that always fails certs */
  1341. myVerifyAction = VERIFY_FORCE_FAIL;
  1342. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1343. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1344. }
  1345. #endif
  1346. wolfSSL_CertManagerFree(cm);
  1347. myVerifyAction = tmp;
  1348. #endif
  1349. return 0;
  1350. }
  1351. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1352. defined(DEBUG_UNIT_TEST_CERTS)
  1353. /* Used when debugging name constraint tests. Not static to allow use in
  1354. * multiple locations with complex define guards. */
  1355. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1356. {
  1357. BIO* out = BIO_new_file(fileName, "wb");
  1358. if (out != NULL) {
  1359. PEM_write_bio_X509(out, x509);
  1360. BIO_free(out);
  1361. }
  1362. }
  1363. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1364. {
  1365. BIO* out = BIO_new_file(fileName, "wb");
  1366. if (out != NULL) {
  1367. BIO_write(out, der, derSz);
  1368. BIO_free(out);
  1369. }
  1370. }
  1371. #else
  1372. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1373. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1374. #endif
  1375. static int test_wolfSSL_CertManagerNameConstraint(void)
  1376. {
  1377. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1378. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1379. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1380. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1381. !defined(NO_SHA256)
  1382. WOLFSSL_CERT_MANAGER* cm;
  1383. WOLFSSL_EVP_PKEY *priv;
  1384. WOLFSSL_X509_NAME* name;
  1385. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1386. const char* server_cert = "./certs/test/server-goodcn.pem";
  1387. int i = 0;
  1388. static const byte extNameConsOid[] = {85, 29, 30};
  1389. RsaKey key;
  1390. WC_RNG rng;
  1391. byte *der;
  1392. int derSz;
  1393. word32 idx = 0;
  1394. byte *pt;
  1395. WOLFSSL_X509 *x509, *ca;
  1396. wc_InitRng(&rng);
  1397. /* load in CA private key for signing */
  1398. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1399. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1400. sizeof_server_key_der_2048), 0);
  1401. /* get ca certificate then alter it */
  1402. AssertNotNull(der =
  1403. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1404. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1405. WOLFSSL_FILETYPE_ASN1));
  1406. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1407. XMEMCPY(der, pt, derSz);
  1408. /* find the name constraint extension and alter it */
  1409. pt = der;
  1410. for (i = 0; i < derSz - 3; i++) {
  1411. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1412. pt += 3;
  1413. break;
  1414. }
  1415. pt++;
  1416. }
  1417. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1418. /* go to the length value and set it to 0 */
  1419. while (i < derSz && *pt != 0x81) {
  1420. pt++;
  1421. i++;
  1422. }
  1423. AssertIntNE(i, derSz); /* did not place to alter */
  1424. pt++;
  1425. *pt = 0x00;
  1426. /* resign the altered certificate */
  1427. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1428. FOURK_BUF, &key, NULL, &rng)), 0);
  1429. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1430. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1431. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1432. wolfSSL_CertManagerFree(cm);
  1433. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1434. wolfSSL_X509_free(x509);
  1435. wc_FreeRsaKey(&key);
  1436. wc_FreeRng(&rng);
  1437. /* add email alt name to satisfy constraint */
  1438. pt = (byte*)server_key_der_2048;
  1439. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1440. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1441. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1442. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1443. WOLFSSL_FILETYPE_ASN1));
  1444. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1445. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1446. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1447. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1448. /* Good cert test with proper alt email name */
  1449. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1450. WOLFSSL_FILETYPE_PEM));
  1451. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1452. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1453. AssertNotNull(name = X509_NAME_new());
  1454. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1455. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1456. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1457. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1458. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1459. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1460. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1461. X509_NAME_free(name);
  1462. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1463. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1464. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1465. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1466. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1467. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1468. wolfSSL_X509_free(x509);
  1469. /* Cert with bad alt name list */
  1470. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1471. WOLFSSL_FILETYPE_PEM));
  1472. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1473. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1474. AssertNotNull(name = X509_NAME_new());
  1475. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1476. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1477. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1478. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1479. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1480. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1481. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1482. X509_NAME_free(name);
  1483. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1484. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1485. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1486. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1487. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1488. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1489. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1490. wolfSSL_CertManagerFree(cm);
  1491. wolfSSL_X509_free(x509);
  1492. wolfSSL_X509_free(ca);
  1493. wolfSSL_EVP_PKEY_free(priv);
  1494. #endif
  1495. return 0;
  1496. }
  1497. static int test_wolfSSL_CertManagerNameConstraint2(void)
  1498. {
  1499. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1500. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1501. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1502. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1503. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1504. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1505. const char* server_cert = "./certs/server-cert.pem";
  1506. WOLFSSL_CERT_MANAGER* cm;
  1507. WOLFSSL_X509 *x509, *ca;
  1508. const unsigned char *der;
  1509. const unsigned char *pt;
  1510. WOLFSSL_EVP_PKEY *priv;
  1511. WOLFSSL_X509_NAME* name;
  1512. int derSz;
  1513. /* C=US*/
  1514. char altName[] = {
  1515. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1516. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1517. };
  1518. /* C=ID */
  1519. char altNameFail[] = {
  1520. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1521. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1522. };
  1523. /* C=US ST=California*/
  1524. char altNameExc[] = {
  1525. 0x30, 0x22,
  1526. 0x31, 0x0B,
  1527. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1528. 0x31, 0x13,
  1529. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1530. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1531. };
  1532. /* load in CA private key for signing */
  1533. pt = ca_key_der_2048;
  1534. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1535. sizeof_ca_key_der_2048));
  1536. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1537. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1538. WOLFSSL_FILETYPE_ASN1));
  1539. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1540. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1541. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1542. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1543. WOLFSSL_FILETYPE_PEM));
  1544. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1545. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1546. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1547. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1548. #else
  1549. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1550. #endif
  1551. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1552. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1553. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1554. /* add in matching DIR alt name and resign */
  1555. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1556. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1557. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1558. #else
  1559. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1560. #endif
  1561. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1562. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1563. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1564. wolfSSL_X509_free(x509);
  1565. /* check verify fail */
  1566. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1567. WOLFSSL_FILETYPE_PEM));
  1568. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1569. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1570. /* add in miss matching DIR alt name and resign */
  1571. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1572. ASN_DIR_TYPE);
  1573. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1574. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1575. #else
  1576. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1577. #endif
  1578. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1579. #ifndef WOLFSSL_NO_ASN_STRICT
  1580. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1581. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1582. #else
  1583. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1584. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1585. #endif
  1586. /* check that it still fails if one bad altname and one good altname is in
  1587. * the certificate */
  1588. wolfSSL_X509_free(x509);
  1589. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1590. WOLFSSL_FILETYPE_PEM));
  1591. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1592. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1593. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1594. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1595. ASN_DIR_TYPE);
  1596. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1597. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1598. #else
  1599. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1600. #endif
  1601. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1602. #ifndef WOLFSSL_NO_ASN_STRICT
  1603. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1604. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1605. #else
  1606. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1607. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1608. #endif
  1609. /* check it fails with switching position of bad altname */
  1610. wolfSSL_X509_free(x509);
  1611. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1612. WOLFSSL_FILETYPE_PEM));
  1613. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1614. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1615. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1616. ASN_DIR_TYPE);
  1617. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1618. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1619. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1620. #else
  1621. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1622. #endif
  1623. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1624. #ifndef WOLFSSL_NO_ASN_STRICT
  1625. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1626. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1627. #else
  1628. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1629. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1630. #endif
  1631. wolfSSL_CertManagerFree(cm);
  1632. wolfSSL_X509_free(x509);
  1633. wolfSSL_X509_free(ca);
  1634. /* now test with excluded name constraint */
  1635. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1636. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  1637. WOLFSSL_FILETYPE_ASN1));
  1638. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1639. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1640. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1641. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1642. WOLFSSL_FILETYPE_PEM));
  1643. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  1644. ASN_DIR_TYPE);
  1645. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1646. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1647. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1648. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1649. #else
  1650. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1651. #endif
  1652. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1653. #ifndef WOLFSSL_NO_ASN_STRICT
  1654. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1655. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1656. #else
  1657. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1658. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1659. #endif
  1660. wolfSSL_CertManagerFree(cm);
  1661. wolfSSL_X509_free(x509);
  1662. wolfSSL_X509_free(ca);
  1663. wolfSSL_EVP_PKEY_free(priv);
  1664. #endif
  1665. return 0;
  1666. }
  1667. static int test_wolfSSL_CertManagerNameConstraint3(void)
  1668. {
  1669. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1670. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1671. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1672. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1673. !defined(NO_SHA256)
  1674. WOLFSSL_CERT_MANAGER* cm;
  1675. WOLFSSL_EVP_PKEY *priv;
  1676. WOLFSSL_X509_NAME* name;
  1677. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  1678. const char* server_cert = "./certs/test/server-goodcn.pem";
  1679. byte *der;
  1680. int derSz;
  1681. byte *pt;
  1682. WOLFSSL_X509 *x509, *ca;
  1683. pt = (byte*)server_key_der_2048;
  1684. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1685. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1686. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1687. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1688. WOLFSSL_FILETYPE_ASN1));
  1689. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1690. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1691. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1692. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1693. /* check satisfying .wolfssl.com constraint passes */
  1694. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1695. WOLFSSL_FILETYPE_PEM));
  1696. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1697. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1698. AssertNotNull(name = X509_NAME_new());
  1699. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1700. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1701. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1702. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1703. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1704. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1705. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1706. X509_NAME_free(name);
  1707. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1708. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1709. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1710. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1711. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1712. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1713. wolfSSL_X509_free(x509);
  1714. /* check satisfying .random.com constraint passes */
  1715. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1716. WOLFSSL_FILETYPE_PEM));
  1717. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1718. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1719. AssertNotNull(name = X509_NAME_new());
  1720. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1721. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1722. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1723. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1724. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1725. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  1726. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1727. X509_NAME_free(name);
  1728. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  1729. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1730. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1731. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1732. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1733. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1734. wolfSSL_X509_free(x509);
  1735. /* check fail case when neither constraint is matched */
  1736. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1737. WOLFSSL_FILETYPE_PEM));
  1738. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1739. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1740. AssertNotNull(name = X509_NAME_new());
  1741. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1742. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1743. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1744. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1745. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1746. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  1747. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1748. X509_NAME_free(name);
  1749. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1750. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1751. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1752. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1753. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1754. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1755. wolfSSL_CertManagerFree(cm);
  1756. wolfSSL_X509_free(x509);
  1757. wolfSSL_X509_free(ca);
  1758. wolfSSL_EVP_PKEY_free(priv);
  1759. #endif
  1760. return 0;
  1761. }
  1762. static int test_wolfSSL_CertManagerNameConstraint4(void)
  1763. {
  1764. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1765. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1766. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1767. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1768. !defined(NO_SHA256)
  1769. WOLFSSL_CERT_MANAGER* cm;
  1770. WOLFSSL_EVP_PKEY *priv;
  1771. WOLFSSL_X509_NAME* name;
  1772. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  1773. const char* server_cert = "./certs/test/server-goodcn.pem";
  1774. byte *der;
  1775. int derSz;
  1776. byte *pt;
  1777. WOLFSSL_X509 *x509, *ca;
  1778. pt = (byte*)server_key_der_2048;
  1779. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1780. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1781. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1782. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1783. WOLFSSL_FILETYPE_ASN1));
  1784. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1785. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1786. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1787. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1788. /* check satisfying wolfssl.com constraint passes */
  1789. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1790. WOLFSSL_FILETYPE_PEM));
  1791. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1792. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1793. AssertNotNull(name = X509_NAME_new());
  1794. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1795. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1796. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1797. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1798. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1799. X509_NAME_free(name);
  1800. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1801. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1802. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  1803. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1804. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1805. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1806. wolfSSL_X509_free(x509);
  1807. /* check satisfying example.com constraint passes */
  1808. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1809. WOLFSSL_FILETYPE_PEM));
  1810. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1811. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1812. AssertNotNull(name = X509_NAME_new());
  1813. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1814. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1815. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1816. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  1817. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1818. X509_NAME_free(name);
  1819. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  1820. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1821. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  1822. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1823. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1824. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1825. wolfSSL_X509_free(x509);
  1826. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  1827. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1828. WOLFSSL_FILETYPE_PEM));
  1829. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1830. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1831. AssertNotNull(name = X509_NAME_new());
  1832. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1833. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1834. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1835. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1836. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1837. X509_NAME_free(name);
  1838. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1839. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1840. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  1841. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1842. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  1843. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1844. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1845. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1846. wolfSSL_X509_free(x509);
  1847. /* check fail when one DNS in the list is bad */
  1848. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1849. WOLFSSL_FILETYPE_PEM));
  1850. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1851. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1852. AssertNotNull(name = X509_NAME_new());
  1853. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1854. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1855. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1856. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1857. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1858. X509_NAME_free(name);
  1859. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  1860. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  1861. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  1862. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1863. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  1864. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1865. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1866. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1867. wolfSSL_X509_free(x509);
  1868. /* check fail case when neither constraint is matched */
  1869. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1870. WOLFSSL_FILETYPE_PEM));
  1871. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1872. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1873. AssertNotNull(name = X509_NAME_new());
  1874. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1875. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1876. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1877. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  1878. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1879. X509_NAME_free(name);
  1880. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  1881. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1882. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1883. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1884. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1885. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1886. wolfSSL_CertManagerFree(cm);
  1887. wolfSSL_X509_free(x509);
  1888. wolfSSL_X509_free(ca);
  1889. wolfSSL_EVP_PKEY_free(priv);
  1890. #endif
  1891. return 0;
  1892. }
  1893. static int test_wolfSSL_CertManagerNameConstraint5(void)
  1894. {
  1895. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1896. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1897. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1898. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1899. !defined(NO_SHA256)
  1900. WOLFSSL_CERT_MANAGER* cm;
  1901. WOLFSSL_EVP_PKEY *priv;
  1902. WOLFSSL_X509_NAME* name;
  1903. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  1904. const char* server_cert = "./certs/test/server-goodcn.pem";
  1905. byte *der;
  1906. int derSz;
  1907. byte *pt;
  1908. WOLFSSL_X509 *x509, *ca;
  1909. pt = (byte*)server_key_der_2048;
  1910. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1911. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1912. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1913. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1914. WOLFSSL_FILETYPE_ASN1));
  1915. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1916. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1917. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1918. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1919. /* check satisfying wolfssl.com constraint passes */
  1920. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1921. WOLFSSL_FILETYPE_PEM));
  1922. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1923. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1924. AssertNotNull(name = X509_NAME_new());
  1925. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1926. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1927. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1928. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  1929. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1930. X509_NAME_free(name);
  1931. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  1932. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  1933. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1934. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1935. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1936. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1937. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1938. wolfSSL_X509_free(x509);
  1939. /* fail with DNS check because of common name */
  1940. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1941. WOLFSSL_FILETYPE_PEM));
  1942. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1943. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1944. AssertNotNull(name = X509_NAME_new());
  1945. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1946. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1947. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1948. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1949. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1950. X509_NAME_free(name);
  1951. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1952. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  1953. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1954. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  1955. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1956. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1957. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1958. wolfSSL_X509_free(x509);
  1959. /* fail on permitted DNS name constraint */
  1960. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1961. WOLFSSL_FILETYPE_PEM));
  1962. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1963. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1964. AssertNotNull(name = X509_NAME_new());
  1965. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1966. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1967. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1968. X509_NAME_free(name);
  1969. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  1970. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  1971. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1972. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1973. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  1974. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1975. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1976. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1977. wolfSSL_X509_free(x509);
  1978. /* fail on permitted email name constraint */
  1979. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1980. WOLFSSL_FILETYPE_PEM));
  1981. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1982. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1983. AssertNotNull(name = X509_NAME_new());
  1984. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1985. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1986. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1987. X509_NAME_free(name);
  1988. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  1989. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  1990. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  1991. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1992. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  1993. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1994. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1995. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1996. wolfSSL_X509_free(x509);
  1997. /* success with empty email name */
  1998. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1999. WOLFSSL_FILETYPE_PEM));
  2000. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2001. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2002. AssertNotNull(name = X509_NAME_new());
  2003. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2004. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2005. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2006. X509_NAME_free(name);
  2007. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2008. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2009. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2010. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2011. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2012. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2013. wolfSSL_X509_free(x509);
  2014. wolfSSL_CertManagerFree(cm);
  2015. wolfSSL_X509_free(ca);
  2016. wolfSSL_EVP_PKEY_free(priv);
  2017. #endif
  2018. return 0;
  2019. }
  2020. static int test_wolfSSL_FPKI(void)
  2021. {
  2022. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2023. XFILE f;
  2024. const char* fpkiCert = "./certs/fpki-cert.der";
  2025. DecodedCert cert;
  2026. byte buf[4096];
  2027. byte* uuid;
  2028. byte* fascn;
  2029. word32 fascnSz;
  2030. word32 uuidSz;
  2031. int bytes;
  2032. printf(testingFmt, "test_wolfSSL_FPKI");
  2033. f = XFOPEN(fpkiCert, "rb");
  2034. AssertTrue((f != XBADFILE));
  2035. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2036. XFCLOSE(f);
  2037. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2038. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2039. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2040. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2041. AssertNotNull(fascn);
  2042. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2043. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2044. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2045. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2046. AssertNotNull(uuid);
  2047. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2048. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2049. wc_FreeDecodedCert(&cert);
  2050. printf(resultFmt, passed);
  2051. #endif
  2052. return 0;
  2053. }
  2054. static int test_wolfSSL_CertRsaPss(void)
  2055. {
  2056. /* FIPS v2 and below don't support long salts. */
  2057. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2058. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2059. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2060. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2061. XFILE f;
  2062. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2063. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2064. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2065. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2066. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2067. #endif
  2068. DecodedCert cert;
  2069. byte buf[4096];
  2070. int bytes;
  2071. WOLFSSL_CERT_MANAGER* cm;
  2072. printf(testingFmt, "test_CertRsaPss");
  2073. cm = wolfSSL_CertManagerNew();
  2074. AssertNotNull(cm);
  2075. AssertIntEQ(WOLFSSL_SUCCESS,
  2076. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2077. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2078. AssertIntEQ(WOLFSSL_SUCCESS,
  2079. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2080. #endif
  2081. f = XFOPEN(rsaPssSha256Cert, "rb");
  2082. AssertTrue((f != XBADFILE));
  2083. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2084. XFCLOSE(f);
  2085. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2086. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2087. wc_FreeDecodedCert(&cert);
  2088. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2089. RSA_MAX_SIZE >= 3072
  2090. f = XFOPEN(rsaPssSha384Cert, "rb");
  2091. AssertTrue((f != XBADFILE));
  2092. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2093. XFCLOSE(f);
  2094. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2095. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2096. wc_FreeDecodedCert(&cert);
  2097. #endif
  2098. wolfSSL_CertManagerFree(cm);
  2099. printf(resultFmt, passed);
  2100. #endif
  2101. return 0;
  2102. }
  2103. static int test_wolfSSL_CertManagerCRL(void)
  2104. {
  2105. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2106. !defined(NO_RSA)
  2107. const char* ca_cert = "./certs/ca-cert.pem";
  2108. const char* crl1 = "./certs/crl/crl.pem";
  2109. const char* crl2 = "./certs/crl/crl2.pem";
  2110. WOLFSSL_CERT_MANAGER* cm = NULL;
  2111. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2112. AssertIntEQ(WOLFSSL_SUCCESS,
  2113. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2114. AssertIntEQ(WOLFSSL_SUCCESS,
  2115. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2116. AssertIntEQ(WOLFSSL_SUCCESS,
  2117. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2118. wolfSSL_CertManagerFreeCRL(cm);
  2119. AssertIntEQ(WOLFSSL_SUCCESS,
  2120. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2121. AssertIntEQ(WOLFSSL_SUCCESS,
  2122. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2123. wolfSSL_CertManagerFree(cm);
  2124. #endif
  2125. return 0;
  2126. }
  2127. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2128. {
  2129. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2130. !defined(NO_WOLFSSL_CLIENT)
  2131. WOLFSSL_CTX* ctx;
  2132. const char* ca_cert = "./certs/ca-cert.pem";
  2133. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2134. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2135. AssertNotNull(ctx);
  2136. /* test good CA */
  2137. AssertTrue(WOLFSSL_SUCCESS ==
  2138. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2139. WOLFSSL_LOAD_FLAG_NONE));
  2140. /* test expired CA */
  2141. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2142. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2143. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2144. #else
  2145. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2146. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2147. #endif
  2148. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2149. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2150. wolfSSL_CTX_free(ctx);
  2151. #endif
  2152. return 0;
  2153. }
  2154. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2155. {
  2156. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2157. defined(USE_CERT_BUFFERS_2048)
  2158. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2159. WOLFSSL_CTX* ctx;
  2160. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2161. byte ca_expired_cert[TWOK_BUF];
  2162. word32 sizeof_ca_expired_cert;
  2163. XFILE fp;
  2164. #ifndef NO_WOLFSSL_CLIENT
  2165. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2166. #else
  2167. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2168. #endif
  2169. AssertNotNull(ctx);
  2170. /* test good CA */
  2171. AssertTrue(WOLFSSL_SUCCESS ==
  2172. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2173. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2174. WOLFSSL_LOAD_FLAG_NONE));
  2175. /* load expired CA */
  2176. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2177. fp = XFOPEN(ca_expired_cert_file, "rb");
  2178. AssertTrue(fp != XBADFILE);
  2179. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2180. sizeof(ca_expired_cert), fp);
  2181. XFCLOSE(fp);
  2182. /* test expired CA failure */
  2183. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2184. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2185. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2186. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2187. #else
  2188. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2189. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2190. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2191. #endif
  2192. /* test expired CA success */
  2193. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2194. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2195. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2196. wolfSSL_CTX_free(ctx);
  2197. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2198. #endif
  2199. return 0;
  2200. }
  2201. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2202. {
  2203. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2204. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2205. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2206. WOLFSSL_CTX* ctx;
  2207. #ifndef NO_WOLFSSL_CLIENT
  2208. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2209. #else
  2210. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2211. #endif
  2212. AssertTrue(WOLFSSL_SUCCESS ==
  2213. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  2214. sizeof_ca_cert_chain_der,
  2215. WOLFSSL_FILETYPE_ASN1));
  2216. wolfSSL_CTX_free(ctx);
  2217. #endif
  2218. return 0;
  2219. }
  2220. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2221. {
  2222. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2223. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2224. WOLFSSL_CTX* ctx;
  2225. WOLFSSL* ssl;
  2226. const char *certChain[] = {
  2227. "./certs/intermediate/client-int-cert.pem",
  2228. "./certs/intermediate/ca-int2-cert.pem",
  2229. "./certs/intermediate/ca-int-cert.pem",
  2230. "./certs/ca-cert.pem",
  2231. NULL
  2232. };
  2233. const char** cert;
  2234. WOLFSSL_X509* x509;
  2235. WOLF_STACK_OF(X509)* chain = NULL;
  2236. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2237. AssertNotNull(ssl = wolfSSL_new(ctx));
  2238. for (cert = certChain; *cert != NULL; cert++) {
  2239. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2240. AssertNotNull(x509);
  2241. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2242. X509_free(x509);
  2243. }
  2244. for (cert = certChain; *cert != NULL; cert++) {
  2245. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2246. AssertNotNull(x509);
  2247. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2248. X509_free(x509);
  2249. }
  2250. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2251. AssertIntEQ(sk_X509_num(chain), 3);
  2252. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2253. AssertIntEQ(sk_X509_num(chain), 3);
  2254. SSL_free(ssl);
  2255. SSL_CTX_free(ctx);
  2256. #endif
  2257. return 0;
  2258. }
  2259. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2260. {
  2261. int ret = 0;
  2262. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2263. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2264. const char* server_chain_der = "./certs/server-cert-chain.der";
  2265. const char* client_single_pem = "./certs/client-cert.pem";
  2266. WOLFSSL_CTX* ctx;
  2267. (void)server_chain_der;
  2268. (void)client_single_pem;
  2269. (void)ctx;
  2270. #ifndef NO_WOLFSSL_CLIENT
  2271. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2272. AssertNotNull(ctx);
  2273. #else
  2274. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2275. AssertNotNull(ctx);
  2276. #endif
  2277. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2278. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2279. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2280. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2281. wolfSSL_CTX_free(ctx);
  2282. #endif
  2283. return ret;
  2284. }
  2285. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2286. {
  2287. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2288. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2289. WOLFSSL_CTX *ctx;
  2290. (void)ctx;
  2291. #ifndef NO_WOLFSSL_CLIENT
  2292. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2293. #else
  2294. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2295. #endif
  2296. /* invalid context */
  2297. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2298. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2299. /* invalid dhParamFile file */
  2300. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2301. NULL, WOLFSSL_FILETYPE_PEM));
  2302. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2303. bogusFile, WOLFSSL_FILETYPE_PEM));
  2304. /* success */
  2305. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2306. WOLFSSL_FILETYPE_PEM));
  2307. wolfSSL_CTX_free(ctx);
  2308. #endif
  2309. return 0;
  2310. }
  2311. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2312. {
  2313. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2314. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2315. WOLFSSL_CTX *ctx;
  2316. (void)ctx;
  2317. #ifndef NO_WOLFSSL_CLIENT
  2318. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2319. #else
  2320. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2321. #endif
  2322. /* invalid context */
  2323. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2324. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2325. /* invalid dhParamFile file */
  2326. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2327. 0, WOLFSSL_FILETYPE_ASN1));
  2328. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2329. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2330. /* success */
  2331. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2332. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2333. wolfSSL_CTX_free(ctx);
  2334. #endif
  2335. return 0;
  2336. }
  2337. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2338. {
  2339. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2340. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2341. WOLFSSL_CTX *ctx;
  2342. (void)ctx;
  2343. #ifndef NO_WOLFSSL_CLIENT
  2344. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2345. AssertNotNull(ctx);
  2346. #else
  2347. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2348. AssertNotNull(ctx);
  2349. #endif
  2350. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2351. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2352. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2353. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2354. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2355. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2356. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2357. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2358. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2359. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2360. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2361. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2362. wolfSSL_CTX_free(ctx);
  2363. #endif
  2364. return 0;
  2365. }
  2366. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2367. {
  2368. #if defined(WOLFSSL_DER_LOAD) && \
  2369. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2370. WOLFSSL_CTX* ctx = NULL;
  2371. const char* derCert = "./certs/server-cert.der";
  2372. const char* nullPath = NULL;
  2373. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2374. const char* emptyPath = "";
  2375. /* der load Case 1 ctx NULL */
  2376. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2377. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2378. #ifndef NO_WOLFSSL_CLIENT
  2379. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2380. #else
  2381. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2382. #endif
  2383. /* Case 2 filePath NULL */
  2384. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2385. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2386. /* Case 3 invalid format */
  2387. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2388. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2389. /* Case 4 filePath not valid */
  2390. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2391. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2392. /* Case 5 filePath empty */
  2393. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2394. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2395. #ifndef NO_RSA
  2396. /* Case 6 success case */
  2397. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2398. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2399. #endif
  2400. wolfSSL_CTX_free(ctx);
  2401. #endif
  2402. return 0;
  2403. }
  2404. static int test_wolfSSL_CTX_enable_disable(void)
  2405. {
  2406. #ifndef NO_CERTS
  2407. WOLFSSL_CTX* ctx = NULL;
  2408. #ifdef HAVE_CRL
  2409. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2410. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2411. #endif
  2412. #ifdef HAVE_OCSP
  2413. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2414. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2415. #endif
  2416. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2417. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2418. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2419. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2420. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2421. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2422. #endif
  2423. #ifndef NO_WOLFSSL_CLIENT
  2424. #ifdef HAVE_EXTENDED_MASTER
  2425. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2426. #endif
  2427. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2428. AssertNotNull(ctx);
  2429. #ifdef HAVE_EXTENDED_MASTER
  2430. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2431. #endif
  2432. #elif !defined(NO_WOLFSSL_SERVER)
  2433. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2434. #else
  2435. return 0;
  2436. #endif
  2437. #ifdef HAVE_CRL
  2438. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2439. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2440. #endif
  2441. #ifdef HAVE_OCSP
  2442. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2443. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2444. WOLFSSL_SUCCESS);
  2445. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2446. WOLFSSL_SUCCESS);
  2447. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2448. WOLFSSL_SUCCESS);
  2449. #endif
  2450. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2451. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2452. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2453. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2454. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2455. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2456. #endif
  2457. wolfSSL_CTX_free(ctx);
  2458. #endif /* NO_CERTS */
  2459. return 0;
  2460. }
  2461. static int test_wolfSSL_CTX_ticket_API(void)
  2462. {
  2463. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2464. WOLFSSL_CTX* ctx = NULL;
  2465. void *userCtx = (void*)"this is my ctx";
  2466. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2467. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2468. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2469. wolfSSL_CTX_free(ctx);
  2470. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2471. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2472. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2473. return 0;
  2474. }
  2475. static int test_wolfSSL_set_minmax_proto_version(void)
  2476. {
  2477. #ifdef OPENSSL_EXTRA
  2478. WOLFSSL_CTX *ctx;
  2479. WOLFSSL *ssl;
  2480. int ret;
  2481. (void)ret;
  2482. (void)ssl;
  2483. printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
  2484. #ifndef NO_WOLFSSL_CLIENT
  2485. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2486. AssertNotNull(ssl = wolfSSL_new(ctx));
  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. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2492. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2493. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2494. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2495. wolfSSL_free(ssl);
  2496. wolfSSL_CTX_free(ctx);
  2497. #endif
  2498. #ifndef NO_WOLFSSL_SERVER
  2499. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2500. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2501. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2502. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2503. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2504. wolfSSL_CTX_free(ctx);
  2505. #endif
  2506. printf(resultFmt, passed);
  2507. #endif
  2508. return 0;
  2509. }
  2510. /*----------------------------------------------------------------------------*
  2511. | SSL
  2512. *----------------------------------------------------------------------------*/
  2513. static int test_server_wolfSSL_new(void)
  2514. {
  2515. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2516. !defined(NO_WOLFSSL_SERVER)
  2517. WOLFSSL_CTX *ctx;
  2518. WOLFSSL_CTX *ctx_nocert;
  2519. WOLFSSL *ssl;
  2520. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2521. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2522. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2523. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2524. /* invalid context */
  2525. AssertNull(ssl = wolfSSL_new(NULL));
  2526. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2527. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2528. #endif
  2529. /* success */
  2530. AssertNotNull(ssl = wolfSSL_new(ctx));
  2531. wolfSSL_free(ssl);
  2532. wolfSSL_CTX_free(ctx);
  2533. wolfSSL_CTX_free(ctx_nocert);
  2534. #endif
  2535. return 0;
  2536. }
  2537. static int test_client_wolfSSL_new(void)
  2538. {
  2539. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2540. !defined(NO_WOLFSSL_CLIENT)
  2541. WOLFSSL_CTX *ctx;
  2542. WOLFSSL_CTX *ctx_nocert;
  2543. WOLFSSL *ssl;
  2544. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2545. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2546. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2547. /* invalid context */
  2548. AssertNull(ssl = wolfSSL_new(NULL));
  2549. /* success */
  2550. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2551. wolfSSL_free(ssl);
  2552. /* success */
  2553. AssertNotNull(ssl = wolfSSL_new(ctx));
  2554. wolfSSL_free(ssl);
  2555. wolfSSL_CTX_free(ctx);
  2556. wolfSSL_CTX_free(ctx_nocert);
  2557. #endif
  2558. return 0;
  2559. }
  2560. static int test_wolfSSL_SetTmpDH_file(void)
  2561. {
  2562. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2563. !defined(NO_WOLFSSL_SERVER)
  2564. WOLFSSL_CTX *ctx;
  2565. WOLFSSL *ssl;
  2566. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2567. #ifndef NO_RSA
  2568. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2569. WOLFSSL_FILETYPE_PEM));
  2570. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2571. WOLFSSL_FILETYPE_PEM));
  2572. #elif defined(HAVE_ECC)
  2573. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  2574. WOLFSSL_FILETYPE_PEM));
  2575. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  2576. WOLFSSL_FILETYPE_PEM));
  2577. #elif defined(HAVE_ED25519)
  2578. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  2579. WOLFSSL_FILETYPE_PEM));
  2580. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  2581. WOLFSSL_FILETYPE_PEM));
  2582. #elif defined(HAVE_ED448)
  2583. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  2584. WOLFSSL_FILETYPE_PEM));
  2585. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  2586. WOLFSSL_FILETYPE_PEM));
  2587. #endif
  2588. AssertNotNull(ssl = wolfSSL_new(ctx));
  2589. /* invalid ssl */
  2590. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  2591. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2592. /* invalid dhParamFile file */
  2593. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2594. NULL, WOLFSSL_FILETYPE_PEM));
  2595. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2596. bogusFile, WOLFSSL_FILETYPE_PEM));
  2597. /* success */
  2598. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  2599. WOLFSSL_FILETYPE_PEM));
  2600. wolfSSL_free(ssl);
  2601. wolfSSL_CTX_free(ctx);
  2602. #endif
  2603. return 0;
  2604. }
  2605. static int test_wolfSSL_SetTmpDH_buffer(void)
  2606. {
  2607. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2608. WOLFSSL_CTX *ctx;
  2609. WOLFSSL *ssl;
  2610. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2611. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2612. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2613. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2614. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2615. AssertNotNull(ssl = wolfSSL_new(ctx));
  2616. /* invalid ssl */
  2617. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2618. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2619. /* invalid dhParamFile file */
  2620. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  2621. 0, WOLFSSL_FILETYPE_ASN1));
  2622. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  2623. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2624. /* success */
  2625. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2626. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2627. wolfSSL_free(ssl);
  2628. wolfSSL_CTX_free(ctx);
  2629. #endif
  2630. return 0;
  2631. }
  2632. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  2633. {
  2634. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2635. WOLFSSL_CTX *ctx, *ctx2;
  2636. WOLFSSL *ssl, *ssl2;
  2637. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2638. AssertNotNull(ctx);
  2639. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2640. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2641. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2642. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2643. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2644. ssl = wolfSSL_new(ctx);
  2645. AssertNotNull(ssl);
  2646. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2647. AssertNotNull(ctx2);
  2648. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  2649. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2650. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  2651. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2652. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2653. ssl2 = wolfSSL_new(ctx2);
  2654. AssertNotNull(ssl2);
  2655. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2656. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2657. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  2658. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2659. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2660. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  2661. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2662. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2663. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2664. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2665. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  2666. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  2667. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2668. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  2669. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  2670. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2671. wolfSSL_free(ssl2);
  2672. wolfSSL_CTX_free(ctx2);
  2673. wolfSSL_free(ssl);
  2674. wolfSSL_CTX_free(ctx);
  2675. #endif
  2676. return 0;
  2677. }
  2678. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  2679. * allowed.
  2680. * POST: return 1 on success.
  2681. */
  2682. static int test_wolfSSL_SetMinVersion(void)
  2683. {
  2684. int failFlag = WOLFSSL_SUCCESS;
  2685. #ifndef NO_WOLFSSL_CLIENT
  2686. WOLFSSL_CTX* ctx;
  2687. WOLFSSL* ssl;
  2688. int itr;
  2689. #ifndef NO_OLD_TLS
  2690. const int versions[] = {
  2691. #ifdef WOLFSSL_ALLOW_TLSV10
  2692. WOLFSSL_TLSV1,
  2693. #endif
  2694. WOLFSSL_TLSV1_1,
  2695. WOLFSSL_TLSV1_2};
  2696. #elif !defined(WOLFSSL_NO_TLS12)
  2697. const int versions[] = { WOLFSSL_TLSV1_2 };
  2698. #else
  2699. const int versions[] = { WOLFSSL_TLSV1_3 };
  2700. #endif
  2701. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2702. ssl = wolfSSL_new(ctx);
  2703. printf(testingFmt, "wolfSSL_SetMinVersion()");
  2704. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  2705. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  2706. failFlag = WOLFSSL_FAILURE;
  2707. }
  2708. }
  2709. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  2710. wolfSSL_free(ssl);
  2711. wolfSSL_CTX_free(ctx);
  2712. #endif
  2713. if (failFlag == WOLFSSL_SUCCESS) {
  2714. failFlag = 0;
  2715. }
  2716. return failFlag;
  2717. } /* END test_wolfSSL_SetMinVersion */
  2718. /*----------------------------------------------------------------------------*
  2719. | EC
  2720. *----------------------------------------------------------------------------*/
  2721. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  2722. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  2723. */
  2724. #ifdef OPENSSL_EXTRA
  2725. static int test_wolfSSL_EC(void)
  2726. {
  2727. #if !defined(WOLFSSL_SP_MATH) && \
  2728. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  2729. #if defined(HAVE_ECC)
  2730. BN_CTX *ctx;
  2731. EC_GROUP *group;
  2732. EC_GROUP *group2;
  2733. EC_POINT *Gxy, *new_point, *set_point;
  2734. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  2735. BIGNUM *X, *Y;
  2736. BIGNUM *set_point_bn;
  2737. char* hexStr;
  2738. int group_bits;
  2739. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  2740. /* NISTP256R1 Gx/Gy */
  2741. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2742. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2743. #ifndef HAVE_SELFTEST
  2744. EC_POINT *tmp;
  2745. size_t bin_len;
  2746. unsigned char* buf = NULL;
  2747. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  2748. const unsigned char binUncompG[] = {
  2749. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2750. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2751. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2752. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  2753. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  2754. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  2755. };
  2756. #ifdef HAVE_COMP_KEY
  2757. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  2758. const unsigned char binCompG[] = {
  2759. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  2760. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  2761. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  2762. };
  2763. #endif
  2764. #endif
  2765. AssertNotNull(ctx = BN_CTX_new());
  2766. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2767. AssertNotNull(group2 = EC_GROUP_dup(group));
  2768. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  2769. AssertNotNull(Gxy = EC_POINT_new(group));
  2770. AssertNotNull(new_point = EC_POINT_new(group));
  2771. AssertNotNull(set_point = EC_POINT_new(group));
  2772. AssertNotNull(X = BN_new());
  2773. AssertNotNull(Y = BN_new());
  2774. AssertNotNull(set_point_bn = BN_new());
  2775. /* load test values */
  2776. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  2777. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  2778. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  2779. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  2780. /* populate coordinates for input point */
  2781. Gxy->X = Gx;
  2782. Gxy->Y = Gy;
  2783. Gxy->Z = Gz;
  2784. #ifndef HAVE_SELFTEST
  2785. /* perform point multiplication */
  2786. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  2787. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, 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. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  2792. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2793. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2794. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2795. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  2796. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2797. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2798. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2799. #else
  2800. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  2801. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2802. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  2803. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  2804. #endif
  2805. /* check if point X coordinate is zero */
  2806. AssertIntEQ(BN_is_zero(new_point->X), 0);
  2807. #ifdef USE_ECC_B_PARAM
  2808. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  2809. #endif /* USE_ECC_B_PARAM */
  2810. /* Force non-affine coordinates */
  2811. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2812. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  2813. new_point->inSet = 0;
  2814. /* extract the coordinates from point */
  2815. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2816. /* check if point X coordinate is zero */
  2817. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  2818. /* set the same X and Y points in another object */
  2819. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  2820. /* compare points as they should be the same */
  2821. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  2822. /* Test copying */
  2823. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  2824. /* Test inverting */
  2825. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  2826. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  2827. set_point_bn, ctx), set_point_bn);
  2828. /* check bn2hex */
  2829. hexStr = BN_bn2hex(k);
  2830. AssertStrEQ(hexStr, kTest);
  2831. #ifndef NO_FILESYSTEM
  2832. BN_print_fp(stdout, k);
  2833. printf("\n");
  2834. #endif
  2835. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2836. hexStr = BN_bn2hex(Gx);
  2837. AssertStrEQ(hexStr, kGx);
  2838. #ifndef NO_FILESYSTEM
  2839. BN_print_fp(stdout, Gx);
  2840. printf("\n");
  2841. #endif
  2842. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2843. hexStr = BN_bn2hex(Gy);
  2844. AssertStrEQ(hexStr, kGy);
  2845. #ifndef NO_FILESYSTEM
  2846. BN_print_fp(stdout, Gy);
  2847. printf("\n");
  2848. #endif
  2849. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2850. #ifndef HAVE_SELFTEST
  2851. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  2852. AssertStrEQ(hexStr, uncompG);
  2853. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2854. #ifdef HAVE_COMP_KEY
  2855. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  2856. AssertStrEQ(hexStr, compG);
  2857. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  2858. #endif
  2859. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  2860. AssertIntEQ(bin_len, sizeof(binUncompG));
  2861. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2862. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  2863. bin_len, ctx), bin_len);
  2864. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  2865. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2866. #ifdef HAVE_COMP_KEY
  2867. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  2868. AssertIntEQ(bin_len, sizeof(binCompG));
  2869. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  2870. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  2871. bin_len, ctx), bin_len);
  2872. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  2873. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  2874. #endif
  2875. AssertNotNull(tmp = EC_POINT_new(group));
  2876. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  2877. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2878. EC_POINT_free(tmp);
  2879. #ifdef HAVE_COMP_KEY
  2880. AssertNotNull(tmp = EC_POINT_new(group));
  2881. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  2882. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  2883. EC_POINT_free(tmp);
  2884. #endif
  2885. #endif
  2886. /* test BN_mod_add */
  2887. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  2888. (WOLFSSL_BIGNUM*)BN_value_one(),
  2889. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  2890. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  2891. /* cleanup */
  2892. BN_free(X);
  2893. BN_free(Y);
  2894. BN_free(k);
  2895. BN_free(set_point_bn);
  2896. EC_POINT_free(new_point);
  2897. EC_POINT_free(set_point);
  2898. EC_POINT_free(Gxy);
  2899. EC_GROUP_free(group);
  2900. EC_GROUP_free(group2);
  2901. BN_CTX_free(ctx);
  2902. #endif /* HAVE_ECC */
  2903. #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  2904. return 0;
  2905. }
  2906. #endif /* OPENSSL_EXTRA */
  2907. #ifndef NO_BIO
  2908. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  2909. {
  2910. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  2911. EC_GROUP *group;
  2912. BIO* bio;
  2913. AssertNotNull(bio = BIO_new(BIO_s_file()));
  2914. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  2915. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  2916. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  2917. EC_GROUP_free(group);
  2918. BIO_free(bio);
  2919. #endif /* HAVE_ECC */
  2920. return 0;
  2921. }
  2922. #endif /* !NO_BIO */
  2923. # if defined(OPENSSL_EXTRA)
  2924. static int test_wolfSSL_ECDSA_SIG(void)
  2925. {
  2926. #ifdef HAVE_ECC
  2927. WOLFSSL_ECDSA_SIG* sig = NULL;
  2928. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  2929. const unsigned char* cp;
  2930. unsigned char* p;
  2931. unsigned char outSig[8];
  2932. unsigned char sigData[8] =
  2933. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  2934. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  2935. AssertNull(sig);
  2936. cp = sigData;
  2937. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  2938. AssertIntEQ((cp == sigData + 8), 1);
  2939. cp = sigData;
  2940. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  2941. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  2942. AssertIntEQ((sig == sig2), 1);
  2943. cp = outSig;
  2944. p = outSig;
  2945. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  2946. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  2947. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  2948. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  2949. AssertIntEQ((p == outSig + 8), 1);
  2950. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  2951. wolfSSL_ECDSA_SIG_free(sig);
  2952. #endif /* HAVE_ECC */
  2953. return 0;
  2954. }
  2955. static int test_EC_i2d(void)
  2956. {
  2957. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  2958. EC_KEY *key;
  2959. EC_KEY *copy;
  2960. int len;
  2961. unsigned char *buf = NULL;
  2962. const unsigned char *tmp = NULL;
  2963. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2964. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2965. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  2966. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  2967. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2968. buf = NULL;
  2969. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  2970. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  2971. tmp = buf;
  2972. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  2973. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2974. buf = NULL;
  2975. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  2976. tmp = buf;
  2977. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  2978. AssertIntEQ(EC_KEY_check_key(key), 1);
  2979. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  2980. EC_KEY_free(key);
  2981. EC_KEY_free(copy);
  2982. #endif /* HAVE_ECC */
  2983. return 0;
  2984. }
  2985. static int test_ECDSA_size_sign(void)
  2986. {
  2987. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  2988. EC_KEY *key;
  2989. int id;
  2990. byte hash[WC_MAX_DIGEST_SIZE];
  2991. byte sig[ECC_MAX_SIG_SIZE];
  2992. unsigned int sigSz = sizeof(sig);
  2993. XMEMSET(hash, 123, sizeof(hash));
  2994. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  2995. AssertIntEQ(id, ECC_SECP256R1);
  2996. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  2997. AssertIntEQ(EC_KEY_generate_key(key), 1);
  2998. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  2999. AssertIntGE(ECDSA_size(key), sigSz);
  3000. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  3001. EC_KEY_free(key);
  3002. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  3003. return 0;
  3004. }
  3005. static int test_ED25519(void)
  3006. {
  3007. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3008. defined(WOLFSSL_KEY_GEN)
  3009. byte priv[ED25519_PRV_KEY_SIZE];
  3010. unsigned int privSz = (unsigned int)sizeof(priv);
  3011. byte pub[ED25519_PUB_KEY_SIZE];
  3012. unsigned int pubSz = (unsigned int)sizeof(pub);
  3013. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3014. const char* msg = TEST_STRING;
  3015. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3016. byte sig[ED25519_SIG_SIZE];
  3017. unsigned int sigSz = (unsigned int)sizeof(sig);
  3018. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3019. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3020. WOLFSSL_SUCCESS);
  3021. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3022. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3023. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3024. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3025. &sigSz), WOLFSSL_SUCCESS);
  3026. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3027. #ifdef HAVE_ED25519_VERIFY
  3028. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3029. sigSz), WOLFSSL_SUCCESS);
  3030. #endif /* HAVE_ED25519_VERIFY */
  3031. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3032. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3033. return 0;
  3034. }
  3035. static int test_ED448(void)
  3036. {
  3037. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3038. defined(WOLFSSL_KEY_GEN)
  3039. byte priv[ED448_PRV_KEY_SIZE];
  3040. unsigned int privSz = (unsigned int)sizeof(priv);
  3041. byte pub[ED448_PUB_KEY_SIZE];
  3042. unsigned int pubSz = (unsigned int)sizeof(pub);
  3043. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3044. const char* msg = TEST_STRING;
  3045. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3046. byte sig[ED448_SIG_SIZE];
  3047. unsigned int sigSz = (unsigned int)sizeof(sig);
  3048. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3049. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3050. WOLFSSL_SUCCESS);
  3051. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3052. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3053. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3054. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3055. &sigSz), WOLFSSL_SUCCESS);
  3056. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3057. #ifdef HAVE_ED448_VERIFY
  3058. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3059. sigSz), WOLFSSL_SUCCESS);
  3060. #endif /* HAVE_ED448_VERIFY */
  3061. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3062. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3063. return 0;
  3064. }
  3065. #endif /* OPENSSL_EXTRA */
  3066. #include <wolfssl/openssl/pem.h>
  3067. /*----------------------------------------------------------------------------*
  3068. | EVP
  3069. *----------------------------------------------------------------------------*/
  3070. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3071. {
  3072. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3073. WOLFSSL_BIO* rbio = NULL;
  3074. WOLFSSL_BIO* wbio = NULL;
  3075. WOLFSSL_EVP_PKEY* pkey = NULL;
  3076. char line[256] = { 0 };
  3077. char line1[256] = { 0 };
  3078. int i;
  3079. printf(testingFmt, "EVP_PKEY_print_public()");
  3080. /* test error cases */
  3081. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3082. /*
  3083. * test RSA public key print
  3084. * in this test, pass '3' for indent
  3085. */
  3086. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3087. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3088. sizeof_client_keypub_der_1024);
  3089. AssertNotNull(rbio);
  3090. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3091. AssertNotNull(pkey);
  3092. wbio = BIO_new(BIO_s_mem());
  3093. AssertNotNull(wbio);
  3094. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3095. BIO_gets(wbio, line, sizeof(line));
  3096. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3097. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3098. BIO_gets(wbio, line, sizeof(line));
  3099. strcpy(line1, " Modulus:\n");
  3100. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3101. BIO_gets(wbio, line, sizeof(line));
  3102. strcpy(line1, " 00:BC:73:0E:A8:49:F3:74:A2:A9:EF:18:A5:DA:55:\n");
  3103. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3104. /* skip to the end of modulus element*/
  3105. for( i = 0; i < 8 ;i++) {
  3106. BIO_gets(wbio, line, sizeof(line));
  3107. }
  3108. BIO_gets(wbio, line, sizeof(line));
  3109. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3110. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3111. /* should reach EOF */
  3112. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3113. EVP_PKEY_free(pkey);
  3114. pkey = NULL;
  3115. BIO_free(rbio);
  3116. BIO_free(wbio);
  3117. rbio = NULL;
  3118. wbio = NULL;
  3119. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3120. /*
  3121. * test DSA public key print
  3122. */
  3123. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3124. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3125. sizeof_dsa_pub_key_der_2048);
  3126. AssertNotNull(rbio);
  3127. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3128. AssertNotNull(pkey);
  3129. wbio = BIO_new(BIO_s_mem());
  3130. AssertNotNull(wbio);
  3131. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3132. BIO_gets(wbio, line, sizeof(line));
  3133. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3134. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3135. BIO_gets(wbio, line, sizeof(line));
  3136. strcpy(line1, "pub:\n");
  3137. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3138. BIO_gets(wbio, line, sizeof(line));
  3139. strcpy(line1,
  3140. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3141. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3142. /* skip to the end of pub element*/
  3143. for( i = 0; i < 17 ;i++) {
  3144. BIO_gets(wbio, line, sizeof(line));
  3145. }
  3146. BIO_gets(wbio, line, sizeof(line));
  3147. strcpy(line1, "P:\n");
  3148. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3149. /* skip to the end of P element*/
  3150. for( i = 0; i < 18 ;i++) {
  3151. BIO_gets(wbio, line, sizeof(line));
  3152. }
  3153. BIO_gets(wbio, line, sizeof(line));
  3154. strcpy(line1, "Q:\n");
  3155. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3156. /* skip to the end of Q element*/
  3157. for( i = 0; i < 3 ;i++) {
  3158. BIO_gets(wbio, line, sizeof(line));
  3159. }
  3160. BIO_gets(wbio, line, sizeof(line));
  3161. strcpy(line1, "G:\n");
  3162. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3163. /* skip to the end of G element*/
  3164. for( i = 0; i < 18 ;i++) {
  3165. BIO_gets(wbio, line, sizeof(line));
  3166. }
  3167. /* should reach EOF */
  3168. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3169. EVP_PKEY_free(pkey);
  3170. pkey = NULL;
  3171. BIO_free(rbio);
  3172. BIO_free(wbio);
  3173. rbio = NULL;
  3174. wbio = NULL;
  3175. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3176. /*
  3177. * test ECC public key print
  3178. */
  3179. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3180. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3181. sizeof_ecc_clikeypub_der_256);
  3182. AssertNotNull(rbio);
  3183. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3184. AssertNotNull(pkey);
  3185. wbio = BIO_new(BIO_s_mem());
  3186. AssertNotNull(wbio);
  3187. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3188. BIO_gets(wbio, line, sizeof(line));
  3189. strcpy(line1, "Public-Key: (256 bit)\n");
  3190. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3191. BIO_gets(wbio, line, sizeof(line));
  3192. strcpy(line1, "pub:\n");
  3193. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3194. BIO_gets(wbio, line, sizeof(line));
  3195. strcpy(line1,
  3196. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3197. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3198. /* skip to the end of pub element*/
  3199. for( i = 0; i < 4 ;i++) {
  3200. BIO_gets(wbio, line, sizeof(line));
  3201. }
  3202. BIO_gets(wbio, line, sizeof(line));
  3203. strcpy(line1, "ASN1 OID: prime256v1\n");
  3204. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3205. BIO_gets(wbio, line, sizeof(line));
  3206. strcpy(line1, "NIST CURVE: P-256\n");
  3207. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3208. /* should reach EOF */
  3209. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3210. EVP_PKEY_free(pkey);
  3211. pkey = NULL;
  3212. BIO_free(rbio);
  3213. BIO_free(wbio);
  3214. rbio = NULL;
  3215. wbio = NULL;
  3216. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3217. /*
  3218. * test DH public key print
  3219. */
  3220. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3221. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3222. sizeof_dh_pub_key_der_2048);
  3223. AssertNotNull(rbio);
  3224. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3225. AssertNotNull(pkey);
  3226. wbio = BIO_new(BIO_s_mem());
  3227. AssertNotNull(wbio);
  3228. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3229. BIO_gets(wbio, line, sizeof(line));
  3230. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3231. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3232. BIO_gets(wbio, line, sizeof(line));
  3233. strcpy(line1, "public-key:\n");
  3234. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3235. BIO_gets(wbio, line, sizeof(line));
  3236. strcpy(line1,
  3237. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3238. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3239. /* skip to the end of public-key element*/
  3240. for( i = 0; i < 17 ;i++) {
  3241. BIO_gets(wbio, line, sizeof(line));
  3242. }
  3243. BIO_gets(wbio, line, sizeof(line));
  3244. strcpy(line1, "prime:\n");
  3245. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3246. BIO_gets(wbio, line, sizeof(line));
  3247. strcpy(line1,
  3248. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3249. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3250. /* skip to the end of prime element*/
  3251. for( i = 0; i < 17 ;i++) {
  3252. BIO_gets(wbio, line, sizeof(line));
  3253. }
  3254. BIO_gets(wbio, line, sizeof(line));
  3255. strcpy(line1, "generator: 2 (0x02)\n");
  3256. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3257. /* should reach EOF */
  3258. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3259. EVP_PKEY_free(pkey);
  3260. pkey = NULL;
  3261. BIO_free(rbio);
  3262. BIO_free(wbio);
  3263. rbio = NULL;
  3264. wbio = NULL;
  3265. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3266. /* to prevent "unused variable" warning */
  3267. (void)pkey;
  3268. (void)wbio;
  3269. (void)rbio;
  3270. (void)line;
  3271. (void)line1;
  3272. (void)i;
  3273. printf(resultFmt, passed);
  3274. #endif /* OPENSSL_EXTRA */
  3275. return 0;
  3276. }
  3277. /* Test functions for base64 encode/decode */
  3278. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3279. {
  3280. #if defined(OPENSSL_EXTRA) && \
  3281. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3282. EVP_ENCODE_CTX* ctx = NULL;
  3283. printf(testingFmt, "EVP_ENCODE_CTX_new()");
  3284. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3285. AssertIntEQ( ctx->remaining,0);
  3286. AssertIntEQ( ctx->data[0],0);
  3287. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3288. EVP_ENCODE_CTX_free(ctx);
  3289. printf(resultFmt, passed);
  3290. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3291. return 0;
  3292. }
  3293. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3294. {
  3295. #if defined(OPENSSL_EXTRA) && \
  3296. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3297. EVP_ENCODE_CTX* ctx = NULL;
  3298. printf(testingFmt, "EVP_ENCODE_CTX_free()");
  3299. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3300. EVP_ENCODE_CTX_free(ctx);
  3301. printf(resultFmt, passed);
  3302. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3303. return 0;
  3304. }
  3305. static int test_wolfSSL_EVP_EncodeInit(void)
  3306. {
  3307. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3308. EVP_ENCODE_CTX* ctx = NULL;
  3309. printf(testingFmt, "EVP_EncodeInit()");
  3310. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3311. AssertIntEQ( ctx->remaining,0);
  3312. AssertIntEQ( ctx->data[0],0);
  3313. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3314. /* make ctx dirty */
  3315. ctx->remaining = 10;
  3316. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3317. EVP_EncodeInit(ctx);
  3318. AssertIntEQ( ctx->remaining,0);
  3319. AssertIntEQ( ctx->data[0],0);
  3320. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3321. EVP_ENCODE_CTX_free(ctx);
  3322. printf(resultFmt, passed);
  3323. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3324. return 0;
  3325. }
  3326. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3327. {
  3328. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3329. int outl;
  3330. int total;
  3331. const unsigned char plain0[] = {"Th"};
  3332. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3333. const unsigned char plain2[] = {"This is additional data."};
  3334. const unsigned char enc0[] = {"VGg=\n"};
  3335. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3336. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3337. const unsigned char enc2[] =
  3338. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3339. unsigned char encOutBuff[300];
  3340. EVP_ENCODE_CTX* ctx = NULL;
  3341. printf(testingFmt, "EVP_EncodeUpdate()");
  3342. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3343. EVP_EncodeInit(ctx);
  3344. /* illegal parameter test */
  3345. AssertIntEQ(
  3346. EVP_EncodeUpdate(
  3347. NULL, /* pass NULL as ctx */
  3348. encOutBuff,
  3349. &outl,
  3350. plain1,
  3351. sizeof(plain1)-1),
  3352. 0 /* expected result code 0: fail */
  3353. );
  3354. AssertIntEQ(
  3355. EVP_EncodeUpdate(
  3356. ctx,
  3357. NULL, /* pass NULL as out buff */
  3358. &outl,
  3359. plain1,
  3360. sizeof(plain1)-1),
  3361. 0 /* expected result code 0: fail */
  3362. );
  3363. AssertIntEQ(
  3364. EVP_EncodeUpdate(
  3365. ctx,
  3366. encOutBuff,
  3367. NULL, /* pass NULL as outl */
  3368. plain1,
  3369. sizeof(plain1)-1),
  3370. 0 /* expected result code 0: fail */
  3371. );
  3372. AssertIntEQ(
  3373. EVP_EncodeUpdate(
  3374. ctx,
  3375. encOutBuff,
  3376. &outl,
  3377. NULL, /* pass NULL as in */
  3378. sizeof(plain1)-1),
  3379. 0 /* expected result code 0: fail */
  3380. );
  3381. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3382. /* meaningless parameter test */
  3383. AssertIntEQ(
  3384. EVP_EncodeUpdate(
  3385. ctx,
  3386. encOutBuff,
  3387. &outl,
  3388. plain1,
  3389. 0), /* pass zero input */
  3390. 1 /* expected result code 1: success */
  3391. );
  3392. /* very small data encoding test */
  3393. EVP_EncodeInit(ctx);
  3394. AssertIntEQ(
  3395. EVP_EncodeUpdate(
  3396. ctx,
  3397. encOutBuff,
  3398. &outl,
  3399. plain0,
  3400. sizeof(plain0)-1),
  3401. 1 /* expected result code 1: success */
  3402. );
  3403. AssertIntEQ(outl,0);
  3404. EVP_EncodeFinal(
  3405. ctx,
  3406. encOutBuff + outl,
  3407. &outl);
  3408. AssertIntEQ( outl, sizeof(enc0)-1);
  3409. AssertIntEQ(
  3410. XSTRNCMP(
  3411. (const char*)encOutBuff,
  3412. (const char*)enc0,sizeof(enc0) ),
  3413. 0);
  3414. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3415. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3416. sizeof(enc0)-1);
  3417. AssertIntEQ(
  3418. XSTRNCMP(
  3419. (const char*)encOutBuff,
  3420. (const char*)enc0,sizeof(enc0) ),
  3421. 0);
  3422. /* pass small size( < 48bytes ) input, then make sure they are not
  3423. * encoded and just stored in ctx
  3424. */
  3425. EVP_EncodeInit(ctx);
  3426. total = 0;
  3427. outl = 0;
  3428. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3429. AssertIntEQ(
  3430. EVP_EncodeUpdate(
  3431. ctx,
  3432. encOutBuff, /* buffer for output */
  3433. &outl, /* size of output */
  3434. plain1, /* input */
  3435. sizeof(plain1)-1), /* size of input */
  3436. 1); /* expected result code 1:success */
  3437. total += outl;
  3438. AssertIntEQ(outl, 0); /* no output expected */
  3439. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3440. AssertTrue(
  3441. XSTRNCMP((const char*)(ctx->data),
  3442. (const char*)plain1,
  3443. ctx->remaining) ==0 );
  3444. AssertTrue(encOutBuff[0] == 0);
  3445. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3446. * the stored data and the new input together
  3447. */
  3448. AssertIntEQ(
  3449. EVP_EncodeUpdate(
  3450. ctx,
  3451. encOutBuff + outl, /* buffer for output */
  3452. &outl, /* size of output */
  3453. plain2, /* additional input */
  3454. sizeof(plain2) -1), /* size of additional input */
  3455. 1); /* expected result code 1:success */
  3456. total += outl;
  3457. AssertIntNE(outl, 0); /* some output is expected this time*/
  3458. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3459. AssertIntEQ(
  3460. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3461. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3462. EVP_EncodeFinal(
  3463. ctx,
  3464. encOutBuff + outl,
  3465. &outl);
  3466. total += outl;
  3467. AssertIntNE(total,0);
  3468. AssertIntNE(outl,0);
  3469. AssertIntEQ(XSTRNCMP(
  3470. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3471. /* test with illeagal parameters */
  3472. outl = 1;
  3473. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3474. AssertIntEQ(outl, 0);
  3475. outl = 1;
  3476. EVP_EncodeFinal(ctx, NULL, &outl);
  3477. AssertIntEQ(outl, 0);
  3478. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3479. EVP_EncodeFinal(NULL, NULL, NULL);
  3480. EVP_ENCODE_CTX_free(ctx);
  3481. printf(resultFmt, passed);
  3482. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3483. return 0;
  3484. }
  3485. static int test_wolfSSL_EVP_EncodeFinal(void)
  3486. {
  3487. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3488. printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
  3489. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3490. * test_wolfSSL_EVP_EncodeUpdate
  3491. */
  3492. printf(resultFmt, passed);
  3493. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3494. return 0;
  3495. }
  3496. static int test_wolfSSL_EVP_DecodeInit(void)
  3497. {
  3498. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3499. EVP_ENCODE_CTX* ctx = NULL;
  3500. printf(testingFmt, "EVP_DecodeInit()");
  3501. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3502. AssertIntEQ( ctx->remaining,0);
  3503. AssertIntEQ( ctx->data[0],0);
  3504. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3505. /* make ctx dirty */
  3506. ctx->remaining = 10;
  3507. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3508. EVP_DecodeInit(ctx);
  3509. AssertIntEQ( ctx->remaining,0);
  3510. AssertIntEQ( ctx->data[0],0);
  3511. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3512. EVP_ENCODE_CTX_free(ctx);
  3513. printf(resultFmt, passed);
  3514. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3515. return 0;
  3516. }
  3517. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3518. {
  3519. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3520. int outl;
  3521. unsigned char decOutBuff[300];
  3522. EVP_ENCODE_CTX* ctx;
  3523. static const unsigned char enc1[] =
  3524. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3525. /* const unsigned char plain1[] =
  3526. {"This is a base64 decoding test."} */
  3527. printf(testingFmt, "EVP_DecodeUpdate()");
  3528. ctx = EVP_ENCODE_CTX_new();
  3529. EVP_DecodeInit(ctx);
  3530. /* illegal parameter tests */
  3531. /* pass NULL as ctx */
  3532. AssertIntEQ(
  3533. EVP_DecodeUpdate(
  3534. NULL, /* pass NULL as ctx */
  3535. decOutBuff,
  3536. &outl,
  3537. enc1,
  3538. sizeof(enc1)-1),
  3539. -1 /* expected result code -1: fail */
  3540. );
  3541. AssertIntEQ( outl, 0);
  3542. /* pass NULL as output */
  3543. AssertIntEQ(
  3544. EVP_DecodeUpdate(
  3545. ctx,
  3546. NULL, /* pass NULL as out buff */
  3547. &outl,
  3548. enc1,
  3549. sizeof(enc1)-1),
  3550. -1 /* expected result code -1: fail */
  3551. );
  3552. AssertIntEQ( outl, 0);
  3553. /* pass NULL as outl */
  3554. AssertIntEQ(
  3555. EVP_DecodeUpdate(
  3556. ctx,
  3557. decOutBuff,
  3558. NULL, /* pass NULL as outl */
  3559. enc1,
  3560. sizeof(enc1)-1),
  3561. -1 /* expected result code -1: fail */
  3562. );
  3563. /* pass NULL as input */
  3564. AssertIntEQ(
  3565. EVP_DecodeUpdate(
  3566. ctx,
  3567. decOutBuff,
  3568. &outl,
  3569. NULL, /* pass NULL as in */
  3570. sizeof(enc1)-1),
  3571. -1 /* expected result code -1: fail */
  3572. );
  3573. AssertIntEQ( outl, 0);
  3574. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3575. /* pass zero length input */
  3576. AssertIntEQ(
  3577. EVP_DecodeUpdate(
  3578. ctx,
  3579. decOutBuff,
  3580. &outl,
  3581. enc1,
  3582. 0), /* pass zero as input len */
  3583. 1 /* expected result code 1: success */
  3584. );
  3585. /* decode correct base64 string */
  3586. {
  3587. static const unsigned char enc2[] =
  3588. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3589. static const unsigned char plain2[] =
  3590. {"This is a base64 decoding test."};
  3591. EVP_EncodeInit(ctx);
  3592. AssertIntEQ(
  3593. EVP_DecodeUpdate(
  3594. ctx,
  3595. decOutBuff,
  3596. &outl,
  3597. enc2,
  3598. sizeof(enc2)-1),
  3599. 0 /* expected result code 0: success */
  3600. );
  3601. AssertIntEQ(outl,sizeof(plain2) -1);
  3602. AssertIntEQ(
  3603. EVP_DecodeFinal(
  3604. ctx,
  3605. decOutBuff + outl,
  3606. &outl),
  3607. 1 /* expected result code 1: success */
  3608. );
  3609. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3610. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3611. sizeof(plain2) -1 ),0);
  3612. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3613. sizeof(plain2)-1);
  3614. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3615. sizeof(plain2) -1 ),0);
  3616. }
  3617. /* decode correct base64 string which does not have '\n' in its last*/
  3618. {
  3619. static const unsigned char enc3[] =
  3620. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3621. static const unsigned char plain3[] =
  3622. {"This is a base64 decoding test."}; /* 31 chars */
  3623. EVP_EncodeInit(ctx);
  3624. AssertIntEQ(
  3625. EVP_DecodeUpdate(
  3626. ctx,
  3627. decOutBuff,
  3628. &outl,
  3629. enc3,
  3630. sizeof(enc3)-1),
  3631. 0 /* expected result code 0: success */
  3632. );
  3633. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  3634. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3635. sizeof(plain3) -1 ),0);
  3636. AssertIntEQ(
  3637. EVP_DecodeFinal(
  3638. ctx,
  3639. decOutBuff + outl,
  3640. &outl),
  3641. 1 /* expected result code 1: success */
  3642. );
  3643. AssertIntEQ(outl,0 );
  3644. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  3645. sizeof(plain3)-1);
  3646. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  3647. sizeof(plain3) -1 ),0);
  3648. }
  3649. /* decode string which has a padding char ('=') in the illegal position*/
  3650. {
  3651. static const unsigned char enc4[] =
  3652. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  3653. EVP_EncodeInit(ctx);
  3654. AssertIntEQ(
  3655. EVP_DecodeUpdate(
  3656. ctx,
  3657. decOutBuff,
  3658. &outl,
  3659. enc4,
  3660. sizeof(enc4)-1),
  3661. -1 /* expected result code -1: error */
  3662. );
  3663. AssertIntEQ(outl,0);
  3664. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  3665. }
  3666. /* small data decode test */
  3667. {
  3668. static const unsigned char enc00[] = {"VG"};
  3669. static const unsigned char enc01[] = {"g=\n"};
  3670. static const unsigned char plain4[] = {"Th"};
  3671. EVP_EncodeInit(ctx);
  3672. AssertIntEQ(
  3673. EVP_DecodeUpdate(
  3674. ctx,
  3675. decOutBuff,
  3676. &outl,
  3677. enc00,
  3678. sizeof(enc00)-1),
  3679. 1 /* expected result code 1: success */
  3680. );
  3681. AssertIntEQ(outl,0);
  3682. AssertIntEQ(
  3683. EVP_DecodeUpdate(
  3684. ctx,
  3685. decOutBuff + outl,
  3686. &outl,
  3687. enc01,
  3688. sizeof(enc01)-1),
  3689. 0 /* expected result code 0: success */
  3690. );
  3691. AssertIntEQ(outl,sizeof(plain4)-1);
  3692. /* test with illegal parameters */
  3693. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  3694. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  3695. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  3696. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  3697. EVP_DecodeFinal(
  3698. ctx,
  3699. decOutBuff + outl,
  3700. &outl);
  3701. AssertIntEQ( outl, 0);
  3702. AssertIntEQ(
  3703. XSTRNCMP(
  3704. (const char*)decOutBuff,
  3705. (const char*)plain4,sizeof(plain4)-1 ),
  3706. 0);
  3707. }
  3708. EVP_ENCODE_CTX_free(ctx);
  3709. printf(resultFmt, passed);
  3710. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3711. return 0;
  3712. }
  3713. static int test_wolfSSL_EVP_DecodeFinal(void)
  3714. {
  3715. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3716. printf(testingFmt, "EVP_DecodeFinal()");
  3717. /* tests for wolfSSL_EVP_DecodeFinal are included in
  3718. * test_wolfSSL_EVP_DecodeUpdate
  3719. */
  3720. printf(resultFmt, passed);
  3721. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3722. return 0;
  3723. }
  3724. /* Test function for wolfSSL_EVP_get_cipherbynid.
  3725. */
  3726. #ifdef OPENSSL_EXTRA
  3727. static int test_wolfSSL_EVP_get_cipherbynid(void)
  3728. {
  3729. #ifndef NO_AES
  3730. const WOLFSSL_EVP_CIPHER* c;
  3731. c = wolfSSL_EVP_get_cipherbynid(419);
  3732. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3733. defined(WOLFSSL_AES_128)
  3734. AssertNotNull(c);
  3735. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  3736. #else
  3737. AssertNull(c);
  3738. #endif
  3739. c = wolfSSL_EVP_get_cipherbynid(423);
  3740. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3741. defined(WOLFSSL_AES_192)
  3742. AssertNotNull(c);
  3743. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  3744. #else
  3745. AssertNull(c);
  3746. #endif
  3747. c = wolfSSL_EVP_get_cipherbynid(427);
  3748. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  3749. defined(WOLFSSL_AES_256)
  3750. AssertNotNull(c);
  3751. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  3752. #else
  3753. AssertNull(c);
  3754. #endif
  3755. c = wolfSSL_EVP_get_cipherbynid(904);
  3756. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  3757. AssertNotNull(c);
  3758. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  3759. #else
  3760. AssertNull(c);
  3761. #endif
  3762. c = wolfSSL_EVP_get_cipherbynid(905);
  3763. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  3764. AssertNotNull(c);
  3765. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  3766. #else
  3767. AssertNull(c);
  3768. #endif
  3769. c = wolfSSL_EVP_get_cipherbynid(906);
  3770. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  3771. AssertNotNull(c);
  3772. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  3773. #else
  3774. AssertNull(c);
  3775. #endif
  3776. c = wolfSSL_EVP_get_cipherbynid(418);
  3777. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  3778. AssertNotNull(c);
  3779. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  3780. #else
  3781. AssertNull(c);
  3782. #endif
  3783. c = wolfSSL_EVP_get_cipherbynid(422);
  3784. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  3785. AssertNotNull(c);
  3786. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  3787. #else
  3788. AssertNull(c);
  3789. #endif
  3790. c = wolfSSL_EVP_get_cipherbynid(426);
  3791. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  3792. AssertNotNull(c);
  3793. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  3794. #else
  3795. AssertNull(c);
  3796. #endif
  3797. #endif /* !NO_AES */
  3798. #ifndef NO_DES3
  3799. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  3800. #ifdef WOLFSSL_DES_ECB
  3801. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  3802. #endif
  3803. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  3804. #ifdef WOLFSSL_DES_ECB
  3805. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  3806. #endif
  3807. #endif /* !NO_DES3 */
  3808. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  3809. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  3810. #endif
  3811. /* test for nid is out of range */
  3812. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  3813. return 0;
  3814. }
  3815. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  3816. {
  3817. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  3818. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  3819. const EVP_CIPHER *init = EVP_aes_128_cbc();
  3820. const EVP_CIPHER *test;
  3821. byte key[AES_BLOCK_SIZE] = {0};
  3822. byte iv[AES_BLOCK_SIZE] = {0};
  3823. AssertNotNull(ctx);
  3824. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  3825. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  3826. test = EVP_CIPHER_CTX_cipher(ctx);
  3827. AssertTrue(init == test);
  3828. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  3829. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  3830. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  3831. EVP_CIPHER_CTX_free(ctx);
  3832. /* test EVP_CIPHER_CTX_cleanup with NULL */
  3833. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  3834. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  3835. return 0;
  3836. }
  3837. #endif /* OPENSSL_EXTRA */
  3838. /*----------------------------------------------------------------------------*
  3839. | IO
  3840. *----------------------------------------------------------------------------*/
  3841. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  3842. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  3843. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  3844. #define HAVE_IO_TESTS_DEPENDENCIES
  3845. #endif
  3846. /* helper functions */
  3847. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  3848. #ifdef WOLFSSL_SESSION_EXPORT
  3849. #ifdef WOLFSSL_DTLS
  3850. /* set up function for sending session information */
  3851. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  3852. {
  3853. WOLFSSL_CTX* ctx = NULL;
  3854. WOLFSSL* ssl = NULL;
  3855. AssertNotNull(inSsl);
  3856. AssertNotNull(buf);
  3857. AssertIntNE(0, sz);
  3858. /* Set ctx to DTLS 1.2 */
  3859. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  3860. AssertNotNull(ctx);
  3861. ssl = wolfSSL_new(ctx);
  3862. AssertNotNull(ssl);
  3863. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  3864. wolfSSL_free(ssl);
  3865. wolfSSL_CTX_free(ctx);
  3866. (void)userCtx;
  3867. return 0;
  3868. }
  3869. #endif
  3870. /* returns negative value on fail and positive (including 0) on success */
  3871. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  3872. {
  3873. int ret, err, loop_count, count, timeout = 10;
  3874. char msg[] = "I hear you fa shizzle!";
  3875. char input[1024];
  3876. loop_count = ((func_args*)args)->argc;
  3877. #ifdef WOLFSSL_ASYNC_CRYPT
  3878. err = 0; /* Reset error */
  3879. #endif
  3880. do {
  3881. #ifdef WOLFSSL_ASYNC_CRYPT
  3882. if (err == WC_PENDING_E) {
  3883. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  3884. if (ret < 0) { break; } else if (ret == 0) { continue; }
  3885. }
  3886. #endif
  3887. ret = wolfSSL_accept(ssl);
  3888. err = wolfSSL_get_error(ssl, 0);
  3889. if (err == WOLFSSL_ERROR_WANT_READ ||
  3890. err == WOLFSSL_ERROR_WANT_WRITE) {
  3891. int select_ret;
  3892. err = WC_PENDING_E;
  3893. select_ret = tcp_select(*sockfd, timeout);
  3894. if (select_ret == TEST_TIMEOUT) {
  3895. return WOLFSSL_FATAL_ERROR;
  3896. }
  3897. }
  3898. } while (err == WC_PENDING_E);
  3899. if (ret != WOLFSSL_SUCCESS) {
  3900. char buff[WOLFSSL_MAX_ERROR_SZ];
  3901. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  3902. return ret;
  3903. }
  3904. for (count = 0; count < loop_count; count++) {
  3905. int select_ret;
  3906. select_ret = tcp_select(*sockfd, timeout);
  3907. if (select_ret == TEST_TIMEOUT) {
  3908. ret = WOLFSSL_FATAL_ERROR;
  3909. break;
  3910. }
  3911. do {
  3912. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  3913. if (ret > 0) {
  3914. input[ret] = '\0';
  3915. printf("Client message: %s\n", input);
  3916. }
  3917. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3918. do {
  3919. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  3920. return WOLFSSL_FATAL_ERROR;
  3921. }
  3922. err = wolfSSL_get_error(ssl, ret);
  3923. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  3924. }
  3925. return ret;
  3926. }
  3927. #endif /* WOLFSSL_SESSION_EXPORT */
  3928. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  3929. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  3930. defined(HAVE_AES_CBC)
  3931. typedef struct openssl_key_ctx {
  3932. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  3933. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  3934. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  3935. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  3936. } openssl_key_ctx;
  3937. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  3938. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  3939. static WC_INLINE int OpenSSLTicketInit(void)
  3940. {
  3941. int ret = wc_InitRng(&myOpenSSLKey_rng);
  3942. if (ret != 0) return ret;
  3943. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  3944. sizeof(myOpenSSLKey_ctx.name));
  3945. if (ret != 0) return ret;
  3946. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  3947. sizeof(myOpenSSLKey_ctx.key));
  3948. if (ret != 0) return ret;
  3949. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  3950. sizeof(myOpenSSLKey_ctx.hmacKey));
  3951. if (ret != 0) return ret;
  3952. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  3953. sizeof(myOpenSSLKey_ctx.iv));
  3954. if (ret != 0) return ret;
  3955. return 0;
  3956. }
  3957. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  3958. byte name[WOLFSSL_TICKET_NAME_SZ],
  3959. byte iv[WOLFSSL_TICKET_IV_SZ],
  3960. WOLFSSL_EVP_CIPHER_CTX *ectx,
  3961. WOLFSSL_HMAC_CTX *hctx, int enc) {
  3962. (void)ssl;
  3963. if (enc) {
  3964. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  3965. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  3966. }
  3967. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  3968. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  3969. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  3970. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  3971. return 0;
  3972. }
  3973. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  3974. if (enc)
  3975. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3976. else
  3977. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  3978. return 1;
  3979. }
  3980. static WC_INLINE void OpenSSLTicketCleanup(void)
  3981. {
  3982. wc_FreeRng(&myOpenSSLKey_rng);
  3983. }
  3984. #endif
  3985. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  3986. #ifdef WC_SHA512_DIGEST_SIZE
  3987. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  3988. #else
  3989. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  3990. #endif
  3991. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  3992. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  3993. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  3994. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  3995. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  3996. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  3997. {
  3998. SOCKET_T sockfd = 0;
  3999. SOCKET_T clientfd = 0;
  4000. word16 port;
  4001. callback_functions* cbf;
  4002. WOLFSSL_CTX* ctx = 0;
  4003. WOLFSSL* ssl = 0;
  4004. func_args* opts = (func_args*)args;
  4005. char msg[] = "I hear you fa shizzle!";
  4006. char input[1024];
  4007. int idx;
  4008. int ret, err = 0;
  4009. int sharedCtx = 0;
  4010. int doUdp = 0;
  4011. SOCKADDR_IN_T cliAddr;
  4012. socklen_t cliLen;
  4013. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4014. size_t msg_len = 0;
  4015. #endif
  4016. #ifdef WOLFSSL_TIRTOS
  4017. fdOpenSession(Task_self());
  4018. #endif
  4019. opts->return_code = TEST_FAIL;
  4020. cbf = opts->callbacks;
  4021. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4022. if (cbf != NULL && cbf->ctx) {
  4023. ctx = cbf->ctx;
  4024. sharedCtx = 1;
  4025. }
  4026. else
  4027. #endif
  4028. {
  4029. WOLFSSL_METHOD* method = NULL;
  4030. if (cbf != NULL && cbf->method != NULL) {
  4031. method = cbf->method();
  4032. }
  4033. else {
  4034. method = wolfSSLv23_server_method();
  4035. }
  4036. ctx = wolfSSL_CTX_new(method);
  4037. }
  4038. if (ctx == NULL) {
  4039. goto done;
  4040. }
  4041. if (cbf == NULL || !cbf->ticNoInit) {
  4042. #if defined(HAVE_SESSION_TICKET) && \
  4043. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4044. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4045. OpenSSLTicketInit();
  4046. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4047. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4048. TicketInit();
  4049. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4050. #endif
  4051. #endif
  4052. }
  4053. #if defined(USE_WINDOWS_API)
  4054. port = opts->signal->port;
  4055. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4056. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4057. /* Let tcp_listen assign port */
  4058. port = 0;
  4059. #else
  4060. /* Use default port */
  4061. port = wolfSSLPort;
  4062. #endif
  4063. if (cbf != NULL)
  4064. doUdp = cbf->doUdp;
  4065. /* do it here to detect failure */
  4066. tcp_accept(
  4067. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4068. if (doUdp) {
  4069. cliLen = sizeof(cliAddr);
  4070. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4071. (struct sockaddr*)&cliAddr, &cliLen);
  4072. AssertIntGT(idx, 0);
  4073. }
  4074. else {
  4075. CloseSocket(sockfd);
  4076. }
  4077. wolfSSL_CTX_set_verify(ctx,
  4078. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4079. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4080. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4081. #endif
  4082. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4083. != WOLFSSL_SUCCESS) {
  4084. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4085. goto done;
  4086. }
  4087. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4088. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4089. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4090. #else
  4091. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4092. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4093. #endif
  4094. /*err_sys("can't load server cert chain file, "
  4095. "Please run from wolfSSL home dir");*/
  4096. goto done;
  4097. }
  4098. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4099. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4100. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4101. #else
  4102. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4103. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4104. #endif
  4105. /*err_sys("can't load server key file, "
  4106. "Please run from wolfSSL home dir");*/
  4107. goto done;
  4108. }
  4109. /* call ctx setup callback */
  4110. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4111. cbf->ctx_ready(ctx);
  4112. }
  4113. ssl = wolfSSL_new(ctx);
  4114. if (ssl == NULL) {
  4115. goto done;
  4116. }
  4117. if (doUdp) {
  4118. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4119. if (err != WOLFSSL_SUCCESS)
  4120. goto done;
  4121. }
  4122. #ifdef WOLFSSL_SESSION_EXPORT
  4123. /* only add in more complex nonblocking case with session export tests */
  4124. if (args && opts->argc > 0) {
  4125. /* set as nonblock and time out for waiting on read/write */
  4126. tcp_set_nonblocking(&clientfd);
  4127. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4128. }
  4129. #endif
  4130. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4131. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4132. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4133. #else
  4134. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  4135. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4136. #endif
  4137. /*err_sys("can't load server cert chain file, "
  4138. "Please run from wolfSSL home dir");*/
  4139. goto done;
  4140. }
  4141. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4142. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4143. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4144. #else
  4145. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4146. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4147. #endif
  4148. /*err_sys("can't load server key file, "
  4149. "Please run from wolfSSL home dir");*/
  4150. goto done;
  4151. }
  4152. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4153. /*err_sys("SSL_set_fd failed");*/
  4154. goto done;
  4155. }
  4156. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4157. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4158. #elif !defined(NO_DH)
  4159. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4160. #endif
  4161. /* call ssl setup callback */
  4162. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4163. cbf->ssl_ready(ssl);
  4164. }
  4165. #ifdef WOLFSSL_SESSION_EXPORT
  4166. /* only add in more complex nonblocking case with session export tests */
  4167. if (opts->argc > 0) {
  4168. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4169. if (ret >= 0) {
  4170. opts->return_code = TEST_SUCCESS;
  4171. }
  4172. #ifdef WOLFSSL_TIRTOS
  4173. Task_yield();
  4174. #endif
  4175. goto done;
  4176. }
  4177. #endif
  4178. #ifdef WOLFSSL_ASYNC_CRYPT
  4179. err = 0; /* Reset error */
  4180. #endif
  4181. do {
  4182. #ifdef WOLFSSL_ASYNC_CRYPT
  4183. if (err == WC_PENDING_E) {
  4184. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4185. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4186. }
  4187. #endif
  4188. ret = wolfSSL_accept(ssl);
  4189. err = wolfSSL_get_error(ssl, 0);
  4190. } while (err == WC_PENDING_E);
  4191. if (ret != WOLFSSL_SUCCESS) {
  4192. char buff[WOLFSSL_MAX_ERROR_SZ];
  4193. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4194. /*err_sys("SSL_accept failed");*/
  4195. goto done;
  4196. }
  4197. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4198. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4199. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4200. AssertIntGE(msg_len, 0);
  4201. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4202. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4203. AssertIntGE(msg_len, 0);
  4204. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4205. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4206. if (idx > 0) {
  4207. input[idx] = '\0';
  4208. printf("Client message: %s\n", input);
  4209. }
  4210. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4211. /*err_sys("SSL_write failed");*/
  4212. #ifdef WOLFSSL_TIRTOS
  4213. return;
  4214. #else
  4215. return 0;
  4216. #endif
  4217. }
  4218. if (cbf != NULL && cbf->on_result != NULL)
  4219. cbf->on_result(ssl);
  4220. #ifdef WOLFSSL_TIRTOS
  4221. Task_yield();
  4222. #endif
  4223. opts->return_code = TEST_SUCCESS;
  4224. done:
  4225. if (cbf != NULL)
  4226. cbf->last_err = err;
  4227. wolfSSL_shutdown(ssl);
  4228. wolfSSL_free(ssl);
  4229. if (!sharedCtx)
  4230. wolfSSL_CTX_free(ctx);
  4231. CloseSocket(clientfd);
  4232. #ifdef WOLFSSL_TIRTOS
  4233. fdCloseSession(Task_self());
  4234. #endif
  4235. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4236. && defined(HAVE_THREAD_LS)
  4237. wc_ecc_fp_free(); /* free per thread cache */
  4238. #endif
  4239. if (cbf == NULL || !cbf->ticNoInit) {
  4240. #if defined(HAVE_SESSION_TICKET) && \
  4241. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4242. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4243. OpenSSLTicketCleanup();
  4244. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4245. TicketCleanup();
  4246. #endif
  4247. #endif
  4248. }
  4249. #ifndef WOLFSSL_TIRTOS
  4250. return 0;
  4251. #endif
  4252. }
  4253. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4254. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4255. {
  4256. SOCKET_T sockfd = 0;
  4257. SOCKET_T clientfd = 0;
  4258. word16 port;
  4259. callback_functions* cbf;
  4260. WOLFSSL_CTX* ctx = 0;
  4261. WOLFSSL* ssl = 0;
  4262. char msg[] = "I hear you fa shizzle!";
  4263. char input[1024];
  4264. int idx;
  4265. int ret, err = 0;
  4266. int sharedCtx = 0;
  4267. int loop_count = ((func_args*)args)->argc;
  4268. int count = 0;
  4269. #ifdef WOLFSSL_TIRTOS
  4270. fdOpenSession(Task_self());
  4271. #endif
  4272. ((func_args*)args)->return_code = TEST_FAIL;
  4273. cbf = ((func_args*)args)->callbacks;
  4274. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4275. if (cbf != NULL && cbf->ctx) {
  4276. ctx = cbf->ctx;
  4277. sharedCtx = 1;
  4278. }
  4279. else
  4280. #endif
  4281. {
  4282. WOLFSSL_METHOD* method = NULL;
  4283. if (cbf != NULL && cbf->method != NULL) {
  4284. method = cbf->method();
  4285. }
  4286. else {
  4287. method = wolfSSLv23_server_method();
  4288. }
  4289. ctx = wolfSSL_CTX_new(method);
  4290. }
  4291. #if defined(USE_WINDOWS_API)
  4292. port = ((func_args*)args)->signal->port;
  4293. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4294. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4295. /* Let tcp_listen assign port */
  4296. port = 0;
  4297. #else
  4298. /* Use default port */
  4299. port = wolfSSLPort;
  4300. #endif
  4301. wolfSSL_CTX_set_verify(ctx,
  4302. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4303. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4304. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4305. #endif
  4306. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4307. != WOLFSSL_SUCCESS) {
  4308. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4309. goto done;
  4310. }
  4311. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4312. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4313. /*err_sys("can't load server cert chain file, "
  4314. "Please run from wolfSSL home dir");*/
  4315. goto done;
  4316. }
  4317. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4318. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4319. /*err_sys("can't load server key file, "
  4320. "Please run from wolfSSL home dir");*/
  4321. goto done;
  4322. }
  4323. /* call ctx setup callback */
  4324. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4325. cbf->ctx_ready(ctx);
  4326. }
  4327. while(count != loop_count) {
  4328. ssl = wolfSSL_new(ctx);
  4329. if (ssl == NULL) {
  4330. goto done;
  4331. }
  4332. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4333. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4334. /*err_sys("can't load server cert chain file, "
  4335. "Please run from wolfSSL home dir");*/
  4336. goto done;
  4337. }
  4338. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4339. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4340. /*err_sys("can't load server key file, "
  4341. "Please run from wolfSSL home dir");*/
  4342. goto done;
  4343. }
  4344. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4345. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4346. #elif !defined(NO_DH)
  4347. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4348. #endif
  4349. /* call ssl setup callback */
  4350. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4351. cbf->ssl_ready(ssl);
  4352. }
  4353. /* do it here to detect failure */
  4354. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4355. CloseSocket(sockfd);
  4356. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4357. /*err_sys("SSL_set_fd failed");*/
  4358. goto done;
  4359. }
  4360. #ifdef WOLFSSL_ASYNC_CRYPT
  4361. err = 0; /* Reset error */
  4362. #endif
  4363. do {
  4364. #ifdef WOLFSSL_ASYNC_CRYPT
  4365. if (err == WC_PENDING_E) {
  4366. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4367. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4368. }
  4369. #endif
  4370. ret = wolfSSL_accept(ssl);
  4371. err = wolfSSL_get_error(ssl, 0);
  4372. } while (err == WC_PENDING_E);
  4373. if (ret != WOLFSSL_SUCCESS) {
  4374. char buff[WOLFSSL_MAX_ERROR_SZ];
  4375. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4376. /*err_sys("SSL_accept failed");*/
  4377. goto done;
  4378. }
  4379. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4380. if (idx > 0) {
  4381. input[idx] = '\0';
  4382. printf("Client message: %s\n", input);
  4383. }
  4384. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4385. /*err_sys("SSL_write failed");*/
  4386. #ifdef WOLFSSL_TIRTOS
  4387. return;
  4388. #else
  4389. return 0;
  4390. #endif
  4391. }
  4392. /* free ssl for this connection */
  4393. wolfSSL_shutdown(ssl);
  4394. wolfSSL_free(ssl); ssl = NULL;
  4395. CloseSocket(clientfd);
  4396. count++;
  4397. }
  4398. #ifdef WOLFSSL_TIRTOS
  4399. Task_yield();
  4400. #endif
  4401. ((func_args*)args)->return_code = TEST_SUCCESS;
  4402. done:
  4403. if(ssl != NULL) {
  4404. wolfSSL_shutdown(ssl);
  4405. wolfSSL_free(ssl);
  4406. }
  4407. if (!sharedCtx)
  4408. wolfSSL_CTX_free(ctx);
  4409. CloseSocket(clientfd);
  4410. #ifdef WOLFSSL_TIRTOS
  4411. fdCloseSession(Task_self());
  4412. #endif
  4413. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4414. && defined(HAVE_THREAD_LS)
  4415. wc_ecc_fp_free(); /* free per thread cache */
  4416. #endif
  4417. #ifndef WOLFSSL_TIRTOS
  4418. return 0;
  4419. #endif
  4420. }
  4421. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4422. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4423. static int test_client_nofail(void* args, cbType cb)
  4424. {
  4425. #if !defined(NO_WOLFSSL_CLIENT)
  4426. SOCKET_T sockfd = 0;
  4427. callback_functions* cbf;
  4428. WOLFSSL_CTX* ctx = 0;
  4429. WOLFSSL* ssl = 0;
  4430. WOLFSSL_CIPHER* cipher;
  4431. char msg[64] = "hello wolfssl!";
  4432. char reply[1024];
  4433. int input;
  4434. int msgSz = (int)XSTRLEN(msg);
  4435. int ret, err = 0;
  4436. int cipherSuite;
  4437. int sharedCtx = 0;
  4438. int doUdp = 0;
  4439. const char* cipherName1, *cipherName2;
  4440. #ifdef WOLFSSL_TIRTOS
  4441. fdOpenSession(Task_self());
  4442. #endif
  4443. ((func_args*)args)->return_code = TEST_FAIL;
  4444. cbf = ((func_args*)args)->callbacks;
  4445. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4446. if (cbf != NULL && cbf->ctx) {
  4447. ctx = cbf->ctx;
  4448. sharedCtx = cbf->isSharedCtx;
  4449. }
  4450. else
  4451. #endif
  4452. {
  4453. WOLFSSL_METHOD* method = NULL;
  4454. if (cbf != NULL && cbf->method != NULL) {
  4455. method = cbf->method();
  4456. }
  4457. else {
  4458. method = wolfSSLv23_client_method();
  4459. }
  4460. ctx = wolfSSL_CTX_new(method);
  4461. }
  4462. if (cbf != NULL)
  4463. doUdp = cbf->doUdp;
  4464. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4465. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4466. #endif
  4467. /* Do connect here so server detects failures */
  4468. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4469. doUdp, 0, NULL);
  4470. /* Connect the socket so that we don't have to set the peer later on */
  4471. if (doUdp)
  4472. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4473. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4474. {
  4475. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4476. goto done;
  4477. }
  4478. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4479. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4480. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4481. #else
  4482. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4483. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4484. #endif
  4485. /*err_sys("can't load client cert file, "
  4486. "Please run from wolfSSL home dir");*/
  4487. goto done;
  4488. }
  4489. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4490. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4491. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4492. #else
  4493. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4494. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4495. #endif
  4496. /*err_sys("can't load client key file, "
  4497. "Please run from wolfSSL home dir");*/
  4498. goto done;
  4499. }
  4500. /* call ctx setup callback */
  4501. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4502. cbf->ctx_ready(ctx);
  4503. }
  4504. ssl = wolfSSL_new(ctx);
  4505. if (ssl == NULL) {
  4506. goto done;
  4507. }
  4508. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4509. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4510. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4511. #else
  4512. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4513. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4514. #endif
  4515. /*err_sys("can't load client cert file, "
  4516. "Please run from wolfSSL home dir");*/
  4517. goto done;
  4518. }
  4519. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4520. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4521. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4522. #else
  4523. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4524. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4525. #endif
  4526. /*err_sys("can't load client key file, "
  4527. "Please run from wolfSSL home dir");*/
  4528. goto done;
  4529. }
  4530. if (!doUdp) {
  4531. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4532. /*err_sys("SSL_set_fd failed");*/
  4533. goto done;
  4534. }
  4535. }
  4536. else {
  4537. #ifdef WOLFSSL_DTLS
  4538. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4539. /*err_sys("SSL_set_fd failed");*/
  4540. goto done;
  4541. }
  4542. #else
  4543. goto done;
  4544. #endif
  4545. }
  4546. /* call ssl setup callback */
  4547. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4548. cbf->ssl_ready(ssl);
  4549. }
  4550. #ifdef WOLFSSL_ASYNC_CRYPT
  4551. err = 0; /* Reset error */
  4552. #endif
  4553. do {
  4554. #ifdef WOLFSSL_ASYNC_CRYPT
  4555. if (err == WC_PENDING_E) {
  4556. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4557. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4558. }
  4559. #endif
  4560. ret = wolfSSL_connect(ssl);
  4561. err = wolfSSL_get_error(ssl, 0);
  4562. } while (err == WC_PENDING_E);
  4563. if (ret != WOLFSSL_SUCCESS) {
  4564. char buff[WOLFSSL_MAX_ERROR_SZ];
  4565. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4566. /*err_sys("SSL_connect failed");*/
  4567. goto done;
  4568. }
  4569. /* test the various get cipher methods */
  4570. /* Internal cipher suite names */
  4571. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4572. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4573. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4574. (cipherSuite >> 8), cipherSuite & 0xFF);
  4575. AssertStrEQ(cipherName1, cipherName2);
  4576. /* IANA Cipher Suites Names */
  4577. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4578. then it's the internal cipher suite name */
  4579. cipher = wolfSSL_get_current_cipher(ssl);
  4580. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4581. cipherName2 = wolfSSL_get_cipher(ssl);
  4582. AssertStrEQ(cipherName1, cipherName2);
  4583. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4584. !defined(WOLFSSL_QT)
  4585. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4586. (cipherSuite >> 8), cipherSuite & 0xFF);
  4587. AssertStrEQ(cipherName1, cipherName2);
  4588. #endif
  4589. if (cb != NULL)
  4590. (cb)(ctx, ssl);
  4591. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4592. /*err_sys("SSL_write failed");*/
  4593. goto done;
  4594. }
  4595. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4596. if (input > 0) {
  4597. reply[input] = '\0';
  4598. printf("Server response: %s\n", reply);
  4599. }
  4600. if (cbf != NULL && cbf->on_result != NULL)
  4601. cbf->on_result(ssl);
  4602. ((func_args*)args)->return_code = TEST_SUCCESS;
  4603. done:
  4604. if (cbf != NULL)
  4605. cbf->last_err = err;
  4606. wolfSSL_free(ssl);
  4607. if (!sharedCtx)
  4608. wolfSSL_CTX_free(ctx);
  4609. CloseSocket(sockfd);
  4610. #ifdef WOLFSSL_TIRTOS
  4611. fdCloseSession(Task_self());
  4612. #endif
  4613. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4614. && defined(HAVE_THREAD_LS)
  4615. wc_ecc_fp_free(); /* free per thread cache */
  4616. #endif
  4617. #else
  4618. (void)args;
  4619. (void)cb;
  4620. #endif /* !NO_WOLFSSL_CLIENT */
  4621. return 0;
  4622. }
  4623. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  4624. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  4625. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  4626. {
  4627. SOCKET_T sockfd = 0;
  4628. callback_functions* cbf;
  4629. WOLFSSL_CTX* ctx = 0;
  4630. WOLFSSL* ssl = 0;
  4631. WOLFSSL_SESSION* session = NULL;
  4632. char msg[64] = "hello wolfssl!";
  4633. char reply[1024];
  4634. int input;
  4635. int msgSz = (int)XSTRLEN(msg);
  4636. int ret, err = 0;
  4637. int sharedCtx = 0;
  4638. #ifdef WOLFSSL_TIRTOS
  4639. fdOpenSession(Task_self());
  4640. #endif
  4641. ((func_args*)args)->return_code = TEST_FAIL;
  4642. cbf = ((func_args*)args)->callbacks;
  4643. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4644. if (cbf != NULL && cbf->ctx) {
  4645. ctx = cbf->ctx;
  4646. sharedCtx = 1;
  4647. }
  4648. else
  4649. #endif
  4650. {
  4651. WOLFSSL_METHOD* method = NULL;
  4652. if (cbf != NULL && cbf->method != NULL) {
  4653. method = cbf->method();
  4654. }
  4655. else {
  4656. method = wolfSSLv23_client_method();
  4657. }
  4658. ctx = wolfSSL_CTX_new(method);
  4659. }
  4660. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4661. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4662. #endif
  4663. /* Do connect here so server detects failures */
  4664. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4665. 0, 0, NULL);
  4666. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4667. {
  4668. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4669. goto done;
  4670. }
  4671. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4672. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4673. /*err_sys("can't load client cert file, "
  4674. "Please run from wolfSSL home dir");*/
  4675. goto done;
  4676. }
  4677. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4678. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4679. /*err_sys("can't load client key file, "
  4680. "Please run from wolfSSL home dir");*/
  4681. goto done;
  4682. }
  4683. /* call ctx setup callback */
  4684. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4685. cbf->ctx_ready(ctx);
  4686. }
  4687. ssl = wolfSSL_new(ctx);
  4688. if (ssl == NULL) {
  4689. goto done;
  4690. }
  4691. /* keep handshakre resources for re-using WOLFSSL obj */
  4692. wolfSSL_KeepArrays(ssl);
  4693. if(wolfSSL_KeepHandshakeResources(ssl)) {
  4694. /* err_sys("SSL_KeepHandshakeResources failed"); */
  4695. goto done;
  4696. }
  4697. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4698. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4699. /*err_sys("can't load client cert file, "
  4700. "Please run from wolfSSL home dir");*/
  4701. goto done;
  4702. }
  4703. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4704. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4705. /*err_sys("can't load client key file, "
  4706. "Please run from wolfSSL home dir");*/
  4707. goto done;
  4708. }
  4709. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4710. /*err_sys("SSL_set_fd failed");*/
  4711. goto done;
  4712. }
  4713. /* call ssl setup callback */
  4714. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4715. cbf->ssl_ready(ssl);
  4716. }
  4717. #ifdef WOLFSSL_ASYNC_CRYPT
  4718. err = 0; /* Reset error */
  4719. #endif
  4720. do {
  4721. #ifdef WOLFSSL_ASYNC_CRYPT
  4722. if (err == WC_PENDING_E) {
  4723. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4724. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4725. }
  4726. #endif
  4727. ret = wolfSSL_connect(ssl);
  4728. err = wolfSSL_get_error(ssl, 0);
  4729. } while (err == WC_PENDING_E);
  4730. if (ret != WOLFSSL_SUCCESS) {
  4731. char buff[WOLFSSL_MAX_ERROR_SZ];
  4732. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4733. /*err_sys("SSL_connect failed");*/
  4734. goto done;
  4735. }
  4736. /* Build first session */
  4737. if (cb != NULL)
  4738. ((cbType)cb)(ctx, ssl);
  4739. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4740. /*err_sys("SSL_write failed");*/
  4741. goto done;
  4742. }
  4743. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4744. if (input > 0) {
  4745. reply[input] = '\0';
  4746. printf("Server response: %s\n", reply);
  4747. }
  4748. /* Session Resumption by re-using WOLFSSL object */
  4749. wolfSSL_set_quiet_shutdown(ssl, 1);
  4750. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  4751. /* err_sys ("SSL shutdown failed"); */
  4752. goto done;
  4753. }
  4754. session = wolfSSL_get1_session(ssl);
  4755. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  4756. /* err_sys ("SSL_clear failed"); */
  4757. goto done;
  4758. }
  4759. wolfSSL_set_session(ssl, session);
  4760. wolfSSL_SESSION_free(session);
  4761. session = NULL;
  4762. /* close socket once */
  4763. CloseSocket(sockfd);
  4764. sockfd = 0;
  4765. /* wait until server ready */
  4766. wait_tcp_ready((func_args*)server_args);
  4767. printf("session resumption\n");
  4768. /* Do re-connect */
  4769. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4770. 0, 0, NULL);
  4771. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4772. /*err_sys("SSL_set_fd failed");*/
  4773. goto done;
  4774. }
  4775. #ifdef WOLFSSL_ASYNC_CRYPT
  4776. err = 0; /* Reset error */
  4777. #endif
  4778. do {
  4779. #ifdef WOLFSSL_ASYNC_CRYPT
  4780. if (err == WC_PENDING_E) {
  4781. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4782. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4783. }
  4784. #endif
  4785. ret = wolfSSL_connect(ssl);
  4786. err = wolfSSL_get_error(ssl, 0);
  4787. } while (err == WC_PENDING_E);
  4788. if (ret != WOLFSSL_SUCCESS) {
  4789. char buff[WOLFSSL_MAX_ERROR_SZ];
  4790. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4791. /*err_sys("SSL_connect failed");*/
  4792. goto done;
  4793. }
  4794. /* Build first session */
  4795. if (cb != NULL)
  4796. ((cbType)cb)(ctx, ssl);
  4797. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4798. /*err_sys("SSL_write failed");*/
  4799. goto done;
  4800. }
  4801. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4802. if (input > 0) {
  4803. reply[input] = '\0';
  4804. printf("Server response: %s\n", reply);
  4805. }
  4806. ((func_args*)args)->return_code = TEST_SUCCESS;
  4807. done:
  4808. wolfSSL_free(ssl);
  4809. if (!sharedCtx)
  4810. wolfSSL_CTX_free(ctx);
  4811. CloseSocket(sockfd);
  4812. #ifdef WOLFSSL_TIRTOS
  4813. fdCloseSession(Task_self());
  4814. #endif
  4815. return;
  4816. }
  4817. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  4818. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  4819. static int test_client_verifyDepth(void* args)
  4820. {
  4821. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  4822. SOCKET_T sockfd = 0;
  4823. callback_functions* cbf;
  4824. WOLFSSL_CTX* ctx = 0;
  4825. WOLFSSL* ssl = 0;
  4826. char msg[64] = "hello wolfssl!";
  4827. char reply[1024];
  4828. int input;
  4829. int msgSz = (int)XSTRLEN(msg);
  4830. int ret, err = 0;
  4831. int verify_depth = ((func_args*)args)->argc;
  4832. ((func_args*)args)->return_code = TEST_FAIL;
  4833. cbf = ((func_args*)args)->callbacks;
  4834. {
  4835. WOLFSSL_METHOD* method = NULL;
  4836. if (cbf != NULL && cbf->method != NULL) {
  4837. method = cbf->method();
  4838. }
  4839. else {
  4840. method = wolfSSLv23_client_method();
  4841. }
  4842. ctx = wolfSSL_CTX_new(method);
  4843. }
  4844. /* Do connect here so server detects failures */
  4845. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4846. 0, 0, NULL);
  4847. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  4848. != WOLFSSL_SUCCESS)
  4849. {
  4850. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4851. goto done;
  4852. }
  4853. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4854. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4855. /*err_sys("can't load client cert file, "
  4856. "Please run from wolfSSL home dir");*/
  4857. goto done;
  4858. }
  4859. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4860. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4861. /*err_sys("can't load client key file, "
  4862. "Please run from wolfSSL home dir");*/
  4863. goto done;
  4864. }
  4865. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  4866. /* set verify depth */
  4867. if (verify_depth == 0) {
  4868. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  4869. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4870. } else if (verify_depth == -1) {
  4871. myVerifyAction = VERIFY_USE_PREVERFIY;
  4872. SSL_CTX_set_verify_depth(ctx, 0);
  4873. } else if (verify_depth > 0) {
  4874. myVerifyAction = VERIFY_USE_PREVERFIY;
  4875. SSL_CTX_set_verify_depth(ctx, verify_depth);
  4876. }
  4877. ssl = wolfSSL_new(ctx);
  4878. if (ssl == NULL) {
  4879. goto done;
  4880. }
  4881. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4882. /*err_sys("SSL_set_fd failed");*/
  4883. goto done;
  4884. }
  4885. #ifdef WOLFSSL_ASYNC_CRYPT
  4886. err = 0; /* Reset error */
  4887. #endif
  4888. do {
  4889. #ifdef WOLFSSL_ASYNC_CRYPT
  4890. if (err == WC_PENDING_E) {
  4891. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4892. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4893. }
  4894. #endif
  4895. ret = wolfSSL_connect(ssl);
  4896. err = wolfSSL_get_error(ssl, 0);
  4897. } while (err == WC_PENDING_E);
  4898. if (ret != WOLFSSL_SUCCESS) {
  4899. char buff[WOLFSSL_MAX_ERROR_SZ];
  4900. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4901. goto done;
  4902. }
  4903. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4904. goto done;
  4905. }
  4906. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4907. if (input > 0) {
  4908. reply[input] = '\0';
  4909. printf("Server response: %s\n", reply);
  4910. }
  4911. ((func_args*)args)->return_code = TEST_SUCCESS;
  4912. done:
  4913. wolfSSL_free(ssl);
  4914. wolfSSL_CTX_free(ctx);
  4915. CloseSocket(sockfd);
  4916. #else
  4917. (void)args;
  4918. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  4919. return 0;
  4920. }
  4921. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  4922. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  4923. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  4924. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  4925. #define HAVE_ALPN_PROTOS_SUPPORT
  4926. #endif
  4927. /* Generic TLS client / server with callbacks for API unit tests
  4928. * Used by SNI / ALPN / crypto callback helper functions */
  4929. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  4930. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  4931. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  4932. #define ENABLE_TLS_CALLBACK_TEST
  4933. #endif
  4934. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  4935. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  4936. /* TLS server for API unit testing - generic */
  4937. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  4938. {
  4939. callback_functions* callbacks = ((func_args*)args)->callbacks;
  4940. WOLFSSL_CTX* ctx = NULL;
  4941. WOLFSSL* ssl = NULL;
  4942. SOCKET_T sfd = 0;
  4943. SOCKET_T cfd = 0;
  4944. word16 port;
  4945. char msg[] = "I hear you fa shizzle!";
  4946. int len = (int) XSTRLEN(msg);
  4947. char input[1024];
  4948. int idx;
  4949. int ret, err = 0;
  4950. ((func_args*)args)->return_code = TEST_FAIL;
  4951. #ifdef WOLFSSL_STATIC_MEMORY
  4952. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  4953. callbacks->memSz > 0) {
  4954. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  4955. callbacks->mem, callbacks->memSz, 0, 1);
  4956. if (ret != WOLFSSL_SUCCESS) {
  4957. printf("CTX static new failed %d\n", ret);
  4958. return 0;
  4959. }
  4960. }
  4961. #endif
  4962. if (ctx == NULL) {
  4963. ctx = wolfSSL_CTX_new(callbacks->method());
  4964. }
  4965. if (ctx == NULL) {
  4966. printf("CTX new failed\n");
  4967. return 0;
  4968. }
  4969. /* set defaults */
  4970. if (callbacks->caPemFile == NULL)
  4971. callbacks->caPemFile = cliCertFile;
  4972. if (callbacks->certPemFile == NULL)
  4973. callbacks->certPemFile = svrCertFile;
  4974. if (callbacks->keyPemFile == NULL)
  4975. callbacks->keyPemFile = svrKeyFile;
  4976. #ifdef WOLFSSL_TIRTOS
  4977. fdOpenSession(Task_self());
  4978. #endif
  4979. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  4980. #if defined(USE_WINDOWS_API)
  4981. port = ((func_args*)args)->signal->port;
  4982. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4983. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4984. /* Let tcp_listen assign port */
  4985. port = 0;
  4986. #else
  4987. /* Use default port */
  4988. port = wolfSSLPort;
  4989. #endif
  4990. wolfSSL_CTX_set_verify(ctx,
  4991. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4992. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4993. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4994. #endif
  4995. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  4996. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  4997. #endif
  4998. AssertIntEQ(WOLFSSL_SUCCESS,
  4999. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5000. AssertIntEQ(WOLFSSL_SUCCESS,
  5001. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5002. WOLFSSL_FILETYPE_PEM));
  5003. AssertIntEQ(WOLFSSL_SUCCESS,
  5004. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5005. WOLFSSL_FILETYPE_PEM));
  5006. if (callbacks->ctx_ready)
  5007. callbacks->ctx_ready(ctx);
  5008. ssl = wolfSSL_new(ctx);
  5009. if (ssl == NULL) {
  5010. printf("SSL new failed\n");
  5011. wolfSSL_CTX_free(ctx);
  5012. return 0;
  5013. }
  5014. if (wolfSSL_dtls(ssl)) {
  5015. SOCKADDR_IN_T cliAddr;
  5016. socklen_t cliLen;
  5017. cliLen = sizeof(cliAddr);
  5018. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5019. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5020. (struct sockaddr*)&cliAddr, &cliLen);
  5021. AssertIntGT(idx, 0);
  5022. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5023. }
  5024. else {
  5025. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5026. CloseSocket(sfd);
  5027. }
  5028. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5029. if (callbacks->loadToSSL) {
  5030. wolfSSL_SetDevId(ssl, callbacks->devId);
  5031. AssertIntEQ(WOLFSSL_SUCCESS,
  5032. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5033. WOLFSSL_FILETYPE_PEM));
  5034. AssertIntEQ(WOLFSSL_SUCCESS,
  5035. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5036. WOLFSSL_FILETYPE_PEM));
  5037. }
  5038. #ifdef NO_PSK
  5039. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5040. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5041. #elif !defined(NO_DH)
  5042. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5043. #endif
  5044. #endif
  5045. if (callbacks->ssl_ready)
  5046. callbacks->ssl_ready(ssl);
  5047. #ifdef WOLFSSL_ASYNC_CRYPT
  5048. err = 0; /* Reset error */
  5049. #endif
  5050. do {
  5051. #ifdef WOLFSSL_ASYNC_CRYPT
  5052. if (err == WC_PENDING_E) {
  5053. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5054. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5055. }
  5056. #endif
  5057. ret = wolfSSL_accept(ssl);
  5058. err = wolfSSL_get_error(ssl, 0);
  5059. } while (err == WC_PENDING_E);
  5060. if (ret != WOLFSSL_SUCCESS) {
  5061. char buff[WOLFSSL_MAX_ERROR_SZ];
  5062. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5063. /*err_sys("SSL_accept failed");*/
  5064. }
  5065. else {
  5066. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  5067. input[idx] = 0;
  5068. printf("Client message: %s\n", input);
  5069. }
  5070. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  5071. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5072. defined(WOLFSSL_DTLS)
  5073. if (wolfSSL_dtls(ssl)) {
  5074. byte* import;
  5075. word32 sz;
  5076. wolfSSL_dtls_export(ssl, NULL, &sz);
  5077. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5078. AssertNotNull(import);
  5079. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5080. AssertIntGE(idx, 0);
  5081. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5082. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5083. }
  5084. #endif
  5085. #ifdef WOLFSSL_TIRTOS
  5086. Task_yield();
  5087. #endif
  5088. ((func_args*)args)->return_code = TEST_SUCCESS;
  5089. }
  5090. if (callbacks->on_result)
  5091. callbacks->on_result(ssl);
  5092. wolfSSL_shutdown(ssl);
  5093. wolfSSL_free(ssl);
  5094. wolfSSL_CTX_free(ctx);
  5095. CloseSocket(cfd);
  5096. #ifdef WOLFSSL_TIRTOS
  5097. fdCloseSession(Task_self());
  5098. #endif
  5099. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5100. && defined(HAVE_THREAD_LS)
  5101. wc_ecc_fp_free(); /* free per thread cache */
  5102. #endif
  5103. #ifndef WOLFSSL_TIRTOS
  5104. return 0;
  5105. #endif
  5106. }
  5107. /* TLS Client for API unit testing - generic */
  5108. static void run_wolfssl_client(void* args)
  5109. {
  5110. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5111. WOLFSSL_CTX* ctx = NULL;
  5112. WOLFSSL* ssl = NULL;
  5113. SOCKET_T sfd = 0;
  5114. char msg[] = "hello wolfssl server!";
  5115. int len = (int) XSTRLEN(msg);
  5116. char input[1024];
  5117. int ret, err = 0;
  5118. ((func_args*)args)->return_code = TEST_FAIL;
  5119. /* set defaults */
  5120. if (callbacks->caPemFile == NULL)
  5121. callbacks->caPemFile = caCertFile;
  5122. if (callbacks->certPemFile == NULL)
  5123. callbacks->certPemFile = cliCertFile;
  5124. if (callbacks->keyPemFile == NULL)
  5125. callbacks->keyPemFile = cliKeyFile;
  5126. #ifdef WOLFSSL_STATIC_MEMORY
  5127. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5128. callbacks->memSz > 0) {
  5129. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5130. callbacks->mem, callbacks->memSz, 0, 1);
  5131. if (ret != WOLFSSL_SUCCESS) {
  5132. printf("CTX static new failed %d\n", ret);
  5133. return;
  5134. }
  5135. }
  5136. #endif
  5137. if (ctx == NULL) {
  5138. ctx = wolfSSL_CTX_new(callbacks->method());
  5139. }
  5140. if (ctx == NULL) {
  5141. printf("CTX new failed\n");
  5142. return;
  5143. }
  5144. #ifdef WOLFSSL_TIRTOS
  5145. fdOpenSession(Task_self());
  5146. #endif
  5147. if (!callbacks->loadToSSL) {
  5148. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5149. }
  5150. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5151. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5152. #endif
  5153. AssertIntEQ(WOLFSSL_SUCCESS,
  5154. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5155. if (!callbacks->loadToSSL) {
  5156. AssertIntEQ(WOLFSSL_SUCCESS,
  5157. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5158. WOLFSSL_FILETYPE_PEM));
  5159. AssertIntEQ(WOLFSSL_SUCCESS,
  5160. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5161. WOLFSSL_FILETYPE_PEM));
  5162. }
  5163. if (callbacks->ctx_ready)
  5164. callbacks->ctx_ready(ctx);
  5165. ssl = wolfSSL_new(ctx);
  5166. if (wolfSSL_dtls(ssl)) {
  5167. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5168. 1, 0, ssl);
  5169. }
  5170. else {
  5171. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5172. 0, 0, ssl);
  5173. }
  5174. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5175. if (callbacks->loadToSSL) {
  5176. wolfSSL_SetDevId(ssl, callbacks->devId);
  5177. AssertIntEQ(WOLFSSL_SUCCESS,
  5178. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5179. WOLFSSL_FILETYPE_PEM));
  5180. AssertIntEQ(WOLFSSL_SUCCESS,
  5181. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5182. WOLFSSL_FILETYPE_PEM));
  5183. }
  5184. if (callbacks->ssl_ready)
  5185. callbacks->ssl_ready(ssl);
  5186. #ifdef WOLFSSL_ASYNC_CRYPT
  5187. err = 0; /* Reset error */
  5188. #endif
  5189. do {
  5190. #ifdef WOLFSSL_ASYNC_CRYPT
  5191. if (err == WC_PENDING_E) {
  5192. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5193. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5194. }
  5195. #endif
  5196. ret = wolfSSL_connect(ssl);
  5197. err = wolfSSL_get_error(ssl, 0);
  5198. } while (err == WC_PENDING_E);
  5199. if (ret != WOLFSSL_SUCCESS) {
  5200. char buff[WOLFSSL_MAX_ERROR_SZ];
  5201. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5202. /*err_sys("SSL_connect failed");*/
  5203. }
  5204. else {
  5205. #ifdef WOLFSSL_ASYNC_CRYPT
  5206. err = 0; /* Reset error */
  5207. #endif
  5208. do {
  5209. #ifdef WOLFSSL_ASYNC_CRYPT
  5210. if (err == WC_PENDING_E) {
  5211. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5212. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5213. }
  5214. #endif
  5215. ret = wolfSSL_write(ssl, msg, len);
  5216. err = wolfSSL_get_error(ssl, 0);
  5217. } while (err == WC_PENDING_E);
  5218. AssertIntEQ(len, ret);
  5219. #ifdef WOLFSSL_ASYNC_CRYPT
  5220. err = 0; /* Reset error */
  5221. #endif
  5222. do {
  5223. #ifdef WOLFSSL_ASYNC_CRYPT
  5224. if (err == WC_PENDING_E) {
  5225. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5226. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5227. }
  5228. #endif
  5229. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5230. err = wolfSSL_get_error(ssl, 0);
  5231. } while (err == WC_PENDING_E);
  5232. if (ret > 0) {
  5233. input[ret] = '\0'; /* null term */
  5234. printf("Server response: %s\n", input);
  5235. }
  5236. ((func_args*)args)->return_code = TEST_SUCCESS;
  5237. }
  5238. if (callbacks->on_result)
  5239. callbacks->on_result(ssl);
  5240. wolfSSL_free(ssl);
  5241. wolfSSL_CTX_free(ctx);
  5242. CloseSocket(sfd);
  5243. #ifdef WOLFSSL_TIRTOS
  5244. fdCloseSession(Task_self());
  5245. #endif
  5246. }
  5247. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5248. static int test_wolfSSL_read_write(void)
  5249. {
  5250. /* The unit testing for read and write shall happen simultaneously, since
  5251. * one can't do anything with one without the other. (Except for a failure
  5252. * test case.) This function will call all the others that will set up,
  5253. * execute, and report their test findings.
  5254. *
  5255. * Set up the success case first. This function will become the template
  5256. * for the other tests. This should eventually be renamed
  5257. *
  5258. * The success case isn't interesting, how can this fail?
  5259. * - Do not give the client context a CA certificate. The connect should
  5260. * fail. Do not need server for this?
  5261. * - Using NULL for the ssl object on server. Do not need client for this.
  5262. * - Using NULL for the ssl object on client. Do not need server for this.
  5263. * - Good ssl objects for client and server. Client write() without server
  5264. * read().
  5265. * - Good ssl objects for client and server. Server write() without client
  5266. * read().
  5267. * - Forgetting the password callback?
  5268. */
  5269. tcp_ready ready;
  5270. func_args client_args;
  5271. func_args server_args;
  5272. THREAD_TYPE serverThread;
  5273. XMEMSET(&client_args, 0, sizeof(func_args));
  5274. XMEMSET(&server_args, 0, sizeof(func_args));
  5275. #ifdef WOLFSSL_TIRTOS
  5276. fdOpenSession(Task_self());
  5277. #endif
  5278. StartTCP();
  5279. InitTcpReady(&ready);
  5280. #if defined(USE_WINDOWS_API)
  5281. /* use RNG to get random port if using windows */
  5282. ready.port = GetRandomPort();
  5283. #endif
  5284. server_args.signal = &ready;
  5285. client_args.signal = &ready;
  5286. start_thread(test_server_nofail, &server_args, &serverThread);
  5287. wait_tcp_ready(&server_args);
  5288. test_client_nofail(&client_args, NULL);
  5289. join_thread(serverThread);
  5290. AssertTrue(client_args.return_code);
  5291. AssertTrue(server_args.return_code);
  5292. FreeTcpReady(&ready);
  5293. #ifdef WOLFSSL_TIRTOS
  5294. fdOpenSession(Task_self());
  5295. #endif
  5296. return 0;
  5297. }
  5298. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  5299. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5300. {
  5301. /* The unit test for session resumption by re-using WOLFSSL object.
  5302. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5303. * for second connection.
  5304. */
  5305. tcp_ready ready;
  5306. func_args client_args;
  5307. func_args server_args;
  5308. THREAD_TYPE serverThread;
  5309. XMEMSET(&client_args, 0, sizeof(func_args));
  5310. XMEMSET(&server_args, 0, sizeof(func_args));
  5311. #ifdef WOLFSSL_TIRTOS
  5312. fdOpenSession(Task_self());
  5313. #endif
  5314. StartTCP();
  5315. InitTcpReady(&ready);
  5316. #if defined(USE_WINDOWS_API)
  5317. /* use RNG to get random port if using windows */
  5318. ready.port = GetRandomPort();
  5319. #endif
  5320. server_args.signal = &ready;
  5321. client_args.signal = &ready;
  5322. /* the var is used for loop number */
  5323. server_args.argc = 2;
  5324. start_thread(test_server_loop, &server_args, &serverThread);
  5325. wait_tcp_ready(&server_args);
  5326. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5327. join_thread(serverThread);
  5328. AssertTrue(client_args.return_code);
  5329. AssertTrue(server_args.return_code);
  5330. FreeTcpReady(&ready);
  5331. #ifdef WOLFSSL_TIRTOS
  5332. fdOpenSession(Task_self());
  5333. #endif
  5334. return 0;
  5335. }
  5336. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5337. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5338. {
  5339. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5340. /* This unit test is to check set verify Depth */
  5341. tcp_ready ready;
  5342. func_args client_args;
  5343. func_args server_args;
  5344. THREAD_TYPE serverThread;
  5345. callback_functions client_cbf;
  5346. XMEMSET(&client_args, 0, sizeof(func_args));
  5347. XMEMSET(&server_args, 0, sizeof(func_args));
  5348. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5349. printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
  5350. #ifdef WOLFSSL_TLS13
  5351. client_cbf.method = wolfTLSv1_3_client_method;
  5352. #endif /* WOLFSSL_TLS13 */
  5353. client_args.callbacks = &client_cbf;
  5354. StartTCP();
  5355. InitTcpReady(&ready);
  5356. #if defined(USE_WINDOWS_API)
  5357. /* use RNG to get random port if using windows */
  5358. ready.port = GetRandomPort();
  5359. #endif
  5360. server_args.signal = &ready;
  5361. client_args.signal = &ready;
  5362. /* the var is used for loop number */
  5363. server_args.argc = 1;
  5364. /* test case 1 verify depth is equal to peer chain */
  5365. {
  5366. start_thread(test_server_nofail, &server_args, &serverThread);
  5367. wait_tcp_ready(&server_args);
  5368. /* the var is used for verify depth */
  5369. client_args.argc = 2;
  5370. test_client_verifyDepth(&client_args);
  5371. join_thread(serverThread);
  5372. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5373. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5374. }
  5375. /* test case 2
  5376. * verify depth is zero, number of peer's chain is 2.
  5377. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5378. * callback.
  5379. */
  5380. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5381. {
  5382. start_thread(test_server_nofail, &server_args, &serverThread);
  5383. wait_tcp_ready(&server_args);
  5384. client_args.argc = 0;
  5385. test_client_verifyDepth(&client_args);
  5386. join_thread(serverThread);
  5387. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5388. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5389. }
  5390. /* test case 3
  5391. * verify depth is zero, number of peer's chain is 2
  5392. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5393. * therefore, handshake becomes failure.
  5394. */
  5395. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5396. {
  5397. start_thread(test_server_nofail, &server_args, &serverThread);
  5398. wait_tcp_ready(&server_args);
  5399. client_args.argc = -1;
  5400. test_client_verifyDepth(&client_args);
  5401. join_thread(serverThread);
  5402. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5403. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5404. }
  5405. FreeTcpReady(&ready);
  5406. printf(resultFmt, passed);
  5407. #else
  5408. (void)test_client_verifyDepth;
  5409. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5410. return 0;
  5411. }
  5412. static int test_client_get_finished(void* args, cbType cb)
  5413. {
  5414. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  5415. SOCKET_T sockfd = 0;
  5416. callback_functions* cbf;
  5417. WOLFSSL_CTX* ctx = 0;
  5418. WOLFSSL* ssl = 0;
  5419. char msg[64] = "hello wolfssl!";
  5420. char reply[1024];
  5421. int msgSz = (int)XSTRLEN(msg);
  5422. int ret, err = 0;
  5423. WOLFSSL_METHOD* method = NULL;
  5424. size_t msg_len = 0;
  5425. (void) args;
  5426. (void) cb;
  5427. ((func_args*)args)->return_code = TEST_FAIL;
  5428. cbf = ((func_args*)args)->callbacks;
  5429. if (cbf != NULL && cbf->method != NULL) {
  5430. method = cbf->method();
  5431. }
  5432. else {
  5433. method = wolfSSLv23_client_method();
  5434. }
  5435. ctx = wolfSSL_CTX_new(method);
  5436. /* Do connect here so server detects failures */
  5437. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5438. 0, 0, NULL);
  5439. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5440. {
  5441. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5442. goto done;
  5443. }
  5444. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5445. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5446. goto done;
  5447. }
  5448. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5449. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5450. goto done;
  5451. }
  5452. /* call ctx setup callback */
  5453. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5454. cbf->ctx_ready(ctx);
  5455. }
  5456. ssl = wolfSSL_new(ctx);
  5457. if (ssl == NULL) {
  5458. goto done;
  5459. }
  5460. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5461. goto done;
  5462. }
  5463. /* call ssl setup callback */
  5464. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5465. cbf->ssl_ready(ssl);
  5466. }
  5467. #ifdef WOLFSSL_ASYNC_CRYPT
  5468. err = 0; /* Reset error */
  5469. #endif
  5470. do {
  5471. #ifdef WOLFSSL_ASYNC_CRYPT
  5472. if (err == WC_PENDING_E) {
  5473. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5474. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5475. }
  5476. #endif
  5477. ret = wolfSSL_connect(ssl);
  5478. err = wolfSSL_get_error(ssl, 0);
  5479. } while (err == WC_PENDING_E);
  5480. if (ret != WOLFSSL_SUCCESS) {
  5481. char buff[WOLFSSL_MAX_ERROR_SZ];
  5482. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5483. goto done;
  5484. }
  5485. /* get_finished test */
  5486. /* 1. get own sent message */
  5487. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  5488. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  5489. AssertIntGE(msg_len, 0);
  5490. /* 2. get peer message */
  5491. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  5492. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  5493. AssertIntGE(msg_len, 0);
  5494. if (cb != NULL)
  5495. (cb)(ctx, ssl);
  5496. #ifdef WOLFSSL_ASYNC_CRYPT
  5497. err = 0; /* Reset error */
  5498. #endif
  5499. do {
  5500. #ifdef WOLFSSL_ASYNC_CRYPT
  5501. if (err == WC_PENDING_E) {
  5502. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5503. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5504. }
  5505. #endif
  5506. ret = wolfSSL_write(ssl, msg, msgSz);
  5507. err = wolfSSL_get_error(ssl, 0);
  5508. } while (err == WC_PENDING_E);
  5509. if (ret != msgSz) {
  5510. /*err_sys("SSL_write failed");*/
  5511. goto done;
  5512. }
  5513. #ifdef WOLFSSL_ASYNC_CRYPT
  5514. err = 0; /* Reset error */
  5515. #endif
  5516. do {
  5517. #ifdef WOLFSSL_ASYNC_CRYPT
  5518. if (err == WC_PENDING_E) {
  5519. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5520. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5521. }
  5522. #endif
  5523. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5524. err = wolfSSL_get_error(ssl, 0);
  5525. } while (err == WC_PENDING_E);
  5526. if (ret > 0) {
  5527. reply[ret] = '\0';
  5528. printf("Server response: %s\n", reply);
  5529. }
  5530. ((func_args*)args)->return_code = TEST_SUCCESS;
  5531. done:
  5532. wolfSSL_free(ssl);
  5533. wolfSSL_CTX_free(ctx);
  5534. CloseSocket(sockfd);
  5535. #else
  5536. (void)args;
  5537. (void)cb;
  5538. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  5539. return 0;
  5540. }
  5541. static int test_wolfSSL_get_finished(void)
  5542. {
  5543. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  5544. tcp_ready ready;
  5545. func_args client_args;
  5546. func_args server_args;
  5547. THREAD_TYPE serverThread;
  5548. XMEMSET(&client_args, 0, sizeof(func_args));
  5549. XMEMSET(&server_args, 0, sizeof(func_args));
  5550. StartTCP();
  5551. InitTcpReady(&ready);
  5552. #if defined(USE_WINDOWS_API)
  5553. /* use RNG to get random port if using windows */
  5554. ready.port = GetRandomPort();
  5555. #endif
  5556. server_args.signal = &ready;
  5557. client_args.signal = &ready;
  5558. start_thread(test_server_nofail, &server_args, &serverThread);
  5559. wait_tcp_ready(&server_args);
  5560. test_client_get_finished(&client_args, NULL);
  5561. join_thread(serverThread);
  5562. AssertTrue(client_args.return_code);
  5563. AssertTrue(server_args.return_code);
  5564. /* test received msg vs sent msg */
  5565. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  5566. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  5567. FreeTcpReady(&ready);
  5568. #else
  5569. (void)test_client_get_finished;
  5570. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  5571. return 0;
  5572. }
  5573. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  5574. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  5575. !defined(NO_SESSION_CACHE)
  5576. /* Sessions to restore/store */
  5577. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  5578. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  5579. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  5580. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  5581. {
  5582. /* Don't store sessions. Lookup is still enabled. */
  5583. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  5584. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  5585. /* Require both peers to provide certs */
  5586. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  5587. }
  5588. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  5589. {
  5590. WOLFSSL_SESSION** sess;
  5591. if (wolfSSL_is_server(ssl))
  5592. sess = &test_wolfSSL_CTX_add_session_server_sess;
  5593. else
  5594. sess = &test_wolfSSL_CTX_add_session_client_sess;
  5595. if (*sess == NULL) {
  5596. #ifdef NO_SESSION_CACHE_REF
  5597. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  5598. #else
  5599. /* Test for backwards compatibility */
  5600. if (wolfSSL_is_server(ssl)) {
  5601. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  5602. }
  5603. else {
  5604. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  5605. }
  5606. #endif
  5607. /* Now save the session in the internal store to make it available
  5608. * for lookup. For TLS 1.3, we can't save the session without
  5609. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  5610. * session from cache. */
  5611. if (wolfSSL_is_server(ssl)
  5612. #ifndef WOLFSSL_TICKET_HAVE_ID
  5613. && wolfSSL_version(ssl) != TLS1_3_VERSION
  5614. #endif
  5615. )
  5616. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  5617. *sess), WOLFSSL_SUCCESS);
  5618. }
  5619. else {
  5620. /* If we have a session retrieved then remaining connections should be
  5621. * resuming on that session */
  5622. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  5623. }
  5624. /* Save CTX to be able to decrypt tickets */
  5625. if (wolfSSL_is_server(ssl) &&
  5626. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  5627. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  5628. = wolfSSL_get_SSL_CTX(ssl));
  5629. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  5630. WOLFSSL_SUCCESS);
  5631. }
  5632. #ifdef SESSION_CERTS
  5633. #ifndef WOLFSSL_TICKET_HAVE_ID
  5634. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  5635. wolfSSL_session_reused(ssl))
  5636. #endif
  5637. {
  5638. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  5639. * for all connections. TLS 1.3 only has tickets so if we don't
  5640. * include the session id in the ticket then the certificates
  5641. * will not be available on resumption. */
  5642. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  5643. AssertNotNull(peer);
  5644. wolfSSL_X509_free(peer);
  5645. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  5646. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  5647. }
  5648. #endif
  5649. }
  5650. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  5651. {
  5652. /* Set the session to reuse for the client */
  5653. AssertIntEQ(wolfSSL_set_session(ssl,
  5654. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  5655. }
  5656. #endif
  5657. static int test_wolfSSL_CTX_add_session(void)
  5658. {
  5659. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  5660. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  5661. !defined(NO_SESSION_CACHE)
  5662. tcp_ready ready;
  5663. func_args client_args;
  5664. func_args server_args;
  5665. THREAD_TYPE serverThread;
  5666. callback_functions client_cb;
  5667. callback_functions server_cb;
  5668. method_provider methods[][2] = {
  5669. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  5670. !defined(NO_DES3))
  5671. /* Without AES there are almost no ciphersuites available. This leads
  5672. * to no ciphersuites being available and an error. */
  5673. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  5674. #endif
  5675. #ifndef WOLFSSL_NO_TLS12
  5676. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  5677. #endif
  5678. /* Needs the default ticket callback since it is tied to the
  5679. * connection context and this makes it easy to carry over the ticket
  5680. * crypto context between connections */
  5681. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  5682. defined(HAVE_SESSION_TICKET)
  5683. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  5684. #endif
  5685. };
  5686. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  5687. size_t i, j;
  5688. printf(testingFmt, "wolfSSL_CTX_add_session()");
  5689. for (i = 0; i < methodsLen; i++) {
  5690. /* First run creates a connection while the second+ run will attempt
  5691. * to resume the connection. The trick is that the internal cache
  5692. * is turned off. wolfSSL_CTX_add_session should put the session in
  5693. * the cache anyway. */
  5694. test_wolfSSL_CTX_add_session_client_sess = NULL;
  5695. test_wolfSSL_CTX_add_session_server_sess = NULL;
  5696. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  5697. for (j = 0; j < 5; j++) {
  5698. #ifdef WOLFSSL_TIRTOS
  5699. fdOpenSession(Task_self());
  5700. #endif
  5701. StartTCP();
  5702. InitTcpReady(&ready);
  5703. XMEMSET(&client_args, 0, sizeof(func_args));
  5704. XMEMSET(&server_args, 0, sizeof(func_args));
  5705. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  5706. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  5707. client_cb.method = methods[i][0];
  5708. server_cb.method = methods[i][1];
  5709. server_args.signal = &ready;
  5710. server_args.callbacks = &server_cb;
  5711. client_args.signal = &ready;
  5712. client_args.callbacks = &client_cb;
  5713. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  5714. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  5715. server_cb.isSharedCtx = 1;
  5716. }
  5717. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  5718. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  5719. if (j != 0)
  5720. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  5721. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  5722. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  5723. server_cb.ticNoInit = 1; /* Use default builtin */
  5724. start_thread(test_server_nofail, &server_args, &serverThread);
  5725. wait_tcp_ready(&server_args);
  5726. test_client_nofail(&client_args, NULL);
  5727. join_thread(serverThread);
  5728. AssertTrue(client_args.return_code);
  5729. AssertTrue(server_args.return_code);
  5730. FreeTcpReady(&ready);
  5731. }
  5732. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  5733. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  5734. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  5735. }
  5736. printf(resultFmt, passed);
  5737. #endif
  5738. return 0;
  5739. }
  5740. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5741. /* canned export of a session using older version 3 */
  5742. static unsigned char version_3[] = {
  5743. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  5744. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  5745. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  5746. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  5747. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  5748. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5749. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  5750. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  5751. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  5752. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5753. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5754. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  5755. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5756. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  5757. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  5758. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  5759. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  5760. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  5761. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  5762. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  5763. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5764. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5765. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5766. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5767. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5768. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5769. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5770. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5771. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5772. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5773. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5774. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  5775. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  5776. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  5777. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  5778. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  5779. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  5780. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  5781. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  5782. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  5783. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  5784. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5785. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  5786. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  5787. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5788. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  5789. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  5790. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  5791. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  5792. 0x2E, 0x31, 0xED, 0x4F
  5793. };
  5794. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  5795. static int test_wolfSSL_dtls_export(void)
  5796. {
  5797. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  5798. tcp_ready ready;
  5799. func_args client_args;
  5800. func_args server_args;
  5801. THREAD_TYPE serverThread;
  5802. callback_functions server_cbf;
  5803. callback_functions client_cbf;
  5804. #ifdef WOLFSSL_TIRTOS
  5805. fdOpenSession(Task_self());
  5806. #endif
  5807. InitTcpReady(&ready);
  5808. #if defined(USE_WINDOWS_API)
  5809. /* use RNG to get random port if using windows */
  5810. ready.port = GetRandomPort();
  5811. #endif
  5812. /* set using dtls */
  5813. XMEMSET(&client_args, 0, sizeof(func_args));
  5814. XMEMSET(&server_args, 0, sizeof(func_args));
  5815. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5816. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5817. server_cbf.method = wolfDTLSv1_2_server_method;
  5818. client_cbf.method = wolfDTLSv1_2_client_method;
  5819. server_args.callbacks = &server_cbf;
  5820. client_args.callbacks = &client_cbf;
  5821. server_args.signal = &ready;
  5822. client_args.signal = &ready;
  5823. start_thread(run_wolfssl_server, &server_args, &serverThread);
  5824. wait_tcp_ready(&server_args);
  5825. run_wolfssl_client(&client_args);
  5826. join_thread(serverThread);
  5827. AssertTrue(client_args.return_code);
  5828. AssertTrue(server_args.return_code);
  5829. FreeTcpReady(&ready);
  5830. #ifdef WOLFSSL_TIRTOS
  5831. fdOpenSession(Task_self());
  5832. #endif
  5833. {
  5834. SOCKET_T sockfd = 0;
  5835. WOLFSSL_CTX* ctx;
  5836. WOLFSSL* ssl;
  5837. char msg[64] = "hello wolfssl!";
  5838. char reply[1024];
  5839. int msgSz = (int)XSTRLEN(msg);
  5840. byte *session, *window;
  5841. unsigned int sessionSz, windowSz;
  5842. #ifndef TEST_IPV6
  5843. struct sockaddr_in peerAddr;
  5844. #else
  5845. struct sockaddr_in6 peerAddr;
  5846. #endif /* TEST_IPV6 */
  5847. int i;
  5848. /* Set ctx to DTLS 1.2 */
  5849. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  5850. AssertNotNull(ssl = wolfSSL_new(ctx));
  5851. /* test importing version 3 */
  5852. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5853. /* test importing bad length and bad version */
  5854. version_3[2] += 1;
  5855. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5856. version_3[2] -= 1; version_3[1] = 0XA0;
  5857. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  5858. wolfSSL_free(ssl);
  5859. wolfSSL_CTX_free(ctx);
  5860. /* check storing client state after connection and storing window only */
  5861. #ifdef WOLFSSL_TIRTOS
  5862. fdOpenSession(Task_self());
  5863. #endif
  5864. InitTcpReady(&ready);
  5865. #if defined(USE_WINDOWS_API)
  5866. /* use RNG to get random port if using windows */
  5867. ready.port = GetRandomPort();
  5868. #endif
  5869. /* set using dtls */
  5870. XMEMSET(&server_args, 0, sizeof(func_args));
  5871. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  5872. server_cbf.method = wolfDTLSv1_2_server_method;
  5873. server_cbf.doUdp = 1;
  5874. server_args.callbacks = &server_cbf;
  5875. server_args.argc = 3; /* set loop_count to 3 */
  5876. server_args.signal = &ready;
  5877. start_thread(test_server_nofail, &server_args, &serverThread);
  5878. wait_tcp_ready(&server_args);
  5879. /* create and connect with client */
  5880. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  5881. AssertIntEQ(WOLFSSL_SUCCESS,
  5882. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  5883. AssertIntEQ(WOLFSSL_SUCCESS,
  5884. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  5885. AssertIntEQ(WOLFSSL_SUCCESS,
  5886. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  5887. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  5888. AssertNotNull(ssl = wolfSSL_new(ctx));
  5889. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5890. /* store server information connected too */
  5891. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  5892. #ifndef TEST_IPV6
  5893. peerAddr.sin_family = AF_INET;
  5894. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  5895. peerAddr.sin_port = XHTONS(server_args.signal->port);
  5896. #else
  5897. peerAddr.sin6_family = AF_INET6;
  5898. AssertIntEQ(
  5899. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  5900. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  5901. #endif
  5902. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  5903. WOLFSSL_SUCCESS);
  5904. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  5905. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  5906. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5907. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  5908. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5909. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5910. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  5911. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5912. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5913. wolfSSL_free(ssl);
  5914. for (i = 1; i < server_args.argc; i++) {
  5915. /* restore state */
  5916. AssertNotNull(ssl = wolfSSL_new(ctx));
  5917. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  5918. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  5919. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  5920. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  5921. WOLFSSL_SUCCESS);
  5922. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  5923. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  5924. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  5925. wolfSSL_free(ssl);
  5926. }
  5927. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5928. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  5929. wolfSSL_CTX_free(ctx);
  5930. printf("done and waiting for server\n");
  5931. join_thread(serverThread);
  5932. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5933. FreeTcpReady(&ready);
  5934. #ifdef WOLFSSL_TIRTOS
  5935. fdOpenSession(Task_self());
  5936. #endif
  5937. }
  5938. printf(testingFmt, "wolfSSL_dtls_export()");
  5939. printf(resultFmt, passed);
  5940. #endif
  5941. return 0;
  5942. }
  5943. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  5944. #ifdef WOLFSSL_TLS13
  5945. static const byte canned_client_tls13_session[] = {
  5946. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  5947. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5948. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  5949. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5950. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  5951. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5952. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5953. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  5954. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5955. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5956. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5957. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  5958. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5959. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5960. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5961. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  5962. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  5963. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  5964. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  5965. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  5966. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  5967. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  5968. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  5969. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  5970. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  5971. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  5972. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  5973. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  5974. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  5975. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  5976. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  5977. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  5978. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  5979. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  5980. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  5981. 0x00, 0x03
  5982. };
  5983. static const byte canned_server_tls13_session[] = {
  5984. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  5985. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  5986. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  5987. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  5988. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5989. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  5990. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  5991. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  5992. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  5993. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5994. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  5995. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  5996. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  5997. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  5998. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  5999. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6000. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6001. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6002. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6003. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6004. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6005. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6006. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6007. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6008. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6009. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6010. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6011. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6012. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6013. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  6014. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  6015. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  6016. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6017. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6018. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6019. 0x00, 0x04
  6020. };
  6021. #endif /* WOLFSSL_TLS13 */
  6022. static const byte canned_client_session[] = {
  6023. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6024. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6025. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  6026. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6027. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6028. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6029. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6030. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  6031. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6032. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6033. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6034. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  6035. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6036. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6037. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6038. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6039. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6040. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6041. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6042. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6043. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6044. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6045. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6046. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6047. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6048. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6049. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6050. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6051. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6052. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6053. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6054. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6055. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6056. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6057. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6058. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6059. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6060. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6061. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6062. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6063. 0x00, 0x03
  6064. };
  6065. static const byte canned_server_session[] = {
  6066. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6067. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6068. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6069. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6070. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6071. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6072. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6073. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  6074. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6075. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6076. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6077. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  6078. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6079. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6080. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6081. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6082. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6083. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6084. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6085. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6086. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6087. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6088. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6089. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6090. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6091. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6092. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6093. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6094. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6095. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6096. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6097. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6098. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6099. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6100. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6101. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6102. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6103. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6104. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6105. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6106. 0x00, 0x04
  6107. };
  6108. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  6109. {
  6110. SOCKET_T sockfd = 0;
  6111. SOCKET_T clientfd = 0;
  6112. word16 port;
  6113. callback_functions* cbf;
  6114. WOLFSSL_CTX* ctx = 0;
  6115. WOLFSSL* ssl = 0;
  6116. char msg[] = "I hear you fa shizzle!";
  6117. char input[1024];
  6118. int idx;
  6119. #ifdef WOLFSSL_TIRTOS
  6120. fdOpenSession(Task_self());
  6121. #endif
  6122. ((func_args*)args)->return_code = TEST_FAIL;
  6123. cbf = ((func_args*)args)->callbacks;
  6124. {
  6125. WOLFSSL_METHOD* method = NULL;
  6126. if (cbf != NULL && cbf->method != NULL) {
  6127. method = cbf->method();
  6128. }
  6129. else {
  6130. method = wolfTLSv1_2_server_method();
  6131. }
  6132. ctx = wolfSSL_CTX_new(method);
  6133. }
  6134. if (ctx == NULL) {
  6135. goto done;
  6136. }
  6137. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6138. #if defined(USE_WINDOWS_API)
  6139. port = ((func_args*)args)->signal->port;
  6140. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6141. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6142. /* Let tcp_listen assign port */
  6143. port = 0;
  6144. #else
  6145. /* Use default port */
  6146. port = wolfSSLPort;
  6147. #endif
  6148. /* do it here to detect failure */
  6149. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6150. CloseSocket(sockfd);
  6151. /* call ctx setup callback */
  6152. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6153. cbf->ctx_ready(ctx);
  6154. }
  6155. ssl = wolfSSL_new(ctx);
  6156. if (ssl == NULL) {
  6157. goto done;
  6158. }
  6159. wolfSSL_set_fd(ssl, clientfd);
  6160. /* call ssl setup callback */
  6161. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6162. cbf->ssl_ready(ssl);
  6163. }
  6164. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6165. if (idx > 0) {
  6166. input[idx] = '\0';
  6167. printf("Client message export/import: %s\n", input);
  6168. }
  6169. else {
  6170. printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx));
  6171. goto done;
  6172. }
  6173. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6174. /*err_sys("SSL_write failed");*/
  6175. #ifdef WOLFSSL_TIRTOS
  6176. return;
  6177. #else
  6178. return 0;
  6179. #endif
  6180. }
  6181. #ifdef WOLFSSL_TIRTOS
  6182. Task_yield();
  6183. #endif
  6184. ((func_args*)args)->return_code = TEST_SUCCESS;
  6185. done:
  6186. wolfSSL_shutdown(ssl);
  6187. wolfSSL_free(ssl);
  6188. wolfSSL_CTX_free(ctx);
  6189. CloseSocket(clientfd);
  6190. #ifdef WOLFSSL_TIRTOS
  6191. fdCloseSession(Task_self());
  6192. #endif
  6193. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6194. && defined(HAVE_THREAD_LS)
  6195. wc_ecc_fp_free(); /* free per thread cache */
  6196. #endif
  6197. #if defined(HAVE_SESSION_TICKET) && \
  6198. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6199. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  6200. OpenSSLTicketCleanup();
  6201. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6202. TicketCleanup();
  6203. #endif
  6204. #endif
  6205. #ifndef WOLFSSL_TIRTOS
  6206. return 0;
  6207. #endif
  6208. }
  6209. static void load_tls12_canned_server(WOLFSSL* ssl)
  6210. {
  6211. int clientfd = wolfSSL_get_fd(ssl);
  6212. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  6213. sizeof(canned_server_session)), sizeof(canned_server_session));
  6214. wolfSSL_set_fd(ssl, clientfd);
  6215. }
  6216. #ifdef WOLFSSL_TLS13
  6217. static void load_tls13_canned_server(WOLFSSL* ssl)
  6218. {
  6219. int clientfd = wolfSSL_get_fd(ssl);
  6220. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  6221. sizeof(canned_server_tls13_session)),
  6222. sizeof(canned_server_tls13_session));
  6223. wolfSSL_set_fd(ssl, clientfd);
  6224. }
  6225. #endif
  6226. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  6227. static int test_wolfSSL_tls_export_run(int v)
  6228. {
  6229. SOCKET_T sockfd = 0;
  6230. WOLFSSL_CTX* ctx = 0;
  6231. WOLFSSL* ssl = 0;
  6232. char msg[64] = "hello wolfssl!";
  6233. char reply[1024];
  6234. word32 replySz;
  6235. int msgSz = (int)XSTRLEN(msg);
  6236. const byte* clientSession = NULL;
  6237. int clientSessionSz = 0;
  6238. tcp_ready ready;
  6239. func_args server_args;
  6240. THREAD_TYPE serverThread;
  6241. callback_functions server_cbf;
  6242. #ifdef WOLFSSL_TIRTOS
  6243. fdOpenSession(Task_self());
  6244. #endif
  6245. InitTcpReady(&ready);
  6246. #if defined(USE_WINDOWS_API)
  6247. /* use RNG to get random port if using windows */
  6248. ready.port = GetRandomPort();
  6249. #endif
  6250. XMEMSET(&server_args, 0, sizeof(func_args));
  6251. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6252. switch (v) {
  6253. case WOLFSSL_TLSV1_2:
  6254. server_cbf.method = wolfTLSv1_2_server_method;
  6255. server_cbf.ssl_ready = load_tls12_canned_server;
  6256. /* setup the client side */
  6257. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6258. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6259. clientSession = canned_client_session;
  6260. clientSessionSz = sizeof(canned_client_session);
  6261. break;
  6262. #ifdef WOLFSSL_TLS13
  6263. case WOLFSSL_TLSV1_3:
  6264. server_cbf.method = wolfTLSv1_3_server_method;
  6265. server_cbf.ssl_ready = load_tls13_canned_server;
  6266. /* setup the client side */
  6267. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6268. clientSession = canned_client_tls13_session;
  6269. clientSessionSz = sizeof(canned_client_tls13_session);
  6270. break;
  6271. #endif
  6272. }
  6273. server_args.callbacks = &server_cbf;
  6274. server_args.signal = &ready;
  6275. start_thread(tls_export_server, &server_args, &serverThread);
  6276. wait_tcp_ready(&server_args);
  6277. #ifdef WOLFSSL_TIRTOS
  6278. fdOpenSession(Task_self());
  6279. #endif
  6280. AssertNotNull(ssl = wolfSSL_new(ctx));
  6281. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6282. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6283. clientSessionSz);
  6284. replySz = sizeof(reply);
  6285. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6286. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6287. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6288. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6289. #endif
  6290. wolfSSL_set_fd(ssl, sockfd);
  6291. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6292. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6293. wolfSSL_free(ssl);
  6294. wolfSSL_CTX_free(ctx);
  6295. CloseSocket(sockfd);
  6296. #ifdef WOLFSSL_TIRTOS
  6297. fdCloseSession(Task_self());
  6298. #endif
  6299. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6300. && defined(HAVE_THREAD_LS)
  6301. wc_ecc_fp_free(); /* free per thread cache */
  6302. #endif
  6303. join_thread(serverThread);
  6304. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6305. FreeTcpReady(&ready);
  6306. #ifdef WOLFSSL_TIRTOS
  6307. fdOpenSession(Task_self());
  6308. #endif
  6309. return 0;
  6310. }
  6311. #endif
  6312. static int test_wolfSSL_tls_export(void)
  6313. {
  6314. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6315. printf(testingFmt, "wolfSSL_tls_export()");
  6316. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6317. #ifdef WOLFSSL_TLS13
  6318. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6319. #endif
  6320. printf(resultFmt, passed);
  6321. #endif
  6322. return 0;
  6323. }
  6324. /*----------------------------------------------------------------------------*
  6325. | TLS extensions tests
  6326. *----------------------------------------------------------------------------*/
  6327. #ifdef ENABLE_TLS_CALLBACK_TEST
  6328. /* Connection test runner - generic */
  6329. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6330. callback_functions* server_callbacks)
  6331. {
  6332. tcp_ready ready;
  6333. func_args client_args;
  6334. func_args server_args;
  6335. THREAD_TYPE serverThread;
  6336. XMEMSET(&client_args, 0, sizeof(func_args));
  6337. XMEMSET(&server_args, 0, sizeof(func_args));
  6338. StartTCP();
  6339. client_args.callbacks = client_callbacks;
  6340. server_args.callbacks = server_callbacks;
  6341. #ifdef WOLFSSL_TIRTOS
  6342. fdOpenSession(Task_self());
  6343. #endif
  6344. /* RUN Server side */
  6345. InitTcpReady(&ready);
  6346. #if defined(USE_WINDOWS_API)
  6347. /* use RNG to get random port if using windows */
  6348. ready.port = GetRandomPort();
  6349. #endif
  6350. server_args.signal = &ready;
  6351. client_args.signal = &ready;
  6352. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6353. wait_tcp_ready(&server_args);
  6354. /* RUN Client side */
  6355. run_wolfssl_client(&client_args);
  6356. join_thread(serverThread);
  6357. FreeTcpReady(&ready);
  6358. #ifdef WOLFSSL_TIRTOS
  6359. fdCloseSession(Task_self());
  6360. #endif
  6361. client_callbacks->return_code = client_args.return_code;
  6362. server_callbacks->return_code = server_args.return_code;
  6363. }
  6364. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6365. #ifdef HAVE_SNI
  6366. static int test_wolfSSL_UseSNI_params(void)
  6367. {
  6368. #if !defined(NO_WOLFSSL_CLIENT)
  6369. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6370. WOLFSSL *ssl = wolfSSL_new(ctx);
  6371. AssertNotNull(ctx);
  6372. AssertNotNull(ssl);
  6373. /* invalid [ctx|ssl] */
  6374. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6375. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6376. /* invalid type */
  6377. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6378. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6379. /* invalid data */
  6380. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6381. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6382. /* success case */
  6383. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6384. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6385. wolfSSL_free(ssl);
  6386. wolfSSL_CTX_free(ctx);
  6387. #endif /* !NO_WOLFSSL_CLIENT */
  6388. return 0;
  6389. }
  6390. /* BEGIN of connection tests callbacks */
  6391. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6392. {
  6393. AssertIntEQ(WOLFSSL_SUCCESS,
  6394. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6395. }
  6396. static void use_SNI_at_ssl(WOLFSSL* ssl)
  6397. {
  6398. AssertIntEQ(WOLFSSL_SUCCESS,
  6399. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6400. }
  6401. static void different_SNI_at_ssl(WOLFSSL* ssl)
  6402. {
  6403. AssertIntEQ(WOLFSSL_SUCCESS,
  6404. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  6405. }
  6406. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  6407. {
  6408. use_SNI_at_ssl(ssl);
  6409. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6410. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  6411. }
  6412. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  6413. {
  6414. use_SNI_at_ssl(ssl);
  6415. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6416. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  6417. }
  6418. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6419. {
  6420. use_SNI_at_ctx(ctx);
  6421. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6422. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6423. }
  6424. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  6425. {
  6426. use_SNI_at_ssl(ssl);
  6427. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6428. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6429. }
  6430. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6431. {
  6432. use_SNI_at_ctx(ctx);
  6433. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6434. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6435. }
  6436. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  6437. {
  6438. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  6439. }
  6440. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  6441. {
  6442. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  6443. }
  6444. static void verify_SNI_no_matching(WOLFSSL* ssl)
  6445. {
  6446. byte type = WOLFSSL_SNI_HOST_NAME;
  6447. void* request = (void*) &type; /* to be overwritten */
  6448. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  6449. AssertNotNull(request);
  6450. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6451. AssertNull(request);
  6452. }
  6453. static void verify_SNI_real_matching(WOLFSSL* ssl)
  6454. {
  6455. byte type = WOLFSSL_SNI_HOST_NAME;
  6456. void* request = NULL;
  6457. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  6458. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6459. AssertNotNull(request);
  6460. AssertStrEQ("www.wolfssl.com", (char*)request);
  6461. }
  6462. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  6463. {
  6464. byte type = WOLFSSL_SNI_HOST_NAME;
  6465. void* request = NULL;
  6466. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  6467. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6468. AssertNotNull(request);
  6469. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  6470. }
  6471. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6472. {
  6473. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  6474. }
  6475. /* END of connection tests callbacks */
  6476. static int test_wolfSSL_UseSNI_connection(void)
  6477. {
  6478. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  6479. callback_functions client_cb;
  6480. callback_functions server_cb;
  6481. size_t i;
  6482. struct {
  6483. method_provider client_meth;
  6484. method_provider server_meth;
  6485. } methods[] = {
  6486. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  6487. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  6488. #endif
  6489. #ifndef WOLFSSL_NO_TLS12
  6490. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  6491. #endif
  6492. #ifdef WOLFSSL_TLS13
  6493. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  6494. #endif
  6495. };
  6496. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  6497. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6498. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6499. client_cb.method = methods[i].client_meth;
  6500. server_cb.method = methods[i].server_meth;
  6501. client_cb.devId = testDevId;
  6502. server_cb.devId = testDevId;
  6503. /* success case at ctx */
  6504. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6505. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6506. test_wolfSSL_client_server(&client_cb, &server_cb);
  6507. /* success case at ssl */
  6508. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  6509. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  6510. test_wolfSSL_client_server(&client_cb, &server_cb);
  6511. /* default mismatch behavior */
  6512. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6513. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  6514. test_wolfSSL_client_server(&client_cb, &server_cb);
  6515. /* continue on mismatch */
  6516. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6517. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  6518. test_wolfSSL_client_server(&client_cb, &server_cb);
  6519. /* fake answer on mismatch */
  6520. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6521. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  6522. test_wolfSSL_client_server(&client_cb, &server_cb);
  6523. /* sni abort - success */
  6524. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6525. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6526. test_wolfSSL_client_server(&client_cb, &server_cb);
  6527. /* sni abort - abort when absent (ctx) */
  6528. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6529. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6530. test_wolfSSL_client_server(&client_cb, &server_cb);
  6531. /* sni abort - abort when absent (ssl) */
  6532. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6533. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6534. test_wolfSSL_client_server(&client_cb, &server_cb);
  6535. /* sni abort - success when overwritten */
  6536. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6537. 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;
  6538. test_wolfSSL_client_server(&client_cb, &server_cb);
  6539. /* sni abort - success when allowing mismatches */
  6540. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6541. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  6542. test_wolfSSL_client_server(&client_cb, &server_cb);
  6543. }
  6544. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  6545. return 0;
  6546. }
  6547. static int test_wolfSSL_SNI_GetFromBuffer(void)
  6548. {
  6549. byte buff[] = { /* www.paypal.com */
  6550. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  6551. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  6552. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  6553. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6554. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6555. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  6556. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  6557. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  6558. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6559. };
  6560. byte buff2[] = { /* api.textmate.org */
  6561. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  6562. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  6563. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  6564. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  6565. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  6566. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  6567. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  6568. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  6569. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  6570. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  6571. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  6572. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  6573. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  6574. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  6575. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  6576. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  6577. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  6578. };
  6579. byte buff3[] = { /* no sni extension */
  6580. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  6581. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  6582. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  6583. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6584. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6585. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  6586. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6587. };
  6588. byte buff4[] = { /* last extension has zero size */
  6589. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  6590. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6591. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6592. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6593. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  6594. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  6595. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  6596. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  6597. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  6598. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  6599. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  6600. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  6601. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  6602. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  6603. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  6604. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  6605. 0x12, 0x00, 0x00
  6606. };
  6607. byte buff5[] = { /* SSL v2.0 client hello */
  6608. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  6609. /* dummy bytes bellow, just to pass size check */
  6610. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  6611. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  6612. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  6613. };
  6614. byte result[32] = {0};
  6615. word32 length = 32;
  6616. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  6617. 0, result, &length));
  6618. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  6619. 0, result, &length));
  6620. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6621. 1, result, &length));
  6622. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6623. 0, result, &length));
  6624. buff[0] = 0x16;
  6625. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6626. 0, result, &length));
  6627. buff[1] = 0x03;
  6628. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  6629. sizeof(buff), 0, result, &length));
  6630. buff[2] = 0x03;
  6631. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  6632. sizeof(buff), 0, result, &length));
  6633. buff[4] = 0x64;
  6634. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  6635. 0, result, &length));
  6636. result[length] = 0;
  6637. AssertStrEQ("www.paypal.com", (const char*) result);
  6638. length = 32;
  6639. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  6640. 0, result, &length));
  6641. result[length] = 0;
  6642. AssertStrEQ("api.textmate.org", (const char*) result);
  6643. /* SSL v2.0 tests */
  6644. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  6645. sizeof(buff5), 0, result, &length));
  6646. buff5[2] = 0x02;
  6647. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6648. sizeof(buff5), 0, result, &length));
  6649. buff5[2] = 0x01; buff5[6] = 0x08;
  6650. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6651. sizeof(buff5), 0, result, &length));
  6652. buff5[6] = 0x09; buff5[8] = 0x01;
  6653. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  6654. sizeof(buff5), 0, result, &length));
  6655. return 0;
  6656. }
  6657. #endif /* HAVE_SNI */
  6658. static int test_wolfSSL_UseSNI(void)
  6659. {
  6660. #ifdef HAVE_SNI
  6661. test_wolfSSL_UseSNI_params();
  6662. test_wolfSSL_UseSNI_connection();
  6663. test_wolfSSL_SNI_GetFromBuffer();
  6664. #endif
  6665. return 0;
  6666. }
  6667. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  6668. static int test_wolfSSL_UseTrustedCA(void)
  6669. {
  6670. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  6671. && !defined(NO_RSA)
  6672. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  6673. WOLFSSL_CTX *ctx;
  6674. WOLFSSL *ssl;
  6675. byte id[20];
  6676. #ifndef NO_WOLFSSL_SERVER
  6677. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  6678. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6679. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6680. #else
  6681. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  6682. #endif
  6683. AssertNotNull((ssl = wolfSSL_new(ctx)));
  6684. XMEMSET(id, 0, sizeof(id));
  6685. /* error cases */
  6686. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  6687. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6688. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  6689. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6690. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  6691. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6692. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  6693. #ifdef NO_SHA
  6694. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6695. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6696. #endif
  6697. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6698. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  6699. /* success cases */
  6700. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6701. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  6702. #ifndef NO_SHA
  6703. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6704. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  6705. #endif
  6706. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  6707. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  6708. wolfSSL_free(ssl);
  6709. wolfSSL_CTX_free(ctx);
  6710. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  6711. #endif /* HAVE_TRUSTED_CA */
  6712. return 0;
  6713. }
  6714. static int test_wolfSSL_UseMaxFragment(void)
  6715. {
  6716. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  6717. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6718. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  6719. #ifndef NO_WOLFSSL_SERVER
  6720. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6721. #else
  6722. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6723. #endif
  6724. WOLFSSL *ssl;
  6725. #ifdef OPENSSL_EXTRA
  6726. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  6727. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  6728. #else
  6729. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  6730. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  6731. #endif
  6732. #ifndef NO_WOLFSSL_SERVER
  6733. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6734. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6735. #endif
  6736. AssertNotNull(ctx);
  6737. ssl = wolfSSL_new(ctx);
  6738. AssertNotNull(ssl);
  6739. #ifdef OPENSSL_EXTRA
  6740. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  6741. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  6742. #else
  6743. UseMaxFragment = wolfSSL_UseMaxFragment;
  6744. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  6745. #endif
  6746. /* error cases */
  6747. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  6748. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  6749. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  6750. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  6751. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  6752. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  6753. /* success case */
  6754. #ifdef OPENSSL_EXTRA
  6755. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6756. #else
  6757. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  6758. #endif
  6759. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  6760. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  6761. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  6762. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  6763. #ifdef OPENSSL_EXTRA
  6764. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6765. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6766. #else
  6767. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  6768. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  6769. #endif
  6770. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  6771. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  6772. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  6773. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  6774. #ifdef OPENSSL_EXTRA
  6775. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6776. #else
  6777. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  6778. #endif
  6779. wolfSSL_free(ssl);
  6780. wolfSSL_CTX_free(ctx);
  6781. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  6782. #endif
  6783. return 0;
  6784. }
  6785. static int test_wolfSSL_UseTruncatedHMAC(void)
  6786. {
  6787. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  6788. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  6789. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  6790. #ifndef NO_WOLFSSL_SERVER
  6791. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  6792. #else
  6793. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6794. #endif
  6795. WOLFSSL *ssl;
  6796. AssertNotNull(ctx);
  6797. #ifndef NO_WOLFSSL_SERVER
  6798. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  6799. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  6800. #endif
  6801. ssl = wolfSSL_new(ctx);
  6802. AssertNotNull(ssl);
  6803. /* error cases */
  6804. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  6805. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  6806. /* success case */
  6807. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  6808. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  6809. wolfSSL_free(ssl);
  6810. wolfSSL_CTX_free(ctx);
  6811. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  6812. #endif
  6813. return 0;
  6814. }
  6815. static int test_wolfSSL_UseSupportedCurve(void)
  6816. {
  6817. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  6818. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6819. WOLFSSL *ssl = wolfSSL_new(ctx);
  6820. AssertNotNull(ctx);
  6821. AssertNotNull(ssl);
  6822. /* error cases */
  6823. AssertIntNE(WOLFSSL_SUCCESS,
  6824. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6825. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  6826. AssertIntNE(WOLFSSL_SUCCESS,
  6827. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  6828. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  6829. /* success case */
  6830. AssertIntEQ(WOLFSSL_SUCCESS,
  6831. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  6832. AssertIntEQ(WOLFSSL_SUCCESS,
  6833. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  6834. wolfSSL_free(ssl);
  6835. wolfSSL_CTX_free(ctx);
  6836. #endif
  6837. return 0;
  6838. }
  6839. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  6840. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6841. {
  6842. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  6843. }
  6844. static void use_ALPN_all(WOLFSSL* ssl)
  6845. {
  6846. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6847. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6848. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6849. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6850. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6851. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6852. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6853. }
  6854. static void use_ALPN_all_continue(WOLFSSL* ssl)
  6855. {
  6856. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6857. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6858. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6859. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6860. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6861. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  6862. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6863. }
  6864. static void use_ALPN_one(WOLFSSL* ssl)
  6865. {
  6866. /* spdy/2 */
  6867. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6868. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6869. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6870. }
  6871. static void use_ALPN_unknown(WOLFSSL* ssl)
  6872. {
  6873. /* http/2.0 */
  6874. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6875. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6876. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  6877. }
  6878. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  6879. {
  6880. /* http/2.0 */
  6881. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  6882. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  6883. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  6884. }
  6885. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  6886. {
  6887. /* spdy/3 */
  6888. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6889. char *proto = NULL;
  6890. word16 protoSz = 0;
  6891. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6892. /* check value */
  6893. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  6894. if (proto) {
  6895. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  6896. }
  6897. }
  6898. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  6899. {
  6900. char *proto = NULL;
  6901. word16 protoSz = 0;
  6902. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  6903. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6904. /* check value */
  6905. AssertIntEQ(1, (0 == protoSz));
  6906. AssertIntEQ(1, (NULL == proto));
  6907. }
  6908. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  6909. {
  6910. /* http/1.1 */
  6911. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6912. char *proto;
  6913. word16 protoSz = 0;
  6914. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6915. /* check value */
  6916. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6917. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6918. }
  6919. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  6920. {
  6921. /* spdy/2 */
  6922. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  6923. char *proto;
  6924. word16 protoSz = 0;
  6925. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  6926. /* check value */
  6927. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  6928. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  6929. }
  6930. static void verify_ALPN_client_list(WOLFSSL* ssl)
  6931. {
  6932. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  6933. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  6934. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  6935. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  6936. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  6937. char *clist = NULL;
  6938. word16 clistSz = 0;
  6939. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  6940. &clistSz));
  6941. /* check value */
  6942. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  6943. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  6944. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  6945. }
  6946. static int test_wolfSSL_UseALPN_connection(void)
  6947. {
  6948. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  6949. callback_functions client_cb;
  6950. callback_functions server_cb;
  6951. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6952. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6953. client_cb.method = wolfSSLv23_client_method;
  6954. server_cb.method = wolfSSLv23_server_method;
  6955. client_cb.devId = testDevId;
  6956. server_cb.devId = testDevId;
  6957. /* success case same list */
  6958. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6959. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  6960. test_wolfSSL_client_server(&client_cb, &server_cb);
  6961. /* success case only one for server */
  6962. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6963. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  6964. test_wolfSSL_client_server(&client_cb, &server_cb);
  6965. /* success case only one for client */
  6966. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  6967. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  6968. test_wolfSSL_client_server(&client_cb, &server_cb);
  6969. /* success case none for client */
  6970. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6971. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  6972. test_wolfSSL_client_server(&client_cb, &server_cb);
  6973. /* success case mismatch behavior but option 'continue' set */
  6974. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  6975. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  6976. test_wolfSSL_client_server(&client_cb, &server_cb);
  6977. /* success case read protocol send by client */
  6978. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6979. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  6980. test_wolfSSL_client_server(&client_cb, &server_cb);
  6981. /* mismatch behavior with same list
  6982. * the first and only this one must be taken */
  6983. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6984. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  6985. test_wolfSSL_client_server(&client_cb, &server_cb);
  6986. /* default mismatch behavior */
  6987. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  6988. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  6989. test_wolfSSL_client_server(&client_cb, &server_cb);
  6990. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  6991. return 0;
  6992. }
  6993. static int test_wolfSSL_UseALPN_params(void)
  6994. {
  6995. #ifndef NO_WOLFSSL_CLIENT
  6996. /* "http/1.1" */
  6997. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  6998. /* "spdy/1" */
  6999. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  7000. /* "spdy/2" */
  7001. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7002. /* "spdy/3" */
  7003. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7004. char buff[256];
  7005. word32 idx;
  7006. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7007. WOLFSSL *ssl = wolfSSL_new(ctx);
  7008. AssertNotNull(ctx);
  7009. AssertNotNull(ssl);
  7010. /* error cases */
  7011. AssertIntNE(WOLFSSL_SUCCESS,
  7012. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  7013. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7014. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  7015. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7016. /* success case */
  7017. /* http1 only */
  7018. AssertIntEQ(WOLFSSL_SUCCESS,
  7019. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  7020. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7021. /* http1, spdy1 */
  7022. XMEMCPY(buff, http1, sizeof(http1));
  7023. idx = sizeof(http1);
  7024. buff[idx++] = ',';
  7025. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7026. idx += sizeof(spdy1);
  7027. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7028. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7029. /* http1, spdy2, spdy1 */
  7030. XMEMCPY(buff, http1, sizeof(http1));
  7031. idx = sizeof(http1);
  7032. buff[idx++] = ',';
  7033. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7034. idx += sizeof(spdy2);
  7035. buff[idx++] = ',';
  7036. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7037. idx += sizeof(spdy1);
  7038. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7039. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7040. /* spdy3, http1, spdy2, spdy1 */
  7041. XMEMCPY(buff, spdy3, sizeof(spdy3));
  7042. idx = sizeof(spdy3);
  7043. buff[idx++] = ',';
  7044. XMEMCPY(buff+idx, http1, sizeof(http1));
  7045. idx += sizeof(http1);
  7046. buff[idx++] = ',';
  7047. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7048. idx += sizeof(spdy2);
  7049. buff[idx++] = ',';
  7050. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7051. idx += sizeof(spdy1);
  7052. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7053. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7054. wolfSSL_free(ssl);
  7055. wolfSSL_CTX_free(ctx);
  7056. #endif
  7057. return 0;
  7058. }
  7059. #endif /* HAVE_ALPN */
  7060. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7061. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  7062. {
  7063. unsigned char p[] = {
  7064. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7065. 6, 's', 'p', 'd', 'y', '/', '2',
  7066. 6, 's', 'p', 'd', 'y', '/', '1',
  7067. };
  7068. unsigned char p_len = sizeof(p);
  7069. int ret;
  7070. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  7071. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7072. AssertIntEQ(ret, 0);
  7073. #else
  7074. AssertIntEQ(ret, SSL_SUCCESS);
  7075. #endif
  7076. }
  7077. static void set_alpn_protos(SSL* ssl)
  7078. {
  7079. unsigned char p[] = {
  7080. 6, 's', 'p', 'd', 'y', '/', '3',
  7081. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7082. 6, 's', 'p', 'd', 'y', '/', '2',
  7083. 6, 's', 'p', 'd', 'y', '/', '1',
  7084. };
  7085. unsigned char p_len = sizeof(p);
  7086. int ret;
  7087. ret = SSL_set_alpn_protos(ssl, p, p_len);
  7088. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7089. AssertIntEQ(ret, 0);
  7090. #else
  7091. AssertIntEQ(ret, SSL_SUCCESS);
  7092. #endif
  7093. }
  7094. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  7095. {
  7096. /* "spdy/3" */
  7097. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7098. const unsigned char *proto;
  7099. unsigned int protoSz = 0;
  7100. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7101. /* check value */
  7102. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7103. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7104. }
  7105. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  7106. {
  7107. /* "http/1.1" */
  7108. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7109. const unsigned char *proto;
  7110. unsigned int protoSz = 0;
  7111. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7112. /* check value */
  7113. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7114. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7115. }
  7116. static int test_wolfSSL_set_alpn_protos(void)
  7117. {
  7118. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7119. callback_functions client_cb;
  7120. callback_functions server_cb;
  7121. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7122. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7123. client_cb.method = wolfSSLv23_client_method;
  7124. server_cb.method = wolfSSLv23_server_method;
  7125. client_cb.devId = testDevId;
  7126. server_cb.devId = testDevId;
  7127. /* use CTX_alpn_protos */
  7128. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7129. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  7130. test_wolfSSL_client_server(&client_cb, &server_cb);
  7131. /* use set_alpn_protos */
  7132. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  7133. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  7134. test_wolfSSL_client_server(&client_cb, &server_cb);
  7135. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7136. return 0;
  7137. }
  7138. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  7139. static int test_wolfSSL_UseALPN(void)
  7140. {
  7141. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  7142. defined(HAVE_IO_TESTS_DEPENDENCIES)
  7143. test_wolfSSL_UseALPN_connection();
  7144. test_wolfSSL_UseALPN_params();
  7145. #endif
  7146. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7147. test_wolfSSL_set_alpn_protos();
  7148. #endif
  7149. return 0;
  7150. }
  7151. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  7152. {
  7153. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  7154. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7155. WOLFSSL *ssl = wolfSSL_new(ctx);
  7156. AssertNotNull(ctx);
  7157. AssertNotNull(ssl);
  7158. /* error cases */
  7159. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  7160. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  7161. /* success cases */
  7162. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  7163. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  7164. wolfSSL_free(ssl);
  7165. wolfSSL_CTX_free(ctx);
  7166. #endif
  7167. return 0;
  7168. }
  7169. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  7170. {
  7171. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  7172. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7173. WOLFSSL *ssl = wolfSSL_new(ctx);
  7174. AssertNotNull(ctx);
  7175. AssertNotNull(ssl);
  7176. /* error cases */
  7177. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  7178. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  7179. /* success cases */
  7180. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  7181. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  7182. wolfSSL_free(ssl);
  7183. wolfSSL_CTX_free(ctx);
  7184. #endif
  7185. return 0;
  7186. }
  7187. /*----------------------------------------------------------------------------*
  7188. | X509 Tests
  7189. *----------------------------------------------------------------------------*/
  7190. static int test_wolfSSL_X509_NAME_get_entry(void)
  7191. {
  7192. #if !defined(NO_CERTS) && !defined(NO_RSA)
  7193. #if defined(OPENSSL_ALL) || \
  7194. (defined(OPENSSL_EXTRA) && \
  7195. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  7196. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  7197. {
  7198. /* use openssl like name to test mapping */
  7199. X509_NAME_ENTRY* ne;
  7200. X509_NAME* name;
  7201. X509* x509;
  7202. #ifndef NO_FILESYSTEM
  7203. ASN1_STRING* asn;
  7204. char* subCN = NULL;
  7205. #endif
  7206. int idx;
  7207. ASN1_OBJECT *object = NULL;
  7208. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  7209. #ifndef NO_BIO
  7210. BIO* bio;
  7211. #endif
  7212. #endif
  7213. #ifndef NO_FILESYSTEM
  7214. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  7215. AssertNotNull(x509);
  7216. name = X509_get_subject_name(x509);
  7217. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7218. AssertIntGE(idx, 0);
  7219. ne = X509_NAME_get_entry(name, idx);
  7220. AssertNotNull(ne);
  7221. asn = X509_NAME_ENTRY_get_data(ne);
  7222. AssertNotNull(asn);
  7223. subCN = (char*)ASN1_STRING_data(asn);
  7224. AssertNotNull(subCN);
  7225. wolfSSL_FreeX509(x509);
  7226. #endif
  7227. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  7228. AssertNotNull(x509);
  7229. name = X509_get_subject_name(x509);
  7230. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7231. AssertIntGE(idx, 0);
  7232. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  7233. #ifndef NO_BIO
  7234. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7235. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  7236. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7237. AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
  7238. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7239. BIO_free(bio);
  7240. #endif
  7241. #endif
  7242. ne = X509_NAME_get_entry(name, idx);
  7243. AssertNotNull(ne);
  7244. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  7245. wolfSSL_FreeX509(x509);
  7246. }
  7247. printf(resultFmt, passed);
  7248. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  7249. #endif /* !NO_CERTS && !NO_RSA */
  7250. return 0;
  7251. }
  7252. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  7253. static int test_wolfSSL_PKCS12(void)
  7254. {
  7255. /* .p12 file is encrypted with DES3 */
  7256. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  7257. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  7258. * Password Key
  7259. */
  7260. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  7261. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  7262. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  7263. byte buf[6000];
  7264. char file[] = "./certs/test-servercert.p12";
  7265. char order[] = "./certs/ecc-rsa-server.p12";
  7266. #ifdef WC_RC2
  7267. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  7268. #endif
  7269. char pass[] = "a password";
  7270. const char goodPsw[] = "wolfSSL test";
  7271. const char badPsw[] = "bad";
  7272. #ifdef HAVE_ECC
  7273. WOLFSSL_X509_NAME* subject;
  7274. WOLFSSL_X509 *x509;
  7275. #endif
  7276. XFILE f;
  7277. int bytes, ret, goodPswLen, badPswLen;
  7278. WOLFSSL_BIO *bio;
  7279. WOLFSSL_EVP_PKEY *pkey;
  7280. WC_PKCS12 *pkcs12;
  7281. WC_PKCS12 *pkcs12_2;
  7282. WOLFSSL_X509 *cert;
  7283. WOLFSSL_X509 *tmp;
  7284. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  7285. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7286. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7287. WOLFSSL_CTX *ctx;
  7288. WOLFSSL *ssl;
  7289. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  7290. #endif
  7291. printf(testingFmt, "wolfSSL_PKCS12()");
  7292. f = XFOPEN(file, "rb");
  7293. AssertTrue((f != XBADFILE));
  7294. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7295. XFCLOSE(f);
  7296. goodPswLen = (int)XSTRLEN(goodPsw);
  7297. badPswLen = (int)XSTRLEN(badPsw);
  7298. bio = BIO_new_mem_buf((void*)buf, bytes);
  7299. AssertNotNull(bio);
  7300. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  7301. AssertNotNull(pkcs12);
  7302. PKCS12_free(pkcs12);
  7303. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  7304. d2i_PKCS12_bio(bio, &pkcs12);
  7305. AssertNotNull(pkcs12);
  7306. BIO_free(bio);
  7307. /* check verify MAC directly */
  7308. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  7309. AssertIntEQ(ret, 1);
  7310. /* check verify MAC fail case directly */
  7311. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  7312. AssertIntEQ(ret, 0);
  7313. /* check verify MAC fail case */
  7314. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7315. AssertIntEQ(ret, 0);
  7316. AssertNull(pkey);
  7317. AssertNull(cert);
  7318. /* check parse with no extra certs kept */
  7319. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7320. AssertIntEQ(ret, 1);
  7321. AssertNotNull(pkey);
  7322. AssertNotNull(cert);
  7323. wolfSSL_EVP_PKEY_free(pkey);
  7324. wolfSSL_X509_free(cert);
  7325. /* check parse with extra certs kept */
  7326. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7327. AssertIntEQ(ret, 1);
  7328. AssertNotNull(pkey);
  7329. AssertNotNull(cert);
  7330. AssertNotNull(ca);
  7331. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7332. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7333. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  7334. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7335. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7336. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7337. #else
  7338. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7339. #endif
  7340. /* Copy stack structure */
  7341. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  7342. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  7343. /* CTX now owns the tmp_ca stack structure */
  7344. tmp_ca = NULL;
  7345. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  7346. AssertNotNull(tmp_ca);
  7347. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  7348. /* Check that the main cert is also set */
  7349. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  7350. AssertNotNull(ssl = SSL_new(ctx));
  7351. AssertNotNull(SSL_get_certificate(ssl));
  7352. SSL_free(ssl);
  7353. SSL_CTX_free(ctx);
  7354. #endif
  7355. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7356. /* should be 2 other certs on stack */
  7357. tmp = sk_X509_pop(ca);
  7358. AssertNotNull(tmp);
  7359. X509_free(tmp);
  7360. tmp = sk_X509_pop(ca);
  7361. AssertNotNull(tmp);
  7362. X509_free(tmp);
  7363. AssertNull(sk_X509_pop(ca));
  7364. EVP_PKEY_free(pkey);
  7365. X509_free(cert);
  7366. sk_X509_pop_free(ca, X509_free);
  7367. /* check PKCS12_create */
  7368. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  7369. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  7370. SSL_SUCCESS);
  7371. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7372. -1, -1, 100, -1, 0)));
  7373. EVP_PKEY_free(pkey);
  7374. X509_free(cert);
  7375. sk_X509_pop_free(ca, NULL);
  7376. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7377. SSL_SUCCESS);
  7378. PKCS12_free(pkcs12_2);
  7379. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7380. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7381. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7382. 2000, 1, 0)));
  7383. EVP_PKEY_free(pkey);
  7384. X509_free(cert);
  7385. sk_X509_pop_free(ca, NULL);
  7386. /* convert to DER then back and parse */
  7387. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7388. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  7389. PKCS12_free(pkcs12_2);
  7390. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  7391. BIO_free(bio);
  7392. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7393. SSL_SUCCESS);
  7394. /* should be 2 other certs on stack */
  7395. tmp = sk_X509_pop(ca);
  7396. AssertNotNull(tmp);
  7397. X509_free(tmp);
  7398. tmp = sk_X509_pop(ca);
  7399. AssertNotNull(tmp);
  7400. X509_free(tmp);
  7401. AssertNull(sk_X509_pop(ca));
  7402. #ifndef NO_RC4
  7403. PKCS12_free(pkcs12_2);
  7404. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  7405. NID_pbe_WithSHA1And128BitRC4,
  7406. NID_pbe_WithSHA1And128BitRC4,
  7407. 2000, 1, 0)));
  7408. EVP_PKEY_free(pkey);
  7409. X509_free(cert);
  7410. sk_X509_pop_free(ca, NULL);
  7411. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7412. SSL_SUCCESS);
  7413. #endif /* NO_RC4 */
  7414. EVP_PKEY_free(pkey);
  7415. X509_free(cert);
  7416. PKCS12_free(pkcs12);
  7417. PKCS12_free(pkcs12_2);
  7418. sk_X509_pop_free(ca, NULL);
  7419. #ifdef HAVE_ECC
  7420. /* test order of parsing */
  7421. f = XFOPEN(order, "rb");
  7422. AssertTrue(f != XBADFILE);
  7423. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7424. XFCLOSE(f);
  7425. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7426. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7427. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  7428. WOLFSSL_SUCCESS);
  7429. /* check use of pkey after parse */
  7430. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7431. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7432. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7433. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7434. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7435. #else
  7436. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7437. #endif
  7438. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  7439. SSL_CTX_free(ctx);
  7440. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7441. #endif
  7442. AssertNotNull(pkey);
  7443. AssertNotNull(cert);
  7444. AssertNotNull(ca);
  7445. /* compare subject lines of certificates */
  7446. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7447. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  7448. SSL_FILETYPE_PEM));
  7449. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7450. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7451. X509_free(x509);
  7452. /* test expected fail case */
  7453. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7454. SSL_FILETYPE_PEM));
  7455. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7456. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7457. X509_free(x509);
  7458. X509_free(cert);
  7459. /* get subject line from ca stack */
  7460. AssertNotNull(cert = sk_X509_pop(ca));
  7461. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7462. /* compare subject from certificate in ca to expected */
  7463. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7464. SSL_FILETYPE_PEM));
  7465. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7466. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7467. EVP_PKEY_free(pkey);
  7468. X509_free(x509);
  7469. X509_free(cert);
  7470. BIO_free(bio);
  7471. PKCS12_free(pkcs12);
  7472. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  7473. /* test order of parsing */
  7474. f = XFOPEN(file, "rb");
  7475. AssertTrue(f != XBADFILE);
  7476. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7477. XFCLOSE(f);
  7478. /* check verify MAC fail case */
  7479. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7480. AssertIntEQ(ret, 0);
  7481. AssertNull(pkey);
  7482. AssertNull(cert);
  7483. /* check parse with no extra certs kept */
  7484. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7485. AssertIntEQ(ret, 1);
  7486. AssertNotNull(pkey);
  7487. AssertNotNull(cert);
  7488. wolfSSL_EVP_PKEY_free(pkey);
  7489. wolfSSL_X509_free(cert);
  7490. /* check parse with extra certs kept */
  7491. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7492. AssertIntEQ(ret, 1);
  7493. AssertNotNull(pkey);
  7494. AssertNotNull(cert);
  7495. AssertNotNull(ca);
  7496. wolfSSL_EVP_PKEY_free(pkey);
  7497. wolfSSL_X509_free(cert);
  7498. sk_X509_pop_free(ca, NULL);
  7499. PKCS12_free(pkcs12);
  7500. #endif /* HAVE_ECC */
  7501. #ifdef WC_RC2
  7502. /* test PKCS#12 with RC2 encryption */
  7503. f = XFOPEN(rc2p12, "rb");
  7504. AssertTrue(f != XBADFILE);
  7505. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7506. XFCLOSE(f);
  7507. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7508. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7509. /* check verify MAC fail case */
  7510. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7511. AssertIntEQ(ret, 0);
  7512. AssertNull(pkey);
  7513. AssertNull(cert);
  7514. /* check parse iwth not extra certs kept */
  7515. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7516. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7517. AssertNotNull(pkey);
  7518. AssertNotNull(cert);
  7519. /* check parse with extra certs kept */
  7520. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7521. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7522. AssertNotNull(pkey);
  7523. AssertNotNull(cert);
  7524. AssertNotNull(ca);
  7525. wolfSSL_EVP_PKEY_free(pkey);
  7526. wolfSSL_X509_free(cert);
  7527. sk_X509_pop_free(ca, NULL);
  7528. BIO_free(bio);
  7529. PKCS12_free(pkcs12);
  7530. #endif /* WC_RC2 */
  7531. /* Test i2d_PKCS12_bio */
  7532. f = XFOPEN(file, "rb");
  7533. AssertTrue((f != XBADFILE));
  7534. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7535. XFCLOSE(f);
  7536. bio = BIO_new(BIO_s_mem());
  7537. AssertNotNull(bio);
  7538. ret = i2d_PKCS12_bio(bio, pkcs12);
  7539. AssertIntEQ(ret, 1);
  7540. ret = i2d_PKCS12_bio(NULL, pkcs12);
  7541. AssertIntEQ(ret, 0);
  7542. ret = i2d_PKCS12_bio(bio, NULL);
  7543. AssertIntEQ(ret, 0);
  7544. PKCS12_free(pkcs12);
  7545. BIO_free(bio);
  7546. (void)order;
  7547. printf(resultFmt, passed);
  7548. #endif /* OPENSSL_EXTRA */
  7549. #endif /* HAVE_FIPS */
  7550. return 0;
  7551. }
  7552. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  7553. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  7554. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  7555. #define TEST_PKCS8_ENC
  7556. #endif
  7557. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7558. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7559. /* used to keep track if FailTestCallback was called */
  7560. static int failTestCallbackCalled = 0;
  7561. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  7562. {
  7563. (void)passwd;
  7564. (void)sz;
  7565. (void)rw;
  7566. (void)userdata;
  7567. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  7568. failTestCallbackCalled = 1;
  7569. return -1;
  7570. }
  7571. #endif
  7572. static int test_wolfSSL_no_password_cb(void)
  7573. {
  7574. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7575. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7576. WOLFSSL_CTX* ctx;
  7577. byte buff[FOURK_BUF];
  7578. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7579. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7580. XFILE f;
  7581. int bytes;
  7582. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  7583. #ifndef NO_WOLFSSL_CLIENT
  7584. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  7585. #else
  7586. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  7587. #endif
  7588. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  7589. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  7590. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7591. XFCLOSE(f);
  7592. AssertIntLE(bytes, sizeof(buff));
  7593. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7594. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7595. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  7596. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7597. XFCLOSE(f);
  7598. AssertIntLE(bytes, sizeof(buff));
  7599. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7600. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7601. wolfSSL_CTX_free(ctx);
  7602. if (failTestCallbackCalled != 0) {
  7603. Fail(("Password callback should not be called by default"),
  7604. ("Password callback was called without attempting "
  7605. "to first decipher private key without password."));
  7606. }
  7607. printf(resultFmt, passed);
  7608. #endif
  7609. return 0;
  7610. }
  7611. #ifdef TEST_PKCS8_ENC
  7612. /* for PKCS8 test case */
  7613. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  7614. {
  7615. int flag = 0;
  7616. (void)rw;
  7617. if (userdata != NULL) {
  7618. flag = *((int*)userdata); /* user set data */
  7619. }
  7620. switch (flag) {
  7621. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  7622. * can be anything the user wishes to be passed to the callback
  7623. * associated with the WOLFSSL_CTX */
  7624. XSTRNCPY(passwd, "yassl123", sz);
  7625. return 8;
  7626. default:
  7627. return BAD_FUNC_ARG;
  7628. }
  7629. }
  7630. #endif /* TEST_PKCS8_ENC */
  7631. /* Testing functions dealing with PKCS8 */
  7632. static int test_wolfSSL_PKCS8(void)
  7633. {
  7634. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  7635. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7636. byte buff[FOURK_BUF];
  7637. byte der[FOURK_BUF];
  7638. #ifndef NO_RSA
  7639. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  7640. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  7641. #endif
  7642. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  7643. #ifdef HAVE_ECC
  7644. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  7645. #endif
  7646. XFILE f;
  7647. int bytes;
  7648. WOLFSSL_CTX* ctx;
  7649. #if defined(HAVE_ECC) && !defined(NO_CODING)
  7650. int ret;
  7651. ecc_key key;
  7652. word32 x = 0;
  7653. #endif
  7654. #ifdef TEST_PKCS8_ENC
  7655. #if !defined(NO_RSA) && !defined(NO_SHA)
  7656. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  7657. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  7658. #endif
  7659. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7660. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  7661. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  7662. #endif
  7663. int flag;
  7664. #endif
  7665. (void)der;
  7666. printf(testingFmt, "wolfSSL_PKCS8()");
  7667. #ifndef NO_WOLFSSL_CLIENT
  7668. #ifndef WOLFSSL_NO_TLS12
  7669. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  7670. #else
  7671. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  7672. #endif
  7673. #else
  7674. #ifndef WOLFSSL_NO_TLS12
  7675. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  7676. #else
  7677. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  7678. #endif
  7679. #endif
  7680. #ifdef TEST_PKCS8_ENC
  7681. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  7682. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  7683. flag = 1; /* used by password callback as return code */
  7684. #if !defined(NO_RSA) && !defined(NO_SHA)
  7685. /* test loading PEM PKCS8 encrypted file */
  7686. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  7687. AssertTrue((f != XBADFILE));
  7688. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7689. XFCLOSE(f);
  7690. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7691. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7692. /* this next case should fail because of password callback return code */
  7693. flag = 0; /* used by password callback as return code */
  7694. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7695. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7696. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7697. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7698. "yassl123"), 0);
  7699. /* test that error value is returned with a bad password */
  7700. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7701. "bad"), 0);
  7702. /* test loading PEM PKCS8 encrypted file */
  7703. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  7704. AssertTrue((f != XBADFILE));
  7705. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7706. XFCLOSE(f);
  7707. flag = 1; /* used by password callback as return code */
  7708. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7709. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7710. /* this next case should fail because of password callback return code */
  7711. flag = 0; /* used by password callback as return code */
  7712. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7713. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7714. #endif /* !NO_RSA && !NO_SHA */
  7715. #if defined(HAVE_ECC) && !defined(NO_SHA)
  7716. /* test loading PEM PKCS8 encrypted ECC Key file */
  7717. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  7718. AssertTrue((f != XBADFILE));
  7719. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7720. XFCLOSE(f);
  7721. flag = 1; /* used by password callback as return code */
  7722. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7723. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7724. /* this next case should fail because of password callback return code */
  7725. flag = 0; /* used by password callback as return code */
  7726. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7727. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7728. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  7729. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7730. "yassl123"), 0);
  7731. /* test that error value is returned with a bad password */
  7732. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  7733. "bad"), 0);
  7734. /* test loading DER PKCS8 encrypted ECC Key file */
  7735. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  7736. AssertTrue((f != XBADFILE));
  7737. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7738. XFCLOSE(f);
  7739. flag = 1; /* used by password callback as return code */
  7740. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7741. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7742. /* this next case should fail because of password callback return code */
  7743. flag = 0; /* used by password callback as return code */
  7744. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7745. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7746. /* leave flag as "okay" */
  7747. flag = 1;
  7748. #endif /* HAVE_ECC && !NO_SHA */
  7749. #endif /* TEST_PKCS8_ENC */
  7750. #ifndef NO_RSA
  7751. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  7752. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  7753. AssertTrue((f != XBADFILE));
  7754. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7755. XFCLOSE(f);
  7756. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7757. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7758. /* test loading PEM PKCS8 private key file (not encrypted) */
  7759. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  7760. AssertTrue((f != XBADFILE));
  7761. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7762. XFCLOSE(f);
  7763. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7764. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7765. #endif /* !NO_RSA */
  7766. /* Test PKCS8 PEM ECC key no crypt */
  7767. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  7768. AssertTrue((f != XBADFILE));
  7769. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7770. XFCLOSE(f);
  7771. #ifdef HAVE_ECC
  7772. /* Test PKCS8 PEM ECC key no crypt */
  7773. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7774. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  7775. #ifndef NO_CODING
  7776. /* decrypt PKCS8 PEM to key in DER format */
  7777. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  7778. (word32)sizeof(der), NULL)), 0);
  7779. ret = wc_ecc_init(&key);
  7780. if (ret == 0) {
  7781. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  7782. wc_ecc_free(&key);
  7783. }
  7784. AssertIntEQ(ret, 0);
  7785. #endif
  7786. /* Test PKCS8 DER ECC key no crypt */
  7787. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  7788. AssertTrue((f != XBADFILE));
  7789. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  7790. XFCLOSE(f);
  7791. /* Test using a PKCS8 ECC PEM */
  7792. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  7793. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7794. #else
  7795. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  7796. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  7797. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  7798. #endif /* HAVE_ECC */
  7799. wolfSSL_CTX_free(ctx);
  7800. printf(resultFmt, passed);
  7801. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7802. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  7803. return 0;
  7804. }
  7805. static int test_wolfSSL_PKCS8_ED25519(void)
  7806. {
  7807. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7808. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  7809. defined(HAVE_ED25519_KEY_IMPORT)
  7810. const byte encPrivKey[] = \
  7811. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7812. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  7813. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  7814. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  7815. "2L9W4v7swXkV+X+a1ww=\n"
  7816. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7817. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7818. byte der[FOURK_BUF];
  7819. WOLFSSL_CTX* ctx;
  7820. int bytes;
  7821. XMEMSET(der, 0, sizeof(der));
  7822. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7823. (word32)sizeof(der), password)), 0);
  7824. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7825. #ifndef NO_WOLFSSL_SERVER
  7826. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7827. #else
  7828. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7829. #endif
  7830. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7831. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7832. wolfSSL_CTX_free(ctx);
  7833. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7834. #endif
  7835. return 0;
  7836. }
  7837. static int test_wolfSSL_PKCS8_ED448(void)
  7838. {
  7839. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  7840. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  7841. defined(HAVE_ED448_KEY_IMPORT)
  7842. const byte encPrivKey[] = \
  7843. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  7844. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  7845. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  7846. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  7847. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  7848. "-----END ENCRYPTED PRIVATE KEY-----\n";
  7849. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  7850. byte der[FOURK_BUF];
  7851. WOLFSSL_CTX* ctx;
  7852. int bytes;
  7853. XMEMSET(der, 0, sizeof(der));
  7854. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  7855. (word32)sizeof(der), password)), 0);
  7856. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7857. #ifndef NO_WOLFSSL_SERVER
  7858. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7859. #else
  7860. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7861. #endif
  7862. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  7863. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  7864. wolfSSL_CTX_free(ctx);
  7865. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7866. #endif
  7867. return 0;
  7868. }
  7869. /* Testing functions dealing with PKCS5 */
  7870. static int test_wolfSSL_PKCS5(void)
  7871. {
  7872. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  7873. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  7874. const char* passwd = "myfipsPa$$W0rd";
  7875. #else
  7876. const char *passwd = "pass1234";
  7877. #endif
  7878. const unsigned char *salt = (unsigned char *)"salt1234";
  7879. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  7880. DYNAMIC_TYPE_TMP_BUFFER);
  7881. int ret = 0;
  7882. AssertNotNull(out);
  7883. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  7884. (int)XSTRLEN((const char *) salt), 10,
  7885. WC_SHA_DIGEST_SIZE,out);
  7886. AssertIntEQ(ret, SSL_SUCCESS);
  7887. #ifdef WOLFSSL_SHA512
  7888. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  7889. (int)XSTRLEN((const char *) salt), 10,
  7890. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  7891. AssertIntEQ(ret, SSL_SUCCESS);
  7892. #endif
  7893. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7894. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  7895. return 0;
  7896. }
  7897. /* test parsing URI from certificate */
  7898. static int test_wolfSSL_URI(void)
  7899. {
  7900. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7901. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  7902. defined(OPENSSL_EXTRA))
  7903. WOLFSSL_X509* x509;
  7904. const char uri[] = "./certs/client-uri-cert.pem";
  7905. const char badUri[] = "./certs/client-relative-uri.pem";
  7906. printf(testingFmt, "wolfSSL URI parse");
  7907. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  7908. AssertNotNull(x509);
  7909. wolfSSL_FreeX509(x509);
  7910. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  7911. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  7912. && !defined(WOLFSSL_FPKI)
  7913. AssertNull(x509);
  7914. #else
  7915. AssertNotNull(x509);
  7916. wolfSSL_FreeX509(x509);
  7917. #endif
  7918. printf(resultFmt, passed);
  7919. #endif
  7920. return 0;
  7921. }
  7922. static int test_wolfSSL_TBS(void)
  7923. {
  7924. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7925. && defined(OPENSSL_EXTRA)
  7926. WOLFSSL_X509* x509;
  7927. const unsigned char* tbs;
  7928. int tbsSz;
  7929. printf(testingFmt, "wolfSSL TBS");
  7930. AssertNotNull(x509 =
  7931. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7932. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  7933. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  7934. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  7935. AssertIntEQ(tbsSz, 1003);
  7936. wolfSSL_FreeX509(x509);
  7937. printf(resultFmt, passed);
  7938. #endif
  7939. return 0;
  7940. }
  7941. static int test_wolfSSL_X509_verify(void)
  7942. {
  7943. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  7944. && defined(OPENSSL_EXTRA)
  7945. WOLFSSL_X509* ca;
  7946. WOLFSSL_X509* serv;
  7947. WOLFSSL_EVP_PKEY* pkey;
  7948. unsigned char buf[2048];
  7949. const unsigned char* pt = NULL;
  7950. int bufSz;
  7951. printf(testingFmt, "wolfSSL X509 verify");
  7952. AssertNotNull(ca =
  7953. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  7954. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  7955. WOLFSSL_SUCCESS);
  7956. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  7957. WOLFSSL_SUCCESS);
  7958. AssertIntEQ(bufSz, 294);
  7959. bufSz = 2048;
  7960. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  7961. WOLFSSL_SUCCESS);
  7962. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  7963. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  7964. AssertNotNull(serv =
  7965. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  7966. /* success case */
  7967. pt = buf;
  7968. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7969. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  7970. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  7971. wolfSSL_EVP_PKEY_free(pkey);
  7972. /* fail case */
  7973. bufSz = 2048;
  7974. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  7975. WOLFSSL_SUCCESS);
  7976. pt = buf;
  7977. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  7978. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  7979. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  7980. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  7981. wolfSSL_EVP_PKEY_free(pkey);
  7982. wolfSSL_FreeX509(ca);
  7983. wolfSSL_FreeX509(serv);
  7984. printf(resultFmt, passed);
  7985. #endif
  7986. return 0;
  7987. }
  7988. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  7989. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  7990. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  7991. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  7992. /* create certificate with version 2 */
  7993. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  7994. {
  7995. WOLFSSL_X509 *x509, *x509v2;
  7996. WOLFSSL_EVP_PKEY *priv, *pub;
  7997. unsigned char *der = NULL, *key = NULL, *pt;
  7998. char *header, *name;
  7999. int derSz;
  8000. long keySz;
  8001. XFILE fp;
  8002. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  8003. time_t t;
  8004. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8005. WOLFSSL_FILETYPE_PEM));
  8006. fp = XFOPEN(cliKeyFile, "rb");
  8007. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  8008. WOLFSSL_SUCCESS);
  8009. XFCLOSE(fp);
  8010. pt = key;
  8011. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  8012. (const unsigned char**)&pt, keySz));
  8013. /* create the version 2 certificate */
  8014. AssertNotNull(x509v2 = X509_new());
  8015. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  8016. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  8017. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  8018. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  8019. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  8020. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  8021. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  8022. t = time(NULL);
  8023. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  8024. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  8025. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  8026. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  8027. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  8028. derSz = wolfSSL_i2d_X509(x509v2, &der);
  8029. AssertIntGT(derSz, 0);
  8030. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  8031. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8032. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  8033. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8034. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8035. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8036. wolfSSL_X509_free(x509);
  8037. wolfSSL_X509_free(x509v2);
  8038. wolfSSL_EVP_PKEY_free(priv);
  8039. wolfSSL_EVP_PKEY_free(pub);
  8040. wolfSSL_ASN1_TIME_free(notBefore);
  8041. wolfSSL_ASN1_TIME_free(notAfter);
  8042. }
  8043. /* override certificate version error */
  8044. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  8045. {
  8046. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8047. AssertIntEQ(store->error, ASN_VERSION_E);
  8048. #else
  8049. AssertIntEQ(store->error, 0);
  8050. #endif
  8051. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  8052. (void)preverify;
  8053. return 1;
  8054. }
  8055. /* set verify callback that will override bad certificate version */
  8056. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  8057. {
  8058. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  8059. }
  8060. #endif
  8061. static int test_wolfSSL_X509_TLS_version(void)
  8062. {
  8063. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8064. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8065. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8066. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8067. tcp_ready ready;
  8068. func_args server_args;
  8069. func_args client_args;
  8070. THREAD_TYPE serverThread;
  8071. callback_functions func_cb_client;
  8072. callback_functions func_cb_server;
  8073. printf(testingFmt, "test_wolfSSL_X509_TLS_version");
  8074. /* test server rejects a client certificate that is not version 3 */
  8075. #ifdef WOLFSSL_TIRTOS
  8076. fdOpenSession(Task_self());
  8077. #endif
  8078. XMEMSET(&server_args, 0, sizeof(func_args));
  8079. XMEMSET(&client_args, 0, sizeof(func_args));
  8080. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8081. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8082. StartTCP();
  8083. InitTcpReady(&ready);
  8084. #if defined(USE_WINDOWS_API)
  8085. /* use RNG to get random port if using windows */
  8086. ready.port = GetRandomPort();
  8087. #endif
  8088. server_args.signal = &ready;
  8089. client_args.signal = &ready;
  8090. server_args.return_code = TEST_FAIL;
  8091. client_args.return_code = TEST_FAIL;
  8092. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8093. #ifndef WOLFSSL_NO_TLS12
  8094. func_cb_client.method = wolfTLSv1_2_client_method;
  8095. #else
  8096. func_cb_client.method = wolfTLSv1_3_client_method;
  8097. #endif
  8098. client_args.callbacks = &func_cb_client;
  8099. #ifndef WOLFSSL_NO_TLS12
  8100. func_cb_server.method = wolfTLSv1_2_server_method;
  8101. #else
  8102. func_cb_server.method = wolfTLSv1_3_server_method;
  8103. #endif
  8104. server_args.callbacks = &func_cb_server;
  8105. start_thread(test_server_nofail, &server_args, &serverThread);
  8106. wait_tcp_ready(&server_args);
  8107. test_client_nofail(&client_args, NULL);
  8108. join_thread(serverThread);
  8109. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8110. AssertIntEQ(client_args.return_code, TEST_FAIL);
  8111. AssertIntEQ(server_args.return_code, TEST_FAIL);
  8112. #else
  8113. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8114. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8115. #endif
  8116. FreeTcpReady(&ready);
  8117. #ifdef WOLFSSL_TIRTOS
  8118. fdCloseSession(Task_self());
  8119. #endif
  8120. /* Now re run but override the bad X509 version */
  8121. #ifdef WOLFSSL_TIRTOS
  8122. fdOpenSession(Task_self());
  8123. #endif
  8124. XMEMSET(&server_args, 0, sizeof(func_args));
  8125. XMEMSET(&client_args, 0, sizeof(func_args));
  8126. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8127. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8128. StartTCP();
  8129. InitTcpReady(&ready);
  8130. #if defined(USE_WINDOWS_API)
  8131. /* use RNG to get random port if using windows */
  8132. ready.port = GetRandomPort();
  8133. #endif
  8134. server_args.signal = &ready;
  8135. client_args.signal = &ready;
  8136. server_args.return_code = TEST_FAIL;
  8137. client_args.return_code = TEST_FAIL;
  8138. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8139. func_cb_server.ctx_ready = &test_set_override_x509;
  8140. #ifndef WOLFSSL_NO_TLS12
  8141. func_cb_client.method = wolfTLSv1_2_client_method;
  8142. #else
  8143. func_cb_client.method = wolfTLSv1_3_client_method;
  8144. #endif
  8145. client_args.callbacks = &func_cb_client;
  8146. #ifndef WOLFSSL_NO_TLS12
  8147. func_cb_server.method = wolfTLSv1_2_server_method;
  8148. #else
  8149. func_cb_server.method = wolfTLSv1_3_server_method;
  8150. #endif
  8151. server_args.callbacks = &func_cb_server;
  8152. start_thread(test_server_nofail, &server_args, &serverThread);
  8153. wait_tcp_ready(&server_args);
  8154. test_client_nofail(&client_args, NULL);
  8155. join_thread(serverThread);
  8156. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8157. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8158. FreeTcpReady(&ready);
  8159. #ifdef WOLFSSL_TIRTOS
  8160. fdCloseSession(Task_self());
  8161. #endif
  8162. printf(resultFmt, passed);
  8163. #endif
  8164. return 0;
  8165. }
  8166. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  8167. * version allowed.
  8168. * POST: 1 on success.
  8169. */
  8170. static int test_wolfSSL_CTX_SetMinVersion(void)
  8171. {
  8172. int failFlag = WOLFSSL_SUCCESS;
  8173. #ifndef NO_WOLFSSL_CLIENT
  8174. WOLFSSL_CTX* ctx;
  8175. int itr;
  8176. #ifndef NO_OLD_TLS
  8177. const int versions[] = {
  8178. #ifdef WOLFSSL_ALLOW_TLSV10
  8179. WOLFSSL_TLSV1,
  8180. #endif
  8181. WOLFSSL_TLSV1_1,
  8182. WOLFSSL_TLSV1_2 };
  8183. #elif !defined(WOLFSSL_NO_TLS12)
  8184. const int versions[] = { WOLFSSL_TLSV1_2 };
  8185. #elif defined(WOLFSSL_TLS13)
  8186. const int versions[] = { WOLFSSL_TLSV1_3 };
  8187. #else
  8188. const int versions[0];
  8189. #endif
  8190. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8191. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  8192. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  8193. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  8194. failFlag = WOLFSSL_FAILURE;
  8195. }
  8196. }
  8197. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  8198. wolfSSL_CTX_free(ctx);
  8199. #endif
  8200. if (failFlag == WOLFSSL_SUCCESS) {
  8201. failFlag = 0;
  8202. }
  8203. return failFlag;
  8204. } /* END test_wolfSSL_CTX_SetMinVersion */
  8205. /*----------------------------------------------------------------------------*
  8206. | OCSP Stapling
  8207. *----------------------------------------------------------------------------*/
  8208. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  8209. * need to contact the CA, lowering the cost of cert revocation checking.
  8210. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  8211. * POST: 1 returned for success.
  8212. */
  8213. static int test_wolfSSL_UseOCSPStapling(void)
  8214. {
  8215. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  8216. !defined(NO_WOLFSSL_CLIENT)
  8217. int ret;
  8218. WOLFSSL_CTX* ctx;
  8219. WOLFSSL* ssl;
  8220. #ifndef NO_WOLFSSL_CLIENT
  8221. #ifndef WOLFSSL_NO_TLS12
  8222. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8223. #else
  8224. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8225. #endif
  8226. #else
  8227. #ifndef WOLFSSL_NO_TLS12
  8228. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8229. #else
  8230. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8231. #endif
  8232. #endif
  8233. ssl = wolfSSL_new(ctx);
  8234. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  8235. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  8236. WOLFSSL_CSR2_OCSP_USE_NONCE);
  8237. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  8238. wolfSSL_free(ssl);
  8239. wolfSSL_CTX_free(ctx);
  8240. if (ret == WOLFSSL_SUCCESS) {
  8241. ret = 0;
  8242. }
  8243. return ret;
  8244. #else
  8245. return 0;
  8246. #endif
  8247. } /*END test_wolfSSL_UseOCSPStapling */
  8248. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  8249. * stapling eliminates the need to contact the CA and lowers cert revocation
  8250. * check.
  8251. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  8252. */
  8253. static int test_wolfSSL_UseOCSPStaplingV2(void)
  8254. {
  8255. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  8256. !defined(NO_WOLFSSL_CLIENT)
  8257. int ret;
  8258. WOLFSSL_CTX* ctx;
  8259. WOLFSSL* ssl;
  8260. #ifndef NO_WOLFSSL_CLIENT
  8261. #ifndef WOLFSSL_NO_TLS12
  8262. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8263. #else
  8264. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8265. #endif
  8266. #else
  8267. #ifndef WOLFSSL_NO_TLS12
  8268. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8269. #else
  8270. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8271. #endif
  8272. #endif
  8273. ssl = wolfSSL_new(ctx);
  8274. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  8275. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  8276. WOLFSSL_CSR2_OCSP_USE_NONCE );
  8277. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  8278. wolfSSL_free(ssl);
  8279. wolfSSL_CTX_free(ctx);
  8280. if (ret == WOLFSSL_SUCCESS) {
  8281. ret = 0;
  8282. }
  8283. return ret;
  8284. #else
  8285. return 0;
  8286. #endif
  8287. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  8288. /*----------------------------------------------------------------------------*
  8289. | Multicast Tests
  8290. *----------------------------------------------------------------------------*/
  8291. static int test_wolfSSL_mcast(void)
  8292. {
  8293. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  8294. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  8295. WOLFSSL_CTX* ctx;
  8296. WOLFSSL* ssl;
  8297. int result;
  8298. byte preMasterSecret[512];
  8299. byte clientRandom[32];
  8300. byte serverRandom[32];
  8301. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  8302. byte buf[256];
  8303. word16 newId;
  8304. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  8305. AssertNotNull(ctx);
  8306. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  8307. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8308. ssl = wolfSSL_new(ctx);
  8309. AssertNotNull(ssl);
  8310. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  8311. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  8312. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  8313. result = wolfSSL_set_secret(ssl, 23,
  8314. preMasterSecret, sizeof(preMasterSecret),
  8315. clientRandom, serverRandom, suite);
  8316. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8317. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  8318. AssertIntLE(result, 0);
  8319. AssertIntLE(newId, 100);
  8320. wolfSSL_free(ssl);
  8321. wolfSSL_CTX_free(ctx);
  8322. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
  8323. return 0;
  8324. }
  8325. /*----------------------------------------------------------------------------*
  8326. | Wolfcrypt
  8327. *----------------------------------------------------------------------------*/
  8328. /*
  8329. * Unit test for the wc_InitBlake2b()
  8330. */
  8331. static int test_wc_InitBlake2b(void)
  8332. {
  8333. int ret = 0;
  8334. #ifdef HAVE_BLAKE2
  8335. Blake2b blake;
  8336. printf(testingFmt, "wc_InitBlake2B()");
  8337. /* Test good arg. */
  8338. ret = wc_InitBlake2b(&blake, 64);
  8339. if (ret != 0) {
  8340. ret = WOLFSSL_FATAL_ERROR;
  8341. }
  8342. /* Test bad arg. */
  8343. if (!ret) {
  8344. ret = wc_InitBlake2b(NULL, 64);
  8345. if (ret == 0) {
  8346. ret = WOLFSSL_FATAL_ERROR;
  8347. } else {
  8348. ret = 0;
  8349. }
  8350. }
  8351. if (!ret) {
  8352. ret = wc_InitBlake2b(NULL, 128);
  8353. if (ret == 0) {
  8354. ret = WOLFSSL_FATAL_ERROR;
  8355. } else {
  8356. ret = 0;
  8357. }
  8358. }
  8359. if (!ret) {
  8360. ret = wc_InitBlake2b(&blake, 128);
  8361. if (ret == 0) {
  8362. ret = WOLFSSL_FATAL_ERROR;
  8363. } else {
  8364. ret = 0;
  8365. }
  8366. }
  8367. if (!ret) {
  8368. ret = wc_InitBlake2b(NULL, 0);
  8369. if (ret == 0) {
  8370. ret = WOLFSSL_FATAL_ERROR;
  8371. } else {
  8372. ret = 0;
  8373. }
  8374. }
  8375. if (!ret) {
  8376. ret = wc_InitBlake2b(&blake, 0);
  8377. if (ret == 0) {
  8378. ret = WOLFSSL_FATAL_ERROR;
  8379. } else {
  8380. ret = 0;
  8381. }
  8382. }
  8383. printf(resultFmt, ret == 0 ? passed : failed);
  8384. #endif
  8385. return ret;
  8386. } /*END test_wc_InitBlake2b*/
  8387. /*
  8388. * Unit test for the wc_InitBlake2b_WithKey()
  8389. */
  8390. static int test_wc_InitBlake2b_WithKey(void)
  8391. {
  8392. int ret = 0;
  8393. #ifdef HAVE_BLAKE2
  8394. Blake2b blake;
  8395. word32 digestSz = BLAKE2B_KEYBYTES;
  8396. byte key[BLAKE2B_KEYBYTES];
  8397. word32 keylen = BLAKE2B_KEYBYTES;
  8398. printf(testingFmt, "wc_InitBlake2b_WithKey()");
  8399. /* Test good arg. */
  8400. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  8401. if (ret != 0) {
  8402. ret = WOLFSSL_FATAL_ERROR;
  8403. }
  8404. /* Test bad args. */
  8405. if (ret == 0) {
  8406. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  8407. if (ret == BAD_FUNC_ARG) {
  8408. ret = 0;
  8409. }
  8410. }
  8411. if (ret == 0) {
  8412. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  8413. if (ret == BAD_FUNC_ARG) {
  8414. ret = 0;
  8415. }
  8416. }
  8417. if (ret == 0) {
  8418. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  8419. }
  8420. printf(resultFmt, ret == 0 ? passed : failed);
  8421. #endif
  8422. return ret;
  8423. } /*END wc_InitBlake2b_WithKey*/
  8424. /*
  8425. * Unit test for the wc_InitBlake2s_WithKey()
  8426. */
  8427. static int test_wc_InitBlake2s_WithKey(void)
  8428. {
  8429. int ret = 0;
  8430. #ifdef HAVE_BLAKE2S
  8431. Blake2s blake;
  8432. word32 digestSz = BLAKE2S_KEYBYTES;
  8433. byte *key = (byte*)"01234567890123456789012345678901";
  8434. word32 keylen = BLAKE2S_KEYBYTES;
  8435. printf(testingFmt, "wc_InitBlake2s_WithKey()");
  8436. /* Test good arg. */
  8437. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  8438. if (ret != 0) {
  8439. ret = WOLFSSL_FATAL_ERROR;
  8440. }
  8441. /* Test bad args. */
  8442. if (ret == 0) {
  8443. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  8444. if (ret == BAD_FUNC_ARG) {
  8445. ret = 0;
  8446. }
  8447. }
  8448. if (ret == 0) {
  8449. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  8450. if (ret == BAD_FUNC_ARG) {
  8451. ret = 0;
  8452. }
  8453. }
  8454. if (ret == 0) {
  8455. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  8456. }
  8457. printf(resultFmt, ret == 0 ? passed : failed);
  8458. #endif
  8459. return ret;
  8460. } /*END wc_InitBlake2s_WithKey*/
  8461. /*
  8462. * Unit test for the wc_InitMd5()
  8463. */
  8464. static int test_wc_InitMd5(void)
  8465. {
  8466. int flag = 0;
  8467. #ifndef NO_MD5
  8468. wc_Md5 md5;
  8469. int ret;
  8470. printf(testingFmt, "wc_InitMd5()");
  8471. /* Test good arg. */
  8472. ret = wc_InitMd5(&md5);
  8473. if (ret != 0) {
  8474. flag = WOLFSSL_FATAL_ERROR;
  8475. }
  8476. /* Test bad arg. */
  8477. if (!flag) {
  8478. ret = wc_InitMd5(NULL);
  8479. if (ret != BAD_FUNC_ARG) {
  8480. flag = WOLFSSL_FATAL_ERROR;
  8481. }
  8482. }
  8483. wc_Md5Free(&md5);
  8484. printf(resultFmt, flag == 0 ? passed : failed);
  8485. #endif
  8486. return flag;
  8487. } /* END test_wc_InitMd5 */
  8488. /*
  8489. * Testing wc_UpdateMd5()
  8490. */
  8491. static int test_wc_Md5Update(void)
  8492. {
  8493. int flag = 0;
  8494. #ifndef NO_MD5
  8495. wc_Md5 md5;
  8496. byte hash[WC_MD5_DIGEST_SIZE];
  8497. testVector a, b, c;
  8498. int ret;
  8499. ret = wc_InitMd5(&md5);
  8500. if (ret != 0) {
  8501. flag = ret;
  8502. }
  8503. printf(testingFmt, "wc_Md5Update()");
  8504. /* Input */
  8505. if (!flag) {
  8506. a.input = "a";
  8507. a.inLen = XSTRLEN(a.input);
  8508. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  8509. if (ret != 0) {
  8510. flag = ret;
  8511. }
  8512. }
  8513. if (!flag) {
  8514. ret = wc_Md5Final(&md5, hash);
  8515. if (ret != 0) {
  8516. flag = ret;
  8517. }
  8518. }
  8519. /* Update input. */
  8520. if (!flag) {
  8521. a.input = "abc";
  8522. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  8523. "\x72";
  8524. a.inLen = XSTRLEN(a.input);
  8525. a.outLen = XSTRLEN(a.output);
  8526. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  8527. if (ret != 0) {
  8528. flag = ret;
  8529. }
  8530. }
  8531. if (!flag) {
  8532. ret = wc_Md5Final(&md5, hash);
  8533. if (ret != 0) {
  8534. flag = ret;
  8535. }
  8536. }
  8537. if (!flag) {
  8538. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  8539. flag = WOLFSSL_FATAL_ERROR;
  8540. }
  8541. }
  8542. /*Pass in bad values. */
  8543. if (!flag) {
  8544. b.input = NULL;
  8545. b.inLen = 0;
  8546. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  8547. if (ret != 0) {
  8548. flag = ret;
  8549. }
  8550. }
  8551. if (!flag) {
  8552. c.input = NULL;
  8553. c.inLen = WC_MD5_DIGEST_SIZE;
  8554. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  8555. if (ret != BAD_FUNC_ARG) {
  8556. flag = WOLFSSL_FATAL_ERROR;
  8557. }
  8558. }
  8559. if (!flag) {
  8560. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  8561. if (ret != BAD_FUNC_ARG) {
  8562. flag = WOLFSSL_FATAL_ERROR;
  8563. }
  8564. }
  8565. wc_Md5Free(&md5);
  8566. printf(resultFmt, flag == 0 ? passed : failed);
  8567. #endif
  8568. return flag;
  8569. } /* END test_wc_Md5Update() */
  8570. /*
  8571. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  8572. */
  8573. static int test_wc_Md5Final(void)
  8574. {
  8575. int flag = 0;
  8576. #ifndef NO_MD5
  8577. /* Instantiate */
  8578. wc_Md5 md5;
  8579. byte* hash_test[3];
  8580. byte hash1[WC_MD5_DIGEST_SIZE];
  8581. byte hash2[2*WC_MD5_DIGEST_SIZE];
  8582. byte hash3[5*WC_MD5_DIGEST_SIZE];
  8583. int times, i, ret;
  8584. /* Initialize */
  8585. ret = wc_InitMd5(&md5);
  8586. if (ret != 0) {
  8587. flag = ret;
  8588. }
  8589. if (!flag) {
  8590. hash_test[0] = hash1;
  8591. hash_test[1] = hash2;
  8592. hash_test[2] = hash3;
  8593. }
  8594. times = sizeof(hash_test)/sizeof(byte*);
  8595. /* Test good args. */
  8596. printf(testingFmt, "wc_Md5Final()");
  8597. for (i = 0; i < times; i++) {
  8598. if (!flag) {
  8599. ret = wc_Md5Final(&md5, hash_test[i]);
  8600. if (ret != 0) {
  8601. flag = WOLFSSL_FATAL_ERROR;
  8602. }
  8603. }
  8604. }
  8605. /* Test bad args. */
  8606. if (!flag) {
  8607. ret = wc_Md5Final(NULL, NULL);
  8608. if (ret != BAD_FUNC_ARG) {
  8609. flag = WOLFSSL_FATAL_ERROR;
  8610. }
  8611. }
  8612. if (!flag) {
  8613. ret = wc_Md5Final(NULL, hash1);
  8614. if (ret != BAD_FUNC_ARG) {
  8615. flag = WOLFSSL_FATAL_ERROR;
  8616. }
  8617. }
  8618. if (!flag) {
  8619. ret = wc_Md5Final(&md5, NULL);
  8620. if (ret != BAD_FUNC_ARG) {
  8621. flag = WOLFSSL_FATAL_ERROR;
  8622. }
  8623. }
  8624. wc_Md5Free(&md5);
  8625. printf(resultFmt, flag == 0 ? passed : failed);
  8626. #endif
  8627. return flag;
  8628. }
  8629. /*
  8630. * Unit test for the wc_InitSha()
  8631. */
  8632. static int test_wc_InitSha(void)
  8633. {
  8634. int flag = 0;
  8635. #ifndef NO_SHA
  8636. wc_Sha sha;
  8637. int ret;
  8638. printf(testingFmt, "wc_InitSha()");
  8639. /* Test good arg. */
  8640. ret = wc_InitSha(&sha);
  8641. if (ret != 0) {
  8642. flag = WOLFSSL_FATAL_ERROR;
  8643. }
  8644. /* Test bad arg. */
  8645. if (!flag) {
  8646. ret = wc_InitSha(NULL);
  8647. if (ret != BAD_FUNC_ARG) {
  8648. flag = WOLFSSL_FATAL_ERROR;
  8649. }
  8650. }
  8651. wc_ShaFree(&sha);
  8652. printf(resultFmt, flag == 0 ? passed : failed);
  8653. #endif
  8654. return flag;
  8655. } /* END test_wc_InitSha */
  8656. /*
  8657. * Tesing wc_ShaUpdate()
  8658. */
  8659. static int test_wc_ShaUpdate(void)
  8660. {
  8661. int flag = 0;
  8662. #ifndef NO_SHA
  8663. wc_Sha sha;
  8664. byte hash[WC_SHA_DIGEST_SIZE];
  8665. testVector a, b, c;
  8666. int ret;
  8667. ret = wc_InitSha(&sha);
  8668. if (ret != 0) {
  8669. flag = ret;
  8670. }
  8671. printf(testingFmt, "wc_ShaUpdate()");
  8672. /* Input. */
  8673. if (!flag) {
  8674. a.input = "a";
  8675. a.inLen = XSTRLEN(a.input);
  8676. ret = wc_ShaUpdate(&sha, NULL, 0);
  8677. if (ret != 0) {
  8678. flag = ret;
  8679. }
  8680. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  8681. if (ret != 0) {
  8682. flag = ret;
  8683. }
  8684. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8685. if (ret != 0) {
  8686. flag = ret;
  8687. }
  8688. }
  8689. if (!flag) {
  8690. ret = wc_ShaFinal(&sha, hash);
  8691. if (ret != 0) {
  8692. flag = ret;
  8693. }
  8694. }
  8695. /* Update input. */
  8696. if (!flag) {
  8697. a.input = "abc";
  8698. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  8699. "\x6C\x9C\xD0\xD8\x9D";
  8700. a.inLen = XSTRLEN(a.input);
  8701. a.outLen = XSTRLEN(a.output);
  8702. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  8703. if (ret != 0) {
  8704. flag = ret;
  8705. }
  8706. }
  8707. if (!flag) {
  8708. ret = wc_ShaFinal(&sha, hash);
  8709. if (ret !=0) {
  8710. flag = ret;
  8711. }
  8712. }
  8713. if (!flag) {
  8714. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  8715. flag = WOLFSSL_FATAL_ERROR;
  8716. }
  8717. }
  8718. /* Try passing in bad values. */
  8719. if (!flag) {
  8720. b.input = NULL;
  8721. b.inLen = 0;
  8722. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  8723. if (ret != 0) {
  8724. flag = ret;
  8725. }
  8726. }
  8727. if (!flag) {
  8728. c.input = NULL;
  8729. c.inLen = WC_SHA_DIGEST_SIZE;
  8730. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  8731. if (ret != BAD_FUNC_ARG) {
  8732. flag = WOLFSSL_FATAL_ERROR;
  8733. }
  8734. }
  8735. if (!flag) {
  8736. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  8737. if (ret != BAD_FUNC_ARG) {
  8738. flag = WOLFSSL_FATAL_ERROR;
  8739. }
  8740. }
  8741. wc_ShaFree(&sha);
  8742. /* If not returned then the unit test passed test vectors. */
  8743. printf(resultFmt, flag == 0 ? passed : failed);
  8744. #endif
  8745. return flag;
  8746. } /* END test_wc_ShaUpdate() */
  8747. /*
  8748. * Unit test on wc_ShaFinal
  8749. */
  8750. static int test_wc_ShaFinal(void)
  8751. {
  8752. int flag = 0;
  8753. #ifndef NO_SHA
  8754. wc_Sha sha;
  8755. byte* hash_test[3];
  8756. byte hash1[WC_SHA_DIGEST_SIZE];
  8757. byte hash2[2*WC_SHA_DIGEST_SIZE];
  8758. byte hash3[5*WC_SHA_DIGEST_SIZE];
  8759. int times, i, ret;
  8760. /*Initialize*/
  8761. ret = wc_InitSha(&sha);
  8762. if (ret) {
  8763. flag = ret;
  8764. }
  8765. if (!flag) {
  8766. hash_test[0] = hash1;
  8767. hash_test[1] = hash2;
  8768. hash_test[2] = hash3;
  8769. }
  8770. times = sizeof(hash_test)/sizeof(byte*);
  8771. /* Good test args. */
  8772. printf(testingFmt, "wc_ShaFinal()");
  8773. for (i = 0; i < times; i++) {
  8774. if (!flag) {
  8775. ret = wc_ShaFinal(&sha, hash_test[i]);
  8776. if (ret != 0) {
  8777. flag = WOLFSSL_FATAL_ERROR;
  8778. }
  8779. }
  8780. }
  8781. /* Test bad args. */
  8782. if (!flag) {
  8783. ret = wc_ShaFinal(NULL, NULL);
  8784. if (ret != BAD_FUNC_ARG) {
  8785. flag = WOLFSSL_FATAL_ERROR;
  8786. }
  8787. }
  8788. if (!flag) {
  8789. ret = wc_ShaFinal(NULL, hash1);
  8790. if (ret != BAD_FUNC_ARG) {
  8791. flag = WOLFSSL_FATAL_ERROR;
  8792. }
  8793. }
  8794. if (!flag) {
  8795. ret = wc_ShaFinal(&sha, NULL);
  8796. if (ret != BAD_FUNC_ARG) {
  8797. flag = WOLFSSL_FATAL_ERROR;
  8798. }
  8799. }
  8800. wc_ShaFree(&sha);
  8801. printf(resultFmt, flag == 0 ? passed : failed);
  8802. #endif
  8803. return flag;
  8804. } /* END test_wc_ShaFinal */
  8805. /*
  8806. * Unit test for wc_InitSha256()
  8807. */
  8808. static int test_wc_InitSha256(void)
  8809. {
  8810. int flag = 0;
  8811. #ifndef NO_SHA256
  8812. wc_Sha256 sha256;
  8813. int ret;
  8814. printf(testingFmt, "wc_InitSha256()");
  8815. /* Test good arg. */
  8816. ret = wc_InitSha256(&sha256);
  8817. if (ret != 0) {
  8818. flag = WOLFSSL_FATAL_ERROR;
  8819. }
  8820. /* Test bad arg. */
  8821. if (!flag) {
  8822. ret = wc_InitSha256(NULL);
  8823. if (ret != BAD_FUNC_ARG) {
  8824. flag = WOLFSSL_FATAL_ERROR;
  8825. }
  8826. }
  8827. wc_Sha256Free(&sha256);
  8828. printf(resultFmt, flag == 0 ? passed : failed);
  8829. #endif
  8830. return flag;
  8831. } /* END test_wc_InitSha256 */
  8832. /*
  8833. * Unit test for wc_Sha256Update()
  8834. */
  8835. static int test_wc_Sha256Update(void)
  8836. {
  8837. int flag = 0;
  8838. #ifndef NO_SHA256
  8839. wc_Sha256 sha256;
  8840. byte hash[WC_SHA256_DIGEST_SIZE];
  8841. testVector a, b, c;
  8842. int ret;
  8843. ret = wc_InitSha256(&sha256);
  8844. if (ret != 0) {
  8845. flag = ret;
  8846. }
  8847. printf(testingFmt, "wc_Sha256Update()");
  8848. /* Input. */
  8849. if (!flag) {
  8850. a.input = "a";
  8851. a.inLen = XSTRLEN(a.input);
  8852. ret = wc_Sha256Update(&sha256, NULL, 0);
  8853. if (ret != 0) {
  8854. flag = ret;
  8855. }
  8856. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  8857. if (ret != 0) {
  8858. flag = ret;
  8859. }
  8860. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8861. if (ret != 0) {
  8862. flag = ret;
  8863. }
  8864. }
  8865. if (!flag) {
  8866. ret = wc_Sha256Final(&sha256, hash);
  8867. if (ret != 0) {
  8868. flag = ret;
  8869. }
  8870. }
  8871. /* Update input. */
  8872. if (!flag) {
  8873. a.input = "abc";
  8874. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  8875. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  8876. "\x15\xAD";
  8877. a.inLen = XSTRLEN(a.input);
  8878. a.outLen = XSTRLEN(a.output);
  8879. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  8880. if (ret != 0) {
  8881. flag = ret;
  8882. }
  8883. }
  8884. if (!flag) {
  8885. ret = wc_Sha256Final(&sha256, hash);
  8886. if (ret != 0) {
  8887. flag = ret;
  8888. }
  8889. }
  8890. if (!flag) {
  8891. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  8892. flag = WOLFSSL_FATAL_ERROR;
  8893. }
  8894. }
  8895. /* Try passing in bad values */
  8896. if (!flag) {
  8897. b.input = NULL;
  8898. b.inLen = 0;
  8899. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  8900. if (ret != 0) {
  8901. flag = ret;
  8902. }
  8903. }
  8904. if (!flag) {
  8905. c.input = NULL;
  8906. c.inLen = WC_SHA256_DIGEST_SIZE;
  8907. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  8908. if (ret != BAD_FUNC_ARG) {
  8909. flag = WOLFSSL_FATAL_ERROR;
  8910. }
  8911. }
  8912. if (!flag) {
  8913. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  8914. if (ret != BAD_FUNC_ARG) {
  8915. flag = WOLFSSL_FATAL_ERROR;
  8916. }
  8917. }
  8918. wc_Sha256Free(&sha256);
  8919. /* If not returned then the unit test passed. */
  8920. printf(resultFmt, flag == 0 ? passed : failed);
  8921. #endif
  8922. return flag;
  8923. } /* END test_wc_Sha256Update */
  8924. /*
  8925. * Unit test function for wc_Sha256Final()
  8926. */
  8927. static int test_wc_Sha256Final(void)
  8928. {
  8929. int flag = 0;
  8930. #ifndef NO_SHA256
  8931. wc_Sha256 sha256;
  8932. byte* hash_test[3];
  8933. byte hash1[WC_SHA256_DIGEST_SIZE];
  8934. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8935. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8936. int times, i, ret;
  8937. /* Initialize */
  8938. ret = wc_InitSha256(&sha256);
  8939. if (ret != 0) {
  8940. flag = ret;
  8941. }
  8942. if (!flag) {
  8943. hash_test[0] = hash1;
  8944. hash_test[1] = hash2;
  8945. hash_test[2] = hash3;
  8946. }
  8947. times = sizeof(hash_test) / sizeof(byte*);
  8948. /* Good test args. */
  8949. printf(testingFmt, "wc_Sha256Final()");
  8950. for (i = 0; i < times; i++) {
  8951. if (!flag) {
  8952. ret = wc_Sha256Final(&sha256, hash_test[i]);
  8953. if (ret != 0) {
  8954. flag = WOLFSSL_FATAL_ERROR;
  8955. }
  8956. }
  8957. }
  8958. /* Test bad args. */
  8959. if (!flag ) {
  8960. ret = wc_Sha256Final(NULL, NULL);
  8961. if (ret != BAD_FUNC_ARG) {
  8962. flag = WOLFSSL_FATAL_ERROR;
  8963. }
  8964. }
  8965. if (!flag) {
  8966. ret = wc_Sha256Final(NULL, hash1);
  8967. if (ret != BAD_FUNC_ARG) {
  8968. flag = WOLFSSL_FATAL_ERROR;
  8969. }
  8970. }
  8971. if (!flag) {
  8972. ret = wc_Sha256Final(&sha256, NULL);
  8973. if (ret != BAD_FUNC_ARG) {
  8974. flag = WOLFSSL_FATAL_ERROR;
  8975. }
  8976. }
  8977. wc_Sha256Free(&sha256);
  8978. printf(resultFmt, flag == 0 ? passed : failed);
  8979. #endif
  8980. return flag;
  8981. } /* END test_wc_Sha256Final */
  8982. /*
  8983. * Unit test function for wc_Sha256FinalRaw()
  8984. */
  8985. static int test_wc_Sha256FinalRaw(void)
  8986. {
  8987. int flag = 0;
  8988. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  8989. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  8990. !defined(WOLFSSL_NO_HASH_RAW)
  8991. wc_Sha256 sha256;
  8992. byte* hash_test[3];
  8993. byte hash1[WC_SHA256_DIGEST_SIZE];
  8994. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  8995. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  8996. int times, i, ret;
  8997. /* Initialize */
  8998. ret = wc_InitSha256(&sha256);
  8999. if (ret != 0) {
  9000. flag = ret;
  9001. }
  9002. if (!flag) {
  9003. hash_test[0] = hash1;
  9004. hash_test[1] = hash2;
  9005. hash_test[2] = hash3;
  9006. }
  9007. times = sizeof(hash_test) / sizeof(byte*);
  9008. /* Good test args. */
  9009. printf(testingFmt, "wc_Sha256FinalRaw()");
  9010. for (i = 0; i < times; i++) {
  9011. if (!flag) {
  9012. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  9013. if (ret != 0) {
  9014. flag = WOLFSSL_FATAL_ERROR;
  9015. }
  9016. }
  9017. }
  9018. /* Test bad args. */
  9019. if (!flag ) {
  9020. ret = wc_Sha256FinalRaw(NULL, NULL);
  9021. if (ret != BAD_FUNC_ARG) {
  9022. flag = WOLFSSL_FATAL_ERROR;
  9023. }
  9024. }
  9025. if (!flag) {
  9026. ret = wc_Sha256FinalRaw(NULL, hash1);
  9027. if (ret != BAD_FUNC_ARG) {
  9028. flag = WOLFSSL_FATAL_ERROR;
  9029. }
  9030. }
  9031. if (!flag) {
  9032. ret = wc_Sha256FinalRaw(&sha256, NULL);
  9033. if (ret != BAD_FUNC_ARG) {
  9034. flag = WOLFSSL_FATAL_ERROR;
  9035. }
  9036. }
  9037. wc_Sha256Free(&sha256);
  9038. printf(resultFmt, flag == 0 ? passed : failed);
  9039. #endif
  9040. return flag;
  9041. } /* END test_wc_Sha256FinalRaw */
  9042. /*
  9043. * Unit test function for wc_Sha256GetFlags()
  9044. */
  9045. static int test_wc_Sha256GetFlags(void)
  9046. {
  9047. int flag = 0;
  9048. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  9049. wc_Sha256 sha256;
  9050. word32 flags = 0;
  9051. printf(testingFmt, "wc_Sha256GetFlags()");
  9052. /* Initialize */
  9053. flag = wc_InitSha256(&sha256);
  9054. if (flag == 0) {
  9055. flag = wc_Sha256GetFlags(&sha256, &flags);
  9056. }
  9057. if (flag == 0) {
  9058. if (flags & WC_HASH_FLAG_ISCOPY) {
  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_Sha256GetFlags */
  9067. /*
  9068. * Unit test function for wc_Sha256Free()
  9069. */
  9070. static int test_wc_Sha256Free(void)
  9071. {
  9072. int flag = 0;
  9073. #ifndef NO_SHA256
  9074. printf(testingFmt, "wc_Sha256Free()");
  9075. wc_Sha256Free(NULL);
  9076. printf(resultFmt, flag == 0 ? passed : failed);
  9077. #endif
  9078. return flag;
  9079. } /* END test_wc_Sha256Free */
  9080. /*
  9081. * Unit test function for wc_Sha256GetHash()
  9082. */
  9083. static int test_wc_Sha256GetHash(void)
  9084. {
  9085. int flag = 0;
  9086. #ifndef NO_SHA256
  9087. wc_Sha256 sha256;
  9088. byte hash1[WC_SHA256_DIGEST_SIZE];
  9089. printf(testingFmt, "wc_Sha256GetHash()");
  9090. /* Initialize */
  9091. flag = wc_InitSha256(&sha256);
  9092. if (flag == 0) {
  9093. flag = wc_Sha256GetHash(&sha256, hash1);
  9094. }
  9095. /*test bad arguments*/
  9096. if (flag == 0) {
  9097. flag = wc_Sha256GetHash(NULL, NULL);
  9098. if (flag == BAD_FUNC_ARG) {
  9099. flag = 0;
  9100. }
  9101. }
  9102. if (flag == 0) {
  9103. flag = wc_Sha256GetHash(NULL, hash1);
  9104. if (flag == BAD_FUNC_ARG) {
  9105. flag = 0;
  9106. }
  9107. }
  9108. if (flag == 0) {
  9109. flag = wc_Sha256GetHash(&sha256, NULL);
  9110. if (flag == BAD_FUNC_ARG) {
  9111. flag = 0;
  9112. }
  9113. }
  9114. wc_Sha256Free(&sha256);
  9115. printf(resultFmt, flag == 0 ? passed : failed);
  9116. #endif
  9117. return flag;
  9118. } /* END test_wc_Sha256GetHash */
  9119. /*
  9120. * Unit test function for wc_Sha256Copy()
  9121. */
  9122. static int test_wc_Sha256Copy(void)
  9123. {
  9124. int flag = 0;
  9125. #ifndef NO_SHA256
  9126. wc_Sha256 sha256;
  9127. wc_Sha256 temp;
  9128. printf(testingFmt, "wc_Sha256Copy()");
  9129. /* Initialize */
  9130. flag = wc_InitSha256(&sha256);
  9131. if (flag == 0) {
  9132. flag = wc_InitSha256(&temp);
  9133. }
  9134. if (flag == 0) {
  9135. flag = wc_Sha256Copy(&sha256, &temp);
  9136. }
  9137. /*test bad arguments*/
  9138. if (flag == 0) {
  9139. flag = wc_Sha256Copy(NULL, NULL);
  9140. if (flag == BAD_FUNC_ARG) {
  9141. flag = 0;
  9142. }
  9143. }
  9144. if (flag == 0) {
  9145. flag = wc_Sha256Copy(NULL, &temp);
  9146. if (flag == BAD_FUNC_ARG) {
  9147. flag = 0;
  9148. }
  9149. }
  9150. if (flag == 0) {
  9151. flag = wc_Sha256Copy(&sha256, NULL);
  9152. if (flag == BAD_FUNC_ARG) {
  9153. flag = 0;
  9154. }
  9155. }
  9156. wc_Sha256Free(&sha256);
  9157. wc_Sha256Free(&temp);
  9158. printf(resultFmt, flag == 0 ? passed : failed);
  9159. #endif
  9160. return flag;
  9161. } /* END test_wc_Sha256Copy */
  9162. /*
  9163. * Testing wc_InitSha512()
  9164. */
  9165. static int test_wc_InitSha512(void)
  9166. {
  9167. int flag = 0;
  9168. #ifdef WOLFSSL_SHA512
  9169. wc_Sha512 sha512;
  9170. int ret;
  9171. printf(testingFmt, "wc_InitSha512()");
  9172. /* Test good arg. */
  9173. ret = wc_InitSha512(&sha512);
  9174. if (ret != 0) {
  9175. flag = WOLFSSL_FATAL_ERROR;
  9176. }
  9177. /* Test bad arg. */
  9178. if (!flag) {
  9179. ret = wc_InitSha512(NULL);
  9180. if (ret != BAD_FUNC_ARG) {
  9181. flag = WOLFSSL_FATAL_ERROR;
  9182. }
  9183. }
  9184. wc_Sha512Free(&sha512);
  9185. printf(resultFmt, flag == 0 ? passed : failed);
  9186. #endif
  9187. return flag;
  9188. } /* END test_wc_InitSha512 */
  9189. /*
  9190. * wc_Sha512Update() test.
  9191. */
  9192. static int test_wc_Sha512Update(void)
  9193. {
  9194. int flag = 0;
  9195. #ifdef WOLFSSL_SHA512
  9196. wc_Sha512 sha512;
  9197. byte hash[WC_SHA512_DIGEST_SIZE];
  9198. testVector a, b, c;
  9199. int ret;
  9200. ret = wc_InitSha512(&sha512);
  9201. if (ret != 0) {
  9202. flag = ret;
  9203. }
  9204. printf(testingFmt, "wc_Sha512Update()");
  9205. /* Input. */
  9206. if (!flag) {
  9207. a.input = "a";
  9208. a.inLen = XSTRLEN(a.input);
  9209. ret = wc_Sha512Update(&sha512, NULL, 0);
  9210. if (ret != 0) {
  9211. flag = ret;
  9212. }
  9213. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  9214. if (ret != 0) {
  9215. flag = ret;
  9216. }
  9217. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9218. if (ret != 0) {
  9219. flag = ret;
  9220. }
  9221. ret = wc_Sha512Final(&sha512, hash);
  9222. if (ret != 0) {
  9223. flag = ret;
  9224. }
  9225. }
  9226. /* Update input. */
  9227. if (!flag) {
  9228. a.input = "abc";
  9229. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  9230. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  9231. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  9232. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  9233. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  9234. a.inLen = XSTRLEN(a.input);
  9235. a.outLen = XSTRLEN(a.output);
  9236. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9237. if (ret != 0) {
  9238. flag = ret;
  9239. }
  9240. }
  9241. if (!flag) {
  9242. ret = wc_Sha512Final(&sha512, hash);
  9243. if (ret != 0) {
  9244. flag = ret;
  9245. }
  9246. }
  9247. if (!flag) {
  9248. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  9249. flag = WOLFSSL_FATAL_ERROR;
  9250. }
  9251. }
  9252. /* Try passing in bad values */
  9253. if (!flag) {
  9254. b.input = NULL;
  9255. b.inLen = 0;
  9256. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  9257. if (ret != 0) {
  9258. flag = ret;
  9259. }
  9260. }
  9261. if (!flag) {
  9262. c.input = NULL;
  9263. c.inLen = WC_SHA512_DIGEST_SIZE;
  9264. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9265. if (ret != BAD_FUNC_ARG) {
  9266. flag = WOLFSSL_FATAL_ERROR;
  9267. }
  9268. }
  9269. if (!flag) {
  9270. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  9271. if (ret != BAD_FUNC_ARG) {
  9272. flag = WOLFSSL_FATAL_ERROR;
  9273. }
  9274. }
  9275. wc_Sha512Free(&sha512);
  9276. /* If not returned then the unit test passed test vectors. */
  9277. printf(resultFmt, flag == 0 ? passed : failed);
  9278. #endif
  9279. return flag;
  9280. } /* END test_wc_Sha512Update */
  9281. #ifdef WOLFSSL_SHA512
  9282. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9283. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9284. /* Perfoms test for
  9285. * - wc_Sha512Final/wc_Sha512FinalRaw
  9286. * - wc_Sha512_224Final/wc_Sha512_224Final
  9287. * - wc_Sha512_256Final/wc_Sha512_256Final
  9288. * parameter:
  9289. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  9290. * WC_HASH_TYPE_SHA512_256
  9291. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  9292. *return 0 on success
  9293. */
  9294. static int test_Sha512_Family_Final(int type, int isRaw)
  9295. {
  9296. wc_Sha512 sha512;
  9297. byte* hash_test[3];
  9298. byte hash1[WC_SHA512_DIGEST_SIZE];
  9299. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9300. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9301. int times, i, ret;
  9302. int(*initFp)(wc_Sha512*);
  9303. int(*finalFp)(wc_Sha512*, byte*);
  9304. void(*freeFp)(wc_Sha512*);
  9305. if (type == WC_HASH_TYPE_SHA512) {
  9306. initFp = wc_InitSha512;
  9307. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9308. !defined(WOLFSSL_NO_HASH_RAW)
  9309. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  9310. #else
  9311. finalFp = (isRaw)? NULL : wc_Sha512Final;
  9312. #endif
  9313. freeFp = wc_Sha512Free;
  9314. }
  9315. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9316. #if !defined(WOLFSSL_NOSHA512_224)
  9317. else if (type == WC_HASH_TYPE_SHA512_224) {
  9318. initFp = wc_InitSha512_224;
  9319. #if !defined(WOLFSSL_NO_HASH_RAW)
  9320. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  9321. #else
  9322. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  9323. #endif
  9324. freeFp = wc_Sha512_224Free;
  9325. }
  9326. #endif
  9327. #if !defined(WOLFSSL_NOSHA512_256)
  9328. else if (type == WC_HASH_TYPE_SHA512_256) {
  9329. initFp = wc_InitSha512_256;
  9330. #if !defined(WOLFSSL_NO_HASH_RAW)
  9331. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  9332. #else
  9333. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  9334. #endif
  9335. freeFp = wc_Sha512_256Free;
  9336. }
  9337. #endif
  9338. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9339. else
  9340. return BAD_FUNC_ARG;
  9341. /* Initialize */
  9342. ret = initFp(&sha512);
  9343. if (!ret) {
  9344. hash_test[0] = hash1;
  9345. hash_test[1] = hash2;
  9346. hash_test[2] = hash3;
  9347. }
  9348. times = sizeof(hash_test) / sizeof(byte *);
  9349. /* Good test args. */
  9350. for (i = 0; i < times && ret == 0; i++) {
  9351. ret = finalFp(&sha512, hash_test[i]);
  9352. }
  9353. /* Test bad args. */
  9354. if (!ret) {
  9355. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  9356. ret = WOLFSSL_FATAL_ERROR;
  9357. }
  9358. }
  9359. if (!ret) {
  9360. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  9361. ret = WOLFSSL_FATAL_ERROR;
  9362. }
  9363. }
  9364. if (!ret) {
  9365. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  9366. ret = WOLFSSL_FATAL_ERROR;
  9367. }
  9368. }
  9369. freeFp(&sha512);
  9370. return ret;
  9371. }
  9372. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9373. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9374. #endif /* WOLFSSL_SHA512 */
  9375. /*
  9376. * Unit test function for wc_Sha512Final()
  9377. */
  9378. static int test_wc_Sha512Final(void)
  9379. {
  9380. int flag = 0;
  9381. #ifdef WOLFSSL_SHA512
  9382. wc_Sha512 sha512;
  9383. byte* hash_test[3];
  9384. byte hash1[WC_SHA512_DIGEST_SIZE];
  9385. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9386. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9387. int times, i, ret;
  9388. /* Initialize */
  9389. ret = wc_InitSha512(&sha512);
  9390. if (ret != 0) {
  9391. flag = ret;
  9392. }
  9393. if (!flag) {
  9394. hash_test[0] = hash1;
  9395. hash_test[1] = hash2;
  9396. hash_test[2] = hash3;
  9397. }
  9398. times = sizeof(hash_test) / sizeof(byte *);
  9399. /* Good test args. */
  9400. printf(testingFmt, "wc_Sha512Final()");
  9401. for (i = 0; i < times; i++) {
  9402. if (!flag) {
  9403. ret = wc_Sha512Final(&sha512, hash_test[i]);
  9404. if (ret != 0) {
  9405. flag = WOLFSSL_FATAL_ERROR;
  9406. }
  9407. }
  9408. }
  9409. /* Test bad args. */
  9410. if (!flag) {
  9411. ret = wc_Sha512Final(NULL, NULL);
  9412. if (ret != BAD_FUNC_ARG) {
  9413. flag = WOLFSSL_FATAL_ERROR;
  9414. }
  9415. if (!flag) {}
  9416. ret = wc_Sha512Final(NULL, hash1);
  9417. if (ret != BAD_FUNC_ARG) {
  9418. flag = WOLFSSL_FATAL_ERROR;
  9419. }
  9420. }
  9421. if (!flag) {
  9422. ret = wc_Sha512Final(&sha512, NULL);
  9423. if (ret != BAD_FUNC_ARG) {
  9424. flag = WOLFSSL_FATAL_ERROR;
  9425. }
  9426. }
  9427. wc_Sha512Free(&sha512);
  9428. printf(resultFmt, flag == 0 ? passed : failed);
  9429. #endif
  9430. return flag;
  9431. } /* END test_wc_Sha512Final */
  9432. /*
  9433. * Unit test function for wc_Sha512GetFlags()
  9434. */
  9435. static int test_wc_Sha512GetFlags(void)
  9436. {
  9437. int flag = 0;
  9438. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  9439. wc_Sha512 sha512;
  9440. word32 flags = 0;
  9441. printf(testingFmt, "wc_Sha512GetFlags()");
  9442. /* Initialize */
  9443. flag = wc_InitSha512(&sha512);
  9444. if (flag == 0) {
  9445. flag = wc_Sha512GetFlags(&sha512, &flags);
  9446. }
  9447. if (flag == 0) {
  9448. if (flags & WC_HASH_FLAG_ISCOPY) {
  9449. flag = 0;
  9450. }
  9451. }
  9452. wc_Sha512Free(&sha512);
  9453. printf(resultFmt, flag == 0 ? passed : failed);
  9454. #endif
  9455. return flag;
  9456. } /* END test_wc_Sha512GetFlags */
  9457. /*
  9458. * Unit test function for wc_Sha512FinalRaw()
  9459. */
  9460. static int test_wc_Sha512FinalRaw(void)
  9461. {
  9462. int flag = 0;
  9463. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  9464. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  9465. !defined(WOLFSSL_NO_HASH_RAW)
  9466. wc_Sha512 sha512;
  9467. byte* hash_test[3];
  9468. byte hash1[WC_SHA512_DIGEST_SIZE];
  9469. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9470. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9471. int times, i, ret;
  9472. /* Initialize */
  9473. ret = wc_InitSha512(&sha512);
  9474. if (ret != 0) {
  9475. flag = ret;
  9476. }
  9477. if (!flag) {
  9478. hash_test[0] = hash1;
  9479. hash_test[1] = hash2;
  9480. hash_test[2] = hash3;
  9481. }
  9482. times = sizeof(hash_test) / sizeof(byte*);
  9483. /* Good test args. */
  9484. printf(testingFmt, "wc_Sha512FinalRaw()");
  9485. for (i = 0; i < times; i++) {
  9486. if (!flag) {
  9487. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  9488. if (ret != 0) {
  9489. flag = WOLFSSL_FATAL_ERROR;
  9490. }
  9491. }
  9492. }
  9493. /* Test bad args. */
  9494. if (!flag ) {
  9495. ret = wc_Sha512FinalRaw(NULL, NULL);
  9496. if (ret != BAD_FUNC_ARG) {
  9497. flag = WOLFSSL_FATAL_ERROR;
  9498. }
  9499. }
  9500. if (!flag) {
  9501. ret = wc_Sha512FinalRaw(NULL, hash1);
  9502. if (ret != BAD_FUNC_ARG) {
  9503. flag = WOLFSSL_FATAL_ERROR;
  9504. }
  9505. }
  9506. if (!flag) {
  9507. ret = wc_Sha512FinalRaw(&sha512, NULL);
  9508. if (ret != BAD_FUNC_ARG) {
  9509. flag = WOLFSSL_FATAL_ERROR;
  9510. }
  9511. }
  9512. wc_Sha512Free(&sha512);
  9513. printf(resultFmt, flag == 0 ? passed : failed);
  9514. #endif
  9515. return flag;
  9516. } /* END test_wc_Sha512FinalRaw */
  9517. /*
  9518. * Unit test function for wc_Sha512Free()
  9519. */
  9520. static int test_wc_Sha512Free(void)
  9521. {
  9522. int flag = 0;
  9523. #ifdef WOLFSSL_SHA512
  9524. printf(testingFmt, "wc_Sha512Free()");
  9525. wc_Sha512Free(NULL);
  9526. printf(resultFmt, flag == 0 ? passed : failed);
  9527. #endif
  9528. return flag;
  9529. } /* END test_wc_Sha512Free */
  9530. #ifdef WOLFSSL_SHA512
  9531. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9532. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9533. static int test_Sha512_Family_GetHash(int type )
  9534. {
  9535. int flag = 0;
  9536. int(*initFp)(wc_Sha512*);
  9537. int(*ghashFp)(wc_Sha512*, byte*);
  9538. wc_Sha512 sha512;
  9539. byte hash1[WC_SHA512_DIGEST_SIZE];
  9540. if (type == WC_HASH_TYPE_SHA512) {
  9541. initFp = wc_InitSha512;
  9542. ghashFp = wc_Sha512GetHash;
  9543. }
  9544. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9545. #if !defined(WOLFSSL_NOSHA512_224)
  9546. else if (type == WC_HASH_TYPE_SHA512_224) {
  9547. initFp = wc_InitSha512_224;
  9548. ghashFp = wc_Sha512_224GetHash;
  9549. }
  9550. #endif
  9551. #if !defined(WOLFSSL_NOSHA512_256)
  9552. else if (type == WC_HASH_TYPE_SHA512_256) {
  9553. initFp = wc_InitSha512_256;
  9554. ghashFp = wc_Sha512_256GetHash;
  9555. }
  9556. #endif
  9557. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9558. else {
  9559. initFp = NULL;
  9560. ghashFp = NULL;
  9561. }
  9562. if (initFp == NULL || ghashFp == NULL)
  9563. return WOLFSSL_FATAL_ERROR;
  9564. if (!flag) {
  9565. flag = initFp(&sha512);
  9566. }
  9567. if (!flag) {
  9568. flag = ghashFp(&sha512, hash1);
  9569. }
  9570. /*test bad arguments*/
  9571. if (!flag) {
  9572. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  9573. flag = WOLFSSL_FATAL_ERROR;
  9574. }
  9575. if (!flag) {
  9576. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  9577. flag = WOLFSSL_FATAL_ERROR;
  9578. }
  9579. if (!flag) {
  9580. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  9581. flag = WOLFSSL_FATAL_ERROR;
  9582. }
  9583. wc_Sha512Free(&sha512);
  9584. return flag;
  9585. }
  9586. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9587. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9588. #endif /* WOLFSSL_SHA512 */
  9589. /*
  9590. * Unit test function for wc_Sha512GetHash()
  9591. */
  9592. static int test_wc_Sha512GetHash(void)
  9593. {
  9594. int flag = 0;
  9595. #ifdef WOLFSSL_SHA512
  9596. wc_Sha512 sha512;
  9597. byte hash1[WC_SHA512_DIGEST_SIZE];
  9598. printf(testingFmt, "wc_Sha512GetHash()");
  9599. /* Initialize */
  9600. flag = wc_InitSha512(&sha512);
  9601. if (flag == 0) {
  9602. flag = wc_Sha512GetHash(&sha512, hash1);
  9603. }
  9604. /*test bad arguments*/
  9605. if (flag == 0) {
  9606. flag = wc_Sha512GetHash(NULL, NULL);
  9607. if (flag == BAD_FUNC_ARG) {
  9608. flag = 0;
  9609. }
  9610. }
  9611. if (flag == 0) {
  9612. flag = wc_Sha512GetHash(NULL, hash1);
  9613. if (flag == BAD_FUNC_ARG) {
  9614. flag = 0;
  9615. }
  9616. }
  9617. if (flag == 0) {
  9618. flag = wc_Sha512GetHash(&sha512, NULL);
  9619. if (flag == BAD_FUNC_ARG) {
  9620. flag = 0;
  9621. }
  9622. }
  9623. wc_Sha512Free(&sha512);
  9624. printf(resultFmt, flag == 0 ? passed : failed);
  9625. #endif
  9626. return flag;
  9627. } /* END test_wc_Sha512GetHash */
  9628. /*
  9629. * Unit test function for wc_Sha512Copy()
  9630. */
  9631. static int test_wc_Sha512Copy(void)
  9632. {
  9633. int flag = 0;
  9634. #ifdef WOLFSSL_SHA512
  9635. wc_Sha512 sha512;
  9636. wc_Sha512 temp;
  9637. printf(testingFmt, "wc_Sha512Copy()");
  9638. /* Initialize */
  9639. flag = wc_InitSha512(&sha512);
  9640. if (flag == 0) {
  9641. flag = wc_InitSha512(&temp);
  9642. }
  9643. if (flag == 0) {
  9644. flag = wc_Sha512Copy(&sha512, &temp);
  9645. }
  9646. /*test bad arguments*/
  9647. if (flag == 0) {
  9648. flag = wc_Sha512Copy(NULL, NULL);
  9649. if (flag == BAD_FUNC_ARG) {
  9650. flag = 0;
  9651. }
  9652. }
  9653. if (flag == 0) {
  9654. flag = wc_Sha512Copy(NULL, &temp);
  9655. if (flag == BAD_FUNC_ARG) {
  9656. flag = 0;
  9657. }
  9658. }
  9659. if (flag == 0) {
  9660. flag = wc_Sha512Copy(&sha512, NULL);
  9661. if (flag == BAD_FUNC_ARG) {
  9662. flag = 0;
  9663. }
  9664. }
  9665. wc_Sha512Free(&sha512);
  9666. wc_Sha512Free(&temp);
  9667. printf(resultFmt, flag == 0 ? passed : failed);
  9668. #endif
  9669. return flag;
  9670. } /* END test_wc_Sha512Copy */
  9671. static int test_wc_InitSha512_224(void)
  9672. {
  9673. int flag = 0;
  9674. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9675. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9676. wc_Sha512 sha512;
  9677. int ret;
  9678. printf(testingFmt, "wc_InitSha512_224()");
  9679. /* Test good arg. */
  9680. ret = wc_InitSha512_224(&sha512);
  9681. if (ret != 0) {
  9682. flag = WOLFSSL_FATAL_ERROR;
  9683. }
  9684. /* Test bad arg. */
  9685. if (!flag) {
  9686. ret = wc_InitSha512_224(NULL);
  9687. if (ret != BAD_FUNC_ARG) {
  9688. flag = WOLFSSL_FATAL_ERROR;
  9689. }
  9690. }
  9691. wc_Sha512_224Free(&sha512);
  9692. printf(resultFmt, flag == 0 ? passed : failed);
  9693. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9694. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9695. return flag;
  9696. }
  9697. static int test_wc_Sha512_224Update(void)
  9698. {
  9699. int flag = 0;
  9700. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9701. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9702. wc_Sha512 sha512;
  9703. byte hash[WC_SHA512_DIGEST_SIZE];
  9704. testVector a, c;
  9705. int ret;
  9706. ret = wc_InitSha512_224(&sha512);
  9707. if (ret != 0) {
  9708. flag = ret;
  9709. }
  9710. printf(testingFmt, "wc_Sha512_224Update()");
  9711. /* Input. */
  9712. if (!flag) {
  9713. a.input = "a";
  9714. a.inLen = XSTRLEN(a.input);
  9715. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  9716. if (ret != 0) {
  9717. flag = ret;
  9718. }
  9719. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  9720. if (ret != 0) {
  9721. flag = ret;
  9722. }
  9723. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9724. if (ret != 0) {
  9725. flag = ret;
  9726. }
  9727. ret = wc_Sha512_224Final(&sha512, hash);
  9728. if (ret != 0) {
  9729. flag = ret;
  9730. }
  9731. }
  9732. /* Update input. */
  9733. if (!flag) {
  9734. a.input = "abc";
  9735. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  9736. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  9737. a.inLen = XSTRLEN(a.input);
  9738. a.outLen = XSTRLEN(a.output);
  9739. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9740. if (ret != 0) {
  9741. flag = ret;
  9742. }
  9743. }
  9744. if (!flag) {
  9745. ret = wc_Sha512_224Final(&sha512, hash);
  9746. if (ret != 0) {
  9747. flag = ret;
  9748. }
  9749. }
  9750. if (!flag) {
  9751. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  9752. flag = WOLFSSL_FATAL_ERROR;
  9753. }
  9754. }
  9755. if (!flag) {
  9756. c.input = NULL;
  9757. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  9758. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9759. if (ret != BAD_FUNC_ARG) {
  9760. flag = WOLFSSL_FATAL_ERROR;
  9761. }
  9762. }
  9763. if (!flag) {
  9764. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  9765. if (ret != BAD_FUNC_ARG) {
  9766. flag = WOLFSSL_FATAL_ERROR;
  9767. }
  9768. }
  9769. wc_Sha512_224Free(&sha512);
  9770. /* If not returned then the unit test passed test vectors. */
  9771. printf(resultFmt, flag == 0 ? passed : failed);
  9772. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9773. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9774. return flag;
  9775. }
  9776. static int test_wc_Sha512_224Final(void)
  9777. {
  9778. int flag = 0;
  9779. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9780. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9781. printf(testingFmt, "wc_Sha512_224Final()");
  9782. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  9783. printf(resultFmt, flag == 0 ? passed : failed);
  9784. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  9785. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9786. return flag;
  9787. }
  9788. static int test_wc_Sha512_224GetFlags(void)
  9789. {
  9790. int flag = 0;
  9791. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9792. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  9793. wc_Sha512 sha512, copy;
  9794. word32 flags = 0;
  9795. printf(testingFmt, "wc_Sha512_224GetFlags()");
  9796. /* Initialize */
  9797. flag = wc_InitSha512_224(&sha512);
  9798. if (!flag) {
  9799. flag = wc_InitSha512_224(&copy);
  9800. }
  9801. if (!flag) {
  9802. flag = wc_Sha512_224Copy(&sha512, &copy);
  9803. }
  9804. if (!flag) {
  9805. flag = wc_Sha512_224GetFlags(&copy, &flags);
  9806. }
  9807. if (!flag) {
  9808. if (flags & WC_HASH_FLAG_ISCOPY)
  9809. flag = 0;
  9810. else
  9811. flag = WOLFSSL_FATAL_ERROR;
  9812. }
  9813. wc_Sha512_224Free(&copy);
  9814. wc_Sha512_224Free(&sha512);
  9815. printf(resultFmt, flag == 0 ? passed : failed);
  9816. #endif
  9817. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9818. return flag;
  9819. }
  9820. static int test_wc_Sha512_224FinalRaw(void)
  9821. {
  9822. int flag = 0;
  9823. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9824. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  9825. !defined(WOLFSSL_NO_HASH_RAW)
  9826. printf(testingFmt, "wc_Sha512_224FinalRaw()");
  9827. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  9828. printf(resultFmt, flag == 0 ? passed : failed);
  9829. #endif
  9830. return flag;
  9831. }
  9832. static int test_wc_Sha512_224Free(void)
  9833. {
  9834. int flag = 0;
  9835. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9836. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9837. printf(testingFmt, "wc_Sha512_224Free()");
  9838. wc_Sha512_224Free(NULL);
  9839. printf(resultFmt, passed);
  9840. #endif
  9841. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9842. return flag;
  9843. }
  9844. static int test_wc_Sha512_224GetHash(void)
  9845. {
  9846. int flag = 0;
  9847. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9848. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9849. printf(testingFmt, "wc_Sha512_224GetHash()");
  9850. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  9851. printf(resultFmt, flag == 0 ? passed : failed);
  9852. #endif
  9853. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9854. return flag;
  9855. }
  9856. static int test_wc_Sha512_224Copy(void)
  9857. {
  9858. int flag = 0;
  9859. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9860. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  9861. wc_Sha512 sha512;
  9862. wc_Sha512 temp;
  9863. printf(testingFmt, "wc_Sha512_224Copy()");
  9864. /* Initialize */
  9865. flag = wc_InitSha512_224(&sha512);
  9866. if (flag == 0) {
  9867. flag = wc_InitSha512_224(&temp);
  9868. }
  9869. if (flag == 0) {
  9870. flag = wc_Sha512_224Copy(&sha512, &temp);
  9871. }
  9872. /*test bad arguments*/
  9873. if (flag == 0) {
  9874. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  9875. flag = WOLFSSL_FATAL_ERROR;
  9876. }
  9877. if (flag == 0) {
  9878. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  9879. flag = WOLFSSL_FATAL_ERROR;
  9880. }
  9881. if (flag == 0) {
  9882. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  9883. flag = WOLFSSL_FATAL_ERROR;
  9884. }
  9885. wc_Sha512_224Free(&sha512);
  9886. wc_Sha512_224Free(&temp);
  9887. printf(resultFmt, flag == 0 ? passed : failed);
  9888. #endif
  9889. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9890. return flag;
  9891. }
  9892. static int test_wc_InitSha512_256(void)
  9893. {
  9894. int flag = 0;
  9895. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9896. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9897. wc_Sha512 sha512;
  9898. int ret;
  9899. printf(testingFmt, "wc_InitSha512_256()");
  9900. /* Test good arg. */
  9901. ret = wc_InitSha512_256(&sha512);
  9902. if (ret != 0) {
  9903. flag = WOLFSSL_FATAL_ERROR;
  9904. }
  9905. /* Test bad arg. */
  9906. if (!flag) {
  9907. ret = wc_InitSha512_256(NULL);
  9908. if (ret != BAD_FUNC_ARG) {
  9909. flag = WOLFSSL_FATAL_ERROR;
  9910. }
  9911. }
  9912. wc_Sha512_256Free(&sha512);
  9913. printf(resultFmt, flag == 0 ? passed : failed);
  9914. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9915. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9916. return flag;
  9917. }
  9918. static int test_wc_Sha512_256Update(void)
  9919. {
  9920. int flag = 0;
  9921. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9922. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  9923. wc_Sha512 sha512;
  9924. byte hash[WC_SHA512_DIGEST_SIZE];
  9925. testVector a, c;
  9926. int ret;
  9927. ret = wc_InitSha512_256(&sha512);
  9928. if (ret != 0) {
  9929. flag = ret;
  9930. }
  9931. printf(testingFmt, "wc_Sha512_256Update()");
  9932. /* Input. */
  9933. if (!flag) {
  9934. a.input = "a";
  9935. a.inLen = XSTRLEN(a.input);
  9936. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  9937. if (ret != 0) {
  9938. flag = ret;
  9939. }
  9940. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  9941. if (ret != 0) {
  9942. flag = ret;
  9943. }
  9944. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9945. if (ret != 0) {
  9946. flag = ret;
  9947. }
  9948. ret = wc_Sha512_256Final(&sha512, hash);
  9949. if (ret != 0) {
  9950. flag = ret;
  9951. }
  9952. }
  9953. /* Update input. */
  9954. if (!flag) {
  9955. a.input = "abc";
  9956. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  9957. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  9958. "\x07\xe7\xaf\x23";
  9959. a.inLen = XSTRLEN(a.input);
  9960. a.outLen = XSTRLEN(a.output);
  9961. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9962. if (ret != 0) {
  9963. flag = ret;
  9964. }
  9965. }
  9966. if (!flag) {
  9967. ret = wc_Sha512_256Final(&sha512, hash);
  9968. if (ret != 0) {
  9969. flag = ret;
  9970. }
  9971. }
  9972. if (!flag) {
  9973. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  9974. flag = WOLFSSL_FATAL_ERROR;
  9975. }
  9976. }
  9977. if (!flag) {
  9978. c.input = NULL;
  9979. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  9980. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9981. if (ret != BAD_FUNC_ARG) {
  9982. flag = WOLFSSL_FATAL_ERROR;
  9983. }
  9984. }
  9985. if (!flag) {
  9986. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9987. if (ret != BAD_FUNC_ARG) {
  9988. flag = WOLFSSL_FATAL_ERROR;
  9989. }
  9990. }
  9991. wc_Sha512_256Free(&sha512);
  9992. /* If not returned then the unit test passed test vectors. */
  9993. printf(resultFmt, flag == 0 ? passed : failed);
  9994. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  9995. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9996. return flag;
  9997. }
  9998. static int test_wc_Sha512_256Final(void)
  9999. {
  10000. int flag = 0;
  10001. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10002. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10003. printf(testingFmt, "wc_Sha512_256Final()");
  10004. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  10005. printf(resultFmt, flag == 0 ? passed : failed);
  10006. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10007. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10008. return flag;
  10009. }
  10010. static int test_wc_Sha512_256GetFlags(void)
  10011. {
  10012. int flag = 0;
  10013. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10014. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  10015. wc_Sha512 sha512, copy;
  10016. word32 flags = 0;
  10017. printf(testingFmt, "wc_Sha512_256GetFlags()");
  10018. /* Initialize */
  10019. flag = wc_InitSha512_256(&sha512);
  10020. if (!flag ) {
  10021. flag = wc_InitSha512_256(&copy);
  10022. }
  10023. if (!flag ) {
  10024. flag = wc_Sha512_256Copy(&sha512, &copy);
  10025. }
  10026. if (!flag ) {
  10027. flag = wc_Sha512_256GetFlags(&copy, &flags);
  10028. }
  10029. if (!flag) {
  10030. if (flags & WC_HASH_FLAG_ISCOPY)
  10031. flag = 0;
  10032. else
  10033. flag = WOLFSSL_FATAL_ERROR;
  10034. }
  10035. wc_Sha512_256Free(&sha512);
  10036. printf(resultFmt, flag == 0 ? passed : failed);
  10037. #endif
  10038. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10039. return flag;
  10040. }
  10041. static int test_wc_Sha512_256FinalRaw(void)
  10042. {
  10043. int flag = 0;
  10044. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10045. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  10046. !defined(WOLFSSL_NO_HASH_RAW)
  10047. printf(testingFmt, "wc_Sha512_256FinalRaw()");
  10048. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  10049. printf(resultFmt, flag == 0 ? passed : failed);
  10050. #endif
  10051. return flag;
  10052. }
  10053. static int test_wc_Sha512_256Free(void)
  10054. {
  10055. int flag = 0;
  10056. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10057. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10058. printf(testingFmt, "wc_Sha512_256Free()");
  10059. wc_Sha512_256Free(NULL);
  10060. printf(resultFmt, passed);
  10061. #endif
  10062. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10063. return flag;
  10064. }
  10065. static int test_wc_Sha512_256GetHash(void)
  10066. {
  10067. int flag = 0;
  10068. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10069. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10070. printf(testingFmt, "wc_Sha512_256GetHash()");
  10071. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  10072. printf(resultFmt, flag == 0 ? passed : failed);
  10073. #endif
  10074. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10075. return flag;
  10076. }
  10077. static int test_wc_Sha512_256Copy(void)
  10078. {
  10079. int flag = 0;
  10080. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10081. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10082. wc_Sha512 sha512;
  10083. wc_Sha512 temp;
  10084. printf(testingFmt, "wc_Sha512_256Copy()");
  10085. /* Initialize */
  10086. flag = wc_InitSha512_256(&sha512);
  10087. if (flag == 0) {
  10088. flag = wc_InitSha512_256(&temp);
  10089. }
  10090. if (flag == 0) {
  10091. flag = wc_Sha512_256Copy(&sha512, &temp);
  10092. }
  10093. /*test bad arguments*/
  10094. if (flag == 0) {
  10095. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  10096. flag = WOLFSSL_FATAL_ERROR;
  10097. }
  10098. if (flag == 0) {
  10099. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  10100. flag = WOLFSSL_FATAL_ERROR;
  10101. }
  10102. if (flag == 0) {
  10103. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10104. flag = WOLFSSL_FATAL_ERROR;
  10105. }
  10106. wc_Sha512_256Free(&sha512);
  10107. wc_Sha512_256Free(&temp);
  10108. printf(resultFmt, flag == 0 ? passed : failed);
  10109. #endif
  10110. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10111. return flag;
  10112. }
  10113. /*
  10114. * Testing wc_InitSha384()
  10115. */
  10116. static int test_wc_InitSha384(void)
  10117. {
  10118. int flag = 0;
  10119. #ifdef WOLFSSL_SHA384
  10120. wc_Sha384 sha384;
  10121. int ret;
  10122. printf(testingFmt, "wc_InitSha384()");
  10123. /* Test good arg. */
  10124. ret = wc_InitSha384(&sha384);
  10125. if (ret != 0) {
  10126. flag = WOLFSSL_FATAL_ERROR;
  10127. }
  10128. /* Test bad arg. */
  10129. if (!flag) {
  10130. ret = wc_InitSha384(NULL);
  10131. if (ret != BAD_FUNC_ARG) {
  10132. flag = WOLFSSL_FATAL_ERROR;
  10133. }
  10134. }
  10135. wc_Sha384Free(&sha384);
  10136. printf(resultFmt, flag == 0 ? passed : failed);
  10137. #endif
  10138. return flag;
  10139. } /* END test_wc_InitSha384 */
  10140. /*
  10141. * test wc_Sha384Update()
  10142. */
  10143. static int test_wc_Sha384Update(void)
  10144. {
  10145. int flag = 0;
  10146. #ifdef WOLFSSL_SHA384
  10147. wc_Sha384 sha384;
  10148. byte hash[WC_SHA384_DIGEST_SIZE];
  10149. testVector a, b, c;
  10150. int ret;
  10151. ret = wc_InitSha384(&sha384);
  10152. if (ret != 0) {
  10153. flag = ret;
  10154. }
  10155. printf(testingFmt, "wc_Sha384Update()");
  10156. /* Input */
  10157. if (!flag) {
  10158. a.input = "a";
  10159. a.inLen = XSTRLEN(a.input);
  10160. ret = wc_Sha384Update(&sha384, NULL, 0);
  10161. if (ret != 0) {
  10162. flag = ret;
  10163. }
  10164. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  10165. if (ret != 0) {
  10166. flag = ret;
  10167. }
  10168. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10169. if (ret != 0) {
  10170. flag = ret;
  10171. }
  10172. }
  10173. if (!flag) {
  10174. ret = wc_Sha384Final(&sha384, hash);
  10175. if (ret != 0) {
  10176. flag = ret;
  10177. }
  10178. }
  10179. /* Update input. */
  10180. if (!flag) {
  10181. a.input = "abc";
  10182. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  10183. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  10184. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  10185. "\xc8\x25\xa7";
  10186. a.inLen = XSTRLEN(a.input);
  10187. a.outLen = XSTRLEN(a.output);
  10188. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10189. if (ret != 0) {
  10190. flag = ret;
  10191. }
  10192. }
  10193. if (!flag) {
  10194. ret = wc_Sha384Final(&sha384, hash);
  10195. if (ret != 0) {
  10196. flag = ret;
  10197. }
  10198. }
  10199. if (!flag) {
  10200. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  10201. flag = WOLFSSL_FATAL_ERROR;
  10202. }
  10203. }
  10204. /* Pass in bad values. */
  10205. if (!flag) {
  10206. b.input = NULL;
  10207. b.inLen = 0;
  10208. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  10209. if (ret != 0) {
  10210. flag = ret;
  10211. }
  10212. }
  10213. if (!flag) {
  10214. c.input = NULL;
  10215. c.inLen = WC_SHA384_DIGEST_SIZE;
  10216. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  10217. if (ret != BAD_FUNC_ARG) {
  10218. flag = WOLFSSL_FATAL_ERROR;
  10219. }
  10220. }
  10221. if (!flag) {
  10222. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  10223. if (ret != BAD_FUNC_ARG) {
  10224. flag = WOLFSSL_FATAL_ERROR;
  10225. }
  10226. }
  10227. wc_Sha384Free(&sha384);
  10228. /* If not returned then the unit test passed test vectors. */
  10229. printf(resultFmt, flag == 0 ? passed : failed);
  10230. #endif
  10231. return flag;
  10232. } /* END test_wc_Sha384Update */
  10233. /*
  10234. * Unit test function for wc_Sha384Final();
  10235. */
  10236. static int test_wc_Sha384Final(void)
  10237. {
  10238. int flag = 0;
  10239. #ifdef WOLFSSL_SHA384
  10240. wc_Sha384 sha384;
  10241. byte* hash_test[3];
  10242. byte hash1[WC_SHA384_DIGEST_SIZE];
  10243. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10244. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10245. int times, i, ret;
  10246. /* Initialize */
  10247. ret = wc_InitSha384(&sha384);
  10248. if (ret) {
  10249. flag = ret;
  10250. }
  10251. if (!flag) {
  10252. hash_test[0] = hash1;
  10253. hash_test[1] = hash2;
  10254. hash_test[2] = hash3;
  10255. }
  10256. times = sizeof(hash_test) / sizeof(byte*);
  10257. /* Good test args. */
  10258. printf(testingFmt, "wc_Sha384Final()");
  10259. for (i = 0; i < times; i++) {
  10260. if (!flag) {
  10261. ret = wc_Sha384Final(&sha384, hash_test[i]);
  10262. if (ret != 0) {
  10263. flag = WOLFSSL_FATAL_ERROR;
  10264. }
  10265. }
  10266. }
  10267. /* Test bad args. */
  10268. if (!flag) {
  10269. ret = wc_Sha384Final(NULL, NULL);
  10270. if (ret != BAD_FUNC_ARG) {
  10271. flag = WOLFSSL_FATAL_ERROR;
  10272. }
  10273. }
  10274. if (!flag) {
  10275. ret = wc_Sha384Final(NULL, hash1);
  10276. if (ret != BAD_FUNC_ARG) {
  10277. flag = WOLFSSL_FATAL_ERROR;
  10278. }
  10279. }
  10280. if (!flag) {
  10281. ret = wc_Sha384Final(&sha384, NULL);
  10282. if (ret != BAD_FUNC_ARG) {
  10283. flag = WOLFSSL_FATAL_ERROR;
  10284. }
  10285. }
  10286. wc_Sha384Free(&sha384);
  10287. printf(resultFmt, flag == 0 ? passed : failed);
  10288. #endif
  10289. return flag;
  10290. } /* END test_wc_Sha384Final */
  10291. /*
  10292. * Unit test function for wc_Sha384GetFlags()
  10293. */
  10294. static int test_wc_Sha384GetFlags(void)
  10295. {
  10296. int flag = 0;
  10297. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  10298. wc_Sha384 sha384;
  10299. word32 flags = 0;
  10300. printf(testingFmt, "wc_Sha384GetFlags()");
  10301. /* Initialize */
  10302. flag = wc_InitSha384(&sha384);
  10303. if (flag == 0) {
  10304. flag = wc_Sha384GetFlags(&sha384, &flags);
  10305. }
  10306. if (flag == 0) {
  10307. if (flags & WC_HASH_FLAG_ISCOPY) {
  10308. flag = 0;
  10309. }
  10310. }
  10311. wc_Sha384Free(&sha384);
  10312. printf(resultFmt, flag == 0 ? passed : failed);
  10313. #endif
  10314. return flag;
  10315. } /* END test_wc_Sha384GetFlags */
  10316. /*
  10317. * Unit test function for wc_Sha384FinalRaw()
  10318. */
  10319. static int test_wc_Sha384FinalRaw(void)
  10320. {
  10321. int flag = 0;
  10322. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10323. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10324. !defined(WOLFSSL_NO_HASH_RAW)
  10325. wc_Sha384 sha384;
  10326. byte* hash_test[3];
  10327. byte hash1[WC_SHA384_DIGEST_SIZE];
  10328. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10329. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10330. int times, i, ret;
  10331. /* Initialize */
  10332. ret = wc_InitSha384(&sha384);
  10333. if (ret != 0) {
  10334. flag = ret;
  10335. }
  10336. if (!flag) {
  10337. hash_test[0] = hash1;
  10338. hash_test[1] = hash2;
  10339. hash_test[2] = hash3;
  10340. }
  10341. times = sizeof(hash_test) / sizeof(byte*);
  10342. /* Good test args. */
  10343. printf(testingFmt, "wc_Sha384FinalRaw()");
  10344. for (i = 0; i < times; i++) {
  10345. if (!flag) {
  10346. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  10347. if (ret != 0) {
  10348. flag = WOLFSSL_FATAL_ERROR;
  10349. }
  10350. }
  10351. }
  10352. /* Test bad args. */
  10353. if (!flag ) {
  10354. ret = wc_Sha384FinalRaw(NULL, NULL);
  10355. if (ret != BAD_FUNC_ARG) {
  10356. flag = WOLFSSL_FATAL_ERROR;
  10357. }
  10358. }
  10359. if (!flag) {
  10360. ret = wc_Sha384FinalRaw(NULL, hash1);
  10361. if (ret != BAD_FUNC_ARG) {
  10362. flag = WOLFSSL_FATAL_ERROR;
  10363. }
  10364. }
  10365. if (!flag) {
  10366. ret = wc_Sha384FinalRaw(&sha384, NULL);
  10367. if (ret != BAD_FUNC_ARG) {
  10368. flag = WOLFSSL_FATAL_ERROR;
  10369. }
  10370. }
  10371. wc_Sha384Free(&sha384);
  10372. printf(resultFmt, flag == 0 ? passed : failed);
  10373. #endif
  10374. return flag;
  10375. } /* END test_wc_Sha384FinalRaw */
  10376. /*
  10377. * Unit test function for wc_Sha384Free()
  10378. */
  10379. static int test_wc_Sha384Free(void)
  10380. {
  10381. int flag = 0;
  10382. #ifdef WOLFSSL_SHA384
  10383. printf(testingFmt, "wc_Sha384Free()");
  10384. wc_Sha384Free(NULL);
  10385. printf(resultFmt, flag == 0 ? passed : failed);
  10386. #endif
  10387. return flag;
  10388. } /* END test_wc_Sha384Free */
  10389. /*
  10390. * Unit test function for wc_Sha384GetHash()
  10391. */
  10392. static int test_wc_Sha384GetHash(void)
  10393. {
  10394. int flag = 0;
  10395. #ifdef WOLFSSL_SHA384
  10396. wc_Sha384 sha384;
  10397. byte hash1[WC_SHA384_DIGEST_SIZE];
  10398. printf(testingFmt, "wc_Sha384GetHash()");
  10399. /* Initialize */
  10400. flag = wc_InitSha384(&sha384);
  10401. if (flag == 0) {
  10402. flag = wc_Sha384GetHash(&sha384, hash1);
  10403. }
  10404. /*test bad arguments*/
  10405. if (flag == 0) {
  10406. flag = wc_Sha384GetHash(NULL, NULL);
  10407. if (flag == BAD_FUNC_ARG) {
  10408. flag = 0;
  10409. }
  10410. }
  10411. if (flag == 0) {
  10412. flag = wc_Sha384GetHash(NULL, hash1);
  10413. if (flag == BAD_FUNC_ARG) {
  10414. flag = 0;
  10415. }
  10416. }
  10417. if (flag == 0) {
  10418. flag = wc_Sha384GetHash(&sha384, NULL);
  10419. if (flag == BAD_FUNC_ARG) {
  10420. flag = 0;
  10421. }
  10422. }
  10423. wc_Sha384Free(&sha384);
  10424. printf(resultFmt, flag == 0 ? passed : failed);
  10425. #endif
  10426. return flag;
  10427. } /* END test_wc_Sha384GetHash */
  10428. /*
  10429. * Unit test function for wc_Sha384Copy()
  10430. */
  10431. static int test_wc_Sha384Copy(void)
  10432. {
  10433. int flag = 0;
  10434. #ifdef WOLFSSL_SHA384
  10435. wc_Sha384 sha384;
  10436. wc_Sha384 temp;
  10437. printf(testingFmt, "wc_Sha384Copy()");
  10438. /* Initialize */
  10439. flag = wc_InitSha384(&sha384);
  10440. if (flag == 0) {
  10441. flag = wc_InitSha384(&temp);
  10442. }
  10443. if (flag == 0) {
  10444. flag = wc_Sha384Copy(&sha384, &temp);
  10445. }
  10446. /*test bad arguments*/
  10447. if (flag == 0) {
  10448. flag = wc_Sha384Copy(NULL, NULL);
  10449. if (flag == BAD_FUNC_ARG) {
  10450. flag = 0;
  10451. }
  10452. }
  10453. if (flag == 0) {
  10454. flag = wc_Sha384Copy(NULL, &temp);
  10455. if (flag == BAD_FUNC_ARG) {
  10456. flag = 0;
  10457. }
  10458. }
  10459. if (flag == 0) {
  10460. flag = wc_Sha384Copy(&sha384, NULL);
  10461. if (flag == BAD_FUNC_ARG) {
  10462. flag = 0;
  10463. }
  10464. }
  10465. wc_Sha384Free(&sha384);
  10466. wc_Sha384Free(&temp);
  10467. printf(resultFmt, flag == 0 ? passed : failed);
  10468. #endif
  10469. return flag;
  10470. } /* END test_wc_Sha384Copy */
  10471. /*
  10472. * Testing wc_InitSha224();
  10473. */
  10474. static int test_wc_InitSha224(void)
  10475. {
  10476. int flag = 0;
  10477. #ifdef WOLFSSL_SHA224
  10478. wc_Sha224 sha224;
  10479. int ret;
  10480. printf(testingFmt, "wc_InitSha224()");
  10481. /* Test good arg. */
  10482. ret = wc_InitSha224(&sha224);
  10483. if (ret != 0) {
  10484. flag = WOLFSSL_FATAL_ERROR;
  10485. }
  10486. /* Test bad arg. */
  10487. if (!flag) {
  10488. ret = wc_InitSha224(NULL);
  10489. if (ret != BAD_FUNC_ARG) {
  10490. flag = WOLFSSL_FATAL_ERROR;
  10491. }
  10492. }
  10493. wc_Sha224Free(&sha224);
  10494. printf(resultFmt, flag == 0 ? passed : failed);
  10495. #endif
  10496. return flag;
  10497. } /* END test_wc_InitSha224 */
  10498. /*
  10499. * Unit test on wc_Sha224Update
  10500. */
  10501. static int test_wc_Sha224Update(void)
  10502. {
  10503. int flag = 0;
  10504. #ifdef WOLFSSL_SHA224
  10505. wc_Sha224 sha224;
  10506. byte hash[WC_SHA224_DIGEST_SIZE];
  10507. testVector a, b, c;
  10508. int ret;
  10509. ret = wc_InitSha224(&sha224);
  10510. if (ret != 0) {
  10511. flag = ret;
  10512. }
  10513. printf(testingFmt, "wc_Sha224Update()");
  10514. /* Input. */
  10515. if (!flag) {
  10516. a.input = "a";
  10517. a.inLen = XSTRLEN(a.input);
  10518. ret = wc_Sha224Update(&sha224, NULL, 0);
  10519. if (ret != 0) {
  10520. flag = ret;
  10521. }
  10522. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  10523. if (ret != 0) {
  10524. flag = ret;
  10525. }
  10526. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10527. if (ret != 0) {
  10528. flag = ret;
  10529. }
  10530. }
  10531. if (!flag) {
  10532. ret = wc_Sha224Final(&sha224, hash);
  10533. if (ret != 0) {
  10534. flag = ret;
  10535. }
  10536. }
  10537. /* Update input. */
  10538. if (!flag) {
  10539. a.input = "abc";
  10540. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  10541. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  10542. a.inLen = XSTRLEN(a.input);
  10543. a.outLen = XSTRLEN(a.output);
  10544. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10545. if (ret != 0) {
  10546. flag = ret;
  10547. }
  10548. }
  10549. if (!flag) {
  10550. ret = wc_Sha224Final(&sha224, hash);
  10551. if (ret != 0) {
  10552. flag = ret;
  10553. }
  10554. }
  10555. if (!flag) {
  10556. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  10557. flag = WOLFSSL_FATAL_ERROR;
  10558. }
  10559. }
  10560. /* Pass in bad values. */
  10561. if (!flag) {
  10562. b.input = NULL;
  10563. b.inLen = 0;
  10564. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  10565. if (ret != 0) {
  10566. flag = ret;
  10567. }
  10568. }
  10569. if (!flag) {
  10570. c.input = NULL;
  10571. c.inLen = WC_SHA224_DIGEST_SIZE;
  10572. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  10573. if (ret != BAD_FUNC_ARG) {
  10574. flag = WOLFSSL_FATAL_ERROR;
  10575. }
  10576. }
  10577. if (!flag) {
  10578. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10579. if (ret != BAD_FUNC_ARG) {
  10580. flag = WOLFSSL_FATAL_ERROR;
  10581. }
  10582. }
  10583. wc_Sha224Free(&sha224);
  10584. /* If not returned then the unit test passed test vectors. */
  10585. printf(resultFmt, flag == 0 ? passed : failed);
  10586. #endif
  10587. return flag;
  10588. } /* END test_wc_Sha224Update */
  10589. /*
  10590. * Unit test for wc_Sha224Final();
  10591. */
  10592. static int test_wc_Sha224Final(void)
  10593. {
  10594. int flag = 0;
  10595. #ifdef WOLFSSL_SHA224
  10596. wc_Sha224 sha224;
  10597. byte* hash_test[3];
  10598. byte hash1[WC_SHA224_DIGEST_SIZE];
  10599. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  10600. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  10601. int times, i, ret;
  10602. /* Initialize */
  10603. ret = wc_InitSha224(&sha224);
  10604. if (ret) {
  10605. flag = ret;
  10606. }
  10607. if (!flag) {
  10608. hash_test[0] = hash1;
  10609. hash_test[1] = hash2;
  10610. hash_test[2] = hash3;
  10611. }
  10612. times = sizeof(hash_test) / sizeof(byte*);
  10613. /* Good test args. */
  10614. printf(testingFmt, "wc_sha224Final()");
  10615. /* Testing oversized buffers. */
  10616. for (i = 0; i < times; i++) {
  10617. if (!flag) {
  10618. ret = wc_Sha224Final(&sha224, hash_test[i]);
  10619. if (ret != 0) {
  10620. flag = WOLFSSL_FATAL_ERROR;
  10621. }
  10622. }
  10623. }
  10624. /* Test bad args. */
  10625. if (!flag) {
  10626. ret = wc_Sha224Final(NULL, NULL);
  10627. if (ret != BAD_FUNC_ARG) {
  10628. flag = WOLFSSL_FATAL_ERROR;
  10629. }
  10630. }
  10631. if (!flag) {
  10632. ret = wc_Sha224Final(NULL, hash1);
  10633. if (ret != BAD_FUNC_ARG) {
  10634. flag = WOLFSSL_FATAL_ERROR;
  10635. }
  10636. }
  10637. if (!flag) {
  10638. ret = wc_Sha224Final(&sha224, NULL);
  10639. if (ret != BAD_FUNC_ARG) {
  10640. flag = WOLFSSL_FATAL_ERROR;
  10641. }
  10642. }
  10643. wc_Sha224Free(&sha224);
  10644. printf(resultFmt, flag == 0 ? passed : failed);
  10645. #endif
  10646. return flag;
  10647. } /* END test_wc_Sha224Final */
  10648. /*
  10649. * Unit test function for wc_Sha224SetFlags()
  10650. */
  10651. static int test_wc_Sha224SetFlags(void)
  10652. {
  10653. int flag = 0;
  10654. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10655. wc_Sha224 sha224;
  10656. word32 flags = 0;
  10657. printf(testingFmt, "wc_Sha224SetFlags()");
  10658. /* Initialize */
  10659. flag = wc_InitSha224(&sha224);
  10660. if (flag == 0) {
  10661. flag = wc_Sha224SetFlags(&sha224, flags);
  10662. }
  10663. if (flag == 0) {
  10664. if (flags & WC_HASH_FLAG_ISCOPY) {
  10665. flag = 0;
  10666. }
  10667. }
  10668. wc_Sha224Free(&sha224);
  10669. printf(resultFmt, flag == 0 ? passed : failed);
  10670. #endif
  10671. return flag;
  10672. } /* END test_wc_Sha224SetFlags */
  10673. /*
  10674. * Unit test function for wc_Sha224GetFlags()
  10675. */
  10676. static int test_wc_Sha224GetFlags(void)
  10677. {
  10678. int flag = 0;
  10679. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  10680. wc_Sha224 sha224;
  10681. word32 flags = 0;
  10682. printf(testingFmt, "wc_Sha224GetFlags()");
  10683. /* Initialize */
  10684. flag = wc_InitSha224(&sha224);
  10685. if (flag == 0) {
  10686. flag = wc_Sha224GetFlags(&sha224, &flags);
  10687. }
  10688. if (flag == 0) {
  10689. if (flags & WC_HASH_FLAG_ISCOPY) {
  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_Sha224GetFlags */
  10698. /*
  10699. * Unit test function for wc_Sha224Free()
  10700. */
  10701. static int test_wc_Sha224Free(void)
  10702. {
  10703. int flag = 0;
  10704. #ifdef WOLFSSL_SHA224
  10705. printf(testingFmt, "wc_Sha224Free()");
  10706. wc_Sha224Free(NULL);
  10707. printf(resultFmt, flag == 0 ? passed : failed);
  10708. #endif
  10709. return flag;
  10710. } /* END test_wc_Sha224Free */
  10711. /*
  10712. * Unit test function for wc_Sha224GetHash()
  10713. */
  10714. static int test_wc_Sha224GetHash(void)
  10715. {
  10716. int flag = 0;
  10717. #ifdef WOLFSSL_SHA224
  10718. wc_Sha224 sha224;
  10719. byte hash1[WC_SHA224_DIGEST_SIZE];
  10720. printf(testingFmt, "wc_Sha224GetHash()");
  10721. /* Initialize */
  10722. flag = wc_InitSha224(&sha224);
  10723. if (flag == 0) {
  10724. flag = wc_Sha224GetHash(&sha224, hash1);
  10725. }
  10726. /*test bad arguments*/
  10727. if (flag == 0) {
  10728. flag = wc_Sha224GetHash(NULL, NULL);
  10729. if (flag == BAD_FUNC_ARG) {
  10730. flag = 0;
  10731. }
  10732. }
  10733. if (flag == 0) {
  10734. flag = wc_Sha224GetHash(NULL, hash1);
  10735. if (flag == BAD_FUNC_ARG) {
  10736. flag = 0;
  10737. }
  10738. }
  10739. if (flag == 0) {
  10740. flag = wc_Sha224GetHash(&sha224, NULL);
  10741. if (flag == BAD_FUNC_ARG) {
  10742. flag = 0;
  10743. }
  10744. }
  10745. wc_Sha224Free(&sha224);
  10746. printf(resultFmt, flag == 0 ? passed : failed);
  10747. #endif
  10748. return flag;
  10749. } /* END test_wc_Sha224GetHash */
  10750. /*
  10751. * Unit test function for wc_Sha224Copy()
  10752. */
  10753. static int test_wc_Sha224Copy(void)
  10754. {
  10755. int flag = 0;
  10756. #ifdef WOLFSSL_SHA224
  10757. wc_Sha224 sha224;
  10758. wc_Sha224 temp;
  10759. printf(testingFmt, "wc_Sha224Copy()");
  10760. /* Initialize */
  10761. flag = wc_InitSha224(&sha224);
  10762. if (flag == 0) {
  10763. flag = wc_InitSha224(&temp);
  10764. }
  10765. if (flag == 0) {
  10766. flag = wc_Sha224Copy(&sha224, &temp);
  10767. }
  10768. /*test bad arguments*/
  10769. if (flag == 0) {
  10770. flag = wc_Sha224Copy(NULL, NULL);
  10771. if (flag == BAD_FUNC_ARG) {
  10772. flag = 0;
  10773. }
  10774. }
  10775. if (flag == 0) {
  10776. flag = wc_Sha224Copy(NULL, &temp);
  10777. if (flag == BAD_FUNC_ARG) {
  10778. flag = 0;
  10779. }
  10780. }
  10781. if (flag == 0) {
  10782. flag = wc_Sha224Copy(&sha224, NULL);
  10783. if (flag == BAD_FUNC_ARG) {
  10784. flag = 0;
  10785. }
  10786. }
  10787. wc_Sha224Free(&sha224);
  10788. wc_Sha224Free(&temp);
  10789. printf(resultFmt, flag == 0 ? passed : failed);
  10790. #endif
  10791. return flag;
  10792. } /* END test_wc_Sha224Copy */
  10793. /*
  10794. * Testing wc_InitRipeMd()
  10795. */
  10796. static int test_wc_InitRipeMd(void)
  10797. {
  10798. int flag = 0;
  10799. #ifdef WOLFSSL_RIPEMD
  10800. RipeMd ripemd;
  10801. int ret;
  10802. printf(testingFmt, "wc_InitRipeMd()");
  10803. /* Test good arg. */
  10804. ret = wc_InitRipeMd(&ripemd);
  10805. if (ret != 0) {
  10806. flag = WOLFSSL_FATAL_ERROR;
  10807. }
  10808. /* Test bad arg. */
  10809. if (!flag) {
  10810. ret = wc_InitRipeMd(NULL);
  10811. if (ret != BAD_FUNC_ARG) {
  10812. flag = WOLFSSL_FATAL_ERROR;
  10813. }
  10814. }
  10815. printf(resultFmt, flag == 0 ? passed : failed);
  10816. #endif
  10817. return flag;
  10818. } /* END test_wc_InitRipeMd */
  10819. /*
  10820. * Testing wc_RipeMdUpdate()
  10821. */
  10822. static int test_wc_RipeMdUpdate(void)
  10823. {
  10824. int flag = 0;
  10825. #ifdef WOLFSSL_RIPEMD
  10826. RipeMd ripemd;
  10827. byte hash[RIPEMD_DIGEST_SIZE];
  10828. testVector a, b, c;
  10829. int ret;
  10830. ret = wc_InitRipeMd(&ripemd);
  10831. if (ret != 0) {
  10832. flag = ret;
  10833. }
  10834. printf(testingFmt, "wc_RipeMdUpdate()");
  10835. /* Input */
  10836. if (!flag) {
  10837. a.input = "a";
  10838. a.inLen = XSTRLEN(a.input);
  10839. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10840. if (ret != 0) {
  10841. flag = ret;
  10842. }
  10843. }
  10844. if (!flag) {
  10845. ret = wc_RipeMdFinal(&ripemd, hash);
  10846. if (ret != 0) {
  10847. flag = ret;
  10848. }
  10849. }
  10850. /* Update input. */
  10851. if (!flag) {
  10852. a.input = "abc";
  10853. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  10854. "\xb0\x87\xf1\x5a\x0b\xfc";
  10855. a.inLen = XSTRLEN(a.input);
  10856. a.outLen = XSTRLEN(a.output);
  10857. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  10858. if (ret != 0) {
  10859. flag = ret;
  10860. }
  10861. }
  10862. if (!flag) {
  10863. ret = wc_RipeMdFinal(&ripemd, hash);
  10864. if (ret != 0) {
  10865. flag = ret;
  10866. }
  10867. }
  10868. if (!flag) {
  10869. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  10870. flag = WOLFSSL_FATAL_ERROR;
  10871. }
  10872. }
  10873. /* Pass in bad values. */
  10874. if (!flag) {
  10875. b.input = NULL;
  10876. b.inLen = 0;
  10877. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  10878. if (ret != 0) {
  10879. flag = ret;
  10880. }
  10881. }
  10882. if (!flag) {
  10883. c.input = NULL;
  10884. c.inLen = RIPEMD_DIGEST_SIZE;
  10885. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  10886. if (ret != BAD_FUNC_ARG) {
  10887. flag = WOLFSSL_FATAL_ERROR;
  10888. }
  10889. }
  10890. if (!flag) {
  10891. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  10892. if (ret != BAD_FUNC_ARG) {
  10893. flag = WOLFSSL_FATAL_ERROR;
  10894. }
  10895. }
  10896. printf(resultFmt, flag == 0 ? passed : failed);
  10897. #endif
  10898. return flag;
  10899. } /* END test_wc_RipeMdUdpate */
  10900. /*
  10901. * Unit test function for wc_RipeMdFinal()
  10902. */
  10903. static int test_wc_RipeMdFinal(void)
  10904. {
  10905. int flag = 0;
  10906. #ifdef WOLFSSL_RIPEMD
  10907. RipeMd ripemd;
  10908. byte* hash_test[3];
  10909. byte hash1[RIPEMD_DIGEST_SIZE];
  10910. byte hash2[2*RIPEMD_DIGEST_SIZE];
  10911. byte hash3[5*RIPEMD_DIGEST_SIZE];
  10912. int times, i, ret;
  10913. /* Initialize */
  10914. ret = wc_InitRipeMd(&ripemd);
  10915. if (ret != 0) {
  10916. flag = ret;
  10917. }
  10918. if (!flag) {
  10919. hash_test[0] = hash1;
  10920. hash_test[1] = hash2;
  10921. hash_test[2] = hash3;
  10922. }
  10923. times = sizeof(hash_test) / sizeof(byte*);
  10924. /* Good test args. */
  10925. printf(testingFmt, "wc_RipeMdFinal()");
  10926. /* Testing oversized buffers. */
  10927. for (i = 0; i < times; i++) {
  10928. if (!flag) {
  10929. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  10930. if (ret != 0) {
  10931. flag = WOLFSSL_FATAL_ERROR;
  10932. }
  10933. }
  10934. }
  10935. /* Test bad args. */
  10936. if (!flag) {
  10937. ret = wc_RipeMdFinal(NULL, NULL);
  10938. if (ret != BAD_FUNC_ARG) {
  10939. flag = WOLFSSL_FATAL_ERROR;
  10940. }
  10941. }
  10942. if (!flag) {
  10943. ret = wc_RipeMdFinal(NULL, hash1);
  10944. if (ret != BAD_FUNC_ARG) {
  10945. flag = WOLFSSL_FATAL_ERROR;
  10946. }
  10947. }
  10948. if (!flag) {
  10949. ret = wc_RipeMdFinal(&ripemd, NULL);
  10950. if (ret != BAD_FUNC_ARG) {
  10951. flag = WOLFSSL_FATAL_ERROR;
  10952. }
  10953. }
  10954. printf(resultFmt, flag == 0 ? passed : failed);
  10955. #endif
  10956. return flag;
  10957. } /* END test_wc_RipeMdFinal */
  10958. /*
  10959. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  10960. * wc_InitSha3_512
  10961. */
  10962. static int test_wc_InitSha3(void)
  10963. {
  10964. int ret = 0;
  10965. #if defined(WOLFSSL_SHA3)
  10966. wc_Sha3 sha3;
  10967. (void)sha3;
  10968. #if !defined(WOLFSSL_NOSHA3_224)
  10969. printf(testingFmt, "wc_InitSha3_224()");
  10970. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  10971. /* Test bad args. */
  10972. if (ret == 0) {
  10973. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  10974. if (ret == BAD_FUNC_ARG) {
  10975. ret = 0;
  10976. } else if (ret == 0) {
  10977. ret = WOLFSSL_FATAL_ERROR;
  10978. }
  10979. }
  10980. wc_Sha3_224_Free(&sha3);
  10981. printf(resultFmt, ret == 0 ? passed : failed);
  10982. #endif /* NOSHA3_224 */
  10983. #if !defined(WOLFSSL_NOSHA3_256)
  10984. if (ret == 0) {
  10985. printf(testingFmt, "wc_InitSha3_256()");
  10986. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  10987. /* Test bad args. */
  10988. if (ret == 0) {
  10989. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  10990. if (ret == BAD_FUNC_ARG) {
  10991. ret = 0;
  10992. } else if (ret == 0) {
  10993. ret = WOLFSSL_FATAL_ERROR;
  10994. }
  10995. }
  10996. wc_Sha3_256_Free(&sha3);
  10997. printf(resultFmt, ret == 0 ? passed : failed);
  10998. } /* END sha3_256 */
  10999. #endif /* NOSHA3_256 */
  11000. #if !defined(WOLFSSL_NOSHA3_384)
  11001. if (ret == 0) {
  11002. printf(testingFmt, "wc_InitSha3_384()");
  11003. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11004. /* Test bad args. */
  11005. if (ret == 0) {
  11006. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  11007. if (ret == BAD_FUNC_ARG) {
  11008. ret = 0;
  11009. } else if (ret == 0) {
  11010. ret = WOLFSSL_FATAL_ERROR;
  11011. }
  11012. }
  11013. wc_Sha3_384_Free(&sha3);
  11014. printf(resultFmt, ret == 0 ? passed : failed);
  11015. } /* END sha3_384 */
  11016. #endif /* NOSHA3_384 */
  11017. #if !defined(WOLFSSL_NOSHA3_512)
  11018. if (ret == 0) {
  11019. printf(testingFmt, "wc_InitSha3_512()");
  11020. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11021. /* Test bad args. */
  11022. if (ret == 0) {
  11023. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  11024. if (ret == BAD_FUNC_ARG) {
  11025. ret = 0;
  11026. } else if (ret == 0) {
  11027. ret = WOLFSSL_FATAL_ERROR;
  11028. }
  11029. }
  11030. wc_Sha3_512_Free(&sha3);
  11031. printf(resultFmt, ret == 0 ? passed : failed);
  11032. } /* END sha3_512 */
  11033. #endif /* NOSHA3_512 */
  11034. #endif
  11035. return ret;
  11036. } /* END test_wc_InitSha3 */
  11037. /*
  11038. * Testing wc_Sha3_Update()
  11039. */
  11040. static int testing_wc_Sha3_Update(void)
  11041. {
  11042. int ret = 0;
  11043. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  11044. !defined(WOLFSSL_AFALG_XILINX)
  11045. wc_Sha3 sha3;
  11046. byte msg[] = "Everybody's working for the weekend.";
  11047. byte msg2[] = "Everybody gets Friday off.";
  11048. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11049. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11050. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11051. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11052. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11053. word32 msglen = sizeof(msg) - 1;
  11054. word32 msg2len = sizeof(msg2);
  11055. word32 msgCmplen = sizeof(msgCmp);
  11056. #if !defined(WOLFSSL_NOSHA3_224)
  11057. printf(testingFmt, "wc_Sha3_224_Update()");
  11058. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11059. if (ret != 0) {
  11060. return ret;
  11061. }
  11062. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  11063. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11064. ret = WOLFSSL_FATAL_ERROR;
  11065. }
  11066. if (ret == 0) {
  11067. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11068. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11069. ret = WOLFSSL_FATAL_ERROR;
  11070. }
  11071. }
  11072. /* Pass bad args. */
  11073. if (ret == 0) {
  11074. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  11075. if (ret == BAD_FUNC_ARG) {
  11076. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  11077. }
  11078. if (ret == BAD_FUNC_ARG) {
  11079. wc_Sha3_224_Free(&sha3);
  11080. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  11081. return ret;
  11082. }
  11083. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  11084. if (ret == 0) {
  11085. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11086. }
  11087. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11088. ret = WOLFSSL_FATAL_ERROR;
  11089. }
  11090. }
  11091. }
  11092. wc_Sha3_224_Free(&sha3);
  11093. printf(resultFmt, ret == 0 ? passed : failed);
  11094. #endif /* SHA3_224 */
  11095. #if !defined(WOLFSSL_NOSHA3_256)
  11096. if (ret == 0) {
  11097. printf(testingFmt, "wc_Sha3_256_Update()");
  11098. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11099. if (ret != 0) {
  11100. return ret;
  11101. }
  11102. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  11103. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11104. ret = WOLFSSL_FATAL_ERROR;
  11105. }
  11106. if (ret == 0) {
  11107. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11108. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11109. ret = WOLFSSL_FATAL_ERROR;
  11110. }
  11111. }
  11112. /* Pass bad args. */
  11113. if (ret == 0) {
  11114. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  11115. if (ret == BAD_FUNC_ARG) {
  11116. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  11117. }
  11118. if (ret == BAD_FUNC_ARG) {
  11119. wc_Sha3_256_Free(&sha3);
  11120. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  11121. return ret;
  11122. }
  11123. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  11124. if (ret == 0) {
  11125. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11126. }
  11127. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11128. ret = WOLFSSL_FATAL_ERROR;
  11129. }
  11130. }
  11131. }
  11132. wc_Sha3_256_Free(&sha3);
  11133. printf(resultFmt, ret == 0 ? passed : failed);
  11134. }
  11135. #endif /* SHA3_256 */
  11136. #if !defined(WOLFSSL_NOSHA3_384)
  11137. if (ret == 0) {
  11138. printf(testingFmt, "wc_Sha3_384_Update()");
  11139. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11140. if (ret != 0) {
  11141. return ret;
  11142. }
  11143. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  11144. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11145. ret = WOLFSSL_FATAL_ERROR;
  11146. }
  11147. if (ret == 0) {
  11148. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11149. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11150. ret = WOLFSSL_FATAL_ERROR;
  11151. }
  11152. }
  11153. /* Pass bad args. */
  11154. if (ret == 0) {
  11155. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  11156. if (ret == BAD_FUNC_ARG) {
  11157. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  11158. }
  11159. if (ret == BAD_FUNC_ARG) {
  11160. wc_Sha3_384_Free(&sha3);
  11161. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  11162. return ret;
  11163. }
  11164. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  11165. if (ret == 0) {
  11166. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11167. }
  11168. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11169. ret = WOLFSSL_FATAL_ERROR;
  11170. }
  11171. }
  11172. }
  11173. wc_Sha3_384_Free(&sha3);
  11174. printf(resultFmt, ret == 0 ? passed : failed);
  11175. }
  11176. #endif /* SHA3_384 */
  11177. #if !defined(WOLFSSL_NOSHA3_512)
  11178. if (ret == 0) {
  11179. printf(testingFmt, "wc_Sha3_512_Update()");
  11180. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11181. if (ret != 0) {
  11182. return ret;
  11183. }
  11184. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  11185. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11186. ret = WOLFSSL_FATAL_ERROR;
  11187. }
  11188. if (ret == 0) {
  11189. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11190. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11191. ret = WOLFSSL_FATAL_ERROR;
  11192. }
  11193. }
  11194. /* Pass bad args. */
  11195. if (ret == 0) {
  11196. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  11197. if (ret == BAD_FUNC_ARG) {
  11198. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  11199. }
  11200. if (ret == BAD_FUNC_ARG) {
  11201. wc_Sha3_512_Free(&sha3);
  11202. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  11203. return ret;
  11204. }
  11205. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  11206. if (ret == 0) {
  11207. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11208. }
  11209. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11210. ret = WOLFSSL_FATAL_ERROR;
  11211. }
  11212. }
  11213. }
  11214. wc_Sha3_512_Free(&sha3);
  11215. printf(resultFmt, ret == 0 ? passed : failed);
  11216. }
  11217. #endif /* SHA3_512 */
  11218. #endif /* WOLFSSL_SHA3 */
  11219. return ret;
  11220. } /* END testing_wc_Sha3_Update */
  11221. /*
  11222. * Testing wc_Sha3_224_Final()
  11223. */
  11224. static int test_wc_Sha3_224_Final(void)
  11225. {
  11226. int ret = 0;
  11227. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11228. wc_Sha3 sha3;
  11229. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11230. "nopnopq";
  11231. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  11232. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  11233. "\x64\xea\xd0\xfc\xce\x33";
  11234. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11235. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  11236. /* Init stack variables. */
  11237. XMEMSET(hash, 0, sizeof(hash));
  11238. printf(testingFmt, "wc_Sha3_224_Final()");
  11239. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11240. if (ret != 0) {
  11241. return ret;
  11242. }
  11243. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11244. if (ret == 0) {
  11245. ret = wc_Sha3_224_Final(&sha3, hash);
  11246. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11247. ret = WOLFSSL_FATAL_ERROR;
  11248. }
  11249. }
  11250. /* Test bad args. */
  11251. if (ret == 0) {
  11252. ret = wc_Sha3_224_Final(NULL, hash);
  11253. if (ret == 0) {
  11254. ret = wc_Sha3_224_Final(&sha3, NULL);
  11255. }
  11256. if (ret == BAD_FUNC_ARG) {
  11257. ret = 0;
  11258. } else if (ret == 0) {
  11259. ret = WOLFSSL_FATAL_ERROR;
  11260. }
  11261. }
  11262. wc_Sha3_224_Free(&sha3);
  11263. printf(resultFmt, ret == 0 ? passed : failed);
  11264. if (ret == 0) {
  11265. printf(testingFmt, "wc_Sha3_224_GetHash()");
  11266. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11267. if (ret != 0) {
  11268. return ret;
  11269. }
  11270. /* Init stack variables. */
  11271. XMEMSET(hash, 0, sizeof(hash));
  11272. XMEMSET(hashRet, 0, sizeof(hashRet));
  11273. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11274. if (ret == 0) {
  11275. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  11276. }
  11277. if (ret == 0) {
  11278. ret = wc_Sha3_224_Final(&sha3, hash);
  11279. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11280. ret = WOLFSSL_FATAL_ERROR;
  11281. }
  11282. }
  11283. if (ret == 0) {
  11284. /* Test bad args. */
  11285. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  11286. if (ret == BAD_FUNC_ARG) {
  11287. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  11288. }
  11289. if (ret == BAD_FUNC_ARG) {
  11290. ret = 0;
  11291. } else if (ret == 0) {
  11292. ret = WOLFSSL_FATAL_ERROR;
  11293. }
  11294. }
  11295. printf(resultFmt, ret == 0 ? passed : failed);
  11296. }
  11297. wc_Sha3_224_Free(&sha3);
  11298. #endif
  11299. return ret;
  11300. } /* END test_wc_Sha3_224_Final */
  11301. /*
  11302. * Testing wc_Sha3_256_Final()
  11303. */
  11304. static int test_wc_Sha3_256_Final(void)
  11305. {
  11306. int ret = 0;
  11307. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11308. wc_Sha3 sha3;
  11309. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11310. "nopnopq";
  11311. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  11312. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  11313. "\xdd\x97\x49\x6d\x33\x76";
  11314. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11315. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  11316. /* Init stack variables. */
  11317. XMEMSET(hash, 0, sizeof(hash));
  11318. printf(testingFmt, "wc_Sha3_256_Final()");
  11319. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11320. if (ret != 0) {
  11321. return ret;
  11322. }
  11323. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11324. if (ret == 0) {
  11325. ret = wc_Sha3_256_Final(&sha3, hash);
  11326. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11327. ret = WOLFSSL_FATAL_ERROR;
  11328. }
  11329. }
  11330. /* Test bad args. */
  11331. if (ret == 0) {
  11332. ret = wc_Sha3_256_Final(NULL, hash);
  11333. if (ret == 0) {
  11334. ret = wc_Sha3_256_Final(&sha3, NULL);
  11335. }
  11336. if (ret == BAD_FUNC_ARG) {
  11337. ret = 0;
  11338. } else if (ret == 0) {
  11339. ret = WOLFSSL_FATAL_ERROR;
  11340. }
  11341. }
  11342. wc_Sha3_256_Free(&sha3);
  11343. printf(resultFmt, ret == 0 ? passed : failed);
  11344. if (ret == 0) {
  11345. printf(testingFmt, "wc_Sha3_256_GetHash()");
  11346. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11347. if (ret != 0) {
  11348. return ret;
  11349. }
  11350. /* Init stack variables. */
  11351. XMEMSET(hash, 0, sizeof(hash));
  11352. XMEMSET(hashRet, 0, sizeof(hashRet));
  11353. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11354. if (ret == 0) {
  11355. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  11356. }
  11357. if (ret == 0) {
  11358. ret = wc_Sha3_256_Final(&sha3, hash);
  11359. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11360. ret = WOLFSSL_FATAL_ERROR;
  11361. }
  11362. }
  11363. if (ret == 0) {
  11364. /* Test bad args. */
  11365. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  11366. if (ret == BAD_FUNC_ARG) {
  11367. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  11368. }
  11369. if (ret == BAD_FUNC_ARG) {
  11370. ret = 0;
  11371. } else if (ret == 0) {
  11372. ret = WOLFSSL_FATAL_ERROR;
  11373. }
  11374. }
  11375. printf(resultFmt, ret == 0 ? passed : failed);
  11376. }
  11377. wc_Sha3_256_Free(&sha3);
  11378. #endif
  11379. return ret;
  11380. } /* END test_wc_Sha3_256_Final */
  11381. /*
  11382. * Testing wc_Sha3_384_Final()
  11383. */
  11384. static int test_wc_Sha3_384_Final(void)
  11385. {
  11386. int ret = 0;
  11387. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11388. wc_Sha3 sha3;
  11389. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11390. "nopnopq";
  11391. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  11392. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  11393. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  11394. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  11395. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11396. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  11397. /* Init stack variables. */
  11398. XMEMSET(hash, 0, sizeof(hash));
  11399. printf(testingFmt, "wc_Sha3_384_Final()");
  11400. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11401. if (ret != 0) {
  11402. return ret;
  11403. }
  11404. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11405. if (ret == 0) {
  11406. ret = wc_Sha3_384_Final(&sha3, hash);
  11407. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11408. ret = WOLFSSL_FATAL_ERROR;
  11409. }
  11410. }
  11411. /* Test bad args. */
  11412. if (ret == 0) {
  11413. ret = wc_Sha3_384_Final(NULL, hash);
  11414. if (ret == 0) {
  11415. ret = wc_Sha3_384_Final(&sha3, NULL);
  11416. }
  11417. if (ret == BAD_FUNC_ARG) {
  11418. ret = 0;
  11419. } else if (ret == 0) {
  11420. ret = WOLFSSL_FATAL_ERROR;
  11421. }
  11422. }
  11423. wc_Sha3_384_Free(&sha3);
  11424. printf(resultFmt, ret == 0 ? passed : failed);
  11425. if (ret == 0) {
  11426. printf(testingFmt, "wc_Sha3_384_GetHash()");
  11427. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11428. if (ret != 0) {
  11429. return ret;
  11430. }
  11431. /* Init stack variables. */
  11432. XMEMSET(hash, 0, sizeof(hash));
  11433. XMEMSET(hashRet, 0, sizeof(hashRet));
  11434. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11435. if (ret == 0) {
  11436. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  11437. }
  11438. if (ret == 0) {
  11439. ret = wc_Sha3_384_Final(&sha3, hash);
  11440. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11441. ret = WOLFSSL_FATAL_ERROR;
  11442. }
  11443. }
  11444. if (ret == 0) {
  11445. /* Test bad args. */
  11446. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  11447. if (ret == BAD_FUNC_ARG) {
  11448. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  11449. }
  11450. if (ret == BAD_FUNC_ARG) {
  11451. ret = 0;
  11452. } else if (ret == 0) {
  11453. ret = WOLFSSL_FATAL_ERROR;
  11454. }
  11455. }
  11456. printf(resultFmt, ret == 0 ? passed : failed);
  11457. }
  11458. wc_Sha3_384_Free(&sha3);
  11459. #endif
  11460. return ret;
  11461. } /* END test_wc_Sha3_384_Final */
  11462. /*
  11463. * Testing wc_Sha3_512_Final()
  11464. */
  11465. static int test_wc_Sha3_512_Final(void)
  11466. {
  11467. int ret = 0;
  11468. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  11469. !defined(WOLFSSL_NOSHA3_384)
  11470. wc_Sha3 sha3;
  11471. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11472. "nopnopq";
  11473. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  11474. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  11475. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  11476. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  11477. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  11478. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11479. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  11480. /* Init stack variables. */
  11481. XMEMSET(hash, 0, sizeof(hash));
  11482. printf(testingFmt, "wc_Sha3_512_Final()");
  11483. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11484. if (ret != 0) {
  11485. return ret;
  11486. }
  11487. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11488. if (ret == 0) {
  11489. ret = wc_Sha3_512_Final(&sha3, hash);
  11490. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11491. ret = WOLFSSL_FATAL_ERROR;
  11492. }
  11493. }
  11494. /* Test bad args. */
  11495. if (ret == 0) {
  11496. ret = wc_Sha3_512_Final(NULL, hash);
  11497. if (ret == 0) {
  11498. ret = wc_Sha3_384_Final(&sha3, NULL);
  11499. }
  11500. if (ret == BAD_FUNC_ARG) {
  11501. ret = 0;
  11502. } else if (ret == 0) {
  11503. ret = WOLFSSL_FATAL_ERROR;
  11504. }
  11505. }
  11506. wc_Sha3_512_Free(&sha3);
  11507. printf(resultFmt, ret == 0 ? passed : failed);
  11508. if (ret == 0) {
  11509. printf(testingFmt, "wc_Sha3_512_GetHash()");
  11510. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11511. if (ret != 0) {
  11512. return ret;
  11513. }
  11514. /* Init stack variables. */
  11515. XMEMSET(hash, 0, sizeof(hash));
  11516. XMEMSET(hashRet, 0, sizeof(hashRet));
  11517. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11518. if (ret == 0) {
  11519. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  11520. }
  11521. if (ret == 0) {
  11522. ret = wc_Sha3_512_Final(&sha3, hash);
  11523. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11524. ret = WOLFSSL_FATAL_ERROR;
  11525. }
  11526. }
  11527. if (ret == 0) {
  11528. /* Test bad args. */
  11529. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  11530. if (ret == BAD_FUNC_ARG) {
  11531. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  11532. }
  11533. if (ret == BAD_FUNC_ARG) {
  11534. ret = 0;
  11535. } else if (ret == 0) {
  11536. ret = WOLFSSL_FATAL_ERROR;
  11537. }
  11538. }
  11539. printf(resultFmt, ret == 0 ? passed : failed);
  11540. }
  11541. wc_Sha3_512_Free(&sha3);
  11542. #endif
  11543. return ret;
  11544. } /* END test_wc_Sha3_512_Final */
  11545. /*
  11546. * Testing wc_Sha3_224_Copy()
  11547. */
  11548. static int test_wc_Sha3_224_Copy(void)
  11549. {
  11550. int ret = 0;
  11551. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11552. wc_Sha3 sha3, sha3Cpy;
  11553. const char* msg = TEST_STRING;
  11554. word32 msglen = (word32)TEST_STRING_SZ;
  11555. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11556. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  11557. XMEMSET(hash, 0, sizeof(hash));
  11558. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11559. printf(testingFmt, "wc_Sha3_224_Copy()");
  11560. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11561. if (ret != 0) {
  11562. return ret;
  11563. }
  11564. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  11565. if (ret != 0) {
  11566. wc_Sha3_224_Free(&sha3);
  11567. return ret;
  11568. }
  11569. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  11570. if (ret == 0) {
  11571. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  11572. if (ret == 0) {
  11573. ret = wc_Sha3_224_Final(&sha3, hash);
  11574. if (ret == 0) {
  11575. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  11576. }
  11577. }
  11578. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11579. ret = WOLFSSL_FATAL_ERROR;
  11580. }
  11581. }
  11582. /* Test bad args. */
  11583. if (ret == 0) {
  11584. ret = wc_Sha3_224_Copy(NULL, &sha3);
  11585. if (ret == BAD_FUNC_ARG) {
  11586. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  11587. }
  11588. if (ret == BAD_FUNC_ARG) {
  11589. ret = 0;
  11590. } else if (ret == 0) {
  11591. ret = WOLFSSL_FATAL_ERROR;
  11592. }
  11593. }
  11594. printf(resultFmt, ret == 0 ? passed : failed);
  11595. #endif
  11596. return ret;
  11597. } /* END test_wc_Sha3_224_Copy */
  11598. /*
  11599. * Testing wc_Sha3_256_Copy()
  11600. */
  11601. static int test_wc_Sha3_256_Copy(void)
  11602. {
  11603. int ret = 0;
  11604. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11605. wc_Sha3 sha3, sha3Cpy;
  11606. const char* msg = TEST_STRING;
  11607. word32 msglen = (word32)TEST_STRING_SZ;
  11608. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11609. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  11610. XMEMSET(hash, 0, sizeof(hash));
  11611. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11612. printf(testingFmt, "wc_Sha3_256_Copy()");
  11613. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11614. if (ret != 0) {
  11615. return ret;
  11616. }
  11617. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  11618. if (ret != 0) {
  11619. wc_Sha3_256_Free(&sha3);
  11620. return ret;
  11621. }
  11622. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  11623. if (ret == 0) {
  11624. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  11625. if (ret == 0) {
  11626. ret = wc_Sha3_256_Final(&sha3, hash);
  11627. if (ret == 0) {
  11628. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  11629. }
  11630. }
  11631. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11632. ret = WOLFSSL_FATAL_ERROR;
  11633. }
  11634. }
  11635. /* Test bad args. */
  11636. if (ret == 0) {
  11637. ret = wc_Sha3_256_Copy(NULL, &sha3);
  11638. if (ret == BAD_FUNC_ARG) {
  11639. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  11640. }
  11641. if (ret == BAD_FUNC_ARG) {
  11642. ret = 0;
  11643. } else if (ret == 0) {
  11644. ret = WOLFSSL_FATAL_ERROR;
  11645. }
  11646. }
  11647. printf(resultFmt, ret == 0 ? passed : failed);
  11648. #endif
  11649. return ret;
  11650. } /* END test_wc_Sha3_256_Copy */
  11651. /*
  11652. * Testing wc_Sha3_384_Copy()
  11653. */
  11654. static int test_wc_Sha3_384_Copy(void)
  11655. {
  11656. int ret = 0;
  11657. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11658. wc_Sha3 sha3, sha3Cpy;
  11659. const char* msg = TEST_STRING;
  11660. word32 msglen = (word32)TEST_STRING_SZ;
  11661. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11662. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  11663. XMEMSET(hash, 0, sizeof(hash));
  11664. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11665. printf(testingFmt, "wc_Sha3_384_Copy()");
  11666. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11667. if (ret != 0) {
  11668. return ret;
  11669. }
  11670. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  11671. if (ret != 0) {
  11672. wc_Sha3_384_Free(&sha3);
  11673. return ret;
  11674. }
  11675. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  11676. if (ret == 0) {
  11677. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  11678. if (ret == 0) {
  11679. ret = wc_Sha3_384_Final(&sha3, hash);
  11680. if (ret == 0) {
  11681. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  11682. }
  11683. }
  11684. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11685. ret = WOLFSSL_FATAL_ERROR;
  11686. }
  11687. }
  11688. /* Test bad args. */
  11689. if (ret == 0) {
  11690. ret = wc_Sha3_384_Copy(NULL, &sha3);
  11691. if (ret == BAD_FUNC_ARG) {
  11692. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  11693. }
  11694. if (ret == BAD_FUNC_ARG) {
  11695. ret = 0;
  11696. } else if (ret == 0) {
  11697. ret = WOLFSSL_FATAL_ERROR;
  11698. }
  11699. }
  11700. printf(resultFmt, ret == 0 ? passed : failed);
  11701. #endif
  11702. return ret;
  11703. } /* END test_wc_Sha3_384_Copy */
  11704. /*
  11705. * Testing wc_Sha3_512_Copy()
  11706. */
  11707. static int test_wc_Sha3_512_Copy(void)
  11708. {
  11709. int ret = 0;
  11710. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  11711. wc_Sha3 sha3, sha3Cpy;
  11712. const char* msg = TEST_STRING;
  11713. word32 msglen = (word32)TEST_STRING_SZ;
  11714. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11715. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  11716. XMEMSET(hash, 0, sizeof(hash));
  11717. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11718. printf(testingFmt, "wc_Sha3_512_Copy()");
  11719. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11720. if (ret != 0) {
  11721. return ret;
  11722. }
  11723. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  11724. if (ret != 0) {
  11725. wc_Sha3_512_Free(&sha3);
  11726. return ret;
  11727. }
  11728. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  11729. if (ret == 0) {
  11730. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  11731. if (ret == 0) {
  11732. ret = wc_Sha3_512_Final(&sha3, hash);
  11733. if (ret == 0) {
  11734. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  11735. }
  11736. }
  11737. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11738. ret = WOLFSSL_FATAL_ERROR;
  11739. }
  11740. }
  11741. /* Test bad args. */
  11742. if (ret == 0) {
  11743. ret = wc_Sha3_512_Copy(NULL, &sha3);
  11744. if (ret == BAD_FUNC_ARG) {
  11745. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  11746. }
  11747. if (ret == BAD_FUNC_ARG) {
  11748. ret = 0;
  11749. } else if (ret == 0) {
  11750. ret = WOLFSSL_FATAL_ERROR;
  11751. }
  11752. }
  11753. printf(resultFmt, ret == 0 ? passed : failed);
  11754. #endif
  11755. return ret;
  11756. } /* END test_wc_Sha3_512_Copy */
  11757. /*
  11758. * Unit test function for wc_Sha3_GetFlags()
  11759. */
  11760. static int test_wc_Sha3_GetFlags(void)
  11761. {
  11762. int ret = 0;
  11763. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  11764. wc_Sha3 sha3;
  11765. word32 flags = 0;
  11766. printf(testingFmt, "wc_Sha3_GetFlags()");
  11767. /* Initialize */
  11768. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11769. if (ret != 0) {
  11770. return ret;
  11771. }
  11772. if (ret == 0) {
  11773. ret = wc_Sha3_GetFlags(&sha3, &flags);
  11774. }
  11775. if (ret == 0) {
  11776. if (flags & WC_HASH_FLAG_ISCOPY) {
  11777. ret = 0;
  11778. }
  11779. }
  11780. wc_Sha3_224_Free(&sha3);
  11781. printf(resultFmt, ret == 0 ? passed : failed);
  11782. #endif
  11783. return ret;
  11784. } /* END test_wc_Sha3_GetFlags */
  11785. static int test_wc_InitShake256(void)
  11786. {
  11787. int ret = 0;
  11788. #ifdef WOLFSSL_SHAKE256
  11789. wc_Shake shake;
  11790. printf(testingFmt, "wc_InitShake256()");
  11791. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  11792. /* Test bad args. */
  11793. if (ret == 0) {
  11794. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  11795. if (ret == BAD_FUNC_ARG) {
  11796. ret = 0;
  11797. } else if (ret == 0) {
  11798. ret = WOLFSSL_FATAL_ERROR;
  11799. }
  11800. }
  11801. wc_Shake256_Free(&shake);
  11802. printf(resultFmt, ret == 0 ? passed : failed);
  11803. #endif
  11804. return ret;
  11805. } /* END test_wc_InitSha3 */
  11806. static int testing_wc_Shake256_Update(void)
  11807. {
  11808. int ret = 0;
  11809. #ifdef WOLFSSL_SHAKE256
  11810. wc_Shake shake;
  11811. byte msg[] = "Everybody's working for the weekend.";
  11812. byte msg2[] = "Everybody gets Friday off.";
  11813. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11814. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11815. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11816. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11817. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11818. word32 msglen = sizeof(msg) - 1;
  11819. word32 msg2len = sizeof(msg2);
  11820. word32 msgCmplen = sizeof(msgCmp);
  11821. printf(testingFmt, "wc_Shake256_Update()");
  11822. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  11823. if (ret != 0) {
  11824. return ret;
  11825. }
  11826. ret = wc_Shake256_Update(&shake, msg, msglen);
  11827. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  11828. ret = WOLFSSL_FATAL_ERROR;
  11829. }
  11830. if (ret == 0) {
  11831. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11832. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  11833. ret = WOLFSSL_FATAL_ERROR;
  11834. }
  11835. }
  11836. /* Pass bad args. */
  11837. if (ret == 0) {
  11838. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  11839. if (ret == BAD_FUNC_ARG) {
  11840. ret = wc_Shake256_Update(&shake, NULL, 5);
  11841. }
  11842. if (ret == BAD_FUNC_ARG) {
  11843. wc_Shake256_Free(&shake);
  11844. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  11845. return ret;
  11846. }
  11847. ret = wc_Shake256_Update(&shake, NULL, 0);
  11848. if (ret == 0) {
  11849. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  11850. }
  11851. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  11852. ret = WOLFSSL_FATAL_ERROR;
  11853. }
  11854. }
  11855. }
  11856. wc_Shake256_Free(&shake);
  11857. printf(resultFmt, ret == 0 ? passed : failed);
  11858. #endif /* WOLFSSL_SHAKE256 */
  11859. return ret;
  11860. }
  11861. static int test_wc_Shake256_Final(void)
  11862. {
  11863. int ret = 0;
  11864. #ifdef WOLFSSL_SHAKE256
  11865. wc_Shake shake;
  11866. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11867. "nopnopq";
  11868. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  11869. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  11870. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  11871. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  11872. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  11873. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  11874. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  11875. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  11876. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  11877. byte hash[114];
  11878. /* Init stack variables. */
  11879. XMEMSET(hash, 0, sizeof(hash));
  11880. printf(testingFmt, "wc_Shake256_Final()");
  11881. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  11882. if (ret != 0) {
  11883. return ret;
  11884. }
  11885. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  11886. if (ret == 0) {
  11887. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  11888. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  11889. ret = WOLFSSL_FATAL_ERROR;
  11890. }
  11891. }
  11892. /* Test bad args. */
  11893. if (ret == 0) {
  11894. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  11895. if (ret == 0) {
  11896. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  11897. }
  11898. if (ret == BAD_FUNC_ARG) {
  11899. ret = 0;
  11900. } else if (ret == 0) {
  11901. ret = WOLFSSL_FATAL_ERROR;
  11902. }
  11903. }
  11904. wc_Shake256_Free(&shake);
  11905. printf(resultFmt, ret == 0 ? passed : failed);
  11906. #endif
  11907. return ret;
  11908. }
  11909. /*
  11910. * Testing wc_Shake256_Copy()
  11911. */
  11912. static int test_wc_Shake256_Copy(void)
  11913. {
  11914. int ret = 0;
  11915. #ifdef WOLFSSL_SHAKE256
  11916. wc_Shake shake, shakeCpy;
  11917. const char* msg = TEST_STRING;
  11918. word32 msglen = (word32)TEST_STRING_SZ;
  11919. byte hash[144];
  11920. byte hashCpy[144];
  11921. word32 hashLen = sizeof(hash);
  11922. word32 hashLenCpy = sizeof(hashCpy);
  11923. XMEMSET(hash, 0, sizeof(hash));
  11924. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11925. printf(testingFmt, "wc_Shake256_Copy()");
  11926. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  11927. if (ret != 0) {
  11928. return ret;
  11929. }
  11930. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  11931. if (ret != 0) {
  11932. wc_Shake256_Free(&shake);
  11933. return ret;
  11934. }
  11935. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  11936. if (ret == 0) {
  11937. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  11938. if (ret == 0) {
  11939. ret = wc_Shake256_Final(&shake, hash, hashLen);
  11940. if (ret == 0) {
  11941. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  11942. }
  11943. }
  11944. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  11945. ret = WOLFSSL_FATAL_ERROR;
  11946. }
  11947. }
  11948. /* Test bad args. */
  11949. if (ret == 0) {
  11950. ret = wc_Shake256_Copy(NULL, &shake);
  11951. if (ret == BAD_FUNC_ARG) {
  11952. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  11953. }
  11954. if (ret == BAD_FUNC_ARG) {
  11955. ret = 0;
  11956. } else if (ret == 0) {
  11957. ret = WOLFSSL_FATAL_ERROR;
  11958. }
  11959. }
  11960. wc_Shake256_Free(&shake);
  11961. printf(resultFmt, ret == 0 ? passed : failed);
  11962. #endif
  11963. return ret;
  11964. } /* END test_wc_Shake256_Copy */
  11965. /*
  11966. * Unit test function for wc_Shake256Hash()
  11967. */
  11968. static int test_wc_Shake256Hash(void)
  11969. {
  11970. int ret = 0;
  11971. #ifdef WOLFSSL_SHAKE256
  11972. const byte data[] = { /* Hello World */
  11973. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  11974. 0x72,0x6c,0x64
  11975. };
  11976. word32 len = sizeof(data);
  11977. byte hash[144];
  11978. word32 hashLen = sizeof(hash);
  11979. printf(testingFmt, "wc_Shake256Hash()");
  11980. ret = wc_Shake256Hash(data, len, hash, hashLen);
  11981. printf(resultFmt, ret == 0 ? passed : failed);
  11982. #endif
  11983. return ret;
  11984. } /* END test_wc_Shake256Hash */
  11985. /*
  11986. * Test function for wc_HmacSetKey
  11987. */
  11988. static int test_wc_Md5HmacSetKey(void)
  11989. {
  11990. int flag = 0;
  11991. #if !defined(NO_HMAC) && !defined(NO_MD5)
  11992. Hmac hmac;
  11993. int ret, times, itr;
  11994. const char* keys[]=
  11995. {
  11996. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  11997. #ifndef HAVE_FIPS
  11998. "Jefe", /* smaller than minimum FIPS key size */
  11999. #endif
  12000. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12001. };
  12002. times = sizeof(keys) / sizeof(char*);
  12003. flag = 0;
  12004. printf(testingFmt, "wc_HmacSetKey() with MD5");
  12005. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12006. if (ret != 0)
  12007. return ret;
  12008. for (itr = 0; itr < times; itr++) {
  12009. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12010. (word32)XSTRLEN(keys[itr]));
  12011. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12012. wc_HmacFree(&hmac);
  12013. if (ret == BAD_FUNC_ARG)
  12014. return 0;
  12015. else {
  12016. return WOLFSSL_FATAL_ERROR;
  12017. }
  12018. #else
  12019. if (ret != 0) {
  12020. flag = ret;
  12021. }
  12022. #endif
  12023. }
  12024. /* Bad args. */
  12025. if (!flag) {
  12026. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12027. (word32)XSTRLEN(keys[0]));
  12028. if (ret != BAD_FUNC_ARG) {
  12029. flag = WOLFSSL_FATAL_ERROR;
  12030. }
  12031. }
  12032. if (!flag) {
  12033. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  12034. if (ret != BAD_FUNC_ARG) {
  12035. flag = WOLFSSL_FATAL_ERROR;
  12036. }
  12037. }
  12038. if (!flag) {
  12039. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12040. (word32)XSTRLEN(keys[0]));
  12041. if (ret != BAD_FUNC_ARG) {
  12042. flag = WOLFSSL_FATAL_ERROR;
  12043. }
  12044. }
  12045. if (!flag) {
  12046. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12047. #ifdef HAVE_FIPS
  12048. if (ret != HMAC_MIN_KEYLEN_E) {
  12049. flag = WOLFSSL_FATAL_ERROR;
  12050. }
  12051. #else
  12052. if (ret != 0) {
  12053. flag = WOLFSSL_FATAL_ERROR;
  12054. }
  12055. #endif
  12056. }
  12057. wc_HmacFree(&hmac);
  12058. printf(resultFmt, flag == 0 ? passed : failed);
  12059. #endif
  12060. return flag;
  12061. } /* END test_wc_Md5HmacSetKey */
  12062. /*
  12063. * testing wc_HmacSetKey() on wc_Sha hash.
  12064. */
  12065. static int test_wc_ShaHmacSetKey(void)
  12066. {
  12067. int flag = 0;
  12068. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12069. Hmac hmac;
  12070. int ret, times, itr;
  12071. const char* keys[]=
  12072. {
  12073. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12074. "\x0b\x0b\x0b",
  12075. #ifndef HAVE_FIPS
  12076. "Jefe", /* smaller than minimum FIPS key size */
  12077. #endif
  12078. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12079. "\xAA\xAA\xAA"
  12080. };
  12081. times = sizeof(keys) / sizeof(char*);
  12082. flag = 0;
  12083. printf(testingFmt, "wc_HmacSetKey() with SHA");
  12084. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12085. if (ret != 0)
  12086. return ret;
  12087. for (itr = 0; itr < times; itr++) {
  12088. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12089. (word32)XSTRLEN(keys[itr]));
  12090. if (ret != 0) {
  12091. flag = ret;
  12092. }
  12093. }
  12094. /* Bad args. */
  12095. if (!flag) {
  12096. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12097. (word32)XSTRLEN(keys[0]));
  12098. if (ret != BAD_FUNC_ARG) {
  12099. flag = WOLFSSL_FATAL_ERROR;
  12100. }
  12101. }
  12102. if (!flag) {
  12103. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  12104. if (ret != BAD_FUNC_ARG) {
  12105. flag = WOLFSSL_FATAL_ERROR;
  12106. }
  12107. }
  12108. if (!flag) {
  12109. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12110. (word32)XSTRLEN(keys[0]));
  12111. if (ret != BAD_FUNC_ARG) {
  12112. flag = WOLFSSL_FATAL_ERROR;
  12113. }
  12114. }
  12115. if (!flag) {
  12116. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12117. #ifdef HAVE_FIPS
  12118. if (ret != HMAC_MIN_KEYLEN_E) {
  12119. flag = WOLFSSL_FATAL_ERROR;
  12120. }
  12121. #else
  12122. if (ret != 0) {
  12123. flag = WOLFSSL_FATAL_ERROR;
  12124. }
  12125. #endif
  12126. }
  12127. wc_HmacFree(&hmac);
  12128. printf(resultFmt, flag == 0 ? passed : failed);
  12129. #endif
  12130. return flag;
  12131. } /* END test_wc_ShaHmacSetKey() */
  12132. /*
  12133. * testing wc_HmacSetKey() on Sha224 hash.
  12134. */
  12135. static int test_wc_Sha224HmacSetKey(void)
  12136. {
  12137. int flag = 0;
  12138. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12139. Hmac hmac;
  12140. int ret, times, itr;
  12141. const char* keys[]=
  12142. {
  12143. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12144. "\x0b\x0b\x0b",
  12145. #ifndef HAVE_FIPS
  12146. "Jefe", /* smaller than minimum FIPS key size */
  12147. #endif
  12148. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12149. "\xAA\xAA\xAA"
  12150. };
  12151. times = sizeof(keys) / sizeof(char*);
  12152. flag = 0;
  12153. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  12154. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12155. if (ret != 0)
  12156. return ret;
  12157. for (itr = 0; itr < times; itr++) {
  12158. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12159. (word32)XSTRLEN(keys[itr]));
  12160. if (ret != 0) {
  12161. flag = ret;
  12162. }
  12163. }
  12164. /* Bad args. */
  12165. if (!flag) {
  12166. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12167. (word32)XSTRLEN(keys[0]));
  12168. if (ret != BAD_FUNC_ARG) {
  12169. flag = WOLFSSL_FATAL_ERROR;
  12170. }
  12171. }
  12172. if (!flag) {
  12173. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  12174. if (ret != BAD_FUNC_ARG) {
  12175. flag = WOLFSSL_FATAL_ERROR;
  12176. }
  12177. }
  12178. if (!flag) {
  12179. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12180. (word32)XSTRLEN(keys[0]));
  12181. if (ret != BAD_FUNC_ARG) {
  12182. flag = WOLFSSL_FATAL_ERROR;
  12183. }
  12184. }
  12185. if (!flag) {
  12186. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12187. #ifdef HAVE_FIPS
  12188. if (ret != HMAC_MIN_KEYLEN_E) {
  12189. flag = WOLFSSL_FATAL_ERROR;
  12190. }
  12191. #else
  12192. if (ret != 0) {
  12193. flag = WOLFSSL_FATAL_ERROR;
  12194. }
  12195. #endif
  12196. }
  12197. wc_HmacFree(&hmac);
  12198. printf(resultFmt, flag == 0 ? passed : failed);
  12199. #endif
  12200. return flag;
  12201. } /* END test_wc_Sha224HmacSetKey() */
  12202. /*
  12203. * testing wc_HmacSetKey() on Sha256 hash
  12204. */
  12205. static int test_wc_Sha256HmacSetKey(void)
  12206. {
  12207. int flag = 0;
  12208. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12209. Hmac hmac;
  12210. int ret, times, itr;
  12211. const char* keys[]=
  12212. {
  12213. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12214. "\x0b\x0b\x0b",
  12215. #ifndef HAVE_FIPS
  12216. "Jefe", /* smaller than minimum FIPS key size */
  12217. #endif
  12218. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12219. "\xAA\xAA\xAA"
  12220. };
  12221. times = sizeof(keys) / sizeof(char*);
  12222. flag = 0;
  12223. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  12224. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12225. if (ret != 0)
  12226. return ret;
  12227. for (itr = 0; itr < times; itr++) {
  12228. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12229. (word32)XSTRLEN(keys[itr]));
  12230. if (ret != 0) {
  12231. flag = ret;
  12232. }
  12233. }
  12234. /* Bad args. */
  12235. if (!flag) {
  12236. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12237. (word32)XSTRLEN(keys[0]));
  12238. if (ret != BAD_FUNC_ARG) {
  12239. flag = WOLFSSL_FATAL_ERROR;
  12240. }
  12241. }
  12242. if (!flag) {
  12243. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  12244. if (ret != BAD_FUNC_ARG) {
  12245. flag = WOLFSSL_FATAL_ERROR;
  12246. }
  12247. }
  12248. if (!flag) {
  12249. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12250. (word32)XSTRLEN(keys[0]));
  12251. if (ret != BAD_FUNC_ARG) {
  12252. flag = WOLFSSL_FATAL_ERROR;
  12253. }
  12254. }
  12255. if (!flag) {
  12256. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12257. #ifdef HAVE_FIPS
  12258. if (ret != HMAC_MIN_KEYLEN_E) {
  12259. flag = WOLFSSL_FATAL_ERROR;
  12260. }
  12261. #else
  12262. if (ret != 0) {
  12263. flag = WOLFSSL_FATAL_ERROR;
  12264. }
  12265. #endif
  12266. }
  12267. wc_HmacFree(&hmac);
  12268. printf(resultFmt, flag == 0 ? passed : failed);
  12269. #endif
  12270. return flag;
  12271. } /* END test_wc_Sha256HmacSetKey() */
  12272. /*
  12273. * testing wc_HmacSetKey on Sha384 hash.
  12274. */
  12275. static int test_wc_Sha384HmacSetKey(void)
  12276. {
  12277. int flag = 0;
  12278. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12279. Hmac hmac;
  12280. int ret, times, itr;
  12281. const char* keys[]=
  12282. {
  12283. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12284. "\x0b\x0b\x0b",
  12285. #ifndef HAVE_FIPS
  12286. "Jefe", /* smaller than minimum FIPS key size */
  12287. #endif
  12288. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12289. "\xAA\xAA\xAA"
  12290. };
  12291. times = sizeof(keys) / sizeof(char*);
  12292. flag = 0;
  12293. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  12294. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12295. if (ret != 0)
  12296. return ret;
  12297. for (itr = 0; itr < times; itr++) {
  12298. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12299. (word32)XSTRLEN(keys[itr]));
  12300. if (ret != 0) {
  12301. flag = ret;
  12302. }
  12303. }
  12304. /* Bad args. */
  12305. if (!flag) {
  12306. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12307. (word32)XSTRLEN(keys[0]));
  12308. if (ret != BAD_FUNC_ARG) {
  12309. flag = WOLFSSL_FATAL_ERROR;
  12310. }
  12311. }
  12312. if (!flag) {
  12313. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  12314. if (ret != BAD_FUNC_ARG) {
  12315. flag = WOLFSSL_FATAL_ERROR;
  12316. }
  12317. }
  12318. if (!flag) {
  12319. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12320. (word32)XSTRLEN(keys[0]));
  12321. if (ret != BAD_FUNC_ARG) {
  12322. flag = WOLFSSL_FATAL_ERROR;
  12323. }
  12324. }
  12325. if (!flag) {
  12326. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12327. #ifdef HAVE_FIPS
  12328. if (ret != HMAC_MIN_KEYLEN_E) {
  12329. flag = WOLFSSL_FATAL_ERROR;
  12330. }
  12331. #else
  12332. if (ret != 0) {
  12333. flag = WOLFSSL_FATAL_ERROR;
  12334. }
  12335. #endif
  12336. }
  12337. wc_HmacFree(&hmac);
  12338. printf(resultFmt, flag == 0 ? passed : failed);
  12339. #endif
  12340. return flag;
  12341. } /* END test_wc_Sha384HmacSetKey() */
  12342. /*
  12343. * testing wc_HmacUpdate on wc_Md5 hash.
  12344. */
  12345. static int test_wc_Md5HmacUpdate(void)
  12346. {
  12347. int flag = 0;
  12348. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12349. Hmac hmac;
  12350. testVector a, b;
  12351. int ret;
  12352. #ifdef HAVE_FIPS
  12353. const char* keys =
  12354. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12355. #else
  12356. const char* keys = "Jefe";
  12357. #endif
  12358. a.input = "what do ya want for nothing?";
  12359. a.inLen = XSTRLEN(a.input);
  12360. b.input = "Hi There";
  12361. b.inLen = XSTRLEN(b.input);
  12362. flag = 0;
  12363. printf(testingFmt, "wc_HmacUpdate() with MD5");
  12364. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12365. if (ret != 0)
  12366. return ret;
  12367. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  12368. if (ret != 0) {
  12369. flag = ret;
  12370. }
  12371. if (!flag) {
  12372. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12373. if (ret != 0) {
  12374. flag = ret;
  12375. }
  12376. }
  12377. /* Update Hmac. */
  12378. if (!flag) {
  12379. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12380. if (ret != 0) {
  12381. flag = ret;
  12382. }
  12383. }
  12384. /* Test bad args. */
  12385. if (!flag) {
  12386. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12387. if (ret != BAD_FUNC_ARG) {
  12388. flag = WOLFSSL_FATAL_ERROR;
  12389. }
  12390. }
  12391. if (!flag) {
  12392. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12393. if (ret != BAD_FUNC_ARG) {
  12394. flag = WOLFSSL_FATAL_ERROR;
  12395. }
  12396. }
  12397. if (!flag) {
  12398. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12399. if (ret != 0) {
  12400. flag = ret;
  12401. }
  12402. }
  12403. wc_HmacFree(&hmac);
  12404. printf(resultFmt, flag == 0 ? passed : failed);
  12405. #endif
  12406. return flag;
  12407. } /* END test_wc_Md5HmacUpdate */
  12408. /*
  12409. * testing wc_HmacUpdate on SHA hash.
  12410. */
  12411. static int test_wc_ShaHmacUpdate(void)
  12412. {
  12413. int flag = 0;
  12414. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12415. Hmac hmac;
  12416. testVector a, b;
  12417. int ret;
  12418. #ifdef HAVE_FIPS
  12419. const char* keys =
  12420. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12421. #else
  12422. const char* keys = "Jefe";
  12423. #endif
  12424. a.input = "what do ya want for nothing?";
  12425. a.inLen = XSTRLEN(a.input);
  12426. b.input = "Hi There";
  12427. b.inLen = XSTRLEN(b.input);
  12428. flag = 0;
  12429. printf(testingFmt, "wc_HmacUpdate() with SHA");
  12430. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12431. if (ret != 0)
  12432. return ret;
  12433. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  12434. if (ret != 0) {
  12435. flag = ret;
  12436. }
  12437. if (!flag) {
  12438. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12439. if (ret != 0) {
  12440. flag = ret;
  12441. }
  12442. }
  12443. /* Update Hmac. */
  12444. if (!flag) {
  12445. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12446. if (ret != 0) {
  12447. flag = ret;
  12448. }
  12449. }
  12450. /* Test bad args. */
  12451. if (!flag) {
  12452. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12453. if (ret != BAD_FUNC_ARG) {
  12454. flag = WOLFSSL_FATAL_ERROR;
  12455. }
  12456. }
  12457. if (!flag) {
  12458. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12459. if (ret != BAD_FUNC_ARG) {
  12460. flag = WOLFSSL_FATAL_ERROR;
  12461. }
  12462. }
  12463. if (!flag) {
  12464. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12465. if (ret != 0) {
  12466. flag = ret;
  12467. }
  12468. }
  12469. wc_HmacFree(&hmac);
  12470. printf(resultFmt, flag == 0 ? passed : failed);
  12471. #endif
  12472. return flag;
  12473. } /* END test_wc_ShaHmacUpdate */
  12474. /*
  12475. * testing wc_HmacUpdate on SHA224 hash.
  12476. */
  12477. static int test_wc_Sha224HmacUpdate(void)
  12478. {
  12479. int flag = 0;
  12480. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12481. Hmac hmac;
  12482. testVector a, b;
  12483. int ret;
  12484. #ifdef HAVE_FIPS
  12485. const char* keys =
  12486. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12487. #else
  12488. const char* keys = "Jefe";
  12489. #endif
  12490. a.input = "what do ya want for nothing?";
  12491. a.inLen = XSTRLEN(a.input);
  12492. b.input = "Hi There";
  12493. b.inLen = XSTRLEN(b.input);
  12494. flag = 0;
  12495. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  12496. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12497. if (ret != 0)
  12498. return ret;
  12499. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  12500. if (ret != 0) {
  12501. flag = ret;
  12502. }
  12503. if (!flag) {
  12504. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12505. if (ret != 0) {
  12506. flag = ret;
  12507. }
  12508. }
  12509. /* Update Hmac. */
  12510. if (!flag) {
  12511. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12512. if (ret != 0) {
  12513. flag = ret;
  12514. }
  12515. }
  12516. /* Test bad args. */
  12517. if (!flag) {
  12518. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12519. if (ret != BAD_FUNC_ARG) {
  12520. flag = WOLFSSL_FATAL_ERROR;
  12521. }
  12522. }
  12523. if (!flag) {
  12524. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12525. if (ret != BAD_FUNC_ARG) {
  12526. flag = WOLFSSL_FATAL_ERROR;
  12527. }
  12528. }
  12529. if (!flag) {
  12530. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12531. if (ret != 0) {
  12532. flag = ret;
  12533. }
  12534. }
  12535. wc_HmacFree(&hmac);
  12536. printf(resultFmt, flag == 0 ? passed : failed);
  12537. #endif
  12538. return flag;
  12539. } /* END test_wc_Sha224HmacUpdate */
  12540. /*
  12541. * testing wc_HmacUpdate on SHA256 hash.
  12542. */
  12543. static int test_wc_Sha256HmacUpdate(void)
  12544. {
  12545. int flag = 0;
  12546. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12547. Hmac hmac;
  12548. testVector a, b;
  12549. int ret;
  12550. #ifdef HAVE_FIPS
  12551. const char* keys =
  12552. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12553. #else
  12554. const char* keys = "Jefe";
  12555. #endif
  12556. a.input = "what do ya want for nothing?";
  12557. a.inLen = XSTRLEN(a.input);
  12558. b.input = "Hi There";
  12559. b.inLen = XSTRLEN(b.input);
  12560. flag = 0;
  12561. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  12562. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12563. if (ret != 0)
  12564. return ret;
  12565. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  12566. if (ret != 0) {
  12567. flag = ret;
  12568. }
  12569. if (!flag) {
  12570. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12571. if (ret != 0) {
  12572. flag = ret;
  12573. }
  12574. }
  12575. /* Update Hmac. */
  12576. if (!flag) {
  12577. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12578. if (ret != 0) {
  12579. flag = ret;
  12580. }
  12581. }
  12582. /* Test bad args. */
  12583. if (!flag) {
  12584. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12585. if (ret != BAD_FUNC_ARG) {
  12586. flag = WOLFSSL_FATAL_ERROR;
  12587. }
  12588. }
  12589. if (!flag) {
  12590. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12591. if (ret != BAD_FUNC_ARG) {
  12592. flag = WOLFSSL_FATAL_ERROR;
  12593. }
  12594. }
  12595. if (!flag) {
  12596. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12597. if (ret != 0) {
  12598. flag = ret;
  12599. }
  12600. }
  12601. wc_HmacFree(&hmac);
  12602. printf(resultFmt, flag == 0 ? passed : failed);
  12603. #endif
  12604. return flag;
  12605. } /* END test_wc_Sha256HmacUpdate */
  12606. /*
  12607. * testing wc_HmacUpdate on SHA384 hash.
  12608. */
  12609. static int test_wc_Sha384HmacUpdate(void)
  12610. {
  12611. int flag = 0;
  12612. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12613. Hmac hmac;
  12614. testVector a, b;
  12615. int ret;
  12616. #ifdef HAVE_FIPS
  12617. const char* keys =
  12618. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12619. #else
  12620. const char* keys = "Jefe";
  12621. #endif
  12622. a.input = "what do ya want for nothing?";
  12623. a.inLen = XSTRLEN(a.input);
  12624. b.input = "Hi There";
  12625. b.inLen = XSTRLEN(b.input);
  12626. flag = 0;
  12627. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  12628. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12629. if (ret != 0)
  12630. return ret;
  12631. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  12632. if (ret != 0) {
  12633. flag = ret;
  12634. }
  12635. if (!flag) {
  12636. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12637. if (ret != 0) {
  12638. flag = ret;
  12639. }
  12640. }
  12641. /* Update Hmac. */
  12642. if (!flag) {
  12643. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12644. if (ret != 0) {
  12645. flag = ret;
  12646. }
  12647. }
  12648. /* Test bad args. */
  12649. if (!flag) {
  12650. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12651. if (ret != BAD_FUNC_ARG) {
  12652. flag = WOLFSSL_FATAL_ERROR;
  12653. }
  12654. }
  12655. if (!flag) {
  12656. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12657. if (ret != BAD_FUNC_ARG) {
  12658. flag = WOLFSSL_FATAL_ERROR;
  12659. }
  12660. }
  12661. if (!flag) {
  12662. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12663. if (ret != 0) {
  12664. flag = ret;
  12665. }
  12666. }
  12667. wc_HmacFree(&hmac);
  12668. printf(resultFmt, flag == 0 ? passed : failed);
  12669. #endif
  12670. return flag;
  12671. } /* END test_wc_Sha384HmacUpdate */
  12672. /*
  12673. * Testing wc_HmacFinal() with MD5
  12674. */
  12675. static int test_wc_Md5HmacFinal(void)
  12676. {
  12677. int flag = 0;
  12678. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12679. Hmac hmac;
  12680. byte hash[WC_MD5_DIGEST_SIZE];
  12681. testVector a;
  12682. int ret;
  12683. const char* key;
  12684. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12685. a.input = "Hi There";
  12686. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  12687. "\x9d";
  12688. a.inLen = XSTRLEN(a.input);
  12689. a.outLen = XSTRLEN(a.output);
  12690. flag = 0;
  12691. printf(testingFmt, "wc_HmacFinal() with MD5");
  12692. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12693. if (ret != 0)
  12694. return ret;
  12695. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  12696. if (ret != 0) {
  12697. flag = ret;
  12698. }
  12699. if (!flag) {
  12700. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12701. if (ret != 0) {
  12702. flag = ret;
  12703. }
  12704. }
  12705. if (!flag) {
  12706. ret = wc_HmacFinal(&hmac, hash);
  12707. if (ret != 0) {
  12708. flag = ret;
  12709. }
  12710. }
  12711. if (!flag) {
  12712. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  12713. flag = WOLFSSL_FATAL_ERROR;
  12714. }
  12715. }
  12716. /* Try bad parameters. */
  12717. if (!flag) {
  12718. ret = wc_HmacFinal(NULL, hash);
  12719. if (ret != BAD_FUNC_ARG) {
  12720. flag = WOLFSSL_FATAL_ERROR;
  12721. }
  12722. }
  12723. #ifndef HAVE_FIPS
  12724. if (!flag) {
  12725. ret = wc_HmacFinal(&hmac, NULL);
  12726. if (ret != BAD_FUNC_ARG) {
  12727. flag = WOLFSSL_FATAL_ERROR;
  12728. }
  12729. }
  12730. #endif
  12731. wc_HmacFree(&hmac);
  12732. printf(resultFmt, flag == 0 ? passed : failed);
  12733. #endif
  12734. return flag;
  12735. } /* END test_wc_Md5HmacFinal */
  12736. /*
  12737. * Testing wc_HmacFinal() with SHA
  12738. */
  12739. static int test_wc_ShaHmacFinal(void)
  12740. {
  12741. int flag = 0;
  12742. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12743. Hmac hmac;
  12744. byte hash[WC_SHA_DIGEST_SIZE];
  12745. testVector a;
  12746. int ret;
  12747. const char* key;
  12748. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12749. "\x0b\x0b\x0b";
  12750. a.input = "Hi There";
  12751. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  12752. "\x8e\xf1\x46\xbe\x00";
  12753. a.inLen = XSTRLEN(a.input);
  12754. a.outLen = XSTRLEN(a.output);
  12755. flag = 0;
  12756. printf(testingFmt, "wc_HmacFinal() with SHA");
  12757. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12758. if (ret != 0)
  12759. return ret;
  12760. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  12761. if (ret != 0) {
  12762. flag = ret;
  12763. }
  12764. if (!flag) {
  12765. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12766. if (ret != 0) {
  12767. flag = ret;
  12768. }
  12769. }
  12770. if (!flag) {
  12771. ret = wc_HmacFinal(&hmac, hash);
  12772. if (ret != 0) {
  12773. flag = ret;
  12774. }
  12775. }
  12776. if (!flag) {
  12777. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  12778. flag = WOLFSSL_FATAL_ERROR;
  12779. }
  12780. }
  12781. /* Try bad parameters. */
  12782. if (!flag) {
  12783. ret = wc_HmacFinal(NULL, hash);
  12784. if (ret != BAD_FUNC_ARG) {
  12785. flag = WOLFSSL_FATAL_ERROR;
  12786. }
  12787. }
  12788. #ifndef HAVE_FIPS
  12789. if (!flag) {
  12790. ret = wc_HmacFinal(&hmac, NULL);
  12791. if (ret != BAD_FUNC_ARG) {
  12792. flag = WOLFSSL_FATAL_ERROR;
  12793. }
  12794. }
  12795. #endif
  12796. wc_HmacFree(&hmac);
  12797. printf(resultFmt, flag == 0 ? passed : failed);
  12798. #endif
  12799. return flag;
  12800. } /* END test_wc_ShaHmacFinal */
  12801. /*
  12802. * Testing wc_HmacFinal() with SHA224
  12803. */
  12804. static int test_wc_Sha224HmacFinal(void)
  12805. {
  12806. int flag = 0;
  12807. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12808. Hmac hmac;
  12809. byte hash[WC_SHA224_DIGEST_SIZE];
  12810. testVector a;
  12811. int ret;
  12812. const char* key;
  12813. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12814. "\x0b\x0b\x0b";
  12815. a.input = "Hi There";
  12816. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  12817. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  12818. a.inLen = XSTRLEN(a.input);
  12819. a.outLen = XSTRLEN(a.output);
  12820. flag = 0;
  12821. printf(testingFmt, "wc_HmacFinal() with SHA224");
  12822. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12823. if (ret != 0)
  12824. return ret;
  12825. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  12826. if (ret != 0) {
  12827. flag = ret;
  12828. }
  12829. if (!flag) {
  12830. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12831. if (ret != 0) {
  12832. flag = ret;
  12833. }
  12834. }
  12835. if (!flag) {
  12836. ret = wc_HmacFinal(&hmac, hash);
  12837. if (ret != 0) {
  12838. flag = ret;
  12839. }
  12840. }
  12841. if (!flag) {
  12842. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  12843. flag = WOLFSSL_FATAL_ERROR;
  12844. }
  12845. }
  12846. /* Try bad parameters. */
  12847. if (!flag) {
  12848. ret = wc_HmacFinal(NULL, hash);
  12849. if (ret != BAD_FUNC_ARG) {
  12850. flag = WOLFSSL_FATAL_ERROR;
  12851. }
  12852. }
  12853. #ifndef HAVE_FIPS
  12854. if (!flag) {
  12855. ret = wc_HmacFinal(&hmac, NULL);
  12856. if (ret != BAD_FUNC_ARG) {
  12857. flag = WOLFSSL_FATAL_ERROR;
  12858. }
  12859. }
  12860. #endif
  12861. wc_HmacFree(&hmac);
  12862. printf(resultFmt, flag == 0 ? passed : failed);
  12863. #endif
  12864. return flag;
  12865. } /* END test_wc_Sha224HmacFinal */
  12866. /*
  12867. * Testing wc_HmacFinal() with SHA256
  12868. */
  12869. static int test_wc_Sha256HmacFinal(void)
  12870. {
  12871. int flag = 0;
  12872. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12873. Hmac hmac;
  12874. byte hash[WC_SHA256_DIGEST_SIZE];
  12875. testVector a;
  12876. int ret;
  12877. const char* key;
  12878. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12879. "\x0b\x0b\x0b";
  12880. a.input = "Hi There";
  12881. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  12882. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  12883. "\xcf\xf7";
  12884. a.inLen = XSTRLEN(a.input);
  12885. a.outLen = XSTRLEN(a.output);
  12886. flag = 0;
  12887. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  12888. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12889. if (ret != 0)
  12890. return ret;
  12891. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  12892. if (ret != 0) {
  12893. flag = ret;
  12894. }
  12895. if (!flag) {
  12896. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12897. if (ret != 0) {
  12898. flag = ret;
  12899. }
  12900. }
  12901. if (!flag) {
  12902. ret = wc_HmacFinal(&hmac, hash);
  12903. if (ret != 0) {
  12904. flag = ret;
  12905. }
  12906. }
  12907. if (!flag) {
  12908. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  12909. flag = WOLFSSL_FATAL_ERROR;
  12910. }
  12911. }
  12912. /* Try bad parameters. */
  12913. if (!flag) {
  12914. ret = wc_HmacFinal(NULL, hash);
  12915. if (ret != BAD_FUNC_ARG) {
  12916. flag = WOLFSSL_FATAL_ERROR;
  12917. }
  12918. }
  12919. #ifndef HAVE_FIPS
  12920. if (!flag) {
  12921. ret = wc_HmacFinal(&hmac, NULL);
  12922. if (ret != BAD_FUNC_ARG) {
  12923. flag = WOLFSSL_FATAL_ERROR;
  12924. }
  12925. }
  12926. #endif
  12927. wc_HmacFree(&hmac);
  12928. printf(resultFmt, flag == 0 ? passed : failed);
  12929. #endif
  12930. return flag;
  12931. } /* END test_wc_Sha256HmacFinal */
  12932. /*
  12933. * Testing wc_HmacFinal() with SHA384
  12934. */
  12935. static int test_wc_Sha384HmacFinal(void)
  12936. {
  12937. int flag = 0;
  12938. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12939. Hmac hmac;
  12940. byte hash[WC_SHA384_DIGEST_SIZE];
  12941. testVector a;
  12942. int ret;
  12943. const char* key;
  12944. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12945. "\x0b\x0b\x0b";
  12946. a.input = "Hi There";
  12947. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  12948. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  12949. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  12950. "\xfa\x9c\xb6";
  12951. a.inLen = XSTRLEN(a.input);
  12952. a.outLen = XSTRLEN(a.output);
  12953. flag = 0;
  12954. printf(testingFmt, "wc_HmacFinal() with SHA384");
  12955. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12956. if (ret != 0)
  12957. return ret;
  12958. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  12959. if (ret != 0) {
  12960. flag = ret;
  12961. }
  12962. if (!flag) {
  12963. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12964. if (ret != 0) {
  12965. flag = ret;
  12966. }
  12967. }
  12968. if (!flag) {
  12969. ret = wc_HmacFinal(&hmac, hash);
  12970. if (ret != 0) {
  12971. flag = ret;
  12972. }
  12973. }
  12974. if (!flag) {
  12975. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  12976. flag = WOLFSSL_FATAL_ERROR;
  12977. }
  12978. }
  12979. /* Try bad parameters. */
  12980. if (!flag) {
  12981. ret = wc_HmacFinal(NULL, hash);
  12982. if (ret != BAD_FUNC_ARG) {
  12983. flag = WOLFSSL_FATAL_ERROR;
  12984. }
  12985. }
  12986. #ifndef HAVE_FIPS
  12987. if (!flag) {
  12988. ret = wc_HmacFinal(&hmac, NULL);
  12989. if (ret != BAD_FUNC_ARG) {
  12990. flag = WOLFSSL_FATAL_ERROR;
  12991. }
  12992. }
  12993. #endif
  12994. wc_HmacFree(&hmac);
  12995. printf(resultFmt, flag == 0 ? passed : failed);
  12996. #endif
  12997. return flag;
  12998. } /* END test_wc_Sha384HmacFinal */
  12999. /*
  13000. * Testing wc_InitCmac()
  13001. */
  13002. static int test_wc_InitCmac(void)
  13003. {
  13004. int ret = 0;
  13005. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13006. Cmac cmac1, cmac2, cmac3;
  13007. /* AES 128 key. */
  13008. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13009. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13010. /* AES 192 key. */
  13011. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13012. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13013. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13014. /* AES 256 key. */
  13015. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13016. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13017. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13018. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13019. word32 key1Sz = (word32)sizeof(key1) - 1;
  13020. word32 key2Sz = (word32)sizeof(key2) - 1;
  13021. word32 key3Sz = (word32)sizeof(key3) - 1;
  13022. int type = WC_CMAC_AES;
  13023. printf(testingFmt, "wc_InitCmac()");
  13024. #ifdef WOLFSSL_AES_128
  13025. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  13026. #endif
  13027. #ifdef WOLFSSL_AES_192
  13028. if (ret == 0) {
  13029. wc_AesFree(&cmac1.aes);
  13030. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  13031. }
  13032. #endif
  13033. #ifdef WOLFSSL_AES_256
  13034. if (ret == 0) {
  13035. wc_AesFree(&cmac2.aes);
  13036. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  13037. }
  13038. #endif
  13039. /* Test bad args. */
  13040. if (ret == 0) {
  13041. wc_AesFree(&cmac3.aes);
  13042. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  13043. if (ret == BAD_FUNC_ARG) {
  13044. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  13045. }
  13046. if (ret == BAD_FUNC_ARG) {
  13047. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  13048. }
  13049. if (ret == BAD_FUNC_ARG) {
  13050. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  13051. }
  13052. if (ret == BAD_FUNC_ARG) {
  13053. ret = 0;
  13054. } else {
  13055. ret = WOLFSSL_FATAL_ERROR;
  13056. }
  13057. }
  13058. (void)key1;
  13059. (void)key1Sz;
  13060. (void)key2;
  13061. (void)key2Sz;
  13062. (void)cmac1;
  13063. (void)cmac2;
  13064. printf(resultFmt, ret == 0 ? passed : failed);
  13065. #endif
  13066. return ret;
  13067. } /* END test_wc_InitCmac */
  13068. /*
  13069. * Testing wc_CmacUpdate()
  13070. */
  13071. static int test_wc_CmacUpdate(void)
  13072. {
  13073. int ret = 0;
  13074. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13075. Cmac cmac;
  13076. byte key[] =
  13077. {
  13078. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13079. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13080. };
  13081. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13082. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13083. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13084. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13085. "\xf4";
  13086. word32 inSz = (word32)sizeof(in) - 1;
  13087. word32 keySz = (word32)sizeof(key);
  13088. int type = WC_CMAC_AES;
  13089. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13090. if (ret != 0) {
  13091. return ret;
  13092. }
  13093. printf(testingFmt, "wc_CmacUpdate()");
  13094. ret = wc_CmacUpdate(&cmac, in, inSz);
  13095. /* Test bad args. */
  13096. if (ret == 0) {
  13097. ret = wc_CmacUpdate(NULL, in, inSz);
  13098. if (ret == BAD_FUNC_ARG) {
  13099. ret = wc_CmacUpdate(&cmac, NULL, 30);
  13100. }
  13101. if (ret == BAD_FUNC_ARG) {
  13102. ret = 0;
  13103. } else if (ret == 0) {
  13104. ret = WOLFSSL_FATAL_ERROR;
  13105. }
  13106. wc_AesFree(&cmac.aes);
  13107. }
  13108. printf(resultFmt, ret == 0 ? passed : failed);
  13109. #endif
  13110. return ret;
  13111. } /* END test_wc_CmacUpdate */
  13112. /*
  13113. * Testing wc_CmacFinal()
  13114. */
  13115. static int test_wc_CmacFinal(void)
  13116. {
  13117. int ret = 0;
  13118. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13119. Cmac cmac;
  13120. byte key[] =
  13121. {
  13122. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13123. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13124. };
  13125. byte msg[] =
  13126. {
  13127. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13128. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13129. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13130. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13131. 0xf4
  13132. };
  13133. /* Test vectors from CMACGenAES128.rsp from
  13134. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13135. * Per RFC4493 truncation of lsb is possible.
  13136. */
  13137. byte expMac[] =
  13138. {
  13139. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13140. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13141. };
  13142. byte mac[AES_BLOCK_SIZE];
  13143. word32 msgSz = (word32)sizeof(msg);
  13144. word32 keySz = (word32)sizeof(key);
  13145. word32 macSz = sizeof(mac);
  13146. word32 badMacSz = 17;
  13147. int expMacSz = sizeof(expMac);
  13148. int type = WC_CMAC_AES;
  13149. XMEMSET(mac, 0, macSz);
  13150. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13151. if (ret != 0) {
  13152. return ret;
  13153. }
  13154. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13155. printf(testingFmt, "wc_CmacFinal()");
  13156. if (ret == 0) {
  13157. ret = wc_CmacFinal(&cmac, mac, &macSz);
  13158. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13159. ret = WOLFSSL_FATAL_ERROR;
  13160. }
  13161. /* Pass in bad args. */
  13162. if (ret == 0) {
  13163. ret = wc_CmacFinal(NULL, mac, &macSz);
  13164. if (ret == BAD_FUNC_ARG) {
  13165. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  13166. }
  13167. if (ret == BAD_FUNC_ARG) {
  13168. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  13169. if (ret == BUFFER_E) {
  13170. ret = 0;
  13171. }
  13172. } else if (ret == 0) {
  13173. ret = WOLFSSL_FATAL_ERROR;
  13174. }
  13175. }
  13176. }
  13177. printf(resultFmt, ret == 0 ? passed : failed);
  13178. #endif
  13179. return ret;
  13180. } /* END test_wc_CmacFinal */
  13181. /*
  13182. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13183. */
  13184. static int test_wc_AesCmacGenerate(void)
  13185. {
  13186. int ret = 0;
  13187. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13188. Cmac cmac;
  13189. byte key[] =
  13190. {
  13191. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13192. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13193. };
  13194. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13195. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13196. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13197. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13198. byte mac[AES_BLOCK_SIZE];
  13199. word32 keySz = sizeof(key);
  13200. word32 macSz = sizeof(mac);
  13201. word32 msgSz = sizeof(msg) - 1;
  13202. word32 expMacSz = sizeof(expMac) - 1;
  13203. int type = WC_CMAC_AES;
  13204. XMEMSET(mac, 0, macSz);
  13205. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13206. if (ret != 0) {
  13207. return ret;
  13208. }
  13209. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13210. if (ret != 0) {
  13211. return ret;
  13212. } else {
  13213. wc_AesFree(&cmac.aes);
  13214. }
  13215. printf(testingFmt, "wc_AesCmacGenerate()");
  13216. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  13217. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13218. ret = WOLFSSL_FATAL_ERROR;
  13219. }
  13220. /* Pass in bad args. */
  13221. if (ret == 0) {
  13222. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  13223. if (ret == BAD_FUNC_ARG) {
  13224. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  13225. }
  13226. if (ret == BAD_FUNC_ARG) {
  13227. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  13228. }
  13229. if (ret == BAD_FUNC_ARG) {
  13230. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  13231. }
  13232. if (ret == BAD_FUNC_ARG) {
  13233. ret = 0;
  13234. } else if (ret == 0) {
  13235. ret = WOLFSSL_FATAL_ERROR;
  13236. }
  13237. }
  13238. printf(resultFmt, ret == 0 ? passed : failed);
  13239. if (ret == 0) {
  13240. printf(testingFmt, "wc_AesCmacVerify()");
  13241. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  13242. /* Test bad args. */
  13243. if (ret == 0) {
  13244. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  13245. if (ret == BAD_FUNC_ARG) {
  13246. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  13247. }
  13248. if (ret == BAD_FUNC_ARG) {
  13249. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  13250. }
  13251. if (ret == BAD_FUNC_ARG) {
  13252. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  13253. }
  13254. if (ret == BAD_FUNC_ARG) {
  13255. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  13256. }
  13257. if (ret == BAD_FUNC_ARG) {
  13258. ret = 0;
  13259. } else if (ret == 0) {
  13260. ret = WOLFSSL_FATAL_ERROR;
  13261. }
  13262. }
  13263. printf(resultFmt, ret == 0 ? passed : failed);
  13264. }
  13265. #endif
  13266. return ret;
  13267. } /* END test_wc_AesCmacGenerate */
  13268. /*
  13269. * Testing streaming AES-GCM API.
  13270. */
  13271. static int test_wc_AesGcmStream(void)
  13272. {
  13273. int ret = 0;
  13274. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13275. defined(WOLFSSL_AESGCM_STREAM)
  13276. int i;
  13277. WC_RNG rng[1];
  13278. Aes aesEnc[1];
  13279. Aes aesDec[1];
  13280. byte tag[AES_BLOCK_SIZE];
  13281. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13282. byte out[AES_BLOCK_SIZE * 3 + 2];
  13283. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13284. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13285. byte key[AES_128_KEY_SIZE] = { 0, };
  13286. byte iv[AES_IV_SIZE] = { 1, };
  13287. byte ivOut[AES_IV_SIZE];
  13288. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13289. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13290. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13291. };
  13292. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13293. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13294. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13295. };
  13296. static const byte expTag[AES_BLOCK_SIZE] = {
  13297. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13298. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13299. };
  13300. /* Create a random for generating IV/nonce. */
  13301. AssertIntEQ(wc_InitRng(rng), 0);
  13302. /* Initialize data structures. */
  13303. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13304. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13305. /* BadParameters to streaming init. */
  13306. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13307. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13308. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13309. BAD_FUNC_ARG);
  13310. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13311. BAD_FUNC_ARG);
  13312. /* Bad parameters to encrypt update. */
  13313. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13314. BAD_FUNC_ARG);
  13315. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13316. BAD_FUNC_ARG);
  13317. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13318. BAD_FUNC_ARG);
  13319. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13320. BAD_FUNC_ARG);
  13321. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13322. BAD_FUNC_ARG);
  13323. /* Bad parameters to decrypt update. */
  13324. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13325. BAD_FUNC_ARG);
  13326. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13327. BAD_FUNC_ARG);
  13328. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13329. BAD_FUNC_ARG);
  13330. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13331. BAD_FUNC_ARG);
  13332. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13333. BAD_FUNC_ARG);
  13334. /* Bad parameters to encrypt final. */
  13335. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13336. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13337. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13338. BAD_FUNC_ARG);
  13339. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13340. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13341. BAD_FUNC_ARG);
  13342. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13343. BAD_FUNC_ARG);
  13344. /* Bad parameters to decrypt final. */
  13345. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13346. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13347. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13348. BAD_FUNC_ARG);
  13349. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13350. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13351. BAD_FUNC_ARG);
  13352. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13353. BAD_FUNC_ARG);
  13354. /* Check calling final before setting key fails. */
  13355. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13356. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13357. /* Check calling update before setting key else fails. */
  13358. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13359. MISSING_KEY);
  13360. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13361. MISSING_KEY);
  13362. /* Set key but not IV. */
  13363. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13364. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13365. /* Check calling final before setting IV fails. */
  13366. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13367. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13368. /* Check calling update before setting IV else fails. */
  13369. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13370. MISSING_IV);
  13371. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13372. MISSING_IV);
  13373. /* Set IV using fixed part IV and external IV APIs. */
  13374. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13375. rng), 0);
  13376. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13377. GCM_NONCE_MID_SZ), 0);
  13378. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13379. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13380. /* Encrypt and decrypt data. */
  13381. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13382. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13383. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13384. /* Finalize and check tag matches. */
  13385. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13386. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13387. /* Set key and IV through streaming init API. */
  13388. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13389. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13390. /* Encrypt/decrypt one block and AAD of one block. */
  13391. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13392. AES_BLOCK_SIZE), 0);
  13393. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13394. AES_BLOCK_SIZE), 0);
  13395. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13396. /* Finalize and check tag matches. */
  13397. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13398. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13399. /* Set key and IV through streaming init API. */
  13400. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13401. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13402. /* No data to encrypt/decrypt one byte of AAD. */
  13403. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13404. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13405. /* Finalize and check tag matches. */
  13406. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13407. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13408. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13409. /* Set key and IV through streaming init API. */
  13410. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13411. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13412. /* Encrypt/decrypt one byte and no AAD. */
  13413. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13414. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13415. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13416. /* Finalize and check tag matches. */
  13417. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13418. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13419. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13420. /* Set key and IV through streaming init API. */
  13421. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13422. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13423. /* Encryption AES is one byte at a time */
  13424. for (i = 0; i < (int)sizeof(aad); i++) {
  13425. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13426. 0);
  13427. }
  13428. for (i = 0; i < (int)sizeof(in); i++) {
  13429. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13430. 0);
  13431. }
  13432. /* Decryption AES is two bytes at a time */
  13433. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13434. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13435. 0);
  13436. }
  13437. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13438. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13439. 0), 0);
  13440. }
  13441. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13442. /* Finalize and check tag matches. */
  13443. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13444. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13445. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13446. /* Check streaming encryption can be decrypted with one shot. */
  13447. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13448. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13449. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13450. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13451. wc_AesFree(aesEnc);
  13452. wc_AesFree(aesDec);
  13453. wc_FreeRng(rng);
  13454. #endif
  13455. return ret;
  13456. } /* END test_wc_AesGcmStream */
  13457. /*
  13458. * unit test for wc_Des3_SetIV()
  13459. */
  13460. static int test_wc_Des3_SetIV(void)
  13461. {
  13462. int ret = 0;
  13463. #ifndef NO_DES3
  13464. Des3 des;
  13465. const byte key[] =
  13466. {
  13467. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13468. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13469. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13470. };
  13471. const byte iv[] =
  13472. {
  13473. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13474. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13475. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13476. };
  13477. printf(testingFmt, "wc_Des3_SetIV()");
  13478. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13479. if (ret != 0)
  13480. return ret;
  13481. /* DES_ENCRYPTION or DES_DECRYPTION */
  13482. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13483. if (ret == 0) {
  13484. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13485. ret = WOLFSSL_FATAL_ERROR;
  13486. }
  13487. }
  13488. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  13489. /* Test explicitly wc_Des3_SetIV() */
  13490. if (ret == 0) {
  13491. ret = wc_Des3_SetIV(NULL, iv);
  13492. if (ret == BAD_FUNC_ARG) {
  13493. ret = wc_Des3_SetIV(&des, NULL);
  13494. } else if (ret == 0) {
  13495. ret = WOLFSSL_FATAL_ERROR;
  13496. }
  13497. }
  13498. #endif
  13499. wc_Des3Free(&des);
  13500. printf(resultFmt, ret == 0 ? passed : failed);
  13501. #endif
  13502. return ret;
  13503. } /* END test_wc_Des3_SetIV */
  13504. /*
  13505. * unit test for wc_Des3_SetKey()
  13506. */
  13507. static int test_wc_Des3_SetKey(void)
  13508. {
  13509. int ret = 0;
  13510. #ifndef NO_DES3
  13511. Des3 des;
  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. printf(testingFmt, "wc_Des3_SetKey()");
  13525. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13526. if (ret != 0)
  13527. return ret;
  13528. /* DES_ENCRYPTION or DES_DECRYPTION */
  13529. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13530. if (ret == 0) {
  13531. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13532. ret = WOLFSSL_FATAL_ERROR;
  13533. }
  13534. }
  13535. /* Test bad args. */
  13536. if (ret == 0) {
  13537. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  13538. if (ret == BAD_FUNC_ARG) {
  13539. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  13540. }
  13541. if (ret == BAD_FUNC_ARG) {
  13542. ret = wc_Des3_SetKey(&des, key, iv, -1);
  13543. }
  13544. if (ret == BAD_FUNC_ARG) {
  13545. /* Default case. Should return 0. */
  13546. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  13547. }
  13548. } /* END if ret != 0 */
  13549. wc_Des3Free(&des);
  13550. printf(resultFmt, ret == 0 ? passed : failed);
  13551. #endif
  13552. return ret;
  13553. } /* END test_wc_Des3_SetKey */
  13554. /*
  13555. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  13556. */
  13557. static int test_wc_Des3_CbcEncryptDecrypt(void)
  13558. {
  13559. int ret = 0;
  13560. #ifndef NO_DES3
  13561. Des3 des;
  13562. byte cipher[24];
  13563. byte plain[24];
  13564. const byte key[] =
  13565. {
  13566. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13567. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13568. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13569. };
  13570. const byte iv[] =
  13571. {
  13572. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13573. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13574. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13575. };
  13576. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13577. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13578. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13579. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13580. };
  13581. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  13582. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13583. if (ret != 0)
  13584. return ret;
  13585. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13586. if (ret == 0) {
  13587. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  13588. if (ret == 0) {
  13589. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  13590. }
  13591. if (ret == 0) {
  13592. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  13593. }
  13594. }
  13595. if (ret == 0) {
  13596. if (XMEMCMP(plain, vector, 24) != 0) {
  13597. ret = WOLFSSL_FATAL_ERROR;
  13598. }
  13599. }
  13600. /* Pass in bad args. */
  13601. if (ret == 0) {
  13602. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  13603. if (ret == BAD_FUNC_ARG) {
  13604. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  13605. }
  13606. if (ret == BAD_FUNC_ARG) {
  13607. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  13608. }
  13609. if (ret != BAD_FUNC_ARG) {
  13610. ret = WOLFSSL_FATAL_ERROR;
  13611. } else {
  13612. ret = 0;
  13613. }
  13614. }
  13615. if (ret == 0) {
  13616. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  13617. if (ret == BAD_FUNC_ARG) {
  13618. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  13619. }
  13620. if (ret == BAD_FUNC_ARG) {
  13621. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  13622. }
  13623. if (ret != BAD_FUNC_ARG) {
  13624. ret = WOLFSSL_FATAL_ERROR;
  13625. } else {
  13626. ret = 0;
  13627. }
  13628. }
  13629. wc_Des3Free(&des);
  13630. printf(resultFmt, ret == 0 ? passed : failed);
  13631. #endif
  13632. return ret;
  13633. } /* END wc_Des3_CbcEncrypt */
  13634. /*
  13635. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  13636. */
  13637. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  13638. {
  13639. int ret = 0;
  13640. #ifndef NO_DES3
  13641. word32 vectorSz, cipherSz;
  13642. byte cipher[24];
  13643. byte plain[24];
  13644. byte vector[] = /* Now is the time for all w/o trailing 0 */
  13645. {
  13646. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13647. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13648. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13649. };
  13650. byte key[] =
  13651. {
  13652. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13653. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13654. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13655. };
  13656. byte iv[] =
  13657. {
  13658. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13659. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13660. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13661. };
  13662. vectorSz = sizeof(byte) * 24;
  13663. cipherSz = sizeof(byte) * 24;
  13664. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  13665. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  13666. if (ret == 0) {
  13667. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  13668. if (ret == 0) {
  13669. if (XMEMCMP(plain, vector, 24) != 0) {
  13670. ret = WOLFSSL_FATAL_ERROR;
  13671. }
  13672. }
  13673. }
  13674. /* pass in bad args. */
  13675. if (ret == 0) {
  13676. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  13677. if (ret == BAD_FUNC_ARG) {
  13678. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  13679. }
  13680. if (ret == BAD_FUNC_ARG) {
  13681. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  13682. }
  13683. if (ret == BAD_FUNC_ARG) {
  13684. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  13685. key, NULL);
  13686. } else {
  13687. /* Return code catch. */
  13688. ret = WOLFSSL_FAILURE;
  13689. }
  13690. }
  13691. if (ret == 0) {
  13692. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  13693. if (ret == BAD_FUNC_ARG) {
  13694. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  13695. }
  13696. if (ret == BAD_FUNC_ARG) {
  13697. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  13698. }
  13699. if (ret == BAD_FUNC_ARG) {
  13700. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  13701. } else {
  13702. ret = WOLFSSL_FAILURE;
  13703. }
  13704. }
  13705. printf(resultFmt, ret == 0 ? passed : failed);
  13706. #endif
  13707. return ret;
  13708. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  13709. /*
  13710. * Unit test for wc_Des3_EcbEncrypt
  13711. */
  13712. static int test_wc_Des3_EcbEncrypt(void)
  13713. {
  13714. int ret = 0;
  13715. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  13716. Des3 des;
  13717. byte cipher[24];
  13718. word32 cipherSz = sizeof(cipher);
  13719. const byte key[] =
  13720. {
  13721. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13722. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13723. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13724. };
  13725. const byte iv[] =
  13726. {
  13727. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13728. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13729. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13730. };
  13731. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  13732. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  13733. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  13734. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  13735. };
  13736. printf(testingFmt, "wc_Des3_EcbEncrypt()");
  13737. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13738. if (ret != 0) {
  13739. return ret;
  13740. }
  13741. if (ret == 0 ) {
  13742. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13743. }
  13744. /* Bad Cases */
  13745. if (ret == 0) {
  13746. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  13747. if (ret == BAD_FUNC_ARG) {
  13748. ret = 0;
  13749. }
  13750. }
  13751. if (ret == 0) {
  13752. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  13753. if (ret == BAD_FUNC_ARG) {
  13754. ret = 0;
  13755. }
  13756. }
  13757. if (ret == 0) {
  13758. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  13759. if (ret == BAD_FUNC_ARG) {
  13760. ret = 0;
  13761. }
  13762. }
  13763. if (ret == 0) {
  13764. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  13765. if (ret == BAD_FUNC_ARG) {
  13766. ret = 0;
  13767. }
  13768. }
  13769. if (ret == 0) {
  13770. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  13771. if (ret == BAD_FUNC_ARG) {
  13772. ret = 0;
  13773. }
  13774. }
  13775. /* Good Cases */
  13776. if (ret == 0) {
  13777. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  13778. }
  13779. wc_Des3Free(&des);
  13780. printf(resultFmt, ret == 0 ? passed : failed);
  13781. #endif
  13782. return ret;
  13783. } /* END test_wc_Des3_EcbEncrypt */
  13784. /*
  13785. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  13786. */
  13787. static int test_wc_Chacha_SetKey(void)
  13788. {
  13789. int ret = 0;
  13790. #ifdef HAVE_CHACHA
  13791. ChaCha ctx;
  13792. const byte key[] =
  13793. {
  13794. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13796. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13797. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13798. };
  13799. byte cipher[128];
  13800. printf(testingFmt, "wc_Chacha_SetKey()");
  13801. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13802. /* Test bad args. */
  13803. if (ret == 0) {
  13804. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13805. if (ret == BAD_FUNC_ARG) {
  13806. ret = wc_Chacha_SetKey(&ctx, key, 18);
  13807. }
  13808. if (ret == BAD_FUNC_ARG) {
  13809. ret = 0;
  13810. } else {
  13811. ret = WOLFSSL_FATAL_ERROR;
  13812. }
  13813. }
  13814. printf(resultFmt, ret == 0 ? passed : failed);
  13815. if (ret != 0) {
  13816. return ret;
  13817. }
  13818. printf(testingFmt, "wc_Chacha_SetIV");
  13819. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  13820. if (ret == 0) {
  13821. /* Test bad args. */
  13822. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  13823. if (ret == BAD_FUNC_ARG) {
  13824. ret = 0;
  13825. } else {
  13826. ret = WOLFSSL_FAILURE;
  13827. }
  13828. }
  13829. printf(resultFmt, ret == 0 ? passed : failed);
  13830. #endif
  13831. return ret;
  13832. } /* END test_wc_Chacha_SetKey */
  13833. /*
  13834. * unit test for wc_Poly1305SetKey()
  13835. */
  13836. static int test_wc_Poly1305SetKey(void)
  13837. {
  13838. int ret = 0;
  13839. #ifdef HAVE_POLY1305
  13840. Poly1305 ctx;
  13841. const byte key[] =
  13842. {
  13843. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13844. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13845. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13846. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13847. };
  13848. printf(testingFmt, "wc_Poly1305_SetKey()");
  13849. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  13850. /* Test bad args. */
  13851. if (ret == 0) {
  13852. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  13853. if(ret == BAD_FUNC_ARG) {
  13854. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  13855. }
  13856. if (ret == BAD_FUNC_ARG) {
  13857. ret = wc_Poly1305SetKey(&ctx, key, 18);
  13858. }
  13859. if (ret == BAD_FUNC_ARG) {
  13860. ret = 0;
  13861. } else {
  13862. ret = WOLFSSL_FATAL_ERROR;
  13863. }
  13864. }
  13865. printf(resultFmt, ret == 0 ? passed : failed);
  13866. #endif
  13867. return ret;
  13868. } /* END test_wc_Poly1305_SetKey() */
  13869. /*
  13870. * Testing wc_Chacha_Process()
  13871. */
  13872. static int test_wc_Chacha_Process(void)
  13873. {
  13874. int ret = 0;
  13875. #ifdef HAVE_CHACHA
  13876. ChaCha enc, dec;
  13877. byte cipher[128];
  13878. byte plain[128];
  13879. const byte key[] =
  13880. {
  13881. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13882. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13883. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  13884. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  13885. };
  13886. const char* input = "Everybody gets Friday off.";
  13887. word32 keySz = sizeof(key)/sizeof(byte);
  13888. unsigned long int inlen = XSTRLEN(input);
  13889. /*Initialize stack varialbes.*/
  13890. XMEMSET(cipher, 0, 128);
  13891. XMEMSET(plain, 0, 128);
  13892. printf(testingFmt, "wc_Chacha_Process()");
  13893. ret = wc_Chacha_SetKey(&enc, key, keySz);
  13894. AssertIntEQ(ret, 0);
  13895. ret = wc_Chacha_SetKey(&dec, key, keySz);
  13896. AssertIntEQ(ret, 0);
  13897. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13898. AssertIntEQ(ret, 0);
  13899. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13900. AssertIntEQ(ret, 0);
  13901. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  13902. AssertIntEQ(ret, 0);
  13903. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  13904. AssertIntEQ(ret, 0);
  13905. ret = XMEMCMP(input, plain, (int)inlen);
  13906. AssertIntEQ(ret, 0);
  13907. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  13908. /* test checking and using leftovers, currently just in C code */
  13909. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  13910. AssertIntEQ(ret, 0);
  13911. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  13912. AssertIntEQ(ret, 0);
  13913. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  13914. AssertIntEQ(ret, 0);
  13915. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  13916. (byte*)input + (inlen - 2), 2);
  13917. AssertIntEQ(ret, 0);
  13918. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  13919. AssertIntEQ(ret, 0);
  13920. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  13921. (byte*)input + (inlen - 2), 2);
  13922. AssertIntEQ(ret, 0);
  13923. ret = XMEMCMP(input, plain, (int)inlen);
  13924. AssertIntEQ(ret, 0);
  13925. /* check edge cases with counter increment */
  13926. {
  13927. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  13928. const byte expected[] = {
  13929. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  13930. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  13931. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  13932. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  13933. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  13934. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  13935. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  13936. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  13937. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  13938. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  13939. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  13940. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  13941. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  13942. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  13943. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  13944. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  13945. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  13946. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  13947. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  13948. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  13949. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  13950. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  13951. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  13952. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  13953. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  13954. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  13955. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  13956. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  13957. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  13958. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  13959. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  13960. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  13961. };
  13962. const byte iv2[] = {
  13963. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  13964. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  13965. };
  13966. byte input2[256];
  13967. int i;
  13968. for (i = 0; i < 256; i++)
  13969. input2[i] = i;
  13970. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  13971. AssertIntEQ(ret, 0);
  13972. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  13973. AssertIntEQ(ret, 0);
  13974. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  13975. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  13976. AssertIntEQ(ret, 0);
  13977. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  13978. /* partial */
  13979. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  13980. AssertIntEQ(ret, 0);
  13981. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  13982. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  13983. AssertIntEQ(ret, 0);
  13984. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  13985. }
  13986. #endif
  13987. /* Test bad args. */
  13988. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  13989. AssertIntEQ(ret, BAD_FUNC_ARG);
  13990. if (ret == BAD_FUNC_ARG) {
  13991. ret = 0;
  13992. }
  13993. printf(resultFmt, ret == 0 ? passed : failed);
  13994. #endif
  13995. return ret;
  13996. } /* END test_wc_Chacha_Process */
  13997. /*
  13998. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  13999. */
  14000. static int test_wc_ChaCha20Poly1305_aead(void)
  14001. {
  14002. int ret = 0;
  14003. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14004. const byte key[] = {
  14005. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14006. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14007. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14008. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14009. };
  14010. const byte plaintext[] = {
  14011. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14012. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14013. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14014. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14015. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14016. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14017. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14018. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14019. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14020. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14021. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14022. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14023. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14024. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14025. 0x74, 0x2e
  14026. };
  14027. const byte iv[] = {
  14028. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14029. 0x44, 0x45, 0x46, 0x47
  14030. };
  14031. const byte aad[] = { /* additional data */
  14032. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14033. 0xc4, 0xc5, 0xc6, 0xc7
  14034. };
  14035. const byte cipher[] = { /* expected output from operation */
  14036. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14037. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14038. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14039. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14040. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14041. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14042. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14043. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14044. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14045. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14046. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14047. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14048. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14049. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14050. 0x61, 0x16
  14051. };
  14052. const byte authTag[] = { /* expected output from operation */
  14053. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14054. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14055. };
  14056. byte generatedCiphertext[272];
  14057. byte generatedPlaintext[272];
  14058. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14059. /* Initialize stack variables. */
  14060. XMEMSET(generatedCiphertext, 0, 272);
  14061. XMEMSET(generatedPlaintext, 0, 272);
  14062. /* Test Encrypt */
  14063. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  14064. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  14065. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14066. AssertIntEQ(ret, 0);
  14067. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  14068. AssertIntEQ(ret, 0);
  14069. /* Test bad args. */
  14070. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  14071. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14072. AssertIntEQ(ret, BAD_FUNC_ARG);
  14073. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14074. plaintext, sizeof(plaintext),
  14075. generatedCiphertext, generatedAuthTag);
  14076. AssertIntEQ(ret, BAD_FUNC_ARG);
  14077. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14078. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14079. AssertIntEQ(ret, BAD_FUNC_ARG);
  14080. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14081. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14082. AssertIntEQ(ret, BAD_FUNC_ARG);
  14083. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14084. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  14085. AssertIntEQ(ret, BAD_FUNC_ARG);
  14086. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14087. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  14088. if (ret == BAD_FUNC_ARG) {
  14089. ret = 0;
  14090. }
  14091. printf(resultFmt, ret == 0 ? passed : failed);
  14092. if (ret != 0) {
  14093. return ret;
  14094. }
  14095. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  14096. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14097. sizeof(cipher), authTag, generatedPlaintext);
  14098. AssertIntEQ(ret, 0);
  14099. ret = XMEMCMP(generatedPlaintext, plaintext,
  14100. sizeof(plaintext)/sizeof(byte));
  14101. AssertIntEQ(ret, 0);
  14102. /* Test bad args. */
  14103. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14104. sizeof(cipher), authTag, generatedPlaintext);
  14105. AssertIntEQ(ret, BAD_FUNC_ARG);
  14106. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14107. cipher, sizeof(cipher), authTag, generatedPlaintext);
  14108. AssertIntEQ(ret, BAD_FUNC_ARG);
  14109. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14110. sizeof(cipher), authTag, generatedPlaintext);
  14111. AssertIntEQ(ret, BAD_FUNC_ARG);
  14112. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14113. sizeof(cipher), NULL, generatedPlaintext);
  14114. AssertIntEQ(ret, BAD_FUNC_ARG);
  14115. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14116. sizeof(cipher), authTag, NULL);
  14117. AssertIntEQ(ret, BAD_FUNC_ARG);
  14118. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14119. sizeof(cipher), authTag, generatedPlaintext);
  14120. AssertIntEQ(ret, BAD_FUNC_ARG);
  14121. if (ret == BAD_FUNC_ARG) {
  14122. ret = 0;
  14123. }
  14124. printf(resultFmt, ret == 0 ? passed : failed);
  14125. #endif
  14126. return ret;
  14127. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  14128. /*
  14129. * Testing function for wc_Rc2SetKey().
  14130. */
  14131. static int test_wc_Rc2SetKey(void)
  14132. {
  14133. int ret = 0;
  14134. #ifdef WC_RC2
  14135. Rc2 rc2;
  14136. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14137. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14138. printf(testingFmt, "wc_Rc2SetKey()");
  14139. /* valid key and IV */
  14140. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14141. iv, 40);
  14142. if (ret == 0) {
  14143. /* valid key, no IV */
  14144. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14145. NULL, 40);
  14146. }
  14147. /* bad arguments */
  14148. if (ret == 0) {
  14149. /* null Rc2 struct */
  14150. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14151. iv, 40);
  14152. if (ret == BAD_FUNC_ARG) {
  14153. ret = 0;
  14154. }
  14155. }
  14156. if (ret == 0) {
  14157. /* null key */
  14158. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14159. iv, 40);
  14160. if (ret == BAD_FUNC_ARG) {
  14161. ret = 0;
  14162. }
  14163. }
  14164. if (ret == 0) {
  14165. /* key size == 0 */
  14166. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  14167. if (ret == WC_KEY_SIZE_E) {
  14168. ret = 0;
  14169. }
  14170. }
  14171. if (ret == 0) {
  14172. /* key size > 128 */
  14173. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  14174. if (ret == WC_KEY_SIZE_E) {
  14175. ret = 0;
  14176. }
  14177. }
  14178. if (ret == 0) {
  14179. /* effective bits == 0 */
  14180. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14181. iv, 0);
  14182. if (ret == WC_KEY_SIZE_E) {
  14183. ret = 0;
  14184. }
  14185. }
  14186. if (ret == 0) {
  14187. /* effective bits > 1024 */
  14188. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14189. iv, 1025);
  14190. if (ret == WC_KEY_SIZE_E) {
  14191. ret = 0;
  14192. }
  14193. }
  14194. printf(resultFmt, ret == 0 ? passed : failed);
  14195. #endif
  14196. return ret;
  14197. } /* END test_wc_Rc2SetKey */
  14198. /*
  14199. * Testing function for wc_Rc2SetIV().
  14200. */
  14201. static int test_wc_Rc2SetIV(void)
  14202. {
  14203. int ret = 0;
  14204. #ifdef WC_RC2
  14205. Rc2 rc2;
  14206. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14207. printf(testingFmt, "wc_Rc2SetIV()");
  14208. /* valid IV */
  14209. ret = wc_Rc2SetIV(&rc2, iv);
  14210. if (ret == 0) {
  14211. /* valid NULL IV */
  14212. ret = wc_Rc2SetIV(&rc2, NULL);
  14213. }
  14214. /* bad arguments */
  14215. if (ret == 0) {
  14216. ret = wc_Rc2SetIV(NULL, iv);
  14217. if (ret == BAD_FUNC_ARG) {
  14218. ret = 0;
  14219. }
  14220. }
  14221. printf(resultFmt, ret == 0 ? passed : failed);
  14222. #endif
  14223. return ret;
  14224. } /* END test_wc_Rc2SetKey */
  14225. /*
  14226. * Testing function for wc_Rc2EcbEncrypt().
  14227. */
  14228. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14229. {
  14230. int ret = 0;
  14231. #ifdef WC_RC2
  14232. Rc2 rc2;
  14233. int effectiveKeyBits = 63;
  14234. byte cipher[RC2_BLOCK_SIZE];
  14235. byte plain[RC2_BLOCK_SIZE];
  14236. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14237. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14238. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14239. printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
  14240. XMEMSET(cipher, 0, sizeof(cipher));
  14241. XMEMSET(plain, 0, sizeof(plain));
  14242. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14243. NULL, effectiveKeyBits);
  14244. if (ret == 0) {
  14245. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  14246. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  14247. ret = WOLFSSL_FATAL_ERROR;
  14248. }
  14249. if (ret == 0) {
  14250. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  14251. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  14252. ret = WOLFSSL_FATAL_ERROR;
  14253. }
  14254. }
  14255. }
  14256. /* Rc2EcbEncrypt bad arguments */
  14257. if (ret == 0) {
  14258. /* null Rc2 struct */
  14259. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  14260. if (ret == BAD_FUNC_ARG) {
  14261. ret = 0;
  14262. }
  14263. }
  14264. if (ret == 0) {
  14265. /* null out buffer */
  14266. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  14267. if (ret == BAD_FUNC_ARG) {
  14268. ret = 0;
  14269. }
  14270. }
  14271. if (ret == 0) {
  14272. /* null input buffer */
  14273. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  14274. if (ret == BAD_FUNC_ARG) {
  14275. ret = 0;
  14276. }
  14277. }
  14278. if (ret == 0) {
  14279. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14280. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  14281. if (ret == BUFFER_E) {
  14282. ret = 0;
  14283. }
  14284. }
  14285. /* Rc2EcbDecrypt bad arguments */
  14286. if (ret == 0) {
  14287. /* null Rc2 struct */
  14288. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  14289. if (ret == BAD_FUNC_ARG) {
  14290. ret = 0;
  14291. }
  14292. }
  14293. if (ret == 0) {
  14294. /* null out buffer */
  14295. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  14296. if (ret == BAD_FUNC_ARG) {
  14297. ret = 0;
  14298. }
  14299. }
  14300. if (ret == 0) {
  14301. /* null input buffer */
  14302. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  14303. if (ret == BAD_FUNC_ARG) {
  14304. ret = 0;
  14305. }
  14306. }
  14307. if (ret == 0) {
  14308. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14309. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  14310. if (ret == BUFFER_E) {
  14311. ret = 0;
  14312. }
  14313. }
  14314. printf(resultFmt, ret == 0 ? passed : failed);
  14315. #endif
  14316. return ret;
  14317. } /* END test_wc_Rc2SetKey */
  14318. /*
  14319. * Testing function for wc_Rc2CbcEncrypt().
  14320. */
  14321. static int test_wc_Rc2CbcEncryptDecrypt(void)
  14322. {
  14323. int ret = 0;
  14324. #ifdef WC_RC2
  14325. Rc2 rc2;
  14326. int effectiveKeyBits = 63;
  14327. byte cipher[RC2_BLOCK_SIZE*2];
  14328. byte plain[RC2_BLOCK_SIZE*2];
  14329. /* vector taken from test.c */
  14330. byte key[] = {
  14331. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14332. };
  14333. byte iv[] = {
  14334. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14335. };
  14336. byte input[] = {
  14337. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14338. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14339. };
  14340. byte output[] = {
  14341. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14342. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14343. };
  14344. printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
  14345. XMEMSET(cipher, 0, sizeof(cipher));
  14346. XMEMSET(plain, 0, sizeof(plain));
  14347. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14348. iv, effectiveKeyBits);
  14349. if (ret == 0) {
  14350. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  14351. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  14352. ret = WOLFSSL_FATAL_ERROR;
  14353. } else {
  14354. /* reset IV for decrypt */
  14355. ret = wc_Rc2SetIV(&rc2, iv);
  14356. }
  14357. if (ret == 0) {
  14358. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  14359. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  14360. ret = WOLFSSL_FATAL_ERROR;
  14361. }
  14362. }
  14363. }
  14364. /* Rc2CbcEncrypt bad arguments */
  14365. if (ret == 0) {
  14366. /* null Rc2 struct */
  14367. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  14368. if (ret == BAD_FUNC_ARG) {
  14369. ret = 0;
  14370. }
  14371. }
  14372. if (ret == 0) {
  14373. /* null out buffer */
  14374. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  14375. if (ret == BAD_FUNC_ARG) {
  14376. ret = 0;
  14377. }
  14378. }
  14379. if (ret == 0) {
  14380. /* null input buffer */
  14381. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  14382. if (ret == BAD_FUNC_ARG) {
  14383. ret = 0;
  14384. }
  14385. }
  14386. /* Rc2CbcDecrypt bad arguments */
  14387. if (ret == 0) {
  14388. /* in size is 0 */
  14389. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  14390. if (ret != 0) {
  14391. ret = WOLFSSL_FATAL_ERROR;
  14392. }
  14393. }
  14394. if (ret == 0) {
  14395. /* null Rc2 struct */
  14396. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  14397. if (ret == BAD_FUNC_ARG) {
  14398. ret = 0;
  14399. }
  14400. }
  14401. if (ret == 0) {
  14402. /* null out buffer */
  14403. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  14404. if (ret == BAD_FUNC_ARG) {
  14405. ret = 0;
  14406. }
  14407. }
  14408. if (ret == 0) {
  14409. /* null input buffer */
  14410. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  14411. if (ret == BAD_FUNC_ARG) {
  14412. ret = 0;
  14413. }
  14414. }
  14415. printf(resultFmt, ret == 0 ? passed : failed);
  14416. #endif
  14417. return ret;
  14418. } /* END test_wc_Rc2SetKey */
  14419. /*
  14420. * Testing function for wc_AesSetIV
  14421. */
  14422. static int test_wc_AesSetIV(void)
  14423. {
  14424. int ret = 0;
  14425. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14426. Aes aes;
  14427. byte key16[] =
  14428. {
  14429. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14430. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14431. };
  14432. byte iv1[] = "1234567890abcdef";
  14433. byte iv2[] = "0987654321fedcba";
  14434. printf(testingFmt, "wc_AesSetIV()");
  14435. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14436. if (ret != 0)
  14437. return ret;
  14438. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14439. iv1, AES_ENCRYPTION);
  14440. if(ret == 0) {
  14441. ret = wc_AesSetIV(&aes, iv2);
  14442. }
  14443. /* Test bad args. */
  14444. if(ret == 0) {
  14445. ret = wc_AesSetIV(NULL, iv1);
  14446. if(ret == BAD_FUNC_ARG) {
  14447. /* NULL iv should return 0. */
  14448. ret = wc_AesSetIV(&aes, NULL);
  14449. } else {
  14450. ret = WOLFSSL_FATAL_ERROR;
  14451. }
  14452. }
  14453. wc_AesFree(&aes);
  14454. printf(resultFmt, ret == 0 ? passed : failed);
  14455. #endif
  14456. return ret;
  14457. } /* test_wc_AesSetIV */
  14458. /*
  14459. * Testing function for wc_AesSetKey().
  14460. */
  14461. static int test_wc_AesSetKey(void)
  14462. {
  14463. int ret = 0;
  14464. #ifndef NO_AES
  14465. Aes aes;
  14466. byte key16[] =
  14467. {
  14468. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14469. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14470. };
  14471. #ifdef WOLFSSL_AES_192
  14472. byte key24[] =
  14473. {
  14474. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14475. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14476. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14477. };
  14478. #endif
  14479. #ifdef WOLFSSL_AES_256
  14480. byte key32[] =
  14481. {
  14482. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14483. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14484. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14485. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14486. };
  14487. #endif
  14488. byte badKey16[] =
  14489. {
  14490. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14491. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14492. };
  14493. byte iv[] = "1234567890abcdef";
  14494. printf(testingFmt, "wc_AesSetKey()");
  14495. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14496. if (ret != 0)
  14497. return ret;
  14498. #ifdef WOLFSSL_AES_128
  14499. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14500. iv, AES_ENCRYPTION);
  14501. #endif
  14502. #ifdef WOLFSSL_AES_192
  14503. if (ret == 0) {
  14504. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  14505. iv, AES_ENCRYPTION);
  14506. }
  14507. #endif
  14508. #ifdef WOLFSSL_AES_256
  14509. if (ret == 0) {
  14510. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  14511. iv, AES_ENCRYPTION);
  14512. }
  14513. #endif
  14514. /* Pass in bad args. */
  14515. if (ret == 0) {
  14516. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  14517. iv, AES_ENCRYPTION);
  14518. if (ret == BAD_FUNC_ARG) {
  14519. ret = wc_AesSetKey(&aes, badKey16,
  14520. (word32) sizeof(badKey16) / sizeof(byte),
  14521. iv, AES_ENCRYPTION);
  14522. }
  14523. if (ret == BAD_FUNC_ARG) {
  14524. ret = 0;
  14525. } else {
  14526. ret = WOLFSSL_FATAL_ERROR;
  14527. }
  14528. }
  14529. wc_AesFree(&aes);
  14530. printf(resultFmt, ret == 0 ? passed : failed);
  14531. #endif
  14532. return ret;
  14533. } /* END test_wc_AesSetKey */
  14534. /*
  14535. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  14536. * and wc_AesCbcDecryptWithKey()
  14537. */
  14538. static int test_wc_AesCbcEncryptDecrypt(void)
  14539. {
  14540. int ret = 0;
  14541. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  14542. defined(WOLFSSL_AES_256)
  14543. Aes aes;
  14544. byte key32[] =
  14545. {
  14546. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14547. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14548. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14549. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14550. };
  14551. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  14552. {
  14553. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14554. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14555. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  14556. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  14557. };
  14558. byte iv[] = "1234567890abcdef";
  14559. byte enc[sizeof(vector)];
  14560. byte dec[sizeof(vector)];
  14561. int cbcE = WOLFSSL_FATAL_ERROR;
  14562. int cbcD = WOLFSSL_FATAL_ERROR;
  14563. int cbcDWK = WOLFSSL_FATAL_ERROR;
  14564. byte dec2[sizeof(vector)];
  14565. /* Init stack variables. */
  14566. XMEMSET(enc, 0, sizeof(enc));
  14567. XMEMSET(dec, 0, sizeof(vector));
  14568. XMEMSET(dec2, 0, sizeof(vector));
  14569. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14570. if (ret != 0)
  14571. return ret;
  14572. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  14573. if (ret == 0) {
  14574. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  14575. if (ret == 0) {
  14576. /* Re init for decrypt and set flag. */
  14577. cbcE = 0;
  14578. wc_AesFree(&aes);
  14579. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  14580. iv, AES_DECRYPTION);
  14581. }
  14582. if (ret == 0) {
  14583. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  14584. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  14585. ret = WOLFSSL_FATAL_ERROR;
  14586. } else {
  14587. /* Set flag. */
  14588. cbcD = 0;
  14589. }
  14590. }
  14591. }
  14592. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  14593. if (ret == 0 || cbcE == 0) {
  14594. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14595. key32, sizeof(key32)/sizeof(byte), iv);
  14596. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  14597. cbcDWK = 0;
  14598. }
  14599. }
  14600. printf(testingFmt, "wc_AesCbcEncrypt()");
  14601. /* Pass in bad args */
  14602. if (cbcE == 0) {
  14603. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  14604. if (cbcE == BAD_FUNC_ARG) {
  14605. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  14606. }
  14607. if (cbcE == BAD_FUNC_ARG) {
  14608. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  14609. }
  14610. if (cbcE == BAD_FUNC_ARG) {
  14611. cbcE = 0;
  14612. } else {
  14613. cbcE = WOLFSSL_FATAL_ERROR;
  14614. }
  14615. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14616. if (cbcE == 0) {
  14617. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  14618. }
  14619. if (cbcE == BAD_LENGTH_E) {
  14620. cbcE = 0;
  14621. } else {
  14622. cbcE = WOLFSSL_FATAL_ERROR;
  14623. }
  14624. #endif
  14625. }
  14626. if (cbcE == 0) {
  14627. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14628. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  14629. printf("Zero length inputs not supported with AESNI in FIPS mode (v2),"
  14630. " skip test");
  14631. #else
  14632. /* Test passing in size of 0 */
  14633. XMEMSET(enc, 0, sizeof(enc));
  14634. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  14635. if (cbcE == 0) {
  14636. /* Check enc was not modified */
  14637. int i;
  14638. for (i = 0; i < (int)sizeof(enc); i++)
  14639. cbcE |= enc[i];
  14640. }
  14641. #endif
  14642. }
  14643. printf(resultFmt, cbcE == 0 ? passed : failed);
  14644. if (cbcE != 0) {
  14645. wc_AesFree(&aes);
  14646. return cbcE;
  14647. }
  14648. printf(testingFmt, "wc_AesCbcDecrypt()");
  14649. if (cbcD == 0) {
  14650. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  14651. if (cbcD == BAD_FUNC_ARG) {
  14652. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  14653. }
  14654. if (cbcD == BAD_FUNC_ARG) {
  14655. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  14656. }
  14657. if (cbcD == BAD_FUNC_ARG) {
  14658. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  14659. }
  14660. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  14661. if (cbcD == BAD_LENGTH_E) {
  14662. cbcD = 0;
  14663. } else {
  14664. cbcD = WOLFSSL_FATAL_ERROR;
  14665. }
  14666. #else
  14667. if (cbcD == BAD_FUNC_ARG) {
  14668. cbcD = 0;
  14669. } else {
  14670. cbcD = WOLFSSL_FATAL_ERROR;
  14671. }
  14672. #endif
  14673. }
  14674. if (cbcD == 0) {
  14675. /* Test passing in size of 0 */
  14676. XMEMSET(dec, 0, sizeof(dec));
  14677. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  14678. if (cbcD == 0) {
  14679. /* Check dec was not modified */
  14680. int i;
  14681. for (i = 0; i < (int)sizeof(dec); i++)
  14682. cbcD |= dec[i];
  14683. }
  14684. }
  14685. printf(resultFmt, cbcD == 0 ? passed : failed);
  14686. if (cbcD != 0) {
  14687. wc_AesFree(&aes);
  14688. return cbcD;
  14689. }
  14690. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  14691. if (cbcDWK == 0) {
  14692. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  14693. key32, sizeof(key32)/sizeof(byte), iv);
  14694. if (cbcDWK == BAD_FUNC_ARG) {
  14695. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  14696. key32, sizeof(key32)/sizeof(byte), iv);
  14697. }
  14698. if (cbcDWK == BAD_FUNC_ARG) {
  14699. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14700. NULL, sizeof(key32)/sizeof(byte), iv);
  14701. }
  14702. if (cbcDWK == BAD_FUNC_ARG) {
  14703. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  14704. key32, sizeof(key32)/sizeof(byte), NULL);
  14705. }
  14706. if (cbcDWK == BAD_FUNC_ARG) {
  14707. cbcDWK = 0;
  14708. } else {
  14709. cbcDWK = WOLFSSL_FATAL_ERROR;
  14710. }
  14711. }
  14712. wc_AesFree(&aes);
  14713. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  14714. if (cbcDWK != 0) {
  14715. return cbcDWK;
  14716. }
  14717. #endif
  14718. return ret;
  14719. } /* END test_wc_AesCbcEncryptDecrypt */
  14720. /*
  14721. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  14722. */
  14723. static int test_wc_AesCtrEncryptDecrypt(void)
  14724. {
  14725. int ret = 0;
  14726. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  14727. Aes aesEnc, aesDec;
  14728. byte key32[] =
  14729. {
  14730. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14731. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14732. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14733. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14734. };
  14735. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14736. {
  14737. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14738. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14739. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14740. };
  14741. byte iv[] = "1234567890abcdef";
  14742. byte enc[AES_BLOCK_SIZE * 2];
  14743. byte dec[AES_BLOCK_SIZE * 2];
  14744. /* Init stack variables. */
  14745. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  14746. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  14747. printf(testingFmt, "wc_AesCtrEncrypt()");
  14748. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  14749. if (ret != 0)
  14750. return ret;
  14751. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  14752. if (ret != 0) {
  14753. wc_AesFree(&aesEnc);
  14754. return ret;
  14755. }
  14756. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  14757. iv, AES_ENCRYPTION);
  14758. if (ret == 0) {
  14759. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  14760. sizeof(vector)/sizeof(byte));
  14761. if (ret == 0) {
  14762. /* Decrypt with wc_AesCtrEncrypt() */
  14763. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  14764. iv, AES_ENCRYPTION);
  14765. }
  14766. if (ret == 0) {
  14767. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  14768. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  14769. ret = WOLFSSL_FATAL_ERROR;
  14770. }
  14771. }
  14772. }
  14773. /* Test bad args. */
  14774. if (ret == 0) {
  14775. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  14776. if (ret == BAD_FUNC_ARG) {
  14777. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  14778. }
  14779. if (ret == BAD_FUNC_ARG) {
  14780. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  14781. }
  14782. if (ret == BAD_FUNC_ARG) {
  14783. ret = 0;
  14784. } else {
  14785. ret = WOLFSSL_FATAL_ERROR;
  14786. }
  14787. }
  14788. wc_AesFree(&aesEnc);
  14789. wc_AesFree(&aesDec);
  14790. printf(resultFmt, ret == 0 ? passed : failed);
  14791. #endif
  14792. return ret;
  14793. } /* END test_wc_AesCtrEncryptDecrypt */
  14794. /*
  14795. * test function for wc_AesGcmSetKey()
  14796. */
  14797. static int test_wc_AesGcmSetKey(void)
  14798. {
  14799. int ret = 0;
  14800. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  14801. Aes aes;
  14802. #ifdef WOLFSSL_AES_128
  14803. byte key16[] =
  14804. {
  14805. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14806. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14807. };
  14808. #endif
  14809. #ifdef WOLFSSL_AES_192
  14810. byte key24[] =
  14811. {
  14812. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14813. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14814. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14815. };
  14816. #endif
  14817. #ifdef WOLFSSL_AES_256
  14818. byte key32[] =
  14819. {
  14820. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14821. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14822. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14823. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14824. };
  14825. #endif
  14826. byte badKey16[] =
  14827. {
  14828. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14829. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14830. };
  14831. byte badKey24[] =
  14832. {
  14833. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14834. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14835. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  14836. };
  14837. byte badKey32[] =
  14838. {
  14839. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  14840. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14841. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14842. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14843. };
  14844. printf(testingFmt, "wc_AesGcmSetKey()");
  14845. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14846. if (ret != 0)
  14847. return ret;
  14848. #ifdef WOLFSSL_AES_128
  14849. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  14850. #endif
  14851. #ifdef WOLFSSL_AES_192
  14852. if (ret == 0) {
  14853. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  14854. }
  14855. #endif
  14856. #ifdef WOLFSSL_AES_256
  14857. if (ret == 0) {
  14858. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14859. }
  14860. #endif
  14861. /* Pass in bad args. */
  14862. if (ret == 0) {
  14863. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  14864. if (ret == BAD_FUNC_ARG) {
  14865. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  14866. }
  14867. if (ret == BAD_FUNC_ARG) {
  14868. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  14869. }
  14870. if (ret == BAD_FUNC_ARG) {
  14871. ret = 0;
  14872. } else {
  14873. ret = WOLFSSL_FATAL_ERROR;
  14874. }
  14875. }
  14876. wc_AesFree(&aes);
  14877. printf(resultFmt, ret == 0 ? passed : failed);
  14878. #endif
  14879. return ret;
  14880. } /* END test_wc_AesGcmSetKey */
  14881. /*
  14882. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  14883. */
  14884. static int test_wc_AesGcmEncryptDecrypt(void)
  14885. {
  14886. int ret = 0;
  14887. /* WOLFSSL_AFALG requires 12 byte IV */
  14888. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  14889. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  14890. Aes aes;
  14891. byte key32[] =
  14892. {
  14893. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14894. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14895. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14896. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14897. };
  14898. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14899. {
  14900. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14901. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14902. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14903. };
  14904. const byte a[] =
  14905. {
  14906. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14907. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  14908. 0xab, 0xad, 0xda, 0xd2
  14909. };
  14910. byte iv[] = "1234567890a";
  14911. byte longIV[] = "1234567890abcdefghij";
  14912. byte enc[sizeof(vector)];
  14913. byte resultT[AES_BLOCK_SIZE];
  14914. byte dec[sizeof(vector)];
  14915. int gcmD = WOLFSSL_FATAL_ERROR;
  14916. int gcmE = WOLFSSL_FATAL_ERROR;
  14917. /* Init stack variables. */
  14918. XMEMSET(enc, 0, sizeof(vector));
  14919. XMEMSET(dec, 0, sizeof(vector));
  14920. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  14921. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14922. if (ret != 0)
  14923. return ret;
  14924. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  14925. if (ret == 0) {
  14926. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  14927. iv, sizeof(iv)/sizeof(byte), resultT,
  14928. sizeof(resultT), a, sizeof(a));
  14929. }
  14930. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  14931. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  14932. iv, sizeof(iv)/sizeof(byte), resultT,
  14933. sizeof(resultT), a, sizeof(a));
  14934. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  14935. gcmD = WOLFSSL_FATAL_ERROR;
  14936. }
  14937. }
  14938. printf(testingFmt, "wc_AesGcmEncrypt()");
  14939. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  14940. if (gcmE == 0) {
  14941. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  14942. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  14943. a, sizeof(a));
  14944. if (gcmE == BAD_FUNC_ARG) {
  14945. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14946. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14947. resultT, sizeof(resultT) + 1, a, sizeof(a));
  14948. }
  14949. if (gcmE == BAD_FUNC_ARG) {
  14950. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14951. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  14952. resultT, sizeof(resultT) - 5, a, sizeof(a));
  14953. }
  14954. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  14955. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  14956. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14957. /* FIPS does not check the lower bound of ivSz */
  14958. #else
  14959. if (gcmE == BAD_FUNC_ARG) {
  14960. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  14961. sizeof(vector), iv, 0,
  14962. resultT, sizeof(resultT), a, sizeof(a));
  14963. }
  14964. #endif
  14965. if (gcmE == BAD_FUNC_ARG) {
  14966. gcmE = 0;
  14967. } else {
  14968. gcmE = WOLFSSL_FATAL_ERROR;
  14969. }
  14970. }
  14971. /* This case is now considered good. Long IVs are now allowed.
  14972. * Except for the original FIPS release, it still has an upper
  14973. * bound on the IV length. */
  14974. #if (!defined(HAVE_FIPS) || \
  14975. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  14976. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  14977. if (gcmE == 0) {
  14978. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  14979. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  14980. a, sizeof(a));
  14981. }
  14982. #else
  14983. (void)longIV;
  14984. #endif /* Old FIPS */
  14985. /* END wc_AesGcmEncrypt */
  14986. printf(resultFmt, gcmE == 0 ? passed : failed);
  14987. if (gcmE != 0) {
  14988. wc_AesFree(&aes);
  14989. return gcmE;
  14990. }
  14991. #ifdef HAVE_AES_DECRYPT
  14992. printf(testingFmt, "wc_AesGcmDecrypt()");
  14993. if (gcmD == 0) {
  14994. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  14995. iv, sizeof(iv)/sizeof(byte), resultT,
  14996. sizeof(resultT), a, sizeof(a));
  14997. if (gcmD == BAD_FUNC_ARG) {
  14998. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  14999. iv, sizeof(iv)/sizeof(byte), resultT,
  15000. sizeof(resultT), a, sizeof(a));
  15001. }
  15002. if (gcmD == BAD_FUNC_ARG) {
  15003. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  15004. iv, sizeof(iv)/sizeof(byte), resultT,
  15005. sizeof(resultT), a, sizeof(a));
  15006. }
  15007. if (gcmD == BAD_FUNC_ARG) {
  15008. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15009. NULL, sizeof(iv)/sizeof(byte), resultT,
  15010. sizeof(resultT), a, sizeof(a));
  15011. }
  15012. if (gcmD == BAD_FUNC_ARG) {
  15013. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15014. iv, sizeof(iv)/sizeof(byte), NULL,
  15015. sizeof(resultT), a, sizeof(a));
  15016. }
  15017. if (gcmD == BAD_FUNC_ARG) {
  15018. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15019. iv, sizeof(iv)/sizeof(byte), resultT,
  15020. sizeof(resultT) + 1, a, sizeof(a));
  15021. }
  15022. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15023. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15024. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15025. /* FIPS does not check the lower bound of ivSz */
  15026. #else
  15027. if (gcmD == BAD_FUNC_ARG) {
  15028. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15029. iv, 0, resultT,
  15030. sizeof(resultT), a, sizeof(a));
  15031. }
  15032. #endif
  15033. if (gcmD == BAD_FUNC_ARG) {
  15034. gcmD = 0;
  15035. } else {
  15036. gcmD = WOLFSSL_FATAL_ERROR;
  15037. }
  15038. } /* END wc_AesGcmDecrypt */
  15039. printf(resultFmt, gcmD == 0 ? passed : failed);
  15040. #endif /* HAVE_AES_DECRYPT */
  15041. wc_AesFree(&aes);
  15042. #endif
  15043. return ret;
  15044. } /* END test_wc_AesGcmEncryptDecrypt */
  15045. /*
  15046. * unit test for wc_GmacSetKey()
  15047. */
  15048. static int test_wc_GmacSetKey(void)
  15049. {
  15050. int ret = 0;
  15051. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15052. Gmac gmac;
  15053. byte key16[] =
  15054. {
  15055. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15056. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15057. };
  15058. #ifdef WOLFSSL_AES_192
  15059. byte key24[] =
  15060. {
  15061. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15062. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15063. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15064. };
  15065. #endif
  15066. #ifdef WOLFSSL_AES_256
  15067. byte key32[] =
  15068. {
  15069. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15070. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15071. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15072. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15073. };
  15074. #endif
  15075. byte badKey16[] =
  15076. {
  15077. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15078. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15079. };
  15080. byte badKey24[] =
  15081. {
  15082. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15083. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15084. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15085. };
  15086. byte badKey32[] =
  15087. {
  15088. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15089. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15090. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15091. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15092. };
  15093. printf(testingFmt, "wc_GmacSetKey()");
  15094. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15095. if (ret != 0)
  15096. return ret;
  15097. #ifdef WOLFSSL_AES_128
  15098. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  15099. #endif
  15100. #ifdef WOLFSSL_AES_192
  15101. if (ret == 0) {
  15102. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15103. }
  15104. #endif
  15105. #ifdef WOLFSSL_AES_256
  15106. if (ret == 0) {
  15107. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15108. }
  15109. #endif
  15110. /* Pass in bad args. */
  15111. if (ret == 0) {
  15112. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  15113. if (ret == BAD_FUNC_ARG) {
  15114. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  15115. }
  15116. if (ret == BAD_FUNC_ARG) {
  15117. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  15118. }
  15119. if (ret == BAD_FUNC_ARG) {
  15120. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  15121. }
  15122. if (ret == BAD_FUNC_ARG) {
  15123. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  15124. }
  15125. if (ret == BAD_FUNC_ARG) {
  15126. ret = 0;
  15127. } else {
  15128. ret = WOLFSSL_FATAL_ERROR;
  15129. }
  15130. }
  15131. wc_AesFree(&gmac.aes);
  15132. printf(resultFmt, ret == 0 ? passed : failed);
  15133. #endif
  15134. return ret;
  15135. } /* END test_wc_GmacSetKey */
  15136. /*
  15137. * unit test for wc_GmacUpdate
  15138. */
  15139. static int test_wc_GmacUpdate(void)
  15140. {
  15141. int ret = 0;
  15142. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15143. Gmac gmac;
  15144. #ifdef WOLFSSL_AES_128
  15145. const byte key16[] =
  15146. {
  15147. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15148. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15149. };
  15150. #endif
  15151. #ifdef WOLFSSL_AES_192
  15152. byte key24[] =
  15153. {
  15154. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15155. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15156. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15157. };
  15158. #endif
  15159. #ifdef WOLFSSL_AES_256
  15160. byte key32[] =
  15161. {
  15162. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15163. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15164. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15165. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15166. };
  15167. #endif
  15168. #ifdef WOLFSSL_AES_128
  15169. const byte authIn[] =
  15170. {
  15171. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15172. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15173. };
  15174. #endif
  15175. #ifdef WOLFSSL_AES_192
  15176. const byte authIn2[] =
  15177. {
  15178. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15179. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15180. };
  15181. #endif
  15182. const byte authIn3[] =
  15183. {
  15184. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15185. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15186. };
  15187. #ifdef WOLFSSL_AES_128
  15188. const byte tag1[] = /* Known. */
  15189. {
  15190. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15191. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15192. };
  15193. #endif
  15194. #ifdef WOLFSSL_AES_192
  15195. const byte tag2[] = /* Known */
  15196. {
  15197. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15198. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15199. };
  15200. #endif
  15201. const byte tag3[] = /* Known */
  15202. {
  15203. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15204. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15205. };
  15206. #ifdef WOLFSSL_AES_128
  15207. const byte iv[] =
  15208. {
  15209. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15210. 0xe2, 0x8c, 0x8f, 0x16
  15211. };
  15212. #endif
  15213. #ifdef WOLFSSL_AES_192
  15214. const byte iv2[] =
  15215. {
  15216. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15217. 0x7e, 0x1a, 0x6f, 0xbc
  15218. };
  15219. #endif
  15220. const byte iv3[] =
  15221. {
  15222. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15223. 0xc3, 0xfb, 0x6c, 0x8a
  15224. };
  15225. byte tagOut[16];
  15226. byte tagOut2[24];
  15227. byte tagOut3[32];
  15228. /* Init stack variables. */
  15229. XMEMSET(tagOut, 0, sizeof(tagOut));
  15230. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15231. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15232. printf(testingFmt, "wc_GmacUpdate()");
  15233. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15234. if (ret != 0)
  15235. return ret;
  15236. #ifdef WOLFSSL_AES_128
  15237. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  15238. if (ret == 0) {
  15239. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15240. tagOut, sizeof(tag1));
  15241. if (ret == 0) {
  15242. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  15243. }
  15244. wc_AesFree(&gmac.aes);
  15245. }
  15246. #endif
  15247. #ifdef WOLFSSL_AES_192
  15248. if (ret == 0) {
  15249. XMEMSET(&gmac, 0, sizeof(Gmac));
  15250. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15251. }
  15252. if (ret == 0) {
  15253. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  15254. sizeof(authIn2), tagOut2, sizeof(tag2));
  15255. }
  15256. if (ret == 0) {
  15257. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  15258. wc_AesFree(&gmac.aes);
  15259. }
  15260. #endif
  15261. #ifdef WOLFSSL_AES_256
  15262. if (ret == 0) {
  15263. XMEMSET(&gmac, 0, sizeof(Gmac));
  15264. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15265. }
  15266. if (ret == 0) {
  15267. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15268. sizeof(authIn3), tagOut3, sizeof(tag3));
  15269. }
  15270. if (ret == 0) {
  15271. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  15272. }
  15273. #endif
  15274. /*Pass bad args. */
  15275. if (ret == 0) {
  15276. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  15277. sizeof(authIn3), tagOut3, sizeof(tag3));
  15278. if (ret == BAD_FUNC_ARG) {
  15279. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15280. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  15281. }
  15282. if (ret == BAD_FUNC_ARG) {
  15283. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15284. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  15285. }
  15286. if (ret == BAD_FUNC_ARG) {
  15287. ret = 0;
  15288. } else {
  15289. ret = WOLFSSL_FATAL_ERROR;
  15290. }
  15291. }
  15292. wc_AesFree(&gmac.aes);
  15293. printf(resultFmt, ret == 0 ? passed : failed);
  15294. #endif
  15295. return ret;
  15296. } /* END test_wc_GmacUpdate */
  15297. /*
  15298. * testing wc_CamelliaSetKey
  15299. */
  15300. static int test_wc_CamelliaSetKey(void)
  15301. {
  15302. int ret = 0;
  15303. #ifdef HAVE_CAMELLIA
  15304. Camellia camellia;
  15305. /*128-bit key*/
  15306. static const byte key16[] =
  15307. {
  15308. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15309. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15310. };
  15311. /* 192-bit key */
  15312. static const byte key24[] =
  15313. {
  15314. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15315. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15316. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15317. };
  15318. /* 256-bit key */
  15319. static const byte key32[] =
  15320. {
  15321. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15322. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15323. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15324. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15325. };
  15326. static const byte iv[] =
  15327. {
  15328. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15329. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15330. };
  15331. printf(testingFmt, "wc_CamelliaSetKey()");
  15332. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  15333. if (ret == 0) {
  15334. ret = wc_CamelliaSetKey(&camellia, key16,
  15335. (word32)sizeof(key16), NULL);
  15336. if (ret == 0) {
  15337. ret = wc_CamelliaSetKey(&camellia, key24,
  15338. (word32)sizeof(key24), iv);
  15339. }
  15340. if (ret == 0) {
  15341. ret = wc_CamelliaSetKey(&camellia, key24,
  15342. (word32)sizeof(key24), NULL);
  15343. }
  15344. if (ret == 0) {
  15345. ret = wc_CamelliaSetKey(&camellia, key32,
  15346. (word32)sizeof(key32), iv);
  15347. }
  15348. if (ret == 0) {
  15349. ret = wc_CamelliaSetKey(&camellia, key32,
  15350. (word32)sizeof(key32), NULL);
  15351. }
  15352. }
  15353. /* Bad args. */
  15354. if (ret == 0) {
  15355. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  15356. if (ret != BAD_FUNC_ARG) {
  15357. ret = WOLFSSL_FATAL_ERROR;
  15358. } else {
  15359. ret = 0;
  15360. }
  15361. } /* END bad args. */
  15362. #endif
  15363. return ret;
  15364. } /* END test_wc_CammeliaSetKey */
  15365. /*
  15366. * Testing wc_CamelliaSetIV()
  15367. */
  15368. static int test_wc_CamelliaSetIV(void)
  15369. {
  15370. int ret = 0;
  15371. #ifdef HAVE_CAMELLIA
  15372. Camellia camellia;
  15373. static const byte iv[] =
  15374. {
  15375. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15376. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15377. };
  15378. printf(testingFmt, "wc_CamelliaSetIV()");
  15379. ret = wc_CamelliaSetIV(&camellia, iv);
  15380. if (ret == 0) {
  15381. ret = wc_CamelliaSetIV(&camellia, NULL);
  15382. }
  15383. /* Bad args. */
  15384. if (ret == 0) {
  15385. ret = wc_CamelliaSetIV(NULL, NULL);
  15386. if (ret != BAD_FUNC_ARG) {
  15387. ret = WOLFSSL_FATAL_ERROR;
  15388. } else {
  15389. ret = 0;
  15390. }
  15391. }
  15392. printf(resultFmt, ret == 0 ? passed : failed);
  15393. #endif
  15394. return ret;
  15395. } /*END test_wc_CamelliaSetIV*/
  15396. /*
  15397. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15398. */
  15399. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15400. {
  15401. int ret = 0;
  15402. #ifdef HAVE_CAMELLIA
  15403. Camellia camellia;
  15404. static const byte key24[] =
  15405. {
  15406. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15407. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15408. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15409. };
  15410. static const byte iv[] =
  15411. {
  15412. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15413. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15414. };
  15415. static const byte plainT[] =
  15416. {
  15417. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15418. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15419. };
  15420. byte enc[sizeof(plainT)];
  15421. byte dec[sizeof(enc)];
  15422. int camE = WOLFSSL_FATAL_ERROR;
  15423. int camD = WOLFSSL_FATAL_ERROR;
  15424. /*Init stack variables.*/
  15425. XMEMSET(enc, 0, 16);
  15426. XMEMSET(enc, 0, 16);
  15427. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  15428. if (ret == 0) {
  15429. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  15430. if (ret == 0) {
  15431. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  15432. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15433. ret = WOLFSSL_FATAL_ERROR;
  15434. }
  15435. }
  15436. }
  15437. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  15438. /* Pass bad args. */
  15439. if (ret == 0) {
  15440. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  15441. if (camE == BAD_FUNC_ARG) {
  15442. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  15443. }
  15444. if (camE == BAD_FUNC_ARG) {
  15445. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  15446. }
  15447. if (camE == BAD_FUNC_ARG) {
  15448. camE = 0;
  15449. } else {
  15450. camE = WOLFSSL_FATAL_ERROR;
  15451. }
  15452. }
  15453. printf(resultFmt, camE == 0 ? passed : failed);
  15454. if (camE != 0) {
  15455. return camE;
  15456. }
  15457. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  15458. if (ret == 0) {
  15459. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  15460. if (camD == BAD_FUNC_ARG) {
  15461. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  15462. }
  15463. if (camD == BAD_FUNC_ARG) {
  15464. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  15465. }
  15466. if (camD == BAD_FUNC_ARG) {
  15467. camD = 0;
  15468. } else {
  15469. camD = WOLFSSL_FATAL_ERROR;
  15470. }
  15471. }
  15472. printf(resultFmt, camD == 0 ? passed : failed);
  15473. if (camD != 0) {
  15474. return camD;
  15475. }
  15476. #endif
  15477. return ret;
  15478. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15479. /*
  15480. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15481. */
  15482. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15483. {
  15484. int ret = 0;
  15485. #ifdef HAVE_CAMELLIA
  15486. Camellia camellia;
  15487. static const byte key24[] =
  15488. {
  15489. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15490. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15491. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15492. };
  15493. static const byte plainT[] =
  15494. {
  15495. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15496. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15497. };
  15498. byte enc[CAMELLIA_BLOCK_SIZE];
  15499. byte dec[CAMELLIA_BLOCK_SIZE];
  15500. int camCbcE = WOLFSSL_FATAL_ERROR;
  15501. int camCbcD = WOLFSSL_FATAL_ERROR;
  15502. /* Init stack variables. */
  15503. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15504. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15505. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15506. if (ret == 0) {
  15507. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15508. if (ret != 0) {
  15509. ret = WOLFSSL_FATAL_ERROR;
  15510. }
  15511. }
  15512. if (ret == 0) {
  15513. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15514. if (ret == 0) {
  15515. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  15516. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15517. ret = WOLFSSL_FATAL_ERROR;
  15518. }
  15519. }
  15520. }
  15521. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  15522. /* Pass in bad args. */
  15523. if (ret == 0) {
  15524. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15525. if (camCbcE == BAD_FUNC_ARG) {
  15526. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15527. CAMELLIA_BLOCK_SIZE);
  15528. }
  15529. if (camCbcE == BAD_FUNC_ARG) {
  15530. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15531. CAMELLIA_BLOCK_SIZE);
  15532. }
  15533. if (camCbcE == BAD_FUNC_ARG) {
  15534. camCbcE = 0;
  15535. } else {
  15536. camCbcE = WOLFSSL_FATAL_ERROR;
  15537. }
  15538. }
  15539. printf(resultFmt, camCbcE == 0 ? passed : failed);
  15540. if (camCbcE != 0) {
  15541. return camCbcE;
  15542. }
  15543. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  15544. if (ret == 0) {
  15545. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  15546. if (camCbcD == BAD_FUNC_ARG) {
  15547. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15548. CAMELLIA_BLOCK_SIZE);
  15549. }
  15550. if (camCbcD == BAD_FUNC_ARG) {
  15551. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15552. CAMELLIA_BLOCK_SIZE);
  15553. }
  15554. if (camCbcD == BAD_FUNC_ARG) {
  15555. camCbcD = 0;
  15556. } else {
  15557. camCbcD = WOLFSSL_FATAL_ERROR;
  15558. }
  15559. } /* END bad args. */
  15560. printf(resultFmt, camCbcD == 0 ? passed : failed);
  15561. if (camCbcD != 0) {
  15562. return camCbcD;
  15563. }
  15564. #endif
  15565. return ret;
  15566. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  15567. /*
  15568. * Testing wc_Arc4SetKey()
  15569. */
  15570. static int test_wc_Arc4SetKey(void)
  15571. {
  15572. int ret = 0;
  15573. #ifndef NO_RC4
  15574. Arc4 arc;
  15575. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15576. int keyLen = 8;
  15577. printf(testingFmt, "wc_Arch4SetKey()");
  15578. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  15579. /* Test bad args. */
  15580. if (ret == 0) {
  15581. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  15582. if (ret == BAD_FUNC_ARG)
  15583. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  15584. if (ret == BAD_FUNC_ARG)
  15585. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  15586. if (ret == BAD_FUNC_ARG)
  15587. ret = WOLFSSL_ERROR_NONE;
  15588. else
  15589. ret = WOLFSSL_FATAL_ERROR;
  15590. } /* END test bad args. */
  15591. printf(resultFmt, ret == 0 ? passed : failed);
  15592. #endif
  15593. return ret;
  15594. } /* END test_wc_Arc4SetKey */
  15595. /*
  15596. * Testing wc_Arc4Process for ENC/DEC.
  15597. */
  15598. static int test_wc_Arc4Process(void)
  15599. {
  15600. int ret = 0;
  15601. #ifndef NO_RC4
  15602. Arc4 enc, dec;
  15603. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15604. int keyLen = 8;
  15605. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  15606. byte cipher[8];
  15607. byte plain[8];
  15608. /* Init stack variables */
  15609. XMEMSET(cipher, 0, sizeof(cipher));
  15610. XMEMSET(plain, 0, sizeof(plain));
  15611. /* Use for async. */
  15612. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  15613. if (ret == 0) {
  15614. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  15615. }
  15616. printf(testingFmt, "wc_Arc4Process()");
  15617. if (ret == 0) {
  15618. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  15619. }
  15620. if (ret == 0) {
  15621. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  15622. }
  15623. if (ret == 0) {
  15624. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  15625. }
  15626. if (ret == 0) {
  15627. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  15628. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  15629. ret = WOLFSSL_FATAL_ERROR;
  15630. } else {
  15631. ret = 0;
  15632. }
  15633. }
  15634. /* Bad args. */
  15635. if (ret == 0) {
  15636. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  15637. if (ret == BAD_FUNC_ARG) {
  15638. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  15639. }
  15640. if (ret == BAD_FUNC_ARG) {
  15641. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  15642. }
  15643. if (ret == BAD_FUNC_ARG) {
  15644. ret = 0;
  15645. } else {
  15646. ret = WOLFSSL_FATAL_ERROR;
  15647. }
  15648. }
  15649. printf(resultFmt, ret == 0 ? passed : failed);
  15650. wc_Arc4Free(&enc);
  15651. wc_Arc4Free(&dec);
  15652. #endif
  15653. return ret;
  15654. }/* END test_wc_Arc4Process */
  15655. /*
  15656. * Testing wc_Init RsaKey()
  15657. */
  15658. static int test_wc_InitRsaKey(void)
  15659. {
  15660. int ret = 0;
  15661. #ifndef NO_RSA
  15662. RsaKey key;
  15663. printf(testingFmt, "wc_InitRsaKey()");
  15664. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15665. /* Test bad args. */
  15666. if (ret == 0) {
  15667. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  15668. #ifndef HAVE_USER_RSA
  15669. if (ret == BAD_FUNC_ARG) {
  15670. ret = 0;
  15671. } else {
  15672. #else
  15673. if (ret == USER_CRYPTO_ERROR) {
  15674. ret = 0;
  15675. } else {
  15676. #endif
  15677. ret = WOLFSSL_FATAL_ERROR;
  15678. }
  15679. } /* end if */
  15680. if (wc_FreeRsaKey(&key) || ret != 0) {
  15681. ret = WOLFSSL_FATAL_ERROR;
  15682. }
  15683. printf(resultFmt, ret == 0 ? passed : failed);
  15684. #endif
  15685. return ret;
  15686. } /* END test_wc_InitRsaKey */
  15687. /*
  15688. * Testing wc_RsaPrivateKeyDecode()
  15689. */
  15690. static int test_wc_RsaPrivateKeyDecode(void)
  15691. {
  15692. int ret = 0;
  15693. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15694. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15695. RsaKey key;
  15696. byte* tmp;
  15697. word32 idx = 0;
  15698. int bytes = 0;
  15699. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  15700. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15701. if (tmp == NULL) {
  15702. ret = WOLFSSL_FATAL_ERROR;
  15703. }
  15704. if (ret == 0) {
  15705. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15706. }
  15707. if (ret == 0) {
  15708. #ifdef USE_CERT_BUFFERS_1024
  15709. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  15710. bytes = sizeof_client_key_der_1024;
  15711. #else
  15712. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  15713. bytes = sizeof_client_key_der_2048;
  15714. #endif /* Use cert buffers. */
  15715. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  15716. }
  15717. #ifndef HAVE_USER_RSA
  15718. /* Test bad args. */
  15719. if (ret == 0) {
  15720. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15721. if (ret == BAD_FUNC_ARG) {
  15722. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15723. }
  15724. if (ret == BAD_FUNC_ARG) {
  15725. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15726. }
  15727. if (ret == BAD_FUNC_ARG) {
  15728. ret = 0;
  15729. } else {
  15730. ret = WOLFSSL_FATAL_ERROR;
  15731. }
  15732. }
  15733. #else
  15734. /* Test bad args. User RSA. */
  15735. if (ret == 0) {
  15736. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  15737. if (ret == USER_CRYPTO_ERROR) {
  15738. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  15739. }
  15740. if (ret == USER_CRYPTO_ERROR) {
  15741. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15742. }
  15743. if (ret == USER_CRYPTO_ERROR) {
  15744. ret = 0;
  15745. } else {
  15746. ret = WOLFSSL_FATAL_ERROR;
  15747. }
  15748. }
  15749. #endif
  15750. if (tmp != NULL) {
  15751. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15752. }
  15753. if (wc_FreeRsaKey(&key) || ret != 0) {
  15754. ret = WOLFSSL_FATAL_ERROR;
  15755. }
  15756. printf(resultFmt, ret == 0 ? passed : failed);
  15757. #endif
  15758. return ret;
  15759. } /* END test_wc_RsaPrivateKeyDecode */
  15760. /*
  15761. * Testing wc_RsaPublicKeyDecode()
  15762. */
  15763. static int test_wc_RsaPublicKeyDecode(void)
  15764. {
  15765. int ret = 0;
  15766. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  15767. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  15768. RsaKey keyPub;
  15769. byte* tmp;
  15770. word32 idx = 0;
  15771. int bytes = 0;
  15772. word32 keySz = 0;
  15773. word32 tstKeySz = 0;
  15774. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15775. XFILE f;
  15776. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  15777. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  15778. byte buf[4096];
  15779. #endif
  15780. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15781. if (tmp == NULL) {
  15782. ret = WOLFSSL_FATAL_ERROR;
  15783. }
  15784. if (ret == 0) {
  15785. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  15786. }
  15787. if (ret == 0) {
  15788. #ifdef USE_CERT_BUFFERS_1024
  15789. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  15790. bytes = sizeof_client_keypub_der_1024;
  15791. keySz = 1024;
  15792. #else
  15793. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  15794. bytes = sizeof_client_keypub_der_2048;
  15795. keySz = 2048;
  15796. #endif
  15797. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  15798. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  15799. }
  15800. #ifndef HAVE_USER_RSA
  15801. /* Pass in bad args. */
  15802. if (ret == 0) {
  15803. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15804. if (ret == BAD_FUNC_ARG) {
  15805. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15806. }
  15807. if (ret == BAD_FUNC_ARG) {
  15808. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15809. }
  15810. if (ret == BAD_FUNC_ARG) {
  15811. ret = 0;
  15812. } else {
  15813. ret = WOLFSSL_FATAL_ERROR;
  15814. }
  15815. }
  15816. #else
  15817. /* Pass in bad args. */
  15818. if (ret == 0) {
  15819. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  15820. if (ret == USER_CRYPTO_ERROR) {
  15821. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  15822. }
  15823. if (ret == USER_CRYPTO_ERROR) {
  15824. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  15825. }
  15826. if (ret == USER_CRYPTO_ERROR) {
  15827. ret = 0;
  15828. } else {
  15829. ret = WOLFSSL_FATAL_ERROR;
  15830. }
  15831. }
  15832. #endif
  15833. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  15834. ret = WOLFSSL_FATAL_ERROR;
  15835. }
  15836. if (ret == 0) {
  15837. /* Test for getting modulus key size */
  15838. idx = 0;
  15839. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  15840. &tstKeySz, NULL, NULL);
  15841. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  15842. }
  15843. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  15844. f = XFOPEN(rsaPssPubKey, "rb");
  15845. AssertTrue((f != XBADFILE));
  15846. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  15847. XFCLOSE(f);
  15848. idx = 0;
  15849. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15850. NULL), 0);
  15851. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  15852. AssertTrue((f != XBADFILE));
  15853. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  15854. XFCLOSE(f);
  15855. idx = 0;
  15856. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  15857. NULL), 0);
  15858. #endif
  15859. if (tmp != NULL) {
  15860. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  15861. }
  15862. printf(resultFmt, ret == 0 ? passed : failed);
  15863. #endif
  15864. return ret;
  15865. } /* END test_wc_RsaPublicKeyDecode */
  15866. /*
  15867. * Testing wc_RsaPublicKeyDecodeRaw()
  15868. */
  15869. static int test_wc_RsaPublicKeyDecodeRaw(void)
  15870. {
  15871. int ret = 0;
  15872. #if !defined(NO_RSA)
  15873. RsaKey key;
  15874. const byte n = 0x23;
  15875. const byte e = 0x03;
  15876. int nSz = sizeof(n);
  15877. int eSz = sizeof(e);
  15878. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  15879. ret = wc_InitRsaKey(&key, HEAP_HINT);
  15880. if (ret == 0) {
  15881. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  15882. }
  15883. #ifndef HAVE_USER_RSA
  15884. /* Pass in bad args. */
  15885. if (ret == 0) {
  15886. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15887. if (ret == BAD_FUNC_ARG) {
  15888. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15889. }
  15890. if (ret == BAD_FUNC_ARG) {
  15891. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15892. }
  15893. if (ret == BAD_FUNC_ARG) {
  15894. ret = 0;
  15895. } else {
  15896. ret = WOLFSSL_FATAL_ERROR;
  15897. }
  15898. }
  15899. #else
  15900. /* Pass in bad args. User RSA. */
  15901. if (ret == 0) {
  15902. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  15903. if (ret == USER_CRYPTO_ERROR) {
  15904. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  15905. }
  15906. if (ret == USER_CRYPTO_ERROR) {
  15907. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  15908. }
  15909. if (ret == USER_CRYPTO_ERROR) {
  15910. ret = 0;
  15911. } else {
  15912. ret = WOLFSSL_FATAL_ERROR;
  15913. }
  15914. }
  15915. #endif
  15916. if (wc_FreeRsaKey(&key) || ret != 0) {
  15917. ret = WOLFSSL_FATAL_ERROR;
  15918. }
  15919. printf(resultFmt, ret == 0 ? passed : failed);
  15920. #endif
  15921. return ret;
  15922. } /* END test_wc_RsaPublicKeyDecodeRaw */
  15923. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  15924. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  15925. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  15926. * trying until it gets a probable prime. */
  15927. #ifdef HAVE_FIPS
  15928. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  15929. {
  15930. int ret;
  15931. for (;;) {
  15932. ret = wc_MakeRsaKey(key, size, e, rng);
  15933. if (ret != PRIME_GEN_E) break;
  15934. printf("MakeRsaKey couldn't find prime; trying again.\n");
  15935. }
  15936. return ret;
  15937. }
  15938. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  15939. #else
  15940. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  15941. #endif
  15942. #endif
  15943. /*
  15944. * Testing wc_MakeRsaKey()
  15945. */
  15946. static int test_wc_MakeRsaKey(void)
  15947. {
  15948. int ret = 0;
  15949. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  15950. RsaKey genKey;
  15951. WC_RNG rng;
  15952. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  15953. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  15954. int bits = 1024;
  15955. #else
  15956. int bits = 2048;
  15957. #endif
  15958. printf(testingFmt, "wc_MakeRsaKey()");
  15959. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  15960. if (ret == 0) {
  15961. ret = wc_InitRng(&rng);
  15962. if (ret == 0) {
  15963. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  15964. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  15965. ret = WOLFSSL_FATAL_ERROR;
  15966. }
  15967. }
  15968. }
  15969. #ifndef HAVE_USER_RSA
  15970. /* Test bad args. */
  15971. if (ret == 0) {
  15972. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15973. if (ret == BAD_FUNC_ARG) {
  15974. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15975. }
  15976. if (ret == BAD_FUNC_ARG) {
  15977. /* e < 3 */
  15978. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  15979. }
  15980. if (ret == BAD_FUNC_ARG) {
  15981. /* e & 1 == 0 */
  15982. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  15983. }
  15984. if (ret == BAD_FUNC_ARG) {
  15985. ret = 0;
  15986. } else {
  15987. ret = WOLFSSL_FATAL_ERROR;
  15988. }
  15989. }
  15990. #else
  15991. /* Test bad args. */
  15992. if (ret == 0) {
  15993. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  15994. if (ret == USER_CRYPTO_ERROR) {
  15995. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  15996. }
  15997. if (ret == USER_CRYPTO_ERROR) {
  15998. /* e < 3 */
  15999. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16000. }
  16001. if (ret == USER_CRYPTO_ERROR) {
  16002. /* e & 1 == 0 */
  16003. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16004. }
  16005. if (ret == USER_CRYPTO_ERROR) {
  16006. ret = 0;
  16007. } else {
  16008. ret = WOLFSSL_FATAL_ERROR;
  16009. }
  16010. }
  16011. #endif
  16012. if (wc_FreeRng(&rng) || ret != 0) {
  16013. ret = WOLFSSL_FATAL_ERROR;
  16014. }
  16015. printf(resultFmt, ret == 0 ? passed : failed);
  16016. #endif
  16017. return ret;
  16018. } /* END test_wc_MakeRsaKey */
  16019. /*
  16020. * Test the bounds checking on the cipher text versus the key modulus.
  16021. * 1. Make a new RSA key.
  16022. * 2. Set c to 1.
  16023. * 3. Decrypt c into k. (error)
  16024. * 4. Copy the key modulus to c and sub 1 from the copy.
  16025. * 5. Decrypt c into k. (error)
  16026. * Valid bounds test cases are covered by all the other RSA tests.
  16027. */
  16028. static int test_RsaDecryptBoundsCheck(void)
  16029. {
  16030. int ret = 0;
  16031. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16032. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16033. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16034. RsaKey key;
  16035. byte flatC[256];
  16036. word32 flatCSz;
  16037. byte out[256];
  16038. word32 outSz = sizeof(out);
  16039. WC_RNG rng;
  16040. printf(testingFmt, "RSA decrypt bounds check");
  16041. XMEMSET(&rng, 0, sizeof(rng));
  16042. ret = wc_InitRng(&rng);
  16043. if (ret == 0)
  16044. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16045. if (ret == 0) {
  16046. const byte* derKey;
  16047. word32 derKeySz;
  16048. word32 idx = 0;
  16049. #ifdef USE_CERT_BUFFERS_1024
  16050. derKey = server_key_der_1024;
  16051. derKeySz = (word32)sizeof_server_key_der_1024;
  16052. flatCSz = 128;
  16053. #else
  16054. derKey = server_key_der_2048;
  16055. derKeySz = (word32)sizeof_server_key_der_2048;
  16056. flatCSz = 256;
  16057. #endif
  16058. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  16059. }
  16060. if (ret == 0) {
  16061. XMEMSET(flatC, 0, flatCSz);
  16062. flatC[flatCSz-1] = 1;
  16063. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16064. RSA_PRIVATE_DECRYPT, &rng);
  16065. if (ret == RSA_OUT_OF_RANGE_E) {
  16066. mp_int c;
  16067. mp_init_copy(&c, &key.n);
  16068. mp_sub_d(&c, 1, &c);
  16069. mp_to_unsigned_bin(&c, flatC);
  16070. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16071. RSA_PRIVATE_DECRYPT, NULL);
  16072. mp_clear(&c);
  16073. }
  16074. if (ret == RSA_OUT_OF_RANGE_E)
  16075. ret = 0;
  16076. else
  16077. ret = WOLFSSL_FATAL_ERROR;
  16078. }
  16079. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  16080. ret = WOLFSSL_FATAL_ERROR;
  16081. printf(resultFmt, ret == 0 ? passed : failed);
  16082. #endif
  16083. return ret;
  16084. } /* END test_wc_RsaDecryptBoundsCheck */
  16085. /*
  16086. * Testing wc_SetKeyUsage()
  16087. */
  16088. static int test_wc_SetKeyUsage(void)
  16089. {
  16090. int ret = 0;
  16091. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16092. Cert myCert;
  16093. ret = wc_InitCert(&myCert);
  16094. printf(testingFmt, "wc_SetKeyUsage()");
  16095. if (ret == 0) {
  16096. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  16097. if (ret == 0) {
  16098. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  16099. }
  16100. if (ret == 0) {
  16101. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  16102. }
  16103. if (ret == 0) {
  16104. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  16105. }
  16106. if (ret == 0) {
  16107. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  16108. }
  16109. }
  16110. /* Test bad args. */
  16111. if (ret == 0) {
  16112. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  16113. if (ret == BAD_FUNC_ARG) {
  16114. ret = wc_SetKeyUsage(&myCert, NULL);
  16115. }
  16116. if (ret == BAD_FUNC_ARG) {
  16117. ret = wc_SetKeyUsage(&myCert, "");
  16118. }
  16119. if (ret == KEYUSAGE_E) {
  16120. ret = wc_SetKeyUsage(&myCert, ",");
  16121. }
  16122. if (ret == KEYUSAGE_E) {
  16123. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  16124. }
  16125. if (ret == KEYUSAGE_E) {
  16126. ret = 0;
  16127. } else {
  16128. ret = WOLFSSL_FATAL_ERROR;
  16129. }
  16130. }
  16131. printf(resultFmt, ret == 0 ? passed : failed);
  16132. #endif
  16133. return ret;
  16134. } /* END test_wc_SetKeyUsage */
  16135. /*
  16136. * Testing wc_CheckProbablePrime()
  16137. */
  16138. static int test_wc_CheckProbablePrime(void)
  16139. {
  16140. int ret = 0;
  16141. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16142. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16143. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16144. RsaKey key;
  16145. WC_RNG rng;
  16146. byte e[3];
  16147. word32 eSz = (word32)sizeof(e);
  16148. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16149. word32 nSz = (word32)sizeof(n);
  16150. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16151. word32 dSz = (word32)sizeof(d);
  16152. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16153. word32 pSz = (word32)sizeof(p);
  16154. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16155. word32 qSz = (word32)sizeof(q);
  16156. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16157. int* isPrime;
  16158. int test[5];
  16159. isPrime = test;
  16160. printf(testingFmt, "wc_CheckProbablePrime()");
  16161. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16162. if (ret == 0) {
  16163. ret = wc_InitRng(&rng);
  16164. }
  16165. if (ret == 0) {
  16166. ret = wc_RsaSetRNG(&key, &rng);
  16167. }
  16168. if (ret == 0) {
  16169. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  16170. }
  16171. if (ret == 0) {
  16172. PRIVATE_KEY_UNLOCK();
  16173. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  16174. p, &pSz, q, &qSz);
  16175. PRIVATE_KEY_LOCK();
  16176. }
  16177. /* Bad cases */
  16178. if (ret == 0) {
  16179. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  16180. nlen, isPrime);
  16181. if (ret == BAD_FUNC_ARG) {
  16182. ret = 0;
  16183. }
  16184. }
  16185. if (ret == 0) {
  16186. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  16187. nlen, isPrime);
  16188. if (ret == BAD_FUNC_ARG) {
  16189. ret = 0;
  16190. }
  16191. }
  16192. if (ret == 0) {
  16193. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  16194. nlen, isPrime);
  16195. if (ret == BAD_FUNC_ARG) {
  16196. ret = 0;
  16197. }
  16198. }
  16199. if (ret == 0) {
  16200. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  16201. nlen, isPrime);
  16202. if (ret == BAD_FUNC_ARG) {
  16203. ret = 0;
  16204. }
  16205. }
  16206. if (ret == 0) {
  16207. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  16208. nlen, isPrime);
  16209. if (ret == BAD_FUNC_ARG) {
  16210. ret = 0;
  16211. }
  16212. }
  16213. if (ret == 0) {
  16214. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  16215. nlen, isPrime);
  16216. if (ret == BAD_FUNC_ARG) {
  16217. ret = 0;
  16218. }
  16219. }
  16220. if (ret == 0) {
  16221. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  16222. nlen, isPrime);
  16223. if (ret == BAD_FUNC_ARG) {
  16224. ret = 0;
  16225. }
  16226. }
  16227. /* Good case */
  16228. if (ret == 0) {
  16229. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  16230. nlen, isPrime);
  16231. }
  16232. wc_FreeRsaKey(&key);
  16233. wc_FreeRng(&rng);
  16234. printf(resultFmt, ret == 0 ? passed : failed);
  16235. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16236. #endif
  16237. return ret;
  16238. } /* END test_wc_CheckProbablePrime */
  16239. /*
  16240. * Testing wc_RsaPSS_Verify()
  16241. */
  16242. static int test_wc_RsaPSS_Verify(void)
  16243. {
  16244. int ret = 0;
  16245. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16246. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16247. RsaKey key;
  16248. WC_RNG rng;
  16249. int sz = 256;
  16250. byte* pt;
  16251. const char* szMessage = "This is the string to be signed";
  16252. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16253. unsigned char pDecrypted[2048/8];
  16254. word32 outLen = sizeof(pDecrypted);
  16255. pt = pDecrypted;
  16256. printf(testingFmt, "wc_RsaPSS_Verify()");
  16257. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16258. if (ret == 0) {
  16259. ret = wc_InitRng(&rng);
  16260. }
  16261. if (ret == 0) {
  16262. ret = wc_RsaSetRNG(&key, &rng);
  16263. }
  16264. if (ret == 0) {
  16265. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16266. }
  16267. if (ret == 0) {
  16268. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  16269. pSignature, sizeof(pSignature),
  16270. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16271. if (ret > 0 ){
  16272. sz = ret;
  16273. ret = 0;
  16274. }
  16275. }
  16276. /* Bad cases */
  16277. if (ret == 0) {
  16278. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16279. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16280. if (ret == BAD_FUNC_ARG) {
  16281. ret = 0;
  16282. }
  16283. }
  16284. if (ret == 0) {
  16285. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16286. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16287. if (ret == BAD_FUNC_ARG) {
  16288. ret = 0;
  16289. }
  16290. }
  16291. if (ret == 0) {
  16292. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16293. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16294. if (ret == BAD_FUNC_ARG) {
  16295. ret = 0;
  16296. }
  16297. }
  16298. if (ret == 0) {
  16299. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16300. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16301. if (ret == BAD_FUNC_ARG) {
  16302. ret = 0;
  16303. }
  16304. }
  16305. /* Good case */
  16306. if (ret == 0) {
  16307. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16308. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16309. if (ret > 0) {
  16310. ret = 0;
  16311. }
  16312. }
  16313. wc_FreeRsaKey(&key);
  16314. wc_FreeRng(&rng);
  16315. printf(resultFmt, ret == 0 ? passed : failed);
  16316. #endif
  16317. return ret;
  16318. } /* END test_wc_RsaPSS_Verify */
  16319. /*
  16320. * Testing wc_RsaPSS_VerifyCheck()
  16321. */
  16322. static int test_wc_RsaPSS_VerifyCheck(void)
  16323. {
  16324. int ret = 0;
  16325. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16326. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16327. RsaKey key;
  16328. WC_RNG rng;
  16329. int sz = 256; /* 2048/8 */
  16330. byte* pt;
  16331. byte digest[32];
  16332. word32 digestSz = sizeof(digest);
  16333. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16334. word32 pSignatureSz = sizeof(pSignature);
  16335. unsigned char pDecrypted[2048/8];
  16336. word32 outLen = sizeof(pDecrypted);
  16337. pt = pDecrypted;
  16338. printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
  16339. XMEMSET(digest, 0, sizeof(digest));
  16340. XMEMSET(pSignature, 0, sizeof(pSignature));
  16341. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16342. if (ret == 0) {
  16343. ret = wc_InitRng(&rng);
  16344. }
  16345. if (ret == 0) {
  16346. ret = wc_RsaSetRNG(&key, &rng);
  16347. }
  16348. if (ret == 0) {
  16349. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16350. }
  16351. if (ret == 0) {
  16352. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16353. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16354. }
  16355. if (ret == 0) {
  16356. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16357. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16358. if (ret > 0 ){
  16359. sz = ret;
  16360. ret = 0;
  16361. }
  16362. }
  16363. /* Bad cases */
  16364. if (ret == 0) {
  16365. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  16366. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16367. if (ret == BAD_FUNC_ARG) {
  16368. ret = 0;
  16369. }
  16370. }
  16371. if (ret == 0) {
  16372. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  16373. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16374. if (ret == BAD_FUNC_ARG) {
  16375. ret = 0;
  16376. }
  16377. }
  16378. if (ret == 0) {
  16379. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  16380. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16381. if (ret == BAD_FUNC_ARG) {
  16382. ret = 0;
  16383. }
  16384. }
  16385. if (ret == 0) {
  16386. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  16387. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16388. if (ret == BAD_FUNC_ARG) {
  16389. ret = 0;
  16390. }
  16391. }
  16392. /* Good case */
  16393. if (ret == 0) {
  16394. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  16395. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16396. if (ret > 0) {
  16397. ret = 0;
  16398. }
  16399. }
  16400. wc_FreeRsaKey(&key);
  16401. wc_FreeRng(&rng);
  16402. printf(resultFmt, ret == 0 ? passed : failed);
  16403. #endif
  16404. return ret;
  16405. } /* END test_wc_RsaPSS_VerifyCheck */
  16406. /*
  16407. * Testing wc_RsaPSS_VerifyCheckInline()
  16408. */
  16409. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16410. {
  16411. int ret = 0;
  16412. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16413. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16414. RsaKey key;
  16415. WC_RNG rng;
  16416. int sz = 256;
  16417. byte* pt;
  16418. byte digest[32];
  16419. word32 digestSz = sizeof(digest);
  16420. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16421. unsigned char pDecrypted[2048/8];
  16422. pt = pDecrypted;
  16423. printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
  16424. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16425. XMEMSET(digest, 0, sizeof(digest));
  16426. XMEMSET(pSignature, 0, sizeof(pSignature));
  16427. if (ret == 0) {
  16428. ret = wc_InitRng(&rng);
  16429. }
  16430. if (ret == 0) {
  16431. ret = wc_RsaSetRNG(&key, &rng);
  16432. }
  16433. if (ret == 0) {
  16434. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16435. }
  16436. if (ret == 0) {
  16437. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16438. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16439. }
  16440. if (ret == 0) {
  16441. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  16442. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16443. if (ret > 0 ){
  16444. sz = ret;
  16445. ret = 0;
  16446. }
  16447. }
  16448. /* Bad Cases */
  16449. if (ret == 0) {
  16450. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  16451. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16452. if (ret == BAD_FUNC_ARG) {
  16453. ret = 0;
  16454. }
  16455. }
  16456. if (ret == 0) {
  16457. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  16458. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16459. if (ret == BAD_FUNC_ARG) {
  16460. ret = 0;
  16461. }
  16462. }
  16463. if (ret == 0) {
  16464. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  16465. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16466. if (ret == BAD_FUNC_ARG) {
  16467. ret = 0;
  16468. }
  16469. }
  16470. if (ret == 0) {
  16471. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16472. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  16473. if (ret == BAD_FUNC_ARG) {
  16474. ret = 0;
  16475. }
  16476. }
  16477. /* Good case */
  16478. if (ret == 0) {
  16479. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16480. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16481. if (ret > 0) {
  16482. ret = 0;
  16483. }
  16484. }
  16485. wc_FreeRsaKey(&key);
  16486. wc_FreeRng(&rng);
  16487. printf(resultFmt, ret == 0 ? passed : failed);
  16488. #endif
  16489. return ret;
  16490. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16491. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16492. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16493. {
  16494. (void)flag;
  16495. (void)type;
  16496. (void)file;
  16497. (void)line;
  16498. }
  16499. #endif
  16500. /*
  16501. * Testing wc_LockMutex_ex
  16502. */
  16503. static int test_wc_LockMutex_ex(void)
  16504. {
  16505. int ret = 0;
  16506. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16507. int flag = CRYPTO_LOCK;
  16508. int type = 0;
  16509. const char* file = "./test-LockMutex_ex.txt";
  16510. int line = 0;
  16511. printf(testingFmt, "wc_LockMutex_ex()");
  16512. /*without SetMutexCb*/
  16513. ret = wc_LockMutex_ex(flag, type, file, line);
  16514. if (ret == BAD_STATE_E) {
  16515. ret = 0;
  16516. }
  16517. /*with SetMutexCb*/
  16518. if (ret == 0) {
  16519. ret = wc_SetMutexCb(sample_mutex_cb);
  16520. if (ret == 0) {
  16521. ret = wc_LockMutex_ex(flag, type, file, line);
  16522. }
  16523. }
  16524. printf(resultFmt, ret == 0 ? passed : failed);
  16525. #endif
  16526. return ret;
  16527. }/*End test_wc_LockMutex_ex*/
  16528. /*
  16529. * Testing wc_SetMutexCb
  16530. */
  16531. static int test_wc_SetMutexCb(void)
  16532. {
  16533. int ret = 0;
  16534. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16535. printf(testingFmt, "wc_SetMutexCb()");
  16536. ret = wc_SetMutexCb(sample_mutex_cb);
  16537. printf(resultFmt, ret == 0 ? passed : failed);
  16538. #endif
  16539. return ret;
  16540. }/*End test_wc_SetMutexCb*/
  16541. /*
  16542. * Testing wc_RsaKeyToDer()
  16543. */
  16544. static int test_wc_RsaKeyToDer(void)
  16545. {
  16546. int ret = 0;
  16547. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16548. RsaKey genKey;
  16549. WC_RNG rng;
  16550. byte* der;
  16551. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16552. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16553. int bits = 1024;
  16554. word32 derSz = 611;
  16555. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16556. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16557. #else
  16558. int bits = 2048;
  16559. word32 derSz = 1196;
  16560. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16561. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16562. #endif
  16563. XMEMSET(&rng, 0, sizeof(rng));
  16564. XMEMSET(&genKey, 0, sizeof(genKey));
  16565. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16566. if (der == NULL) {
  16567. ret = WOLFSSL_FATAL_ERROR;
  16568. }
  16569. /* Init structures. */
  16570. if (ret == 0) {
  16571. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16572. }
  16573. if (ret == 0) {
  16574. ret = wc_InitRng(&rng);
  16575. }
  16576. /* Make key. */
  16577. if (ret == 0) {
  16578. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16579. if (ret != 0) {
  16580. ret = WOLFSSL_FATAL_ERROR;
  16581. }
  16582. }
  16583. printf(testingFmt, "wc_RsaKeyToDer()");
  16584. if (ret == 0) {
  16585. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  16586. if (ret > 0) {
  16587. ret = 0;
  16588. } else {
  16589. ret = WOLFSSL_FATAL_ERROR;
  16590. }
  16591. }
  16592. #ifndef HAVE_USER_RSA
  16593. /* Pass good/bad args. */
  16594. if (ret == 0) {
  16595. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16596. if (ret == BAD_FUNC_ARG) {
  16597. /* Get just the output length */
  16598. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16599. }
  16600. if (ret > 0) {
  16601. /* Try Public Key. */
  16602. genKey.type = 0;
  16603. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16604. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16605. /* Put back to Private Key */
  16606. genKey.type = 1;
  16607. #endif
  16608. }
  16609. if (ret == BAD_FUNC_ARG) {
  16610. ret = 0;
  16611. } else {
  16612. ret = WOLFSSL_FATAL_ERROR;
  16613. }
  16614. }
  16615. #else
  16616. /* Pass good/bad args. */
  16617. if (ret == 0) {
  16618. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  16619. if (ret == USER_CRYPTO_ERROR) {
  16620. /* Get just the output length */
  16621. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  16622. }
  16623. if (ret > 0) {
  16624. /* Try Public Key. */
  16625. genKey.type = 0;
  16626. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  16627. #ifdef WOLFSSL_CHECK_MEM_ZERO
  16628. /* Put back to Private Key */
  16629. genKey.type = 1;
  16630. #endif
  16631. }
  16632. if (ret == USER_CRYPTO_ERROR) {
  16633. ret = 0;
  16634. } else {
  16635. ret = WOLFSSL_FATAL_ERROR;
  16636. }
  16637. }
  16638. #endif
  16639. if (der != NULL) {
  16640. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16641. }
  16642. if (wc_FreeRsaKey(&genKey) || ret != 0) {
  16643. ret = WOLFSSL_FATAL_ERROR;
  16644. }
  16645. if (wc_FreeRng(&rng) || ret != 0) {
  16646. ret = WOLFSSL_FATAL_ERROR;
  16647. }
  16648. printf(resultFmt, ret == 0 ? passed : failed);
  16649. #endif
  16650. return ret;
  16651. } /* END test_wc_RsaKeyToDer */
  16652. /*
  16653. * Testing wc_RsaKeyToPublicDer()
  16654. */
  16655. static int test_wc_RsaKeyToPublicDer(void)
  16656. {
  16657. int ret = 0;
  16658. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16659. RsaKey key;
  16660. WC_RNG rng;
  16661. byte* der;
  16662. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16663. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16664. int bits = 1024;
  16665. word32 derLen = 162;
  16666. #else
  16667. int bits = 2048;
  16668. word32 derLen = 294;
  16669. #endif
  16670. XMEMSET(&rng, 0, sizeof(rng));
  16671. XMEMSET(&key, 0, sizeof(key));
  16672. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16673. if (der == NULL) {
  16674. ret = WOLFSSL_FATAL_ERROR;
  16675. }
  16676. if (ret == 0) {
  16677. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16678. }
  16679. if (ret == 0) {
  16680. ret = wc_InitRng(&rng);
  16681. }
  16682. if (ret == 0) {
  16683. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16684. }
  16685. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  16686. if (ret == 0) {
  16687. /* test getting size only */
  16688. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  16689. if (ret >= 0)
  16690. ret = 0;
  16691. }
  16692. if (ret == 0) {
  16693. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  16694. if (ret >= 0) {
  16695. ret = 0;
  16696. } else {
  16697. ret = WOLFSSL_FATAL_ERROR;
  16698. }
  16699. }
  16700. if (ret == 0) {
  16701. /* test getting size only */
  16702. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  16703. if (ret >= 0)
  16704. ret = 0;
  16705. }
  16706. if (ret == 0) {
  16707. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  16708. if (ret >= 0) {
  16709. ret = 0;
  16710. } else {
  16711. ret = WOLFSSL_FATAL_ERROR;
  16712. }
  16713. }
  16714. #ifndef HAVE_USER_RSA
  16715. /* Pass in bad args. */
  16716. if (ret == 0) {
  16717. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16718. if (ret == BAD_FUNC_ARG) {
  16719. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16720. }
  16721. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  16722. ret = 0;
  16723. } else {
  16724. ret = WOLFSSL_FATAL_ERROR;
  16725. }
  16726. }
  16727. #else
  16728. /* Pass in bad args. */
  16729. if (ret == 0) {
  16730. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  16731. if (ret == USER_CRYPTO_ERROR) {
  16732. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  16733. }
  16734. if (ret == USER_CRYPTO_ERROR) {
  16735. ret = 0;
  16736. } else {
  16737. ret = WOLFSSL_FATAL_ERROR;
  16738. }
  16739. }
  16740. #endif
  16741. if (der != NULL) {
  16742. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16743. }
  16744. if (wc_FreeRsaKey(&key) || ret != 0) {
  16745. ret = WOLFSSL_FATAL_ERROR;
  16746. }
  16747. if (wc_FreeRng(&rng) || ret != 0) {
  16748. ret = WOLFSSL_FATAL_ERROR;
  16749. }
  16750. printf(resultFmt, ret == 0 ? passed : failed);
  16751. #endif
  16752. return ret;
  16753. } /* END test_wc_RsaKeyToPublicDer */
  16754. /*
  16755. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  16756. */
  16757. static int test_wc_RsaPublicEncryptDecrypt(void)
  16758. {
  16759. int ret = 0;
  16760. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16761. RsaKey key;
  16762. WC_RNG rng;
  16763. const char inStr[] = TEST_STRING;
  16764. const word32 plainLen = (word32)TEST_STRING_SZ;
  16765. const word32 inLen = (word32)TEST_STRING_SZ;
  16766. int bits = TEST_RSA_BITS;
  16767. const word32 cipherLen = TEST_RSA_BYTES;
  16768. word32 cipherLenResult = cipherLen;
  16769. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16770. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16771. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16772. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16773. if (in == NULL || plain == NULL || cipher == NULL) {
  16774. printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  16775. return MEMORY_E;
  16776. }
  16777. #endif
  16778. XMEMCPY(in, inStr, inLen);
  16779. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16780. if (ret == 0) {
  16781. ret = wc_InitRng(&rng);
  16782. }
  16783. if (ret == 0) {
  16784. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16785. }
  16786. /* Encrypt. */
  16787. printf(testingFmt, "wc_RsaPublicEncrypt()");
  16788. if (ret == 0) {
  16789. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  16790. if (ret >= 0) {
  16791. cipherLenResult = ret;
  16792. ret = 0;
  16793. } else {
  16794. ret = WOLFSSL_FATAL_ERROR;
  16795. }
  16796. }
  16797. /* Pass bad args. */
  16798. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  16799. printf(resultFmt, ret == 0 ? passed : failed);
  16800. if (ret != 0) {
  16801. return ret;
  16802. }
  16803. /* Decrypt */
  16804. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  16805. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16806. /* Bind rng */
  16807. if (ret == 0) {
  16808. ret = wc_RsaSetRNG(&key, &rng);
  16809. }
  16810. #endif
  16811. if (ret == 0) {
  16812. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  16813. }
  16814. if (ret >= 0) {
  16815. ret = XMEMCMP(plain, inStr, plainLen);
  16816. }
  16817. /* Pass in bad args. */
  16818. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  16819. WC_FREE_VAR(in, NULL);
  16820. WC_FREE_VAR(plain, NULL);
  16821. WC_FREE_VAR(cipher, NULL);
  16822. if (wc_FreeRsaKey(&key) || ret != 0) {
  16823. ret = WOLFSSL_FATAL_ERROR;
  16824. }
  16825. if (wc_FreeRng(&rng) || ret != 0) {
  16826. ret = WOLFSSL_FATAL_ERROR;
  16827. }
  16828. printf(resultFmt, ret == 0 ? passed : failed);
  16829. #endif
  16830. return ret;
  16831. } /* END test_wc_RsaPublicEncryptDecrypt */
  16832. /*
  16833. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  16834. */
  16835. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  16836. {
  16837. int ret = 0;
  16838. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  16839. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  16840. && !defined(NO_SHA)
  16841. RsaKey key;
  16842. WC_RNG rng;
  16843. const char inStr[] = TEST_STRING;
  16844. const word32 inLen = (word32)TEST_STRING_SZ;
  16845. const word32 plainSz = (word32)TEST_STRING_SZ;
  16846. byte* res = NULL;
  16847. int idx = 0;
  16848. int bits = TEST_RSA_BITS;
  16849. const word32 cipherSz = TEST_RSA_BYTES;
  16850. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16851. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16852. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  16853. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16854. if (in == NULL || plain == NULL || cipher == NULL) {
  16855. printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  16856. return MEMORY_E;
  16857. }
  16858. #endif
  16859. XMEMCPY(in, inStr, inLen);
  16860. /* Initialize stack structures. */
  16861. XMEMSET(&rng, 0, sizeof(rng));
  16862. XMEMSET(&key, 0, sizeof(key));
  16863. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  16864. if (ret == 0) {
  16865. ret = wc_InitRng(&rng);
  16866. }
  16867. if (ret == 0) {
  16868. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16869. }
  16870. /* Encrypt */
  16871. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  16872. if (ret == 0) {
  16873. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  16874. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  16875. if (ret >= 0) {
  16876. idx = ret;
  16877. ret = 0;
  16878. } else {
  16879. ret = WOLFSSL_FATAL_ERROR;
  16880. }
  16881. }
  16882. /*Pass bad args.*/
  16883. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  16884. printf(resultFmt, ret == 0 ? passed : failed);
  16885. if (ret != 0) {
  16886. return ret;
  16887. }
  16888. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  16889. /* Decrypt */
  16890. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  16891. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  16892. if (ret == 0) {
  16893. ret = wc_RsaSetRNG(&key, &rng);
  16894. }
  16895. #endif
  16896. if (ret == 0) {
  16897. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  16898. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16899. WC_MGF1SHA1, NULL, 0);
  16900. }
  16901. if (ret >= 0) {
  16902. if (!XMEMCMP(plain, inStr, plainSz)) {
  16903. ret = 0;
  16904. } else {
  16905. ret = WOLFSSL_FATAL_ERROR;
  16906. }
  16907. }
  16908. /*Pass bad args.*/
  16909. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  16910. printf(resultFmt, ret == 0 ? passed : failed);
  16911. if (ret != 0) {
  16912. return ret;
  16913. }
  16914. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  16915. if (ret == 0) {
  16916. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  16917. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  16918. WC_MGF1SHA1, NULL, 0);
  16919. if (ret >= 0) {
  16920. if (!XMEMCMP(inStr, res, plainSz)) {
  16921. ret = 0;
  16922. } else {
  16923. ret = WOLFSSL_FATAL_ERROR;
  16924. }
  16925. }
  16926. }
  16927. #endif
  16928. WC_FREE_VAR(in, NULL);
  16929. WC_FREE_VAR(plain, NULL);
  16930. WC_FREE_VAR(cipher, NULL);
  16931. if (wc_FreeRsaKey(&key) || ret != 0) {
  16932. ret = WOLFSSL_FATAL_ERROR;
  16933. }
  16934. if (wc_FreeRng(&rng) || ret != 0) {
  16935. ret = WOLFSSL_FATAL_ERROR;
  16936. }
  16937. printf(resultFmt, ret == 0 ? passed : failed);
  16938. #endif
  16939. return ret;
  16940. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  16941. /*
  16942. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  16943. */
  16944. static int test_wc_RsaSSL_SignVerify(void)
  16945. {
  16946. int ret = 0;
  16947. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16948. RsaKey key;
  16949. WC_RNG rng;
  16950. const char inStr[] = TEST_STRING;
  16951. const word32 plainSz = (word32)TEST_STRING_SZ;
  16952. const word32 inLen = (word32)TEST_STRING_SZ;
  16953. word32 idx = 0;
  16954. int bits = TEST_RSA_BITS;
  16955. const word32 outSz = TEST_RSA_BYTES;
  16956. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  16957. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  16958. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  16959. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  16960. if (in == NULL || out == NULL || plain == NULL) {
  16961. printf("test_wc_RsaSSL_SignVerify failed\n");
  16962. return MEMORY_E;
  16963. }
  16964. #endif
  16965. XMEMCPY(in, inStr, inLen);
  16966. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16967. if (ret == 0) {
  16968. ret = wc_InitRng(&rng);
  16969. }
  16970. if (ret == 0) {
  16971. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  16972. }
  16973. /* Sign. */
  16974. printf(testingFmt, "wc_RsaSSL_Sign()");
  16975. if (ret == 0) {
  16976. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  16977. if (ret == (int)outSz) {
  16978. idx = ret;
  16979. ret = 0;
  16980. } else {
  16981. ret = WOLFSSL_FATAL_ERROR;
  16982. }
  16983. }
  16984. #ifndef HAVE_USER_RSA
  16985. /* Test bad args. */
  16986. if (ret == 0) {
  16987. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  16988. if (ret == BAD_FUNC_ARG) {
  16989. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  16990. }
  16991. if (ret == BAD_FUNC_ARG) {
  16992. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  16993. }
  16994. if (ret == BAD_FUNC_ARG) {
  16995. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  16996. }
  16997. if (ret == BAD_FUNC_ARG) {
  16998. ret = 0;
  16999. } else {
  17000. ret = WOLFSSL_FATAL_ERROR;
  17001. }
  17002. }
  17003. #else
  17004. /* Test bad args. */
  17005. if (ret == 0) {
  17006. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17007. if (ret == USER_CRYPTO_ERROR) {
  17008. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17009. }
  17010. if (ret == USER_CRYPTO_ERROR) {
  17011. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17012. }
  17013. if (ret == USER_CRYPTO_ERROR) {
  17014. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17015. }
  17016. if (ret == USER_CRYPTO_ERROR) {
  17017. ret = 0;
  17018. } else {
  17019. ret = WOLFSSL_FATAL_ERROR;
  17020. }
  17021. }
  17022. #endif
  17023. printf(resultFmt, ret == 0 ? passed : failed);
  17024. if (ret != 0) {
  17025. return ret;
  17026. }
  17027. /* Verify. */
  17028. printf(testingFmt, "wc_RsaSSL_Verify()");
  17029. if (ret == 0) {
  17030. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  17031. if (ret == (int)inLen) {
  17032. ret = 0;
  17033. } else {
  17034. ret = WOLFSSL_FATAL_ERROR;
  17035. }
  17036. }
  17037. #ifndef HAVE_USER_RSA
  17038. /* Pass bad args. */
  17039. if (ret == 0) {
  17040. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17041. if (ret == BAD_FUNC_ARG) {
  17042. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17043. }
  17044. if (ret == BAD_FUNC_ARG) {
  17045. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17046. }
  17047. if (ret == BAD_FUNC_ARG) {
  17048. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17049. }
  17050. if (ret == BAD_FUNC_ARG) {
  17051. ret = 0;
  17052. } else {
  17053. ret = WOLFSSL_FATAL_ERROR;
  17054. }
  17055. }
  17056. #else
  17057. /* Pass bad args. */
  17058. if (ret == 0) {
  17059. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17060. if (ret == USER_CRYPTO_ERROR) {
  17061. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17062. }
  17063. if (ret == USER_CRYPTO_ERROR) {
  17064. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17065. }
  17066. if (ret == USER_CRYPTO_ERROR) {
  17067. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17068. }
  17069. if (ret == USER_CRYPTO_ERROR) {
  17070. ret = 0;
  17071. } else {
  17072. ret = WOLFSSL_FATAL_ERROR;
  17073. }
  17074. }
  17075. #endif
  17076. WC_FREE_VAR(in, NULL);
  17077. WC_FREE_VAR(out, NULL);
  17078. WC_FREE_VAR(plain, NULL);
  17079. if (wc_FreeRsaKey(&key) || ret != 0) {
  17080. ret = WOLFSSL_FATAL_ERROR;
  17081. }
  17082. if (wc_FreeRng(&rng) || ret != 0) {
  17083. ret = WOLFSSL_FATAL_ERROR;
  17084. }
  17085. printf(resultFmt, ret == 0 ? passed : failed);
  17086. #endif
  17087. return ret;
  17088. } /* END test_wc_RsaSSL_SignVerify */
  17089. /*
  17090. * Testing wc_RsaEncryptSize()
  17091. */
  17092. static int test_wc_RsaEncryptSize(void)
  17093. {
  17094. int ret = 0;
  17095. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17096. RsaKey key;
  17097. WC_RNG rng;
  17098. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17099. if (ret == 0) {
  17100. ret = wc_InitRng(&rng);
  17101. }
  17102. printf(testingFmt, "wc_RsaEncryptSize()");
  17103. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17104. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17105. if (ret == 0) {
  17106. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  17107. if (ret == 0) {
  17108. ret = wc_RsaEncryptSize(&key);
  17109. }
  17110. if (ret == 128) {
  17111. ret = 0;
  17112. } else {
  17113. ret = WOLFSSL_FATAL_ERROR;
  17114. }
  17115. }
  17116. if (wc_FreeRsaKey(&key) || ret != 0) {
  17117. ret = WOLFSSL_FATAL_ERROR;
  17118. } else {
  17119. ret = 0;
  17120. }
  17121. #endif
  17122. if (ret == 0) {
  17123. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  17124. if (ret == 0) {
  17125. ret = wc_RsaEncryptSize(&key);
  17126. }
  17127. if (ret == 256) {
  17128. ret = 0;
  17129. } else {
  17130. ret = WOLFSSL_FATAL_ERROR;
  17131. }
  17132. }
  17133. /* Pass in bad arg. */
  17134. if (ret == 0) {
  17135. ret = wc_RsaEncryptSize(NULL);
  17136. #ifndef HAVE_USER_RSA
  17137. if (ret == BAD_FUNC_ARG) {
  17138. ret = 0;
  17139. } else {
  17140. ret = WOLFSSL_FATAL_ERROR;
  17141. }
  17142. #endif
  17143. }
  17144. if (wc_FreeRsaKey(&key) || ret != 0) {
  17145. ret = WOLFSSL_FATAL_ERROR;
  17146. }
  17147. if (wc_FreeRng(&rng) || ret != 0) {
  17148. ret = WOLFSSL_FATAL_ERROR;
  17149. }
  17150. printf(resultFmt, ret == 0 ? passed : failed);
  17151. #endif
  17152. return ret;
  17153. } /* END test_wc_RsaEncryptSize*/
  17154. /*
  17155. * Testing wc_RsaFlattenPublicKey()
  17156. */
  17157. static int test_wc_RsaFlattenPublicKey(void)
  17158. {
  17159. int ret = 0;
  17160. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17161. RsaKey key;
  17162. WC_RNG rng;
  17163. byte e[256];
  17164. byte n[256];
  17165. word32 eSz = sizeof(e);
  17166. word32 nSz = sizeof(n);
  17167. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17168. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17169. int bits = 1024;
  17170. #else
  17171. int bits = 2048;
  17172. #endif
  17173. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17174. if (ret == 0) {
  17175. ret = wc_InitRng(&rng);
  17176. }
  17177. if (ret == 0) {
  17178. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17179. if (ret >= 0) {
  17180. ret = 0;
  17181. } else {
  17182. ret = WOLFSSL_FATAL_ERROR;
  17183. }
  17184. }
  17185. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  17186. if (ret == 0) {
  17187. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  17188. }
  17189. #ifndef HAVE_USER_RSA
  17190. /* Pass bad args. */
  17191. if (ret == 0) {
  17192. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17193. if (ret == BAD_FUNC_ARG) {
  17194. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17195. }
  17196. if (ret == BAD_FUNC_ARG) {
  17197. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17198. }
  17199. if (ret == BAD_FUNC_ARG) {
  17200. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17201. }
  17202. if (ret == BAD_FUNC_ARG) {
  17203. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17204. }
  17205. if (ret == BAD_FUNC_ARG) {
  17206. ret = 0;
  17207. } else {
  17208. ret = WOLFSSL_FATAL_ERROR;
  17209. }
  17210. }
  17211. #else
  17212. /* Pass bad args. */
  17213. if (ret == 0) {
  17214. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17215. if (ret == USER_CRYPTO_ERROR) {
  17216. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17217. }
  17218. if (ret == USER_CRYPTO_ERROR) {
  17219. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17220. }
  17221. if (ret == USER_CRYPTO_ERROR) {
  17222. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17223. }
  17224. if (ret == USER_CRYPTO_ERROR) {
  17225. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17226. }
  17227. if (ret == USER_CRYPTO_ERROR) {
  17228. ret = 0;
  17229. } else {
  17230. ret = WOLFSSL_FATAL_ERROR;
  17231. }
  17232. }
  17233. #endif
  17234. if (wc_FreeRsaKey(&key) || ret != 0) {
  17235. ret = WOLFSSL_FATAL_ERROR;
  17236. }
  17237. if (wc_FreeRng(&rng) || ret != 0) {
  17238. ret = WOLFSSL_FATAL_ERROR;
  17239. }
  17240. printf(resultFmt, ret == 0 ? passed : failed);
  17241. #endif
  17242. return ret;
  17243. } /* END test_wc_RsaFlattenPublicKey */
  17244. /*
  17245. * unit test for wc_AesCcmSetKey
  17246. */
  17247. static int test_wc_AesCcmSetKey(void)
  17248. {
  17249. int ret = 0;
  17250. #ifdef HAVE_AESCCM
  17251. Aes aes;
  17252. const byte key16[] =
  17253. {
  17254. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17255. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17256. };
  17257. const byte key24[] =
  17258. {
  17259. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17260. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17261. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17262. };
  17263. const byte key32[] =
  17264. {
  17265. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17267. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17268. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17269. };
  17270. printf(testingFmt, "wc_AesCcmSetKey()");
  17271. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17272. if (ret != 0)
  17273. return ret;
  17274. #ifdef WOLFSSL_AES_128
  17275. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17276. #endif
  17277. #ifdef WOLFSSL_AES_192
  17278. if (ret == 0) {
  17279. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  17280. }
  17281. #endif
  17282. #ifdef WOLFSSL_AES_256
  17283. if (ret == 0) {
  17284. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  17285. }
  17286. #endif
  17287. /* Test bad args. */
  17288. if (ret == 0) {
  17289. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  17290. if (ret == BAD_FUNC_ARG) {
  17291. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  17292. }
  17293. if (ret == BAD_FUNC_ARG) {
  17294. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  17295. }
  17296. if (ret != BAD_FUNC_ARG) {
  17297. ret = WOLFSSL_FATAL_ERROR;
  17298. } else {
  17299. ret = 0;
  17300. }
  17301. }
  17302. wc_AesFree(&aes);
  17303. printf(resultFmt, ret == 0 ? passed : failed);
  17304. #endif
  17305. return ret;
  17306. } /* END test_wc_AesCcmSetKey */
  17307. /*
  17308. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17309. */
  17310. static int test_wc_AesCcmEncryptDecrypt(void)
  17311. {
  17312. int ret = 0;
  17313. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17314. Aes aes;
  17315. const byte key16[] =
  17316. {
  17317. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17318. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17319. };
  17320. /* plaintext */
  17321. const byte plainT[] =
  17322. {
  17323. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17324. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17325. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17326. };
  17327. /* nonce */
  17328. const byte iv[] =
  17329. {
  17330. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17331. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17332. };
  17333. const byte c[] = /* cipher text. */
  17334. {
  17335. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17336. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17337. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17338. };
  17339. const byte t[] = /* Auth tag */
  17340. {
  17341. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17342. };
  17343. const byte authIn[] =
  17344. {
  17345. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17346. };
  17347. byte cipherOut[sizeof(plainT)];
  17348. byte authTag[sizeof(t)];
  17349. int ccmE = WOLFSSL_FATAL_ERROR;
  17350. #ifdef HAVE_AES_DECRYPT
  17351. int ccmD = WOLFSSL_FATAL_ERROR;
  17352. byte plainOut[sizeof(cipherOut)];
  17353. #endif
  17354. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17355. if (ret != 0)
  17356. return ret;
  17357. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17358. if (ret == 0) {
  17359. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17360. iv, sizeof(iv), authTag, sizeof(authTag),
  17361. authIn , sizeof(authIn));
  17362. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  17363. XMEMCMP(t, authTag, sizeof(t))) {
  17364. ccmE = WOLFSSL_FATAL_ERROR;
  17365. ret = WOLFSSL_FATAL_ERROR;
  17366. }
  17367. #ifdef HAVE_AES_DECRYPT
  17368. if (ret == 0) {
  17369. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17370. sizeof(plainOut), iv, sizeof(iv),
  17371. authTag, sizeof(authTag),
  17372. authIn, sizeof(authIn));
  17373. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  17374. ccmD = WOLFSSL_FATAL_ERROR;
  17375. }
  17376. }
  17377. #endif
  17378. }
  17379. printf(testingFmt, "wc_AesCcmEncrypt()");
  17380. /* Pass in bad args. Encrypt*/
  17381. if (ret == 0 && ccmE == 0) {
  17382. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17383. iv, sizeof(iv), authTag, sizeof(authTag),
  17384. authIn , sizeof(authIn));
  17385. if (ccmE == BAD_FUNC_ARG) {
  17386. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17387. iv, sizeof(iv), authTag, sizeof(authTag),
  17388. authIn , sizeof(authIn));
  17389. }
  17390. if (ccmE == BAD_FUNC_ARG) {
  17391. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17392. iv, sizeof(iv), authTag, sizeof(authTag),
  17393. authIn , sizeof(authIn));
  17394. }
  17395. if (ccmE == BAD_FUNC_ARG) {
  17396. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17397. NULL, sizeof(iv), authTag, sizeof(authTag),
  17398. authIn , sizeof(authIn));
  17399. }
  17400. if (ccmE == BAD_FUNC_ARG) {
  17401. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17402. iv, sizeof(iv), NULL, sizeof(authTag),
  17403. authIn , sizeof(authIn));
  17404. }
  17405. if (ccmE == BAD_FUNC_ARG) {
  17406. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17407. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  17408. authIn , sizeof(authIn));
  17409. }
  17410. if (ccmE == BAD_FUNC_ARG) {
  17411. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17412. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  17413. authIn , sizeof(authIn));
  17414. }
  17415. if (ccmE != BAD_FUNC_ARG) {
  17416. ccmE = WOLFSSL_FATAL_ERROR;
  17417. } else {
  17418. ccmE = 0;
  17419. }
  17420. } /* End Encrypt */
  17421. printf(resultFmt, ccmE == 0 ? passed : failed);
  17422. if (ccmE != 0) {
  17423. wc_AesFree(&aes);
  17424. return ccmE;
  17425. }
  17426. #ifdef HAVE_AES_DECRYPT
  17427. printf(testingFmt, "wc_AesCcmDecrypt()");
  17428. /* Pass in bad args. Decrypt*/
  17429. if (ret == 0 && ccmD == 0) {
  17430. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17431. iv, sizeof(iv), authTag, sizeof(authTag),
  17432. authIn, sizeof(authIn));
  17433. if (ccmD == BAD_FUNC_ARG) {
  17434. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17435. iv, sizeof(iv), authTag, sizeof(authTag),
  17436. authIn, sizeof(authIn));
  17437. }
  17438. if (ccmD == BAD_FUNC_ARG) {
  17439. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17440. iv, sizeof(iv), authTag, sizeof(authTag),
  17441. authIn, sizeof(authIn));
  17442. }
  17443. if (ccmD == BAD_FUNC_ARG) {
  17444. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17445. sizeof(plainOut), NULL, sizeof(iv),
  17446. authTag, sizeof(authTag),
  17447. authIn, sizeof(authIn));
  17448. }
  17449. if (ccmD == BAD_FUNC_ARG) {
  17450. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17451. sizeof(plainOut), iv, sizeof(iv), NULL,
  17452. sizeof(authTag), authIn, sizeof(authIn));
  17453. }
  17454. if (ccmD == BAD_FUNC_ARG) {
  17455. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17456. sizeof(plainOut), iv, sizeof(iv) + 1,
  17457. authTag, sizeof(authTag),
  17458. authIn, sizeof(authIn));
  17459. }
  17460. if (ccmD == BAD_FUNC_ARG) {
  17461. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17462. sizeof(plainOut), iv, sizeof(iv) - 7,
  17463. authTag, sizeof(authTag),
  17464. authIn, sizeof(authIn));
  17465. }
  17466. if (ccmD != BAD_FUNC_ARG) {
  17467. ccmD = WOLFSSL_FATAL_ERROR;
  17468. } else {
  17469. ccmD = 0;
  17470. }
  17471. } /* END Decrypt */
  17472. printf(resultFmt, ccmD == 0 ? passed : failed);
  17473. if (ccmD != 0) {
  17474. return ccmD;
  17475. }
  17476. #endif
  17477. wc_AesFree(&aes);
  17478. #endif /* HAVE_AESCCM */
  17479. return ret;
  17480. } /* END test_wc_AesCcmEncryptDecrypt */
  17481. /*
  17482. * Testing wc_InitDsaKey()
  17483. */
  17484. static int test_wc_InitDsaKey(void)
  17485. {
  17486. int ret = 0;
  17487. #ifndef NO_DSA
  17488. DsaKey key;
  17489. printf(testingFmt, "wc_InitDsaKey()");
  17490. ret = wc_InitDsaKey(&key);
  17491. /* Pass in bad args. */
  17492. if (ret == 0) {
  17493. ret = wc_InitDsaKey(NULL);
  17494. if (ret == BAD_FUNC_ARG) {
  17495. ret = 0;
  17496. } else {
  17497. ret = WOLFSSL_FATAL_ERROR;
  17498. }
  17499. }
  17500. printf(resultFmt, ret == 0 ? passed : failed);
  17501. wc_FreeDsaKey(&key);
  17502. #endif
  17503. return ret;
  17504. } /* END test_wc_InitDsaKey */
  17505. /*
  17506. * Testing wc_DsaSign() and wc_DsaVerify()
  17507. */
  17508. static int test_wc_DsaSignVerify(void)
  17509. {
  17510. int ret = 0;
  17511. #if !defined(NO_DSA)
  17512. DsaKey key;
  17513. WC_RNG rng;
  17514. wc_Sha sha;
  17515. byte signature[DSA_SIG_SIZE];
  17516. byte hash[WC_SHA_DIGEST_SIZE];
  17517. word32 idx = 0;
  17518. word32 bytes;
  17519. int answer;
  17520. #ifdef USE_CERT_BUFFERS_1024
  17521. byte tmp[ONEK_BUF];
  17522. XMEMSET(tmp, 0, sizeof(tmp));
  17523. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17524. bytes = sizeof_dsa_key_der_1024;
  17525. #elif defined(USE_CERT_BUFFERS_2048)
  17526. byte tmp[TWOK_BUF];
  17527. XMEMSET(tmp, 0, sizeof(tmp));
  17528. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17529. bytes = sizeof_dsa_key_der_2048;
  17530. #else
  17531. byte tmp[TWOK_BUF];
  17532. XMEMSET(tmp, 0, sizeof(tmp));
  17533. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17534. if (fp == XBADFILE) {
  17535. return WOLFSSL_BAD_FILE;
  17536. }
  17537. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17538. XFCLOSE(fp);
  17539. #endif /* END USE_CERT_BUFFERS_1024 */
  17540. ret = wc_InitSha(&sha);
  17541. if (ret == 0) {
  17542. ret = wc_ShaUpdate(&sha, tmp, bytes);
  17543. if (ret == 0) {
  17544. ret = wc_ShaFinal(&sha, hash);
  17545. }
  17546. if (ret == 0) {
  17547. ret = wc_InitDsaKey(&key);
  17548. }
  17549. if (ret == 0) {
  17550. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17551. }
  17552. if (ret == 0) {
  17553. ret = wc_InitRng(&rng);
  17554. }
  17555. }
  17556. printf(testingFmt, "wc_DsaSign()");
  17557. /* Sign. */
  17558. if (ret == 0) {
  17559. ret = wc_DsaSign(hash, signature, &key, &rng);
  17560. }
  17561. /* Test bad args. */
  17562. if (ret == 0) {
  17563. ret = wc_DsaSign(NULL, signature, &key, &rng);
  17564. if (ret == BAD_FUNC_ARG) {
  17565. ret = wc_DsaSign(hash, NULL, &key, &rng);
  17566. }
  17567. if (ret == BAD_FUNC_ARG) {
  17568. ret = wc_DsaSign(hash, signature, NULL, &rng);
  17569. }
  17570. if (ret == BAD_FUNC_ARG) {
  17571. ret = wc_DsaSign(hash, signature, &key, NULL);
  17572. }
  17573. if (ret == BAD_FUNC_ARG) {
  17574. ret = 0;
  17575. } else {
  17576. ret = WOLFSSL_FATAL_ERROR;
  17577. }
  17578. }
  17579. printf(resultFmt, ret == 0 ? passed : failed);
  17580. if (ret != 0) {
  17581. return ret;
  17582. }
  17583. /* Verify. */
  17584. printf(testingFmt, "wc_DsaVerify()");
  17585. ret = wc_DsaVerify(hash, signature, &key, &answer);
  17586. if (ret != 0 || answer != 1) {
  17587. ret = WOLFSSL_FATAL_ERROR;
  17588. } else {
  17589. ret = 0;
  17590. }
  17591. /* Pass in bad args. */
  17592. if (ret == 0) {
  17593. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  17594. if (ret == BAD_FUNC_ARG) {
  17595. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  17596. }
  17597. if (ret == BAD_FUNC_ARG) {
  17598. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  17599. }
  17600. if (ret == BAD_FUNC_ARG) {
  17601. ret = wc_DsaVerify(hash, signature, &key, NULL);
  17602. }
  17603. if (ret == BAD_FUNC_ARG) {
  17604. ret = 0;
  17605. } else {
  17606. ret = WOLFSSL_FATAL_ERROR;
  17607. }
  17608. }
  17609. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  17610. /* hard set q to 0 and test fail case */
  17611. mp_free(&key.q);
  17612. mp_init(&key.q);
  17613. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17614. mp_set(&key.q, 1);
  17615. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  17616. #endif
  17617. if (wc_FreeRng(&rng) && ret == 0) {
  17618. ret = WOLFSSL_FATAL_ERROR;
  17619. }
  17620. printf(resultFmt, ret == 0 ? passed : failed);
  17621. wc_FreeDsaKey(&key);
  17622. wc_ShaFree(&sha);
  17623. #endif
  17624. return ret;
  17625. } /* END test_wc_DsaSign */
  17626. /*
  17627. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  17628. */
  17629. static int test_wc_DsaPublicPrivateKeyDecode(void)
  17630. {
  17631. int ret = 0;
  17632. #if !defined(NO_DSA)
  17633. DsaKey key;
  17634. word32 bytes;
  17635. word32 idx = 0;
  17636. int priv = WOLFSSL_FATAL_ERROR;
  17637. int pub = WOLFSSL_FATAL_ERROR;
  17638. #ifdef USE_CERT_BUFFERS_1024
  17639. byte tmp[ONEK_BUF];
  17640. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17641. bytes = sizeof_dsa_key_der_1024;
  17642. #elif defined(USE_CERT_BUFFERS_2048)
  17643. byte tmp[TWOK_BUF];
  17644. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17645. bytes = sizeof_dsa_key_der_2048;
  17646. #else
  17647. byte tmp[TWOK_BUF];
  17648. XMEMSET(tmp, 0, sizeof(tmp));
  17649. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17650. if (fp == XBADFILE)
  17651. {
  17652. return WOLFSSL_BAD_FILE;
  17653. }
  17654. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17655. XFCLOSE(fp);
  17656. #endif /* END USE_CERT_BUFFERS_1024 */
  17657. ret = wc_InitDsaKey(&key);
  17658. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  17659. if (ret == 0) {
  17660. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17661. /* Test bad args. */
  17662. if (priv == 0) {
  17663. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  17664. if (priv == BAD_FUNC_ARG) {
  17665. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  17666. }
  17667. if (priv == BAD_FUNC_ARG) {
  17668. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  17669. }
  17670. if (priv == BAD_FUNC_ARG) {
  17671. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17672. }
  17673. if (priv == ASN_PARSE_E) {
  17674. priv = 0;
  17675. } else {
  17676. priv = WOLFSSL_FATAL_ERROR;
  17677. }
  17678. }
  17679. wc_FreeDsaKey(&key);
  17680. ret = wc_InitDsaKey(&key);
  17681. }
  17682. printf(resultFmt, priv == 0 ? passed : failed);
  17683. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  17684. if (ret == 0) {
  17685. idx = 0; /* Reset */
  17686. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17687. /* Test bad args. */
  17688. if (pub == 0) {
  17689. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  17690. if (pub == BAD_FUNC_ARG) {
  17691. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  17692. }
  17693. if (pub == BAD_FUNC_ARG) {
  17694. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  17695. }
  17696. if (pub == BAD_FUNC_ARG) {
  17697. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  17698. }
  17699. if (pub == ASN_PARSE_E) {
  17700. pub = 0;
  17701. } else {
  17702. pub = WOLFSSL_FATAL_ERROR;
  17703. }
  17704. }
  17705. } /* END Public Key */
  17706. printf(resultFmt, pub == 0 ? passed : failed);
  17707. wc_FreeDsaKey(&key);
  17708. #endif
  17709. return ret;
  17710. } /* END test_wc_DsaPublicPrivateKeyDecode */
  17711. /*
  17712. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  17713. */
  17714. static int test_wc_MakeDsaKey(void)
  17715. {
  17716. int ret = 0;
  17717. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17718. DsaKey genKey;
  17719. WC_RNG rng;
  17720. XMEMSET(&rng, 0, sizeof(rng));
  17721. XMEMSET(&genKey, 0, sizeof(genKey));
  17722. ret = wc_InitRng(&rng);
  17723. if (ret == 0) {
  17724. ret = wc_InitDsaKey(&genKey);
  17725. }
  17726. printf(testingFmt, "wc_MakeDsaParameters()");
  17727. if (ret == 0) {
  17728. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17729. }
  17730. /* Test bad args. */
  17731. if (ret == 0) {
  17732. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  17733. if (ret == BAD_FUNC_ARG) {
  17734. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  17735. }
  17736. if (ret == BAD_FUNC_ARG) {
  17737. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  17738. }
  17739. if (ret == BAD_FUNC_ARG) {
  17740. ret = 0;
  17741. } else {
  17742. ret = WOLFSSL_FATAL_ERROR;
  17743. }
  17744. }
  17745. printf(resultFmt, ret == 0 ? passed : failed);
  17746. printf(testingFmt, "wc_MakeDsaKey()");
  17747. if (ret == 0) {
  17748. ret = wc_MakeDsaKey(&rng, &genKey);
  17749. }
  17750. /* Test bad args. */
  17751. if (ret == 0) {
  17752. ret = wc_MakeDsaKey(NULL, &genKey);
  17753. if (ret == BAD_FUNC_ARG) {
  17754. ret = wc_MakeDsaKey(&rng, NULL);
  17755. }
  17756. if (ret == BAD_FUNC_ARG) {
  17757. ret = 0;
  17758. } else {
  17759. ret = WOLFSSL_FATAL_ERROR;
  17760. }
  17761. }
  17762. if (wc_FreeRng(&rng) && ret == 0) {
  17763. ret = WOLFSSL_FAILURE;
  17764. }
  17765. printf(resultFmt, ret == 0 ? passed : failed);
  17766. wc_FreeDsaKey(&genKey);
  17767. #endif
  17768. return ret;
  17769. } /* END test_wc_MakeDsaKey */
  17770. /*
  17771. * Testing wc_DsaKeyToDer()
  17772. */
  17773. static int test_wc_DsaKeyToDer(void)
  17774. {
  17775. int ret = 0;
  17776. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17777. DsaKey genKey;
  17778. WC_RNG rng;
  17779. word32 bytes;
  17780. word32 idx = 0;
  17781. #ifdef USE_CERT_BUFFERS_1024
  17782. byte tmp[ONEK_BUF];
  17783. byte der[ONEK_BUF];
  17784. XMEMSET(tmp, 0, sizeof(tmp));
  17785. XMEMSET(der, 0, sizeof(der));
  17786. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17787. bytes = sizeof_dsa_key_der_1024;
  17788. #elif defined(USE_CERT_BUFFERS_2048)
  17789. byte tmp[TWOK_BUF];
  17790. byte der[TWOK_BUF];
  17791. XMEMSET(tmp, 0, sizeof(tmp));
  17792. XMEMSET(der, 0, sizeof(der));
  17793. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17794. bytes = sizeof_dsa_key_der_2048;
  17795. #else
  17796. byte tmp[TWOK_BUF];
  17797. byte der[TWOK_BUF];
  17798. XMEMSET(tmp, 0, sizeof(tmp));
  17799. XMEMSET(der, 0, sizeof(der));
  17800. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17801. if (fp == XBADFILE) {
  17802. return WOLFSSL_BAD_FILE;
  17803. }
  17804. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17805. XFCLOSE(fp);
  17806. #endif /* END USE_CERT_BUFFERS_1024 */
  17807. XMEMSET(&rng, 0, sizeof(rng));
  17808. XMEMSET(&genKey, 0, sizeof(genKey));
  17809. ret = wc_InitRng(&rng);
  17810. if (ret == 0) {
  17811. ret = wc_InitDsaKey(&genKey);
  17812. }
  17813. if (ret == 0) {
  17814. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  17815. if (ret == 0) {
  17816. wc_FreeDsaKey(&genKey);
  17817. ret = wc_InitDsaKey(&genKey);
  17818. }
  17819. }
  17820. if (ret == 0) {
  17821. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  17822. }
  17823. printf(testingFmt, "wc_DsaKeyToDer()");
  17824. if (ret == 0) {
  17825. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  17826. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  17827. ret = 0;
  17828. }
  17829. }
  17830. /* Test bad args. */
  17831. if (ret == 0) {
  17832. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  17833. if (ret == BAD_FUNC_ARG) {
  17834. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  17835. }
  17836. if (ret == BAD_FUNC_ARG) {
  17837. ret = 0;
  17838. } else {
  17839. ret = WOLFSSL_FATAL_ERROR;
  17840. }
  17841. }
  17842. if (wc_FreeRng(&rng) && ret == 0) {
  17843. ret = WOLFSSL_FATAL_ERROR;
  17844. }
  17845. printf(resultFmt, ret == 0 ? passed : failed);
  17846. wc_FreeDsaKey(&genKey);
  17847. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  17848. return ret;
  17849. } /* END test_wc_DsaKeyToDer */
  17850. /*
  17851. * Testing wc_DsaKeyToPublicDer()
  17852. * (indirectly testing setDsaPublicKey())
  17853. */
  17854. static int test_wc_DsaKeyToPublicDer(void)
  17855. {
  17856. int ret = 0;
  17857. #ifndef HAVE_SELFTEST
  17858. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  17859. DsaKey genKey;
  17860. WC_RNG rng;
  17861. byte* der;
  17862. word32 sz;
  17863. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  17864. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17865. if (der == NULL) {
  17866. ret = WOLFSSL_FATAL_ERROR;
  17867. }
  17868. if (ret == 0) {
  17869. ret = wc_InitDsaKey(&genKey);
  17870. }
  17871. if (ret == 0) {
  17872. ret = wc_InitRng(&rng);
  17873. }
  17874. if (ret == 0) {
  17875. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  17876. }
  17877. if (ret == 0) {
  17878. ret = wc_MakeDsaKey(&rng, &genKey);
  17879. }
  17880. if (ret == 0) {
  17881. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  17882. if (ret >= 0) {
  17883. sz = ret;
  17884. ret = 0;
  17885. } else {
  17886. ret = WOLFSSL_FATAL_ERROR;
  17887. }
  17888. }
  17889. if (ret == 0) {
  17890. word32 idx = 0;
  17891. wc_FreeDsaKey(&genKey);
  17892. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17893. }
  17894. /* Test without the SubjectPublicKeyInfo header */
  17895. if (ret == 0) {
  17896. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  17897. if (ret >= 0) {
  17898. sz = ret;
  17899. ret = 0;
  17900. } else {
  17901. ret = WOLFSSL_FATAL_ERROR;
  17902. }
  17903. }
  17904. if (ret == 0) {
  17905. word32 idx = 0;
  17906. wc_FreeDsaKey(&genKey);
  17907. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  17908. }
  17909. /* Test bad args. */
  17910. if (ret == 0) {
  17911. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  17912. if (ret == BAD_FUNC_ARG) {
  17913. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  17914. }
  17915. if (ret == BAD_FUNC_ARG) {
  17916. ret = 0;
  17917. } else {
  17918. ret = WOLFSSL_FATAL_ERROR;
  17919. }
  17920. }
  17921. if (wc_FreeRng(&rng) && ret == 0) {
  17922. ret = WOLFSSL_FATAL_ERROR;
  17923. }
  17924. printf(resultFmt, ret == 0 ? passed : failed);
  17925. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  17926. wc_FreeDsaKey(&genKey);
  17927. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  17928. #endif /* HAVE_SELFTEST */
  17929. return ret;
  17930. } /* END test_wc_DsaKeyToPublicDer */
  17931. /*
  17932. * Testing wc_DsaImportParamsRaw()
  17933. */
  17934. static int test_wc_DsaImportParamsRaw(void)
  17935. {
  17936. int ret = 0;
  17937. #if !defined(NO_DSA)
  17938. DsaKey key;
  17939. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17940. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17941. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17942. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17943. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17944. "47123188f8dc551054ee162b634d60f097f719076640e209"
  17945. "80a0093113a8bd73";
  17946. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  17947. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  17948. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  17949. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  17950. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  17951. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  17952. "76341a7e7d9";
  17953. /* invalid p and q parameters */
  17954. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  17955. const char* invalidQ = "96c5390a";
  17956. printf(testingFmt, "wc_DsaImportParamsRaw()");
  17957. ret = wc_InitDsaKey(&key);
  17958. if (ret == 0) {
  17959. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  17960. }
  17961. /* test bad args */
  17962. if (ret == 0) {
  17963. /* null key struct */
  17964. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  17965. if (ret == BAD_FUNC_ARG) {
  17966. /* null param pointers */
  17967. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  17968. }
  17969. if (ret == BAD_FUNC_ARG) {
  17970. /* illegal p length */
  17971. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  17972. }
  17973. if (ret == BAD_FUNC_ARG) {
  17974. /* illegal q length */
  17975. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  17976. if (ret == BAD_FUNC_ARG)
  17977. ret = 0;
  17978. }
  17979. }
  17980. printf(resultFmt, ret == 0 ? passed : failed);
  17981. wc_FreeDsaKey(&key);
  17982. #endif
  17983. return ret;
  17984. } /* END test_wc_DsaImportParamsRaw */
  17985. /*
  17986. * Testing wc_DsaImportParamsRawCheck()
  17987. */
  17988. static int test_wc_DsaImportParamsRawCheck(void)
  17989. {
  17990. int ret = 0;
  17991. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  17992. DsaKey key;
  17993. int trusted = 0;
  17994. /* [mod = L=1024, N=160], from CAVP KeyPair */
  17995. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  17996. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  17997. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  17998. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  17999. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18000. "80a0093113a8bd73";
  18001. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18002. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18003. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18004. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18005. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18006. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18007. "76341a7e7d9";
  18008. /* invalid p and q parameters */
  18009. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18010. const char* invalidQ = "96c5390a";
  18011. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  18012. ret = wc_InitDsaKey(&key);
  18013. if (ret == 0) {
  18014. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  18015. }
  18016. /* test bad args */
  18017. if (ret == 0) {
  18018. /* null key struct */
  18019. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  18020. if (ret == BAD_FUNC_ARG) {
  18021. /* null param pointers */
  18022. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  18023. }
  18024. if (ret == BAD_FUNC_ARG) {
  18025. /* illegal p length */
  18026. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  18027. }
  18028. if (ret == BAD_FUNC_ARG) {
  18029. /* illegal q length */
  18030. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  18031. if (ret == BAD_FUNC_ARG)
  18032. ret = 0;
  18033. }
  18034. }
  18035. printf(resultFmt, ret == 0 ? passed : failed);
  18036. wc_FreeDsaKey(&key);
  18037. #endif
  18038. return ret;
  18039. } /* END test_wc_DsaImportParamsRawCheck */
  18040. /*
  18041. * Testing wc_DsaExportParamsRaw()
  18042. */
  18043. static int test_wc_DsaExportParamsRaw(void)
  18044. {
  18045. int ret = 0;
  18046. #if !defined(NO_DSA)
  18047. DsaKey key;
  18048. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18049. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18050. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18051. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18052. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18053. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18054. "80a0093113a8bd73";
  18055. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18056. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18057. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18058. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18059. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18060. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18061. "76341a7e7d9";
  18062. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18063. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18064. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18065. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18066. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18067. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18068. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18069. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18070. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18071. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18072. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18073. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18074. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18075. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18076. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18077. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18078. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18079. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18080. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18081. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18082. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18083. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18084. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18085. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18086. byte pOut[MAX_DSA_PARAM_SIZE];
  18087. byte qOut[MAX_DSA_PARAM_SIZE];
  18088. byte gOut[MAX_DSA_PARAM_SIZE];
  18089. word32 pOutSz, qOutSz, gOutSz;
  18090. printf(testingFmt, "wc_DsaExportParamsRaw()");
  18091. ret = wc_InitDsaKey(&key);
  18092. if (ret == 0) {
  18093. /* first test using imported raw parameters, for expected */
  18094. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18095. }
  18096. if (ret == 0) {
  18097. pOutSz = sizeof(pOut);
  18098. qOutSz = sizeof(qOut);
  18099. gOutSz = sizeof(gOut);
  18100. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18101. gOut, &gOutSz);
  18102. }
  18103. if (ret == 0) {
  18104. /* validate exported parameters are correct */
  18105. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  18106. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  18107. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  18108. ret = -1;
  18109. }
  18110. }
  18111. /* test bad args */
  18112. if (ret == 0) {
  18113. /* null key struct */
  18114. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  18115. gOut, &gOutSz);
  18116. if (ret == BAD_FUNC_ARG) {
  18117. /* null output pointers */
  18118. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  18119. NULL, &gOutSz);
  18120. }
  18121. if (ret == LENGTH_ONLY_E) {
  18122. /* null output size pointers */
  18123. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  18124. gOut, NULL);
  18125. }
  18126. if (ret == BAD_FUNC_ARG) {
  18127. /* p output buffer size too small */
  18128. pOutSz = 1;
  18129. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18130. gOut, &gOutSz);
  18131. pOutSz = sizeof(pOut);
  18132. }
  18133. if (ret == BUFFER_E) {
  18134. /* q output buffer size too small */
  18135. qOutSz = 1;
  18136. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18137. gOut, &gOutSz);
  18138. qOutSz = sizeof(qOut);
  18139. }
  18140. if (ret == BUFFER_E) {
  18141. /* g output buffer size too small */
  18142. gOutSz = 1;
  18143. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18144. gOut, &gOutSz);
  18145. if (ret == BUFFER_E)
  18146. ret = 0;
  18147. }
  18148. }
  18149. printf(resultFmt, ret == 0 ? passed : failed);
  18150. wc_FreeDsaKey(&key);
  18151. #endif
  18152. return ret;
  18153. } /* END test_wc_DsaExportParamsRaw */
  18154. /*
  18155. * Testing wc_DsaExportKeyRaw()
  18156. */
  18157. static int test_wc_DsaExportKeyRaw(void)
  18158. {
  18159. int ret = 0;
  18160. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18161. DsaKey key;
  18162. WC_RNG rng;
  18163. byte xOut[MAX_DSA_PARAM_SIZE];
  18164. byte yOut[MAX_DSA_PARAM_SIZE];
  18165. word32 xOutSz, yOutSz;
  18166. printf(testingFmt, "wc_DsaExportKeyRaw()");
  18167. XMEMSET(&rng, 0, sizeof(rng));
  18168. XMEMSET(&key, 0, sizeof(key));
  18169. ret = wc_InitRng(&rng);
  18170. if (ret == 0) {
  18171. ret = wc_InitDsaKey(&key);
  18172. }
  18173. if (ret == 0) {
  18174. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  18175. if (ret == 0) {
  18176. ret = wc_MakeDsaKey(&rng, &key);
  18177. }
  18178. }
  18179. /* try successful export */
  18180. if (ret == 0) {
  18181. xOutSz = sizeof(xOut);
  18182. yOutSz = sizeof(yOut);
  18183. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18184. }
  18185. /* test bad args */
  18186. if (ret == 0) {
  18187. /* null key struct */
  18188. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  18189. if (ret == BAD_FUNC_ARG) {
  18190. /* null output pointers */
  18191. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  18192. }
  18193. if (ret == LENGTH_ONLY_E) {
  18194. /* null output size pointers */
  18195. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  18196. }
  18197. if (ret == BAD_FUNC_ARG) {
  18198. /* x output buffer size too small */
  18199. xOutSz = 1;
  18200. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18201. xOutSz = sizeof(xOut);
  18202. }
  18203. if (ret == BUFFER_E) {
  18204. /* y output buffer size too small */
  18205. yOutSz = 1;
  18206. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18207. if (ret == BUFFER_E)
  18208. ret = 0;
  18209. }
  18210. }
  18211. printf(resultFmt, ret == 0 ? passed : failed);
  18212. wc_FreeDsaKey(&key);
  18213. wc_FreeRng(&rng);
  18214. #endif
  18215. return ret;
  18216. } /* END test_wc_DsaExportParamsRaw */
  18217. /*
  18218. * Testing wc_ed25519_make_key().
  18219. */
  18220. static int test_wc_ed25519_make_key(void)
  18221. {
  18222. int ret = 0;
  18223. #if defined(HAVE_ED25519)
  18224. ed25519_key key;
  18225. WC_RNG rng;
  18226. ret = wc_InitRng(&rng);
  18227. if (ret == 0) {
  18228. ret = wc_ed25519_init(&key);
  18229. }
  18230. printf(testingFmt, "wc_ed25519_make_key()");
  18231. if (ret == 0) {
  18232. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18233. }
  18234. /* Test bad args. */
  18235. if (ret == 0) {
  18236. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  18237. if (ret == BAD_FUNC_ARG) {
  18238. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  18239. }
  18240. if (ret == BAD_FUNC_ARG) {
  18241. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  18242. }
  18243. if (ret == BAD_FUNC_ARG) {
  18244. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  18245. }
  18246. if (ret == BAD_FUNC_ARG) {
  18247. ret = 0;
  18248. } else if (ret == 0) {
  18249. ret = WOLFSSL_FATAL_ERROR;
  18250. }
  18251. }
  18252. printf(resultFmt, ret == 0 ? passed : failed);
  18253. if (wc_FreeRng(&rng) && ret == 0) {
  18254. ret = WOLFSSL_FATAL_ERROR;
  18255. }
  18256. wc_ed25519_free(&key);
  18257. #endif
  18258. return ret;
  18259. } /* END test_wc_ed25519_make_key */
  18260. /*
  18261. * Testing wc_ed25519_init()
  18262. */
  18263. static int test_wc_ed25519_init(void)
  18264. {
  18265. int ret = 0;
  18266. #if defined(HAVE_ED25519)
  18267. ed25519_key key;
  18268. printf(testingFmt, "wc_ed25519_init()");
  18269. ret = wc_ed25519_init(&key);
  18270. /* Test bad args. */
  18271. if (ret == 0) {
  18272. ret = wc_ed25519_init(NULL);
  18273. if (ret == BAD_FUNC_ARG) {
  18274. ret = 0;
  18275. } else if (ret == 0) {
  18276. ret = WOLFSSL_FATAL_ERROR;
  18277. }
  18278. }
  18279. printf(resultFmt, ret == 0 ? passed : failed);
  18280. wc_ed25519_free(&key);
  18281. #endif
  18282. return ret;
  18283. } /* END test_wc_ed25519_init */
  18284. /*
  18285. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  18286. */
  18287. static int test_wc_ed25519_sign_msg(void)
  18288. {
  18289. int ret = 0;
  18290. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  18291. WC_RNG rng;
  18292. ed25519_key key;
  18293. byte msg[] = "Everybody gets Friday off.\n";
  18294. byte sig[ED25519_SIG_SIZE];
  18295. word32 msglen = sizeof(msg);
  18296. word32 siglen = sizeof(sig);
  18297. word32 badSigLen = sizeof(sig) - 1;
  18298. #ifdef HAVE_ED25519_VERIFY
  18299. int verify_ok = 0; /*1 = Verify success.*/
  18300. #endif
  18301. /* Initialize stack variables. */
  18302. XMEMSET(sig, 0, siglen);
  18303. /* Initialize key. */
  18304. ret = wc_InitRng(&rng);
  18305. if (ret == 0) {
  18306. ret = wc_ed25519_init(&key);
  18307. if (ret == 0) {
  18308. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18309. }
  18310. }
  18311. printf(testingFmt, "wc_ed25519_sign_msg()");
  18312. if (ret == 0) {
  18313. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  18314. }
  18315. /* Test bad args. */
  18316. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  18317. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  18318. if (ret == BAD_FUNC_ARG) {
  18319. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  18320. }
  18321. if (ret == BAD_FUNC_ARG) {
  18322. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  18323. }
  18324. if (ret == BAD_FUNC_ARG) {
  18325. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  18326. }
  18327. if (ret == BAD_FUNC_ARG) {
  18328. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  18329. }
  18330. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  18331. badSigLen -= 1;
  18332. ret = 0;
  18333. } else if (ret == 0) {
  18334. ret = WOLFSSL_FATAL_ERROR;
  18335. }
  18336. } /* END sign */
  18337. printf(resultFmt, ret == 0 ? passed : failed);
  18338. #ifdef HAVE_ED25519_VERIFY
  18339. printf(testingFmt, "wc_ed25519_verify_msg()");
  18340. if (ret == 0) {
  18341. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  18342. if (ret == 0 && verify_ok == 1) {
  18343. ret = 0;
  18344. } else if (ret == 0) {
  18345. ret = WOLFSSL_FATAL_ERROR;
  18346. }
  18347. /* Test bad args. */
  18348. if (ret == 0) {
  18349. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  18350. msglen, &verify_ok, &key),
  18351. BAD_FUNC_ARG);
  18352. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  18353. msglen, &verify_ok, &key),
  18354. BAD_FUNC_ARG);
  18355. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18356. &key);
  18357. if (ret == BAD_FUNC_ARG) {
  18358. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  18359. &verify_ok, &key);
  18360. }
  18361. if (ret == BAD_FUNC_ARG) {
  18362. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18363. NULL, &key);
  18364. }
  18365. if (ret == BAD_FUNC_ARG) {
  18366. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18367. &verify_ok, NULL);
  18368. }
  18369. if (ret == BAD_FUNC_ARG) {
  18370. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  18371. &verify_ok, &key);
  18372. }
  18373. if (ret == BAD_FUNC_ARG) {
  18374. ret = 0;
  18375. } else if (ret == 0) {
  18376. ret = WOLFSSL_FATAL_ERROR;
  18377. }
  18378. }
  18379. } /* END verify. */
  18380. printf(resultFmt, ret == 0 ? passed : failed);
  18381. #endif /* Verify. */
  18382. if (wc_FreeRng(&rng) && ret == 0) {
  18383. ret = WOLFSSL_FATAL_ERROR;
  18384. }
  18385. wc_ed25519_free(&key);
  18386. #endif
  18387. return ret;
  18388. } /* END test_wc_ed25519_sign_msg */
  18389. /*
  18390. * Testing wc_ed25519_import_public()
  18391. */
  18392. static int test_wc_ed25519_import_public(void)
  18393. {
  18394. int ret = 0;
  18395. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18396. WC_RNG rng;
  18397. ed25519_key pubKey;
  18398. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  18399. word32 inlen = sizeof(in);
  18400. ret = wc_InitRng(&rng);
  18401. if (ret == 0) {
  18402. ret = wc_ed25519_init(&pubKey);
  18403. if (ret == 0) {
  18404. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  18405. }
  18406. }
  18407. printf(testingFmt, "wc_ed25519_import_public()");
  18408. if (ret == 0) {
  18409. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  18410. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  18411. ret = 0;
  18412. } else {
  18413. ret = WOLFSSL_FATAL_ERROR;
  18414. }
  18415. /* Test bad args. */
  18416. if (ret == 0) {
  18417. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  18418. if (ret == BAD_FUNC_ARG) {
  18419. ret = wc_ed25519_import_public(in, inlen, NULL);
  18420. }
  18421. if (ret == BAD_FUNC_ARG) {
  18422. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  18423. }
  18424. if (ret == BAD_FUNC_ARG) {
  18425. ret = 0;
  18426. } else if (ret == 0) {
  18427. ret = WOLFSSL_FATAL_ERROR;
  18428. }
  18429. }
  18430. }
  18431. printf(resultFmt, ret == 0 ? passed : failed);
  18432. if (wc_FreeRng(&rng) && ret == 0) {
  18433. ret = WOLFSSL_FATAL_ERROR;
  18434. }
  18435. wc_ed25519_free(&pubKey);
  18436. #endif
  18437. return ret;
  18438. } /* END wc_ed25519_import_public */
  18439. /*
  18440. * Testing wc_ed25519_import_private_key()
  18441. */
  18442. static int test_wc_ed25519_import_private_key(void)
  18443. {
  18444. int ret = 0;
  18445. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18446. WC_RNG rng;
  18447. ed25519_key key;
  18448. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  18449. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  18450. word32 privKeySz = sizeof(privKey);
  18451. word32 pubKeySz = sizeof(pubKey);
  18452. #ifdef HAVE_ED25519_KEY_EXPORT
  18453. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18454. word32 bothKeysSz = sizeof(bothKeys);
  18455. #endif
  18456. ret = wc_InitRng(&rng);
  18457. if (ret != 0) {
  18458. return ret;
  18459. }
  18460. ret = wc_ed25519_init(&key);
  18461. if (ret != 0) {
  18462. wc_FreeRng(&rng);
  18463. return ret;
  18464. }
  18465. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18466. printf(testingFmt, "wc_ed25519_import_private_key()");
  18467. if (ret == 0) {
  18468. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  18469. pubKeySz, &key, 1);
  18470. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18471. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18472. ret = WOLFSSL_FATAL_ERROR;
  18473. }
  18474. }
  18475. #ifdef HAVE_ED25519_KEY_EXPORT
  18476. if (ret == 0)
  18477. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  18478. if (ret == 0) {
  18479. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  18480. &key, 1);
  18481. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18482. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18483. ret = WOLFSSL_FATAL_ERROR;
  18484. }
  18485. }
  18486. #endif
  18487. /* Test bad args. */
  18488. if (ret == 0) {
  18489. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18490. &key);
  18491. if (ret == BAD_FUNC_ARG) {
  18492. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18493. pubKeySz, &key);
  18494. }
  18495. if (ret == BAD_FUNC_ARG) {
  18496. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18497. pubKeySz, NULL);
  18498. }
  18499. if (ret == BAD_FUNC_ARG) {
  18500. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  18501. pubKeySz, &key);
  18502. }
  18503. if (ret == BAD_FUNC_ARG) {
  18504. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18505. pubKeySz - 1, &key);
  18506. }
  18507. if (ret == BAD_FUNC_ARG) {
  18508. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18509. 0, &key);
  18510. }
  18511. if (ret == BAD_FUNC_ARG) {
  18512. ret = 0;
  18513. } else if (ret == 0) {
  18514. ret = WOLFSSL_FATAL_ERROR;
  18515. }
  18516. }
  18517. printf(resultFmt, ret == 0 ? passed : failed);
  18518. if (wc_FreeRng(&rng) && ret == 0) {
  18519. ret = WOLFSSL_FATAL_ERROR;
  18520. }
  18521. wc_ed25519_free(&key);
  18522. #endif
  18523. return ret;
  18524. } /* END test_wc_ed25519_import_private_key */
  18525. /*
  18526. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  18527. */
  18528. static int test_wc_ed25519_export(void)
  18529. {
  18530. int ret = 0;
  18531. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18532. WC_RNG rng;
  18533. ed25519_key key;
  18534. byte priv[ED25519_PRV_KEY_SIZE];
  18535. byte pub[ED25519_PUB_KEY_SIZE];
  18536. word32 privSz = sizeof(priv);
  18537. word32 pubSz = sizeof(pub);
  18538. ret = wc_InitRng(&rng);
  18539. if (ret != 0) {
  18540. return ret;
  18541. }
  18542. ret = wc_ed25519_init(&key);
  18543. if (ret != 0) {
  18544. wc_FreeRng(&rng);
  18545. return ret;
  18546. }
  18547. if (ret == 0) {
  18548. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18549. }
  18550. printf(testingFmt, "wc_ed25519_export_public()");
  18551. if (ret == 0) {
  18552. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  18553. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  18554. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  18555. ret = WOLFSSL_FATAL_ERROR;
  18556. }
  18557. if (ret == 0) {
  18558. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  18559. if (ret == BAD_FUNC_ARG) {
  18560. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  18561. }
  18562. if (ret == BAD_FUNC_ARG) {
  18563. ret = wc_ed25519_export_public(&key, pub, NULL);
  18564. }
  18565. if (ret == BAD_FUNC_ARG) {
  18566. ret = 0;
  18567. } else if (ret == 0) {
  18568. ret = WOLFSSL_FATAL_ERROR;
  18569. }
  18570. }
  18571. }
  18572. printf(resultFmt, ret == 0 ? passed : failed);
  18573. printf(testingFmt, "wc_ed25519_export_private_only()");
  18574. if (ret == 0) {
  18575. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  18576. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  18577. || XMEMCMP(key.k, priv, privSz) != 0)) {
  18578. ret = WOLFSSL_FATAL_ERROR;
  18579. }
  18580. if (ret == 0) {
  18581. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  18582. if (ret == BAD_FUNC_ARG) {
  18583. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  18584. }
  18585. if (ret == BAD_FUNC_ARG) {
  18586. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  18587. }
  18588. if (ret == BAD_FUNC_ARG) {
  18589. ret = 0;
  18590. } else if (ret == 0) {
  18591. ret = WOLFSSL_FATAL_ERROR;
  18592. }
  18593. }
  18594. }
  18595. printf(resultFmt, ret == 0 ? passed : failed);
  18596. if (wc_FreeRng(&rng) && ret == 0) {
  18597. ret = WOLFSSL_FATAL_ERROR;
  18598. }
  18599. wc_ed25519_free(&key);
  18600. #endif
  18601. return ret;
  18602. } /* END test_wc_ed25519_export */
  18603. /*
  18604. * Testing wc_ed25519_size()
  18605. */
  18606. static int test_wc_ed25519_size(void)
  18607. {
  18608. int ret = 0;
  18609. #if defined(HAVE_ED25519)
  18610. WC_RNG rng;
  18611. ed25519_key key;
  18612. ret = wc_InitRng(&rng);
  18613. if (ret != 0) {
  18614. return ret;
  18615. }
  18616. ret = wc_ed25519_init(&key);
  18617. if (ret != 0) {
  18618. wc_FreeRng(&rng);
  18619. return ret;
  18620. }
  18621. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18622. if (ret != 0) {
  18623. wc_FreeRng(&rng);
  18624. wc_ed25519_free(&key);
  18625. return ret;
  18626. }
  18627. printf(testingFmt, "wc_ed25519_size()");
  18628. ret = wc_ed25519_size(&key);
  18629. /* Test bad args. */
  18630. if (ret == ED25519_KEY_SIZE) {
  18631. ret = wc_ed25519_size(NULL);
  18632. if (ret == BAD_FUNC_ARG) {
  18633. ret = 0;
  18634. }
  18635. }
  18636. printf(resultFmt, ret == 0 ? passed : failed);
  18637. if (ret == 0) {
  18638. printf(testingFmt, "wc_ed25519_sig_size()");
  18639. ret = wc_ed25519_sig_size(&key);
  18640. if (ret == ED25519_SIG_SIZE) {
  18641. ret = 0;
  18642. }
  18643. /* Test bad args. */
  18644. if (ret == 0) {
  18645. ret = wc_ed25519_sig_size(NULL);
  18646. if (ret == BAD_FUNC_ARG) {
  18647. ret = 0;
  18648. }
  18649. }
  18650. printf(resultFmt, ret == 0 ? passed : failed);
  18651. } /* END wc_ed25519_sig_size() */
  18652. if (ret == 0) {
  18653. printf(testingFmt, "wc_ed25519_pub_size");
  18654. ret = wc_ed25519_pub_size(&key);
  18655. if (ret == ED25519_PUB_KEY_SIZE) {
  18656. ret = 0;
  18657. }
  18658. if (ret == 0) {
  18659. ret = wc_ed25519_pub_size(NULL);
  18660. if (ret == BAD_FUNC_ARG) {
  18661. ret = 0;
  18662. }
  18663. }
  18664. printf(resultFmt, ret == 0 ? passed : failed);
  18665. } /* END wc_ed25519_pub_size */
  18666. if (ret == 0) {
  18667. printf(testingFmt, "wc_ed25519_priv_size");
  18668. ret = wc_ed25519_priv_size(&key);
  18669. if (ret == ED25519_PRV_KEY_SIZE) {
  18670. ret = 0;
  18671. }
  18672. if (ret == 0) {
  18673. ret = wc_ed25519_priv_size(NULL);
  18674. if (ret == BAD_FUNC_ARG) {
  18675. ret = 0;
  18676. }
  18677. }
  18678. printf(resultFmt, ret == 0 ? passed : failed);
  18679. } /* END wc_ed25519_pub_size */
  18680. if (wc_FreeRng(&rng) && ret == 0) {
  18681. ret = WOLFSSL_FATAL_ERROR;
  18682. }
  18683. wc_ed25519_free(&key);
  18684. #endif
  18685. return ret;
  18686. } /* END test_wc_ed25519_size */
  18687. /*
  18688. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  18689. */
  18690. static int test_wc_ed25519_exportKey(void)
  18691. {
  18692. int ret = 0;
  18693. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18694. WC_RNG rng;
  18695. ed25519_key key;
  18696. byte priv[ED25519_PRV_KEY_SIZE];
  18697. byte pub[ED25519_PUB_KEY_SIZE];
  18698. byte privOnly[ED25519_PRV_KEY_SIZE];
  18699. word32 privSz = sizeof(priv);
  18700. word32 pubSz = sizeof(pub);
  18701. word32 privOnlySz = sizeof(privOnly);
  18702. ret = wc_InitRng(&rng);
  18703. if (ret != 0) {
  18704. return ret;
  18705. }
  18706. ret = wc_ed25519_init(&key);
  18707. if (ret != 0) {
  18708. wc_FreeRng(&rng);
  18709. return ret;
  18710. }
  18711. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18712. if (ret != 0) {
  18713. wc_FreeRng(&rng);
  18714. wc_ed25519_free(&key);
  18715. return ret;
  18716. }
  18717. printf(testingFmt, "wc_ed25519_export_private()");
  18718. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  18719. if (ret == 0) {
  18720. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  18721. if (ret == BAD_FUNC_ARG) {
  18722. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  18723. }
  18724. if (ret == BAD_FUNC_ARG) {
  18725. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  18726. }
  18727. if (ret == BAD_FUNC_ARG) {
  18728. ret = 0;
  18729. } else if (ret == 0) {
  18730. ret = WOLFSSL_FATAL_ERROR;
  18731. }
  18732. }
  18733. printf(resultFmt, ret == 0 ? passed : failed);
  18734. if (ret == 0) {
  18735. printf(testingFmt, "wc_ed25519_export_key()");
  18736. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  18737. if (ret == 0) {
  18738. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  18739. if (ret == BAD_FUNC_ARG) {
  18740. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  18741. }
  18742. if (ret == BAD_FUNC_ARG) {
  18743. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  18744. }
  18745. if (ret == BAD_FUNC_ARG) {
  18746. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  18747. }
  18748. if (ret == BAD_FUNC_ARG) {
  18749. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  18750. }
  18751. if (ret == BAD_FUNC_ARG) {
  18752. ret = 0;
  18753. } else if (ret == 0) {
  18754. ret = WOLFSSL_FATAL_ERROR;
  18755. }
  18756. }
  18757. printf(resultFmt, ret == 0 ? passed : failed);
  18758. } /* END wc_ed25519_export_key() */
  18759. /* Cross check output. */
  18760. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  18761. ret = WOLFSSL_FATAL_ERROR;
  18762. }
  18763. if (wc_FreeRng(&rng) && ret == 0) {
  18764. ret = WOLFSSL_FATAL_ERROR;
  18765. }
  18766. wc_ed25519_free(&key);
  18767. #endif
  18768. return ret;
  18769. } /* END test_wc_ed25519_exportKey */
  18770. /*
  18771. * Testing wc_Ed25519PublicKeyToDer
  18772. */
  18773. static int test_wc_Ed25519PublicKeyToDer(void)
  18774. {
  18775. int ret = 0;
  18776. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  18777. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  18778. int tmp;
  18779. ed25519_key key;
  18780. byte derBuf[1024];
  18781. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  18782. /* Test bad args */
  18783. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  18784. if (tmp != BAD_FUNC_ARG) {
  18785. ret = WOLFSSL_FATAL_ERROR;
  18786. }
  18787. if (ret == 0) {
  18788. wc_ed25519_init(&key);
  18789. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  18790. if (tmp != BUFFER_E) {
  18791. ret = WOLFSSL_FATAL_ERROR;
  18792. }
  18793. wc_ed25519_free(&key);
  18794. }
  18795. /* Test good args */
  18796. if (ret == 0) {
  18797. WC_RNG rng;
  18798. ret = wc_InitRng(&rng);
  18799. if (ret != 0) {
  18800. return ret;
  18801. }
  18802. ret = wc_ed25519_init(&key);
  18803. if (ret != 0) {
  18804. wc_FreeRng(&rng);
  18805. return ret;
  18806. }
  18807. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18808. if (ret != 0) {
  18809. wc_FreeRng(&rng);
  18810. wc_ed25519_free(&key);
  18811. return ret;
  18812. }
  18813. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  18814. if (tmp <= 0) {
  18815. ret = WOLFSSL_FATAL_ERROR;
  18816. }
  18817. wc_FreeRng(&rng);
  18818. wc_ed25519_free(&key);
  18819. }
  18820. printf(resultFmt, ret == 0 ? passed : failed);
  18821. #endif
  18822. return ret;
  18823. } /* END testing wc_Ed25519PublicKeyToDer */
  18824. /*
  18825. * Testing wc_curve25519_init and wc_curve25519_free.
  18826. */
  18827. static int test_wc_curve25519_init(void)
  18828. {
  18829. int ret = 0;
  18830. #if defined(HAVE_CURVE25519)
  18831. curve25519_key key;
  18832. printf(testingFmt, "wc_curve25519_init()");
  18833. ret = wc_curve25519_init(&key);
  18834. /* Test bad args for wc_curve25519_init */
  18835. if (ret == 0) {
  18836. ret = wc_curve25519_init(NULL);
  18837. if (ret == BAD_FUNC_ARG) {
  18838. ret = 0;
  18839. } else if (ret == 0) {
  18840. ret = WOLFSSL_FATAL_ERROR;
  18841. }
  18842. }
  18843. printf(resultFmt, ret == 0 ? passed : failed);
  18844. /* Test good args for wc_curve_25519_free */
  18845. wc_curve25519_free(&key);
  18846. wc_curve25519_free(NULL);
  18847. #endif
  18848. return ret;
  18849. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  18850. /*
  18851. * Testing test_wc_curve25519_size.
  18852. */
  18853. static int test_wc_curve25519_size(void)
  18854. {
  18855. int ret = 0;
  18856. #if defined(HAVE_CURVE25519)
  18857. curve25519_key key;
  18858. printf(testingFmt, "wc_curve25519_size()");
  18859. ret = wc_curve25519_init(&key);
  18860. /* Test good args for wc_curve25519_size */
  18861. if (ret == 0) {
  18862. ret = wc_curve25519_size(&key);
  18863. }
  18864. /* Test bad args for wc_curve25519_size */
  18865. if (ret != 0) {
  18866. ret = wc_curve25519_size(NULL);
  18867. }
  18868. printf(resultFmt, ret == 0 ? passed : failed);
  18869. wc_curve25519_free(&key);
  18870. #endif
  18871. return ret;
  18872. } /* END test_wc_curve25519_size*/
  18873. /*
  18874. * Testing test_wc_curve25519_export_key_raw().
  18875. */
  18876. static int test_wc_curve25519_export_key_raw(void)
  18877. {
  18878. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  18879. curve25519_key key;
  18880. WC_RNG rng;
  18881. byte privateKey[CURVE25519_KEYSIZE];
  18882. byte publicKey[CURVE25519_KEYSIZE];
  18883. word32 prvkSz;
  18884. word32 pubkSz;
  18885. byte prik[CURVE25519_KEYSIZE];
  18886. byte pubk[CURVE25519_KEYSIZE];
  18887. word32 prksz;
  18888. word32 pbksz;
  18889. printf(testingFmt, "wc_curve25519_export_key_raw()");
  18890. if(0 != wc_InitRng(&rng)){
  18891. printf(testingFmt, "failed due to wc_InitRng");
  18892. fflush(stdout);
  18893. return 1;
  18894. }
  18895. if(0 != wc_curve25519_init(&key)){
  18896. printf(testingFmt, "failed due to wc_curve25519_init");
  18897. fflush(stdout);
  18898. wc_FreeRng(&rng);
  18899. return 1;
  18900. }
  18901. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  18902. printf(testingFmt, "failed due to wc_curve25519_make_key");
  18903. fflush(stdout);
  18904. wc_curve25519_free(&key);
  18905. wc_FreeRng(&rng);
  18906. return 1;
  18907. }
  18908. /*
  18909. bad-argument-test cases
  18910. target function sould return BAD_FUNC_ARG
  18911. */
  18912. prvkSz = CURVE25519_KEYSIZE;
  18913. pubkSz = CURVE25519_KEYSIZE;
  18914. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18915. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  18916. printf(testingFmt,"failed at bad-arg-case-1.");
  18917. fflush(stdout);
  18918. wc_curve25519_free(&key);
  18919. wc_FreeRng(&rng);
  18920. return 1;
  18921. }
  18922. prvkSz = CURVE25519_KEYSIZE;
  18923. pubkSz = CURVE25519_KEYSIZE;
  18924. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18925. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  18926. printf(testingFmt,"failed at bad-arg-case-2.");
  18927. fflush(stdout);
  18928. wc_curve25519_free(&key);
  18929. wc_FreeRng(&rng);
  18930. return 1;
  18931. }
  18932. prvkSz = CURVE25519_KEYSIZE;
  18933. pubkSz = CURVE25519_KEYSIZE;
  18934. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18935. &key , privateKey, NULL, publicKey, &pubkSz)){
  18936. printf(testingFmt,"failed at bad-arg-case-3.");
  18937. fflush(stdout);
  18938. wc_curve25519_free(&key);
  18939. wc_FreeRng(&rng);
  18940. return 1;
  18941. }
  18942. /* prvkSz = CURVE25519_KEYSIZE; */
  18943. pubkSz = CURVE25519_KEYSIZE;
  18944. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18945. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  18946. printf(testingFmt,"failed at bad-arg-case-4.");
  18947. fflush(stdout);
  18948. wc_curve25519_free(&key);
  18949. wc_FreeRng(&rng);
  18950. return 1;
  18951. }
  18952. prvkSz = CURVE25519_KEYSIZE;
  18953. pubkSz = CURVE25519_KEYSIZE;
  18954. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  18955. &key , privateKey, &prvkSz, publicKey, NULL )){
  18956. printf(testingFmt,"failed at bad-arg-case-5.");
  18957. fflush(stdout);
  18958. wc_curve25519_free(&key);
  18959. wc_FreeRng(&rng);
  18960. return 1;
  18961. }
  18962. /*
  18963. cross-testing
  18964. */
  18965. prksz = CURVE25519_KEYSIZE;
  18966. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  18967. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  18968. fflush(stdout);
  18969. wc_curve25519_free(&key);
  18970. wc_FreeRng(&rng);
  18971. return 1;
  18972. }
  18973. pbksz = CURVE25519_KEYSIZE;
  18974. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  18975. printf(testingFmt,"failed due to wc_curve25519_export_public");
  18976. fflush(stdout);
  18977. wc_curve25519_free(&key);
  18978. wc_FreeRng(&rng);
  18979. return 1;
  18980. }
  18981. prvkSz = CURVE25519_KEYSIZE;
  18982. /* pubkSz = CURVE25519_KEYSIZE; */
  18983. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  18984. publicKey, &pubkSz)){
  18985. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  18986. fflush(stdout);
  18987. wc_curve25519_free(&key);
  18988. wc_FreeRng(&rng);
  18989. return 1;
  18990. }
  18991. if((prksz == CURVE25519_KEYSIZE) &&
  18992. (pbksz == CURVE25519_KEYSIZE) &&
  18993. (prvkSz == CURVE25519_KEYSIZE) &&
  18994. (pubkSz == CURVE25519_KEYSIZE)){
  18995. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  18996. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  18997. printf(resultFmt,passed);
  18998. fflush(stdout);
  18999. wc_curve25519_free(&key);
  19000. wc_FreeRng(&rng);
  19001. return 0;
  19002. }
  19003. else{
  19004. printf(testingFmt,"failed due to key-contents-inconsistency.");
  19005. fflush(stdout);
  19006. wc_curve25519_free(&key);
  19007. wc_FreeRng(&rng);
  19008. return 1;
  19009. }
  19010. }
  19011. else{
  19012. printf(testingFmt,"failed due to bad-key-size.");
  19013. fflush(stdout);
  19014. wc_curve25519_free(&key);
  19015. wc_FreeRng(&rng);
  19016. return 1;
  19017. }
  19018. #endif
  19019. fflush(stdout);
  19020. return 0;
  19021. } /* end of test_wc_curve25519_export_key_raw */
  19022. /*
  19023. * Testing test_wc_curve25519_export_key_raw_ex().
  19024. */
  19025. static int test_wc_curve25519_export_key_raw_ex(void)
  19026. {
  19027. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19028. curve25519_key key;
  19029. WC_RNG rng;
  19030. byte privateKey[CURVE25519_KEYSIZE];
  19031. byte publicKey[CURVE25519_KEYSIZE];
  19032. word32 prvkSz;
  19033. word32 pubkSz;
  19034. byte prik[CURVE25519_KEYSIZE];
  19035. byte pubk[CURVE25519_KEYSIZE];
  19036. word32 prksz;
  19037. word32 pbksz;
  19038. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  19039. if(0 != wc_InitRng(&rng)){
  19040. printf(testingFmt, "failed due to wc_InitRng");
  19041. fflush(stdout);
  19042. return 1;
  19043. }
  19044. if(0 != wc_curve25519_init(&key)){
  19045. printf(testingFmt, "failed due to wc_curve25519_init");
  19046. fflush(stdout);
  19047. wc_FreeRng(&rng);
  19048. return 1;
  19049. }
  19050. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  19051. printf(testingFmt, "failed due to wc_curve25519_make_key");
  19052. fflush(stdout);
  19053. wc_curve25519_free(&key);
  19054. wc_FreeRng(&rng);
  19055. return 1;
  19056. }
  19057. /*
  19058. bad-argument-test cases
  19059. target function sould return BAD_FUNC_ARG
  19060. */
  19061. prvkSz = CURVE25519_KEYSIZE;
  19062. pubkSz = CURVE25519_KEYSIZE;
  19063. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  19064. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19065. printf(testingFmt,"failed at bad-arg-case-1.");
  19066. fflush(stdout);
  19067. wc_curve25519_free(&key);
  19068. wc_FreeRng(&rng);
  19069. return 1;
  19070. }
  19071. prvkSz = CURVE25519_KEYSIZE;
  19072. pubkSz = CURVE25519_KEYSIZE;
  19073. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  19074. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19075. printf(testingFmt,"failed at bad-arg-case-2.");
  19076. fflush(stdout);
  19077. wc_curve25519_free(&key);
  19078. wc_FreeRng(&rng);
  19079. return 1;
  19080. }
  19081. prvkSz = CURVE25519_KEYSIZE;
  19082. pubkSz = CURVE25519_KEYSIZE;
  19083. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  19084. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  19085. printf(testingFmt,"failed at bad-arg-case-3.");
  19086. fflush(stdout);
  19087. wc_curve25519_free(&key);
  19088. wc_FreeRng(&rng);
  19089. return 1;
  19090. }
  19091. /* prvkSz = CURVE25519_KEYSIZE; */
  19092. pubkSz = CURVE25519_KEYSIZE;
  19093. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19094. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19095. printf(testingFmt,"failed at bad-arg-case-4.");
  19096. fflush(stdout);
  19097. wc_curve25519_free(&key);
  19098. wc_FreeRng(&rng);
  19099. return 1;
  19100. }
  19101. prvkSz = CURVE25519_KEYSIZE;
  19102. pubkSz = CURVE25519_KEYSIZE;
  19103. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19104. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  19105. printf(testingFmt,"failed at bad-arg-case-5.");
  19106. fflush(stdout);
  19107. wc_curve25519_free(&key);
  19108. wc_FreeRng(&rng);
  19109. return 1;
  19110. }
  19111. prvkSz = CURVE25519_KEYSIZE;
  19112. /* pubkSz = CURVE25519_KEYSIZE; */
  19113. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  19114. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19115. printf(testingFmt,"failed at bad-arg-case-6.");
  19116. fflush(stdout);
  19117. wc_curve25519_free(&key);
  19118. wc_FreeRng(&rng);
  19119. return 1;
  19120. }
  19121. prvkSz = CURVE25519_KEYSIZE;
  19122. pubkSz = CURVE25519_KEYSIZE;
  19123. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  19124. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19125. printf(testingFmt,"failed at bad-arg-case-7.");
  19126. fflush(stdout);
  19127. wc_curve25519_free(&key);
  19128. wc_FreeRng(&rng);
  19129. return 1;
  19130. }
  19131. prvkSz = CURVE25519_KEYSIZE;
  19132. pubkSz = CURVE25519_KEYSIZE;
  19133. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19134. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19135. printf(testingFmt,"failed at bad-arg-case-8.");
  19136. fflush(stdout);
  19137. wc_curve25519_free(&key);
  19138. wc_FreeRng(&rng);
  19139. return 1;
  19140. }
  19141. /* prvkSz = CURVE25519_KEYSIZE; */
  19142. pubkSz = CURVE25519_KEYSIZE;
  19143. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19144. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  19145. printf(testingFmt,"failed at bad-arg-case-9.");
  19146. fflush(stdout);
  19147. wc_curve25519_free(&key);
  19148. wc_FreeRng(&rng);
  19149. return 1;
  19150. }
  19151. prvkSz = CURVE25519_KEYSIZE;
  19152. pubkSz = CURVE25519_KEYSIZE;
  19153. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19154. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  19155. printf(testingFmt,"failed at bad-arg-case-10.");
  19156. fflush(stdout);
  19157. wc_curve25519_free(&key);
  19158. wc_FreeRng(&rng);
  19159. return 1;
  19160. }
  19161. /* illegal value for endien */
  19162. prvkSz = CURVE25519_KEYSIZE;
  19163. /* pubkSz = CURVE25519_KEYSIZE; */
  19164. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19165. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  19166. printf(testingFmt,"failed at bad-arg-case-11.");
  19167. fflush(stdout);
  19168. wc_curve25519_free(&key);
  19169. wc_FreeRng(&rng);
  19170. return 1;
  19171. }
  19172. /*
  19173. cross-testing
  19174. */
  19175. prksz = CURVE25519_KEYSIZE;
  19176. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  19177. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  19178. fflush(stdout);
  19179. wc_curve25519_free(&key);
  19180. wc_FreeRng(&rng);
  19181. return 1;
  19182. }
  19183. pbksz = CURVE25519_KEYSIZE;
  19184. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  19185. printf(testingFmt,"failed due to wc_curve25519_export_public");
  19186. fflush(stdout);
  19187. wc_curve25519_free(&key);
  19188. wc_FreeRng(&rng);
  19189. return 1;
  19190. }
  19191. prvkSz = CURVE25519_KEYSIZE;
  19192. /* pubkSz = CURVE25519_KEYSIZE; */
  19193. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19194. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19195. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  19196. fflush(stdout);
  19197. wc_curve25519_free(&key);
  19198. wc_FreeRng(&rng);
  19199. return 1;
  19200. }
  19201. if( prksz == CURVE25519_KEYSIZE &&
  19202. pbksz == CURVE25519_KEYSIZE &&
  19203. prvkSz == CURVE25519_KEYSIZE &&
  19204. pubkSz == CURVE25519_KEYSIZE ){
  19205. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  19206. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  19207. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  19208. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19209. if( prvkSz == CURVE25519_KEYSIZE &&
  19210. pubkSz == CURVE25519_KEYSIZE ){
  19211. ; /* proceed to the next test */
  19212. }
  19213. else{
  19214. printf(testingFmt,"failed due to key-size-inconsistency");
  19215. fflush(stdout);
  19216. wc_curve25519_free(&key);
  19217. wc_FreeRng(&rng);
  19218. return 1;
  19219. }
  19220. }
  19221. else{
  19222. printf(testingFmt,
  19223. "failed due to wc_curve25519_export_key_raw_ex");
  19224. fflush(stdout);
  19225. wc_curve25519_free(&key);
  19226. wc_FreeRng(&rng);
  19227. return 1;
  19228. }
  19229. }
  19230. else{
  19231. printf(testingFmt,"failed due to key-contents-inconsistency");
  19232. fflush(stdout);
  19233. wc_curve25519_free(&key);
  19234. wc_FreeRng(&rng);
  19235. return 1;
  19236. }
  19237. }
  19238. else{
  19239. printf(testingFmt,"failed due to bad-key-size");
  19240. fflush(stdout);
  19241. wc_curve25519_free(&key);
  19242. wc_FreeRng(&rng);
  19243. return 1;
  19244. }
  19245. /*
  19246. try once with another endian
  19247. */
  19248. prvkSz = CURVE25519_KEYSIZE;
  19249. pubkSz = CURVE25519_KEYSIZE;
  19250. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  19251. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19252. if( prvkSz == CURVE25519_KEYSIZE &&
  19253. pubkSz == CURVE25519_KEYSIZE ){
  19254. /* no more test*/
  19255. printf(resultFmt, passed );
  19256. fflush(stdout);
  19257. wc_curve25519_free(&key);
  19258. wc_FreeRng(&rng);
  19259. return 0;
  19260. }
  19261. else{
  19262. printf(testingFmt,"failed due to key-size-inconsistency");
  19263. fflush(stdout);
  19264. wc_curve25519_free(&key);
  19265. wc_FreeRng(&rng);
  19266. return 1;
  19267. }
  19268. }
  19269. else{
  19270. printf(testingFmt,
  19271. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  19272. fflush(stdout);
  19273. wc_curve25519_free(&key);
  19274. wc_FreeRng(&rng);
  19275. return 1;
  19276. }
  19277. #else
  19278. return 0;
  19279. #endif
  19280. } /* end of test_wc_curve25519_export_key_raw_ex */
  19281. /*
  19282. * Testing wc_curve25519_make_key
  19283. */
  19284. static int test_wc_curve25519_make_key(void)
  19285. {
  19286. int ret = 0;
  19287. #if defined(HAVE_CURVE25519)
  19288. WC_RNG rng;
  19289. curve25519_key key;
  19290. int keysize;
  19291. printf(testingFmt, "wc_curve25519_make_key()");
  19292. ret = wc_curve25519_init(&key);
  19293. if (ret == 0) {
  19294. ret = wc_InitRng(&rng);
  19295. }
  19296. if (ret == 0) {
  19297. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19298. if (ret == 0) {
  19299. keysize = wc_curve25519_size(&key);
  19300. if (keysize != CURVE25519_KEYSIZE) {
  19301. ret = WOLFSSL_FATAL_ERROR;
  19302. }
  19303. }
  19304. if (ret == 0) {
  19305. ret = wc_curve25519_make_key(&rng, keysize, &key);
  19306. }
  19307. }
  19308. /*test bad cases*/
  19309. if (ret == 0) {
  19310. ret = wc_curve25519_make_key(NULL, 0, NULL);
  19311. if (ret == BAD_FUNC_ARG) {
  19312. ret = 0;
  19313. }
  19314. }
  19315. if (ret == 0) {
  19316. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  19317. if (ret == BAD_FUNC_ARG) {
  19318. ret = 0;
  19319. }
  19320. }
  19321. if (ret == 0) {
  19322. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19323. if (ret == BAD_FUNC_ARG) {
  19324. ret = 0;
  19325. }
  19326. }
  19327. if (ret == 0) {
  19328. ret = wc_curve25519_make_key(&rng, 0, &key);
  19329. if (ret == ECC_BAD_ARG_E) {
  19330. ret = 0;
  19331. }
  19332. }
  19333. printf(resultFmt, ret == 0 ? passed : failed);
  19334. wc_curve25519_free(&key);
  19335. wc_FreeRng(&rng);
  19336. #endif
  19337. return ret;
  19338. } /*END test_wc_curve25519_make_key*/
  19339. /*
  19340. * Testing wc_curve25519_shared_secret_ex
  19341. */
  19342. static int test_wc_curve25519_shared_secret_ex(void)
  19343. {
  19344. int ret = 0;
  19345. #if defined(HAVE_CURVE25519)
  19346. WC_RNG rng;
  19347. curve25519_key private_key, public_key;
  19348. byte out[CURVE25519_KEYSIZE];
  19349. word32 outLen = sizeof(out);
  19350. int endian = EC25519_BIG_ENDIAN;
  19351. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  19352. ret = wc_curve25519_init(&private_key);
  19353. if (ret == 0) {
  19354. ret = wc_curve25519_init(&public_key);
  19355. }
  19356. if (ret == 0) {
  19357. ret = wc_InitRng(&rng);
  19358. }
  19359. if (ret == 0) {
  19360. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19361. }
  19362. if (ret == 0) {
  19363. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19364. }
  19365. if (ret == 0) {
  19366. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19367. &outLen, endian);
  19368. }
  19369. /*test bad cases*/
  19370. if (ret == 0) {
  19371. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19372. 0, endian);
  19373. if (ret == 0) {
  19374. ret = -1;
  19375. }
  19376. if (ret == BAD_FUNC_ARG) {
  19377. ret = 0;
  19378. }
  19379. }
  19380. if (ret == 0) {
  19381. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19382. &outLen, endian);
  19383. if (ret == 0) {
  19384. ret = -1;
  19385. }
  19386. else if (ret == BAD_FUNC_ARG) {
  19387. ret = 0;
  19388. }
  19389. }
  19390. if (ret == 0) {
  19391. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19392. &outLen, endian);
  19393. if (ret == 0) {
  19394. ret = -1;
  19395. }
  19396. else if (ret == BAD_FUNC_ARG) {
  19397. ret = 0;
  19398. }
  19399. }
  19400. if (ret == 0) {
  19401. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19402. &outLen, endian);
  19403. if (ret == 0) {
  19404. ret = -1;
  19405. }
  19406. else if (ret == BAD_FUNC_ARG) {
  19407. ret = 0;
  19408. }
  19409. }
  19410. if (ret == 0) {
  19411. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19412. NULL, endian);
  19413. if (ret == 0) {
  19414. ret = -1;
  19415. }
  19416. else if (ret == BAD_FUNC_ARG) {
  19417. ret = 0;
  19418. }
  19419. }
  19420. if (ret == 0) {
  19421. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  19422. *increasing to 0x8f checks for error being returned*/
  19423. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19424. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19425. &outLen, endian);
  19426. if (ret == 0) {
  19427. ret = -1;
  19428. }
  19429. else if (ret == ECC_BAD_ARG_E) {
  19430. ret = 0;
  19431. }
  19432. }
  19433. outLen = outLen - 2;
  19434. if (ret == 0) {
  19435. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19436. &outLen, endian);
  19437. if (ret == 0) {
  19438. ret = -1;
  19439. }
  19440. else if (ret == BAD_FUNC_ARG) {
  19441. ret = 0;
  19442. }
  19443. }
  19444. printf(resultFmt, ret == 0 ? passed : failed);
  19445. fflush(stdout);
  19446. wc_curve25519_free(&private_key);
  19447. wc_curve25519_free(&public_key);
  19448. wc_FreeRng(&rng);
  19449. #endif
  19450. return ret;
  19451. } /*END test_wc_curve25519_shared_secret_ex*/
  19452. /*
  19453. * Testing wc_curve25519_make_pub
  19454. */
  19455. static int test_wc_curve25519_make_pub(void)
  19456. {
  19457. int ret = 0;
  19458. #ifdef HAVE_CURVE25519
  19459. WC_RNG rng;
  19460. curve25519_key key;
  19461. byte out[CURVE25519_KEYSIZE];
  19462. printf(testingFmt, "wc_curve25519_make_pub()");
  19463. ret = wc_curve25519_init(&key);
  19464. if (ret == 0) {
  19465. ret = wc_InitRng(&rng);
  19466. if (ret == 0) {
  19467. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19468. }
  19469. }
  19470. if (ret == 0) {
  19471. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  19472. }
  19473. /*test bad cases*/
  19474. if (ret == 0) {
  19475. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  19476. if (ret == ECC_BAD_ARG_E) {
  19477. ret = 0;
  19478. }
  19479. }
  19480. if (ret == 0) {
  19481. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  19482. if (ret == ECC_BAD_ARG_E) {
  19483. ret = 0;
  19484. }
  19485. }
  19486. if (ret == 0) {
  19487. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  19488. if (ret == ECC_BAD_ARG_E) {
  19489. ret = 0;
  19490. }
  19491. }
  19492. if (ret == 0) {
  19493. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  19494. if (ret == ECC_BAD_ARG_E) {
  19495. ret = 0;
  19496. }
  19497. }
  19498. if (ret == 0) {
  19499. /* verify clamping test */
  19500. key.k[0] |= ~248;
  19501. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19502. if (ret == ECC_BAD_ARG_E) {
  19503. ret = 0;
  19504. }
  19505. key.k[0] &= 248;
  19506. }
  19507. /* repeat the expected-to-succeed test. */
  19508. if (ret == 0) {
  19509. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19510. }
  19511. printf(resultFmt, ret == 0 ? passed : failed);
  19512. fflush(stdout);
  19513. wc_curve25519_free(&key);
  19514. wc_FreeRng(&rng);
  19515. #endif
  19516. return ret;
  19517. } /*END test_wc_curve25519_make_pub */
  19518. /*
  19519. * Testing test_wc_curve25519_export_public_ex
  19520. */
  19521. static int test_wc_curve25519_export_public_ex(void)
  19522. {
  19523. int ret = 0;
  19524. #if defined(HAVE_CURVE25519)
  19525. WC_RNG rng;
  19526. curve25519_key key;
  19527. byte out[CURVE25519_KEYSIZE];
  19528. word32 outLen = sizeof(out);
  19529. int endian = EC25519_BIG_ENDIAN;
  19530. printf(testingFmt, "wc_curve25519_export_public_ex()");
  19531. ret = wc_curve25519_init(&key);
  19532. if (ret == 0) {
  19533. ret = wc_InitRng(&rng);
  19534. }
  19535. if (ret == 0) {
  19536. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19537. if (ret == 0) {
  19538. ret = wc_curve25519_export_public(&key, out, &outLen);
  19539. }
  19540. if (ret == 0) {
  19541. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19542. }
  19543. }
  19544. /*test bad cases*/
  19545. if (ret == 0) {
  19546. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  19547. if (ret == BAD_FUNC_ARG) {
  19548. ret = 0;
  19549. }
  19550. }
  19551. if (ret == 0) {
  19552. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  19553. if (ret == BAD_FUNC_ARG) {
  19554. ret = 0;
  19555. }
  19556. }
  19557. if (ret == 0) {
  19558. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  19559. if (ret == BAD_FUNC_ARG) {
  19560. ret = 0;
  19561. }
  19562. }
  19563. if (ret == 0) {
  19564. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  19565. if (ret == BAD_FUNC_ARG) {
  19566. ret = 0;
  19567. }
  19568. }
  19569. outLen = outLen - 2;
  19570. if (ret == 0) {
  19571. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19572. if (ret == ECC_BAD_ARG_E) {
  19573. ret = 0;
  19574. }
  19575. }
  19576. printf(resultFmt, ret == 0 ? passed : failed);
  19577. fflush(stdout);
  19578. wc_curve25519_free(&key);
  19579. wc_FreeRng(&rng);
  19580. #endif
  19581. return ret;
  19582. } /*END test_wc_curve25519_export_public_ex*/
  19583. /*
  19584. * Testing test_wc_curve25519_import_private_raw_ex
  19585. */
  19586. static int test_wc_curve25519_import_private_raw_ex(void)
  19587. {
  19588. int ret = 0;
  19589. #if defined(HAVE_CURVE25519)
  19590. WC_RNG rng;
  19591. curve25519_key key;
  19592. byte priv[CURVE25519_KEYSIZE];
  19593. byte pub[CURVE25519_KEYSIZE];
  19594. word32 privSz = sizeof(priv);
  19595. word32 pubSz = sizeof(pub);
  19596. int endian = EC25519_BIG_ENDIAN;
  19597. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  19598. ret = wc_curve25519_init(&key);
  19599. if (ret == 0) {
  19600. ret = wc_InitRng(&rng);
  19601. }
  19602. if (ret == 0) {
  19603. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19604. if (ret == 0) {
  19605. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  19606. }
  19607. if (ret == 0) {
  19608. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  19609. }
  19610. if (ret == 0) {
  19611. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19612. &key, endian);
  19613. }
  19614. }
  19615. /*test bad cases*/
  19616. if (ret == 0) {
  19617. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  19618. endian);
  19619. if (ret == BAD_FUNC_ARG) {
  19620. ret = 0;
  19621. }
  19622. }
  19623. if (ret == 0) {
  19624. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  19625. &key, endian);
  19626. if (ret == BAD_FUNC_ARG) {
  19627. ret = 0;
  19628. }
  19629. }
  19630. if (ret == 0) {
  19631. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  19632. &key, endian);
  19633. if (ret == BAD_FUNC_ARG) {
  19634. ret = 0;
  19635. }
  19636. }
  19637. if (ret == 0) {
  19638. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19639. NULL, endian);
  19640. if (ret == BAD_FUNC_ARG) {
  19641. ret = 0;
  19642. }
  19643. }
  19644. if (ret == 0) {
  19645. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  19646. &key, endian);
  19647. if (ret == ECC_BAD_ARG_E) {
  19648. ret = 0;
  19649. }
  19650. }
  19651. if (ret == 0) {
  19652. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  19653. &key, endian);
  19654. if (ret == ECC_BAD_ARG_E) {
  19655. ret = 0;
  19656. }
  19657. }
  19658. if (ret == 0) {
  19659. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  19660. &key, EC25519_LITTLE_ENDIAN);
  19661. }
  19662. printf(resultFmt, ret == 0 ? passed : failed);
  19663. fflush(stdout);
  19664. wc_curve25519_free(&key);
  19665. wc_FreeRng(&rng);
  19666. #endif
  19667. return ret;
  19668. } /*END test_wc_curve25519_import_private_raw_ex*/
  19669. /*
  19670. * Testing test_wc_curve25519_import_private
  19671. */
  19672. static int test_wc_curve25519_import_private(void)
  19673. {
  19674. int ret = 0;
  19675. #if defined(HAVE_CURVE25519)
  19676. curve25519_key key;
  19677. WC_RNG rng;
  19678. byte priv[CURVE25519_KEYSIZE];
  19679. word32 privSz = sizeof(priv);
  19680. printf(testingFmt, "wc_curve25519_import_private()");
  19681. ret = wc_curve25519_init(&key);
  19682. if (ret == 0) {
  19683. ret = wc_InitRng(&rng);
  19684. }
  19685. if (ret == 0) {
  19686. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19687. if (ret == 0) {
  19688. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  19689. }
  19690. }
  19691. if (ret == 0) {
  19692. ret = wc_curve25519_import_private(priv, privSz, &key);
  19693. }
  19694. printf(resultFmt, ret == 0 ? passed : failed);
  19695. fflush(stdout);
  19696. wc_curve25519_free(&key);
  19697. wc_FreeRng(&rng);
  19698. #endif
  19699. return ret;
  19700. } /*END test_wc_curve25519_import*/
  19701. /*
  19702. * Testing test_wc_curve25519_export_private_raw_ex
  19703. */
  19704. static int test_wc_curve25519_export_private_raw_ex(void)
  19705. {
  19706. int ret = 0;
  19707. #if defined(HAVE_CURVE25519)
  19708. curve25519_key key;
  19709. byte out[CURVE25519_KEYSIZE];
  19710. word32 outLen = sizeof(out);
  19711. int endian = EC25519_BIG_ENDIAN;
  19712. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  19713. ret = wc_curve25519_init(&key);
  19714. if (ret == 0) {
  19715. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19716. }
  19717. /*test bad cases*/
  19718. if (ret == 0) {
  19719. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  19720. if (ret == BAD_FUNC_ARG) {
  19721. ret = 0;
  19722. }
  19723. }
  19724. if (ret == 0) {
  19725. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  19726. if (ret == BAD_FUNC_ARG) {
  19727. ret = 0;
  19728. }
  19729. }
  19730. if (ret == 0) {
  19731. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  19732. if (ret == BAD_FUNC_ARG) {
  19733. ret = 0;
  19734. }
  19735. }
  19736. if (ret == 0) {
  19737. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  19738. if (ret == BAD_FUNC_ARG) {
  19739. ret = 0;
  19740. }
  19741. }
  19742. if (ret == 0) {
  19743. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  19744. EC25519_LITTLE_ENDIAN);
  19745. }
  19746. outLen = outLen - 2;
  19747. if (ret == 0) {
  19748. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  19749. if (ret == ECC_BAD_ARG_E) {
  19750. ret = 0;
  19751. }
  19752. }
  19753. printf(resultFmt, ret == 0 ? passed : failed);
  19754. fflush(stdout);
  19755. wc_curve25519_free(&key);
  19756. #endif
  19757. return ret;
  19758. }/*END test_wc_curve25519_export_private_raw_ex*/
  19759. /*
  19760. * Testing wc_ed448_make_key().
  19761. */
  19762. static int test_wc_ed448_make_key(void)
  19763. {
  19764. int ret = 0;
  19765. #if defined(HAVE_ED448)
  19766. ed448_key key;
  19767. WC_RNG rng;
  19768. ret = wc_InitRng(&rng);
  19769. if (ret == 0) {
  19770. ret = wc_ed448_init(&key);
  19771. }
  19772. printf(testingFmt, "wc_ed448_make_key()");
  19773. if (ret == 0) {
  19774. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19775. }
  19776. /* Test bad args. */
  19777. if (ret == 0) {
  19778. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  19779. if (ret == BAD_FUNC_ARG) {
  19780. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  19781. }
  19782. if (ret == BAD_FUNC_ARG) {
  19783. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  19784. }
  19785. if (ret == BAD_FUNC_ARG) {
  19786. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  19787. }
  19788. if (ret == BAD_FUNC_ARG) {
  19789. ret = 0;
  19790. } else if (ret == 0) {
  19791. ret = WOLFSSL_FATAL_ERROR;
  19792. }
  19793. }
  19794. printf(resultFmt, ret == 0 ? passed : failed);
  19795. fflush(stdout);
  19796. if (wc_FreeRng(&rng) && ret == 0) {
  19797. ret = WOLFSSL_FATAL_ERROR;
  19798. }
  19799. wc_ed448_free(&key);
  19800. #endif
  19801. return ret;
  19802. } /* END test_wc_ed448_make_key */
  19803. /*
  19804. * Testing wc_ed448_init()
  19805. */
  19806. static int test_wc_ed448_init(void)
  19807. {
  19808. int ret = 0;
  19809. #if defined(HAVE_ED448)
  19810. ed448_key key;
  19811. printf(testingFmt, "wc_ed448_init()");
  19812. ret = wc_ed448_init(&key);
  19813. /* Test bad args. */
  19814. if (ret == 0) {
  19815. ret = wc_ed448_init(NULL);
  19816. if (ret == BAD_FUNC_ARG) {
  19817. ret = 0;
  19818. } else if (ret == 0) {
  19819. ret = WOLFSSL_FATAL_ERROR;
  19820. }
  19821. }
  19822. printf(resultFmt, ret == 0 ? passed : failed);
  19823. fflush(stdout);
  19824. wc_ed448_free(&key);
  19825. #endif
  19826. return ret;
  19827. } /* END test_wc_ed448_init */
  19828. /*
  19829. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  19830. */
  19831. static int test_wc_ed448_sign_msg(void)
  19832. {
  19833. int ret = 0;
  19834. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  19835. WC_RNG rng;
  19836. ed448_key key;
  19837. byte msg[] = "Everybody gets Friday off.\n";
  19838. byte sig[ED448_SIG_SIZE];
  19839. word32 msglen = sizeof(msg);
  19840. word32 siglen = sizeof(sig);
  19841. word32 badSigLen = sizeof(sig) - 1;
  19842. #ifdef HAVE_ED448_VERIFY
  19843. int verify_ok = 0; /*1 = Verify success.*/
  19844. #endif
  19845. /* Initialize stack variables. */
  19846. XMEMSET(sig, 0, siglen);
  19847. /* Initialize key. */
  19848. ret = wc_InitRng(&rng);
  19849. if (ret == 0) {
  19850. ret = wc_ed448_init(&key);
  19851. if (ret == 0) {
  19852. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  19853. }
  19854. }
  19855. printf(testingFmt, "wc_ed448_sign_msg()");
  19856. if (ret == 0) {
  19857. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  19858. }
  19859. /* Test bad args. */
  19860. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  19861. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  19862. if (ret == BAD_FUNC_ARG) {
  19863. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  19864. }
  19865. if (ret == BAD_FUNC_ARG) {
  19866. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  19867. }
  19868. if (ret == BAD_FUNC_ARG) {
  19869. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  19870. }
  19871. if (ret == BAD_FUNC_ARG) {
  19872. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  19873. NULL, 0);
  19874. }
  19875. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  19876. badSigLen -= 1;
  19877. ret = 0;
  19878. } else if (ret == 0) {
  19879. ret = WOLFSSL_FATAL_ERROR;
  19880. }
  19881. } /* END sign */
  19882. printf(resultFmt, ret == 0 ? passed : failed);
  19883. fflush(stdout);
  19884. #ifdef HAVE_ED448_VERIFY
  19885. printf(testingFmt, "wc_ed448_verify_msg()");
  19886. if (ret == 0) {
  19887. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  19888. &key, NULL, 0);
  19889. if (ret == 0 && verify_ok == 1) {
  19890. ret = 0;
  19891. } else if (ret == 0) {
  19892. ret = WOLFSSL_FATAL_ERROR;
  19893. }
  19894. /* Test bad args. */
  19895. if (ret == 0) {
  19896. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  19897. msglen, &verify_ok, &key,
  19898. NULL, 0),
  19899. BAD_FUNC_ARG);
  19900. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  19901. msglen, &verify_ok, &key,
  19902. NULL, 0),
  19903. BAD_FUNC_ARG);
  19904. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19905. &key, NULL, 0);
  19906. if (ret == BAD_FUNC_ARG) {
  19907. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  19908. &verify_ok, &key, NULL, 0);
  19909. }
  19910. if (ret == BAD_FUNC_ARG) {
  19911. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19912. NULL, &key, NULL, 0);
  19913. }
  19914. if (ret == BAD_FUNC_ARG) {
  19915. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  19916. &verify_ok, NULL, NULL, 0);
  19917. }
  19918. if (ret == BAD_FUNC_ARG) {
  19919. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  19920. &verify_ok, &key, NULL, 0);
  19921. }
  19922. if (ret == BAD_FUNC_ARG) {
  19923. ret = 0;
  19924. } else if (ret == 0) {
  19925. ret = WOLFSSL_FATAL_ERROR;
  19926. }
  19927. }
  19928. } /* END verify. */
  19929. printf(resultFmt, ret == 0 ? passed : failed);
  19930. fflush(stdout);
  19931. #endif /* Verify. */
  19932. if (wc_FreeRng(&rng) && ret == 0) {
  19933. ret = WOLFSSL_FATAL_ERROR;
  19934. }
  19935. wc_ed448_free(&key);
  19936. #endif
  19937. return ret;
  19938. } /* END test_wc_ed448_sign_msg */
  19939. /*
  19940. * Testing wc_ed448_import_public()
  19941. */
  19942. static int test_wc_ed448_import_public(void)
  19943. {
  19944. int ret = 0;
  19945. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19946. WC_RNG rng;
  19947. ed448_key pubKey;
  19948. const byte in[] =
  19949. "Ed448PublicKeyUnitTest.................................\n";
  19950. word32 inlen = sizeof(in);
  19951. ret = wc_InitRng(&rng);
  19952. if (ret == 0) {
  19953. ret = wc_ed448_init(&pubKey);
  19954. if (ret == 0) {
  19955. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  19956. }
  19957. }
  19958. printf(testingFmt, "wc_ed448_import_public()");
  19959. if (ret == 0) {
  19960. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  19961. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  19962. ret = 0;
  19963. } else {
  19964. ret = WOLFSSL_FATAL_ERROR;
  19965. }
  19966. /* Test bad args. */
  19967. if (ret == 0) {
  19968. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  19969. if (ret == BAD_FUNC_ARG) {
  19970. ret = wc_ed448_import_public(in, inlen, NULL);
  19971. }
  19972. if (ret == BAD_FUNC_ARG) {
  19973. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  19974. }
  19975. if (ret == BAD_FUNC_ARG) {
  19976. ret = 0;
  19977. } else if (ret == 0) {
  19978. ret = WOLFSSL_FATAL_ERROR;
  19979. }
  19980. }
  19981. }
  19982. printf(resultFmt, ret == 0 ? passed : failed);
  19983. fflush(stdout);
  19984. if (wc_FreeRng(&rng) && ret == 0) {
  19985. ret = WOLFSSL_FATAL_ERROR;
  19986. }
  19987. wc_ed448_free(&pubKey);
  19988. #endif
  19989. return ret;
  19990. } /* END wc_ed448_import_public */
  19991. /*
  19992. * Testing wc_ed448_import_private_key()
  19993. */
  19994. static int test_wc_ed448_import_private_key(void)
  19995. {
  19996. int ret = 0;
  19997. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  19998. WC_RNG rng;
  19999. ed448_key key;
  20000. const byte privKey[] =
  20001. "Ed448PrivateKeyUnitTest................................\n";
  20002. const byte pubKey[] =
  20003. "Ed448PublicKeyUnitTest.................................\n";
  20004. word32 privKeySz = sizeof(privKey);
  20005. word32 pubKeySz = sizeof(pubKey);
  20006. #ifdef HAVE_ED448_KEY_EXPORT
  20007. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20008. word32 bothKeysSz = sizeof(bothKeys);
  20009. #endif
  20010. ret = wc_InitRng(&rng);
  20011. if (ret != 0) {
  20012. return ret;
  20013. }
  20014. ret = wc_ed448_init(&key);
  20015. if (ret != 0) {
  20016. wc_FreeRng(&rng);
  20017. return ret;
  20018. }
  20019. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20020. printf(testingFmt, "wc_ed448_import_private_key()");
  20021. if (ret == 0) {
  20022. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20023. pubKeySz, &key, 1);
  20024. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20025. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20026. ret = WOLFSSL_FATAL_ERROR;
  20027. }
  20028. }
  20029. #ifdef HAVE_ED448_KEY_EXPORT
  20030. if (ret == 0)
  20031. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  20032. if (ret == 0) {
  20033. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20034. &key, 1);
  20035. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20036. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20037. ret = WOLFSSL_FATAL_ERROR;
  20038. }
  20039. }
  20040. #endif
  20041. /* Test bad args. */
  20042. if (ret == 0) {
  20043. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20044. &key);
  20045. if (ret == BAD_FUNC_ARG) {
  20046. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20047. pubKeySz, &key);
  20048. }
  20049. if (ret == BAD_FUNC_ARG) {
  20050. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20051. pubKeySz, NULL);
  20052. }
  20053. if (ret == BAD_FUNC_ARG) {
  20054. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20055. pubKeySz, &key);
  20056. }
  20057. if (ret == BAD_FUNC_ARG) {
  20058. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20059. pubKeySz - 1, &key);
  20060. }
  20061. if (ret == BAD_FUNC_ARG) {
  20062. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20063. 0, &key);
  20064. }
  20065. if (ret == BAD_FUNC_ARG) {
  20066. ret = 0;
  20067. } else if (ret == 0) {
  20068. ret = WOLFSSL_FATAL_ERROR;
  20069. }
  20070. }
  20071. printf(resultFmt, ret == 0 ? passed : failed);
  20072. fflush(stdout);
  20073. if (wc_FreeRng(&rng) && ret == 0) {
  20074. ret = WOLFSSL_FATAL_ERROR;
  20075. }
  20076. wc_ed448_free(&key);
  20077. #endif
  20078. return ret;
  20079. } /* END test_wc_ed448_import_private_key */
  20080. /*
  20081. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20082. */
  20083. static int test_wc_ed448_export(void)
  20084. {
  20085. int ret = 0;
  20086. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20087. WC_RNG rng;
  20088. ed448_key key;
  20089. byte priv[ED448_PRV_KEY_SIZE];
  20090. byte pub[ED448_PUB_KEY_SIZE];
  20091. word32 privSz = sizeof(priv);
  20092. word32 pubSz = sizeof(pub);
  20093. ret = wc_InitRng(&rng);
  20094. if (ret != 0) {
  20095. return ret;
  20096. }
  20097. ret = wc_ed448_init(&key);
  20098. if (ret != 0) {
  20099. wc_FreeRng(&rng);
  20100. return ret;
  20101. }
  20102. if (ret == 0) {
  20103. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20104. }
  20105. printf(testingFmt, "wc_ed448_export_public()");
  20106. if (ret == 0) {
  20107. ret = wc_ed448_export_public(&key, pub, &pubSz);
  20108. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  20109. XMEMCMP(key.p, pub, pubSz) != 0)) {
  20110. ret = WOLFSSL_FATAL_ERROR;
  20111. }
  20112. if (ret == 0) {
  20113. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  20114. if (ret == BAD_FUNC_ARG) {
  20115. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  20116. }
  20117. if (ret == BAD_FUNC_ARG) {
  20118. ret = wc_ed448_export_public(&key, pub, NULL);
  20119. }
  20120. if (ret == BAD_FUNC_ARG) {
  20121. ret = 0;
  20122. } else if (ret == 0) {
  20123. ret = WOLFSSL_FATAL_ERROR;
  20124. }
  20125. }
  20126. }
  20127. printf(resultFmt, ret == 0 ? passed : failed);
  20128. fflush(stdout);
  20129. printf(testingFmt, "wc_ed448_export_private_only()");
  20130. if (ret == 0) {
  20131. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  20132. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  20133. XMEMCMP(key.k, priv, privSz) != 0)) {
  20134. ret = WOLFSSL_FATAL_ERROR;
  20135. }
  20136. if (ret == 0) {
  20137. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  20138. if (ret == BAD_FUNC_ARG) {
  20139. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  20140. }
  20141. if (ret == BAD_FUNC_ARG) {
  20142. ret = wc_ed448_export_private_only(&key, priv, NULL);
  20143. }
  20144. if (ret == BAD_FUNC_ARG) {
  20145. ret = 0;
  20146. } else if (ret == 0) {
  20147. ret = WOLFSSL_FATAL_ERROR;
  20148. }
  20149. }
  20150. }
  20151. printf(resultFmt, ret == 0 ? passed : failed);
  20152. fflush(stdout);
  20153. if (wc_FreeRng(&rng) && ret == 0) {
  20154. ret = WOLFSSL_FATAL_ERROR;
  20155. }
  20156. wc_ed448_free(&key);
  20157. #endif
  20158. return ret;
  20159. } /* END test_wc_ed448_export */
  20160. /*
  20161. * Testing wc_ed448_size()
  20162. */
  20163. static int test_wc_ed448_size(void)
  20164. {
  20165. int ret = 0;
  20166. #if defined(HAVE_ED448)
  20167. WC_RNG rng;
  20168. ed448_key key;
  20169. ret = wc_InitRng(&rng);
  20170. if (ret != 0) {
  20171. return ret;
  20172. }
  20173. ret = wc_ed448_init(&key);
  20174. if (ret != 0) {
  20175. wc_FreeRng(&rng);
  20176. return ret;
  20177. }
  20178. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20179. if (ret != 0) {
  20180. wc_FreeRng(&rng);
  20181. wc_ed448_free(&key);
  20182. return ret;
  20183. }
  20184. printf(testingFmt, "wc_ed448_size()");
  20185. ret = wc_ed448_size(&key);
  20186. /* Test bad args. */
  20187. if (ret == ED448_KEY_SIZE) {
  20188. ret = wc_ed448_size(NULL);
  20189. if (ret == BAD_FUNC_ARG) {
  20190. ret = 0;
  20191. }
  20192. }
  20193. printf(resultFmt, ret == 0 ? passed : failed);
  20194. fflush(stdout);
  20195. if (ret == 0) {
  20196. printf(testingFmt, "wc_ed448_sig_size()");
  20197. ret = wc_ed448_sig_size(&key);
  20198. if (ret == ED448_SIG_SIZE) {
  20199. ret = 0;
  20200. }
  20201. /* Test bad args. */
  20202. if (ret == 0) {
  20203. ret = wc_ed448_sig_size(NULL);
  20204. if (ret == BAD_FUNC_ARG) {
  20205. ret = 0;
  20206. }
  20207. }
  20208. printf(resultFmt, ret == 0 ? passed : failed);
  20209. fflush(stdout);
  20210. } /* END wc_ed448_sig_size() */
  20211. if (ret == 0) {
  20212. printf(testingFmt, "wc_ed448_pub_size");
  20213. ret = wc_ed448_pub_size(&key);
  20214. if (ret == ED448_PUB_KEY_SIZE) {
  20215. ret = 0;
  20216. }
  20217. if (ret == 0) {
  20218. ret = wc_ed448_pub_size(NULL);
  20219. if (ret == BAD_FUNC_ARG) {
  20220. ret = 0;
  20221. }
  20222. }
  20223. printf(resultFmt, ret == 0 ? passed : failed);
  20224. fflush(stdout);
  20225. } /* END wc_ed448_pub_size */
  20226. if (ret == 0) {
  20227. printf(testingFmt, "wc_ed448_priv_size");
  20228. ret = wc_ed448_priv_size(&key);
  20229. if (ret == ED448_PRV_KEY_SIZE) {
  20230. ret = 0;
  20231. }
  20232. if (ret == 0) {
  20233. ret = wc_ed448_priv_size(NULL);
  20234. if (ret == BAD_FUNC_ARG) {
  20235. ret = 0;
  20236. }
  20237. }
  20238. printf(resultFmt, ret == 0 ? passed : failed);
  20239. fflush(stdout);
  20240. } /* END wc_ed448_pub_size */
  20241. if (wc_FreeRng(&rng) && ret == 0) {
  20242. ret = WOLFSSL_FATAL_ERROR;
  20243. }
  20244. wc_ed448_free(&key);
  20245. #endif
  20246. return ret;
  20247. } /* END test_wc_ed448_size */
  20248. /*
  20249. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20250. */
  20251. static int test_wc_ed448_exportKey(void)
  20252. {
  20253. int ret = 0;
  20254. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20255. WC_RNG rng;
  20256. ed448_key key;
  20257. byte priv[ED448_PRV_KEY_SIZE];
  20258. byte pub[ED448_PUB_KEY_SIZE];
  20259. byte privOnly[ED448_PRV_KEY_SIZE];
  20260. word32 privSz = sizeof(priv);
  20261. word32 pubSz = sizeof(pub);
  20262. word32 privOnlySz = sizeof(privOnly);
  20263. ret = wc_InitRng(&rng);
  20264. if (ret != 0) {
  20265. return ret;
  20266. }
  20267. ret = wc_ed448_init(&key);
  20268. if (ret != 0) {
  20269. wc_FreeRng(&rng);
  20270. return ret;
  20271. }
  20272. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20273. if (ret != 0) {
  20274. wc_FreeRng(&rng);
  20275. wc_ed448_free(&key);
  20276. return ret;
  20277. }
  20278. printf(testingFmt, "wc_ed448_export_private()");
  20279. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  20280. if (ret == 0) {
  20281. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  20282. if (ret == BAD_FUNC_ARG) {
  20283. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  20284. }
  20285. if (ret == BAD_FUNC_ARG) {
  20286. ret = wc_ed448_export_private(&key, privOnly, NULL);
  20287. }
  20288. if (ret == BAD_FUNC_ARG) {
  20289. ret = 0;
  20290. } else if (ret == 0) {
  20291. ret = WOLFSSL_FATAL_ERROR;
  20292. }
  20293. }
  20294. printf(resultFmt, ret == 0 ? passed : failed);
  20295. fflush(stdout);
  20296. if (ret == 0) {
  20297. printf(testingFmt, "wc_ed448_export_key()");
  20298. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  20299. if (ret == 0) {
  20300. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  20301. if (ret == BAD_FUNC_ARG) {
  20302. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  20303. }
  20304. if (ret == BAD_FUNC_ARG) {
  20305. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  20306. }
  20307. if (ret == BAD_FUNC_ARG) {
  20308. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  20309. }
  20310. if (ret == BAD_FUNC_ARG) {
  20311. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  20312. }
  20313. if (ret == BAD_FUNC_ARG) {
  20314. ret = 0;
  20315. } else if (ret == 0) {
  20316. ret = WOLFSSL_FATAL_ERROR;
  20317. }
  20318. }
  20319. printf(resultFmt, ret == 0 ? passed : failed);
  20320. fflush(stdout);
  20321. } /* END wc_ed448_export_key() */
  20322. /* Cross check output. */
  20323. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20324. ret = WOLFSSL_FATAL_ERROR;
  20325. }
  20326. if (wc_FreeRng(&rng) && ret == 0) {
  20327. ret = WOLFSSL_FATAL_ERROR;
  20328. }
  20329. wc_ed448_free(&key);
  20330. #endif
  20331. return ret;
  20332. } /* END test_wc_ed448_exportKey */
  20333. /*
  20334. * Testing wc_Ed448PublicKeyToDer
  20335. */
  20336. static int test_wc_Ed448PublicKeyToDer(void)
  20337. {
  20338. int ret = 0;
  20339. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20340. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20341. int tmp;
  20342. ed448_key key;
  20343. byte derBuf[1024];
  20344. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  20345. /* Test bad args */
  20346. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20347. if (tmp != BAD_FUNC_ARG) {
  20348. ret = WOLFSSL_FATAL_ERROR;
  20349. }
  20350. if (ret == 0) {
  20351. wc_ed448_init(&key);
  20352. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20353. if (tmp != BUFFER_E) {
  20354. ret = WOLFSSL_FATAL_ERROR;
  20355. }
  20356. wc_ed448_free(&key);
  20357. }
  20358. /* Test good args */
  20359. if (ret == 0) {
  20360. WC_RNG rng;
  20361. ret = wc_InitRng(&rng);
  20362. if (ret != 0) {
  20363. return ret;
  20364. }
  20365. ret = wc_ed448_init(&key);
  20366. if (ret != 0) {
  20367. wc_FreeRng(&rng);
  20368. return ret;
  20369. }
  20370. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20371. if (ret != 0) {
  20372. wc_FreeRng(&rng);
  20373. wc_ed448_free(&key);
  20374. return ret;
  20375. }
  20376. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20377. if (tmp <= 0) {
  20378. ret = WOLFSSL_FATAL_ERROR;
  20379. }
  20380. wc_FreeRng(&rng);
  20381. wc_ed448_free(&key);
  20382. }
  20383. printf(resultFmt, ret == 0 ? passed : failed);
  20384. fflush(stdout);
  20385. #endif
  20386. return ret;
  20387. } /* END testing wc_Ed448PublicKeyToDer */
  20388. /*
  20389. * Testing wc_curve448_init and wc_curve448_free.
  20390. */
  20391. static int test_wc_curve448_init(void)
  20392. {
  20393. int ret = 0;
  20394. #if defined(HAVE_CURVE448)
  20395. curve448_key key;
  20396. printf(testingFmt, "wc_curve448_init()");
  20397. ret = wc_curve448_init(&key);
  20398. /* Test bad args for wc_curve448_init */
  20399. if (ret == 0) {
  20400. ret = wc_curve448_init(NULL);
  20401. if (ret == BAD_FUNC_ARG) {
  20402. ret = 0;
  20403. } else if (ret == 0) {
  20404. ret = WOLFSSL_FATAL_ERROR;
  20405. }
  20406. }
  20407. printf(resultFmt, ret == 0 ? passed : failed);
  20408. fflush(stdout);
  20409. /* Test good args for wc_curve_448_free */
  20410. wc_curve448_free(&key);
  20411. wc_curve448_free(NULL);
  20412. #endif
  20413. return ret;
  20414. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20415. /*
  20416. * Testing wc_curve448_make_key
  20417. */
  20418. static int test_wc_curve448_make_key(void)
  20419. {
  20420. int ret = 0;
  20421. #if defined(HAVE_CURVE448)
  20422. WC_RNG rng;
  20423. curve448_key key;
  20424. int keysize;
  20425. printf(testingFmt, "wc_curve448_make_key()");
  20426. ret = wc_curve448_init(&key);
  20427. if (ret == 0) {
  20428. ret = wc_InitRng(&rng);
  20429. }
  20430. if (ret == 0) {
  20431. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20432. if (ret == 0) {
  20433. keysize = wc_curve448_size(&key);
  20434. if (keysize != CURVE448_KEY_SIZE) {
  20435. ret = WOLFSSL_FATAL_ERROR;
  20436. }
  20437. }
  20438. if (ret == 0) {
  20439. ret = wc_curve448_make_key(&rng, keysize, &key);
  20440. }
  20441. }
  20442. /*test bad cases*/
  20443. if (ret == 0) {
  20444. ret = wc_curve448_make_key(NULL, 0, NULL);
  20445. if (ret == BAD_FUNC_ARG) {
  20446. ret = 0;
  20447. }
  20448. }
  20449. if (ret == 0) {
  20450. ret = wc_curve448_make_key(&rng, keysize, NULL);
  20451. if (ret == BAD_FUNC_ARG) {
  20452. ret = 0;
  20453. }
  20454. }
  20455. if (ret == 0) {
  20456. ret = wc_curve448_make_key(NULL, keysize, &key);
  20457. if (ret == BAD_FUNC_ARG) {
  20458. ret = 0;
  20459. }
  20460. }
  20461. if (ret == 0) {
  20462. ret = wc_curve448_make_key(&rng, 0, &key);
  20463. if (ret == ECC_BAD_ARG_E) {
  20464. ret = 0;
  20465. }
  20466. }
  20467. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20468. ret = WOLFSSL_FATAL_ERROR;
  20469. }
  20470. printf(resultFmt, ret == 0 ? passed : failed);
  20471. fflush(stdout);
  20472. wc_curve448_free(&key);
  20473. #endif
  20474. return ret;
  20475. } /*END test_wc_curve448_make_key*/
  20476. /*
  20477. * Testing test_wc_curve448_shared_secret_ex
  20478. */
  20479. static int test_wc_curve448_shared_secret_ex(void)
  20480. {
  20481. int ret = 0;
  20482. #if defined(HAVE_CURVE448)
  20483. WC_RNG rng;
  20484. curve448_key private_key, public_key;
  20485. byte out[CURVE448_KEY_SIZE];
  20486. word32 outLen = sizeof(out);
  20487. int endian = EC448_BIG_ENDIAN;
  20488. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  20489. ret = wc_curve448_init(&private_key);
  20490. if (ret == 0) {
  20491. ret = wc_InitRng(&rng);
  20492. if (ret == 0) {
  20493. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  20494. }
  20495. }
  20496. if (ret == 0) {
  20497. ret = wc_curve448_init(&public_key);
  20498. }
  20499. if (ret == 0) {
  20500. if (ret == 0) {
  20501. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  20502. }
  20503. }
  20504. if (ret == 0) {
  20505. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20506. &outLen, endian);
  20507. }
  20508. /*test bad cases*/
  20509. if (ret == 0) {
  20510. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  20511. 0, endian);
  20512. if (ret == BAD_FUNC_ARG) {
  20513. ret = 0;
  20514. }
  20515. }
  20516. if (ret == 0) {
  20517. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  20518. &outLen, endian);
  20519. if (ret == BAD_FUNC_ARG) {
  20520. ret = 0;
  20521. }
  20522. }
  20523. if (ret == 0) {
  20524. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  20525. &outLen, endian);
  20526. if (ret == BAD_FUNC_ARG) {
  20527. ret = 0;
  20528. }
  20529. }
  20530. if (ret == 0) {
  20531. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20532. &outLen, endian);
  20533. if (ret == BAD_FUNC_ARG) {
  20534. ret = 0;
  20535. }
  20536. }
  20537. if (ret == 0) {
  20538. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20539. NULL, endian);
  20540. if (ret == BAD_FUNC_ARG) {
  20541. ret = 0;
  20542. }
  20543. }
  20544. outLen = outLen - 2;
  20545. if (ret == 0) {
  20546. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20547. &outLen, endian);
  20548. if (ret == BAD_FUNC_ARG) {
  20549. ret = 0;
  20550. }
  20551. }
  20552. printf(resultFmt, ret == 0 ? passed : failed);
  20553. fflush(stdout);
  20554. wc_curve448_free(&private_key);
  20555. wc_curve448_free(&public_key);
  20556. wc_FreeRng(&rng);
  20557. #endif
  20558. return ret;
  20559. } /*END test_wc_curve448_shared_secret_ex*/
  20560. /*
  20561. * Testing test_wc_curve448_export_public_ex
  20562. */
  20563. static int test_wc_curve448_export_public_ex(void)
  20564. {
  20565. int ret = 0;
  20566. #if defined(HAVE_CURVE448)
  20567. WC_RNG rng;
  20568. curve448_key key;
  20569. byte out[CURVE448_KEY_SIZE];
  20570. word32 outLen = sizeof(out);
  20571. int endian = EC448_BIG_ENDIAN;
  20572. printf(testingFmt, "wc_curve448_export_public_ex()");
  20573. ret = wc_curve448_init(&key);
  20574. if (ret == 0) {
  20575. ret = wc_InitRng(&rng);
  20576. }
  20577. if (ret == 0) {
  20578. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20579. if (ret == 0){
  20580. ret = wc_curve448_export_public(&key, out, &outLen);
  20581. }
  20582. if (ret == 0) {
  20583. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20584. }
  20585. }
  20586. /*test bad cases*/
  20587. if (ret == 0) {
  20588. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  20589. if (ret == BAD_FUNC_ARG) {
  20590. ret = 0;
  20591. }
  20592. }
  20593. if (ret == 0) {
  20594. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  20595. if (ret == BAD_FUNC_ARG) {
  20596. ret = 0;
  20597. }
  20598. }
  20599. if (ret == 0) {
  20600. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  20601. if (ret == BAD_FUNC_ARG) {
  20602. ret = 0;
  20603. }
  20604. }
  20605. if (ret == 0) {
  20606. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  20607. if (ret == BAD_FUNC_ARG) {
  20608. ret = 0;
  20609. }
  20610. }
  20611. outLen = outLen - 2;
  20612. if (ret == 0) {
  20613. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  20614. if (ret == ECC_BAD_ARG_E) {
  20615. ret = 0;
  20616. }
  20617. }
  20618. printf(resultFmt, ret == 0 ? passed : failed);
  20619. fflush(stdout);
  20620. wc_curve448_free(&key);
  20621. wc_FreeRng(&rng);
  20622. #endif
  20623. return ret;
  20624. } /*END test_wc_curve448_export_public_ex*/
  20625. /*
  20626. * Testing test_wc_curve448_export_private_raw_ex
  20627. */
  20628. static int test_wc_curve448_export_private_raw_ex(void)
  20629. {
  20630. int ret = 0;
  20631. #if defined(HAVE_CURVE448)
  20632. curve448_key key;
  20633. byte out[CURVE448_KEY_SIZE];
  20634. word32 outLen = sizeof(out);
  20635. int endian = EC448_BIG_ENDIAN;
  20636. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  20637. ret = wc_curve448_init(&key);
  20638. if (ret == 0) {
  20639. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20640. }
  20641. /*test bad cases*/
  20642. if (ret == 0) {
  20643. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  20644. if (ret == BAD_FUNC_ARG) {
  20645. ret = 0;
  20646. }
  20647. }
  20648. if (ret == 0) {
  20649. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  20650. if (ret == BAD_FUNC_ARG) {
  20651. ret = 0;
  20652. }
  20653. }
  20654. if (ret == 0) {
  20655. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  20656. if (ret == BAD_FUNC_ARG) {
  20657. ret = 0;
  20658. }
  20659. }
  20660. if (ret == 0) {
  20661. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  20662. if (ret == BAD_FUNC_ARG) {
  20663. ret = 0;
  20664. }
  20665. }
  20666. if (ret == 0) {
  20667. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  20668. EC448_LITTLE_ENDIAN);
  20669. }
  20670. outLen = outLen - 2;
  20671. if (ret == 0) {
  20672. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  20673. if (ret == ECC_BAD_ARG_E) {
  20674. ret = 0;
  20675. }
  20676. }
  20677. printf(resultFmt, ret == 0 ? passed : failed);
  20678. fflush(stdout);
  20679. wc_curve448_free(&key);
  20680. #endif
  20681. return ret;
  20682. }/*END test_wc_curve448_export_private_raw_ex*/
  20683. /*
  20684. * Testing test_wc_curve448_import_private_raw_ex
  20685. */
  20686. static int test_wc_curve448_import_private_raw_ex(void)
  20687. {
  20688. int ret = 0;
  20689. #if defined(HAVE_CURVE448)
  20690. WC_RNG rng;
  20691. curve448_key key;
  20692. byte priv[CURVE448_KEY_SIZE];
  20693. byte pub[CURVE448_KEY_SIZE];
  20694. word32 privSz = sizeof(priv);
  20695. word32 pubSz = sizeof(pub);
  20696. int endian = EC448_BIG_ENDIAN;
  20697. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  20698. ret = wc_curve448_init(&key);
  20699. if (ret == 0) {
  20700. ret = wc_InitRng(&rng);
  20701. }
  20702. if (ret == 0) {
  20703. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20704. if (ret == 0){
  20705. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20706. }
  20707. if (ret == 0){
  20708. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20709. }
  20710. if (ret == 0) {
  20711. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20712. &key, endian);
  20713. }
  20714. }
  20715. /*test bad cases*/
  20716. if (ret == 0) {
  20717. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  20718. if (ret == BAD_FUNC_ARG) {
  20719. ret = 0;
  20720. }
  20721. }
  20722. if (ret == 0) {
  20723. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20724. &key, endian);
  20725. if (ret == BAD_FUNC_ARG) {
  20726. ret = 0;
  20727. }
  20728. }
  20729. if (ret == 0) {
  20730. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20731. &key, endian);
  20732. if (ret == BAD_FUNC_ARG) {
  20733. ret = 0;
  20734. }
  20735. }
  20736. if (ret == 0) {
  20737. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20738. NULL, endian);
  20739. if (ret == BAD_FUNC_ARG) {
  20740. ret = 0;
  20741. }
  20742. }
  20743. if (ret == 0) {
  20744. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  20745. &key, endian);
  20746. if (ret == ECC_BAD_ARG_E) {
  20747. ret = 0;
  20748. }
  20749. }
  20750. if (ret == 0) {
  20751. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  20752. &key, endian);
  20753. if (ret == ECC_BAD_ARG_E) {
  20754. ret = 0;
  20755. }
  20756. }
  20757. if (ret == 0) {
  20758. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  20759. &key, EC448_LITTLE_ENDIAN);
  20760. }
  20761. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20762. ret = WOLFSSL_FATAL_ERROR;
  20763. }
  20764. printf(resultFmt, ret == 0 ? passed : failed);
  20765. fflush(stdout);
  20766. wc_curve448_free(&key);
  20767. #endif
  20768. return ret;
  20769. } /*END test_wc_curve448_import_private_raw_ex*/
  20770. /*
  20771. * Testing test_curve448_export_key_raw
  20772. */
  20773. static int test_wc_curve448_export_key_raw(void)
  20774. {
  20775. int ret = 0;
  20776. #if defined(HAVE_CURVE448)
  20777. WC_RNG rng;
  20778. curve448_key key;
  20779. byte priv[CURVE448_KEY_SIZE];
  20780. byte pub[CURVE448_KEY_SIZE];
  20781. word32 privSz = sizeof(priv);
  20782. word32 pubSz = sizeof(pub);
  20783. printf(testingFmt, "wc_curve448_export_key_raw()");
  20784. ret = wc_curve448_init(&key);
  20785. if (ret == 0) {
  20786. ret = wc_InitRng(&rng);
  20787. }
  20788. if (ret == 0) {
  20789. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20790. if (ret == 0) {
  20791. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20792. }
  20793. if (ret == 0) {
  20794. ret = wc_curve448_export_public(&key, pub, &pubSz);
  20795. }
  20796. if (ret == 0) {
  20797. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  20798. }
  20799. }
  20800. printf(resultFmt, ret == 0 ? passed : failed);
  20801. fflush(stdout);
  20802. wc_curve448_free(&key);
  20803. wc_FreeRng(&rng);
  20804. #endif
  20805. return ret;
  20806. }/*END test_wc_curve448_import_private_raw_ex*/
  20807. /*
  20808. * Testing test_wc_curve448_import_private
  20809. */
  20810. static int test_wc_curve448_import_private(void)
  20811. {
  20812. int ret = 0;
  20813. #if defined(HAVE_CURVE448)
  20814. curve448_key key;
  20815. WC_RNG rng;
  20816. byte priv[CURVE448_KEY_SIZE];
  20817. word32 privSz = sizeof(priv);
  20818. printf(testingFmt, "wc_curve448_import_private()");
  20819. ret = wc_curve448_init(&key);
  20820. if (ret == 0) {
  20821. ret = wc_InitRng(&rng);
  20822. }
  20823. if (ret == 0) {
  20824. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20825. if (ret == 0) {
  20826. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  20827. }
  20828. }
  20829. if (ret == 0) {
  20830. ret = wc_curve448_import_private(priv, privSz, &key);
  20831. }
  20832. printf(resultFmt, ret == 0 ? passed : failed);
  20833. fflush(stdout);
  20834. wc_curve448_free(&key);
  20835. wc_FreeRng(&rng);
  20836. #endif
  20837. return ret;
  20838. } /*END test_wc_curve448_import*/
  20839. /*
  20840. * Testing test_wc_curve448_size.
  20841. */
  20842. static int test_wc_curve448_size(void)
  20843. {
  20844. int ret = 0;
  20845. #if defined(HAVE_CURVE448)
  20846. curve448_key key;
  20847. printf(testingFmt, "wc_curve448_size()");
  20848. ret = wc_curve448_init(&key);
  20849. /* Test good args for wc_curve448_size */
  20850. if (ret == 0) {
  20851. ret = wc_curve448_size(&key);
  20852. }
  20853. /* Test bad args for wc_curve448_size */
  20854. if (ret != 0) {
  20855. ret = wc_curve448_size(NULL);
  20856. }
  20857. printf(resultFmt, ret == 0 ? passed : failed);
  20858. fflush(stdout);
  20859. wc_curve448_free(&key);
  20860. #endif
  20861. return ret;
  20862. } /* END test_wc_curve448_size*/
  20863. /*
  20864. * Testing wc_ecc_make_key.
  20865. */
  20866. static int test_wc_ecc_make_key(void)
  20867. {
  20868. int ret = 0;
  20869. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20870. WC_RNG rng;
  20871. ecc_key key;
  20872. printf(testingFmt, "wc_ecc_make_key()");
  20873. ret = wc_InitRng(&rng);
  20874. if (ret != 0)
  20875. return ret;
  20876. ret = wc_ecc_init(&key);
  20877. if (ret == 0) {
  20878. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20879. #if defined(WOLFSSL_ASYNC_CRYPT)
  20880. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20881. #endif
  20882. /* Pass in bad args. */
  20883. if (ret == 0) {
  20884. ret = wc_ecc_make_key(NULL, KEY14, &key);
  20885. if (ret == BAD_FUNC_ARG) {
  20886. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  20887. }
  20888. if (ret == BAD_FUNC_ARG) {
  20889. ret = 0;
  20890. } else if (ret == 0) {
  20891. ret = WOLFSSL_FATAL_ERROR;
  20892. }
  20893. }
  20894. wc_ecc_free(&key);
  20895. }
  20896. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20897. ret = WOLFSSL_FATAL_ERROR;
  20898. }
  20899. #ifdef FP_ECC
  20900. wc_ecc_fp_free();
  20901. #endif
  20902. printf(resultFmt, ret == 0 ? passed : failed);
  20903. fflush(stdout);
  20904. #endif
  20905. return ret;
  20906. } /* END test_wc_ecc_make_key */
  20907. /*
  20908. * Testing wc_ecc_init()
  20909. */
  20910. static int test_wc_ecc_init(void)
  20911. {
  20912. int ret = 0;
  20913. #ifdef HAVE_ECC
  20914. ecc_key key;
  20915. printf(testingFmt, "wc_ecc_init()");
  20916. ret = wc_ecc_init(&key);
  20917. /* Pass in bad args. */
  20918. if (ret == 0) {
  20919. ret = wc_ecc_init(NULL);
  20920. if (ret == BAD_FUNC_ARG) {
  20921. ret = 0;
  20922. } else if (ret == 0) {
  20923. ret = WOLFSSL_FATAL_ERROR;
  20924. }
  20925. }
  20926. printf(resultFmt, ret == 0 ? passed : failed);
  20927. fflush(stdout);
  20928. wc_ecc_free(&key);
  20929. #endif
  20930. return ret;
  20931. } /* END test_wc_ecc_init */
  20932. /*
  20933. * Testing wc_ecc_check_key()
  20934. */
  20935. static int test_wc_ecc_check_key(void)
  20936. {
  20937. int ret = 0;
  20938. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  20939. WC_RNG rng;
  20940. ecc_key key;
  20941. XMEMSET(&rng, 0, sizeof(rng));
  20942. XMEMSET(&key, 0, sizeof(key));
  20943. ret = wc_InitRng(&rng);
  20944. if (ret == 0) {
  20945. ret = wc_ecc_init(&key);
  20946. if (ret == 0) {
  20947. ret = wc_ecc_make_key(&rng, KEY14, &key);
  20948. #if defined(WOLFSSL_ASYNC_CRYPT)
  20949. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  20950. #endif
  20951. }
  20952. }
  20953. printf(testingFmt, "wc_ecc_check_key()");
  20954. if (ret == 0) {
  20955. ret = wc_ecc_check_key(&key);
  20956. }
  20957. /* Pass in bad args. */
  20958. if (ret == 0) {
  20959. ret = wc_ecc_check_key(NULL);
  20960. if (ret == BAD_FUNC_ARG) {
  20961. ret = 0;
  20962. } else if (ret == 0) {
  20963. ret = WOLFSSL_FATAL_ERROR;
  20964. }
  20965. }
  20966. printf(resultFmt, ret == 0 ? passed : failed);
  20967. fflush(stdout);
  20968. if (wc_FreeRng(&rng) && ret == 0) {
  20969. ret = WOLFSSL_FATAL_ERROR;
  20970. }
  20971. wc_ecc_free(&key);
  20972. #ifdef FP_ECC
  20973. wc_ecc_fp_free();
  20974. #endif
  20975. #endif
  20976. return ret;
  20977. } /* END test_wc_ecc_check_key */
  20978. /*
  20979. * Testing wc_ecc_get_generator()
  20980. */
  20981. static int test_wc_ecc_get_generator(void)
  20982. {
  20983. int ret = 0;
  20984. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  20985. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  20986. ecc_point* pt;
  20987. printf(testingFmt, "wc_ecc_new_point()");
  20988. pt = wc_ecc_new_point();
  20989. if (!pt) {
  20990. ret = WOLFSSL_FATAL_ERROR;
  20991. }
  20992. printf(testingFmt, "wc_ecc_get_generator()");
  20993. if (ret == 0) {
  20994. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  20995. }
  20996. /* Test bad args. */
  20997. if (ret == MP_OKAY) {
  20998. /* Returns Zero for bad arg. */
  20999. ret = wc_ecc_get_generator(pt, -1);
  21000. if (ret != MP_OKAY)
  21001. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21002. if (ret != MP_OKAY)
  21003. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  21004. * increase this number */
  21005. if (ret != MP_OKAY)
  21006. wc_ecc_get_generator(NULL, -1);
  21007. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  21008. }
  21009. printf(resultFmt, ret == 0 ? passed : failed);
  21010. fflush(stdout);
  21011. wc_ecc_del_point(pt);
  21012. #endif
  21013. return ret;
  21014. } /* END test_wc_ecc_get_generator */
  21015. /*
  21016. * Testing wc_ecc_size()
  21017. */
  21018. static int test_wc_ecc_size(void)
  21019. {
  21020. int ret = 0;
  21021. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21022. WC_RNG rng;
  21023. ecc_key key;
  21024. XMEMSET(&rng, 0, sizeof(rng));
  21025. XMEMSET(&key, 0, sizeof(key));
  21026. ret = wc_InitRng(&rng);
  21027. if (ret == 0) {
  21028. ret = wc_ecc_init(&key);
  21029. if (ret == 0) {
  21030. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21031. #if defined(WOLFSSL_ASYNC_CRYPT)
  21032. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21033. #endif
  21034. }
  21035. }
  21036. printf(testingFmt, "wc_ecc_size()");
  21037. if (ret == 0) {
  21038. ret = wc_ecc_size(&key);
  21039. if (ret == KEY14) {
  21040. ret = 0;
  21041. } else if (ret == 0){
  21042. ret = WOLFSSL_FATAL_ERROR;
  21043. }
  21044. }
  21045. /* Test bad args. */
  21046. if (ret == 0) {
  21047. /* Returns Zero for bad arg. */
  21048. ret = wc_ecc_size(NULL);
  21049. }
  21050. printf(resultFmt, ret == 0 ? passed : failed);
  21051. fflush(stdout);
  21052. if (wc_FreeRng(&rng) && ret == 0) {
  21053. ret = WOLFSSL_FATAL_ERROR;
  21054. }
  21055. wc_ecc_free(&key);
  21056. #endif
  21057. return ret;
  21058. } /* END test_wc_ecc_size */
  21059. static int test_wc_ecc_params(void)
  21060. {
  21061. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21062. It was added after certifications */
  21063. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21064. const ecc_set_type* ecc_set;
  21065. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21066. /* Test for SECP256R1 curve */
  21067. int curve_id = ECC_SECP256R1;
  21068. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  21069. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  21070. ecc_set = wc_ecc_get_curve_params(curve_idx);
  21071. AssertNotNull(ecc_set);
  21072. AssertIntEQ(ecc_set->id, curve_id);
  21073. #endif
  21074. /* Test case when SECP256R1 is not enabled */
  21075. /* Test that we get curve params for index 0 */
  21076. ecc_set = wc_ecc_get_curve_params(0);
  21077. AssertNotNull(ecc_set);
  21078. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21079. return 0;
  21080. }
  21081. /*
  21082. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21083. */
  21084. static int test_wc_ecc_signVerify_hash(void)
  21085. {
  21086. int ret = 0;
  21087. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21088. WC_RNG rng;
  21089. ecc_key key;
  21090. int signH = WOLFSSL_FATAL_ERROR;
  21091. #ifdef HAVE_ECC_VERIFY
  21092. int verifyH = WOLFSSL_FATAL_ERROR;
  21093. int verify = 0;
  21094. #endif
  21095. word32 siglen = ECC_BUFSIZE;
  21096. byte sig[ECC_BUFSIZE];
  21097. byte adjustedSig[ECC_BUFSIZE+1];
  21098. byte digest[] = TEST_STRING;
  21099. word32 digestlen = (word32)TEST_STRING_SZ;
  21100. /* Init stack var */
  21101. XMEMSET(sig, 0, siglen);
  21102. XMEMSET(&key, 0, sizeof(key));
  21103. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21104. /* Init structs. */
  21105. ret = wc_InitRng(&rng);
  21106. if (ret == 0) {
  21107. ret = wc_ecc_init(&key);
  21108. if (ret == 0) {
  21109. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21110. #if defined(WOLFSSL_ASYNC_CRYPT)
  21111. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21112. #endif
  21113. }
  21114. }
  21115. printf(testingFmt, "wc_ecc_sign_hash()");
  21116. if (ret == 0) {
  21117. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  21118. }
  21119. /* Check bad args. */
  21120. if (ret == 0) {
  21121. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  21122. if (signH == ECC_BAD_ARG_E) {
  21123. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  21124. &rng, &key);
  21125. }
  21126. if (signH == ECC_BAD_ARG_E) {
  21127. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  21128. &rng, &key);
  21129. }
  21130. if (signH == ECC_BAD_ARG_E) {
  21131. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21132. NULL, &key);
  21133. }
  21134. if (signH == ECC_BAD_ARG_E) {
  21135. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21136. &rng, NULL);
  21137. }
  21138. if (signH == ECC_BAD_ARG_E) {
  21139. signH = 0;
  21140. } else if (ret == 0) {
  21141. signH = WOLFSSL_FATAL_ERROR;
  21142. }
  21143. }
  21144. printf(resultFmt, signH == 0 ? passed : failed);
  21145. #ifdef HAVE_ECC_VERIFY
  21146. printf(testingFmt, "wc_ecc_verify_hash()");
  21147. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  21148. if (verify != 1 && ret == 0) {
  21149. ret = WOLFSSL_FATAL_ERROR;
  21150. }
  21151. /* test check on length of signature passed in */
  21152. XMEMCPY(adjustedSig, sig, siglen);
  21153. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21154. #ifndef NO_STRICT_ECDSA_LEN
  21155. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21156. &verify, &key), 0);
  21157. #else
  21158. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21159. * is allowed */
  21160. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21161. &verify, &key), 0);
  21162. #endif
  21163. /* Test bad args. */
  21164. if (ret == 0) {
  21165. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  21166. &verify, &key);
  21167. if (verifyH == ECC_BAD_ARG_E) {
  21168. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  21169. &verify, &key);
  21170. }
  21171. if (verifyH == ECC_BAD_ARG_E) {
  21172. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21173. NULL, &key);
  21174. }
  21175. if (verifyH == ECC_BAD_ARG_E) {
  21176. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21177. &verify, NULL);
  21178. }
  21179. if (verifyH == ECC_BAD_ARG_E) {
  21180. verifyH = 0;
  21181. } else if (ret == 0) {
  21182. verifyH = WOLFSSL_FATAL_ERROR;
  21183. }
  21184. }
  21185. printf(resultFmt, verifyH == 0 ? passed : failed);
  21186. #endif /* HAVE_ECC_VERIFY */
  21187. if (wc_FreeRng(&rng) && ret == 0) {
  21188. ret = WOLFSSL_FATAL_ERROR;
  21189. }
  21190. wc_ecc_free(&key);
  21191. #ifdef FP_ECC
  21192. wc_ecc_fp_free();
  21193. #endif
  21194. #endif
  21195. return ret;
  21196. } /* END test_wc_ecc_sign_hash */
  21197. /*
  21198. * Testing wc_ecc_shared_secret()
  21199. */
  21200. static int test_wc_ecc_shared_secret(void)
  21201. {
  21202. int ret = 0;
  21203. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21204. ecc_key key, pubKey;
  21205. WC_RNG rng;
  21206. byte out[KEY32];
  21207. int keySz = sizeof(out);
  21208. word32 outlen = (word32)sizeof(out);
  21209. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21210. const char* qx =
  21211. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21212. const char* qy =
  21213. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21214. const char* d =
  21215. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21216. const char* curveName = "SECP256R1";
  21217. const byte expected_shared_secret[] =
  21218. {
  21219. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21220. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21221. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21222. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21223. };
  21224. #endif
  21225. PRIVATE_KEY_UNLOCK();
  21226. /* Initialize variables. */
  21227. XMEMSET(out, 0, keySz);
  21228. XMEMSET(&rng, 0, sizeof(rng));
  21229. XMEMSET(&key, 0, sizeof(key));
  21230. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21231. ret = wc_InitRng(&rng);
  21232. if (ret == 0) {
  21233. ret = wc_ecc_init(&key);
  21234. if (ret == 0) {
  21235. ret = wc_ecc_init(&pubKey);
  21236. }
  21237. }
  21238. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21239. if (ret == 0) {
  21240. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21241. }
  21242. if (ret == 0) {
  21243. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  21244. }
  21245. #else
  21246. if (ret == 0) {
  21247. ret = wc_ecc_make_key(&rng, keySz, &key);
  21248. #if defined(WOLFSSL_ASYNC_CRYPT)
  21249. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21250. #endif
  21251. }
  21252. if (ret == 0) {
  21253. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  21254. #if defined(WOLFSSL_ASYNC_CRYPT)
  21255. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21256. #endif
  21257. }
  21258. #endif
  21259. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21260. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21261. !defined(HAVE_SELFTEST)
  21262. if (ret == 0) {
  21263. ret = wc_ecc_set_rng(&key, &rng);
  21264. }
  21265. #endif
  21266. printf(testingFmt, "wc_ecc_shared_secret()");
  21267. if (ret == 0) {
  21268. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21269. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21270. if (ret == 0) {
  21271. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  21272. ret = WOLFSSL_FATAL_ERROR;
  21273. }
  21274. }
  21275. #endif
  21276. /* Test bad args. */
  21277. if (ret == 0) {
  21278. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  21279. if (ret == BAD_FUNC_ARG) {
  21280. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  21281. }
  21282. if (ret == BAD_FUNC_ARG) {
  21283. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  21284. }
  21285. if (ret == BAD_FUNC_ARG) {
  21286. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  21287. }
  21288. if (ret == BAD_FUNC_ARG) {
  21289. /* Invalid length */
  21290. outlen = 1;
  21291. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21292. }
  21293. if (ret == BUFFER_E) {
  21294. ret = 0;
  21295. } else if (ret == 0) {
  21296. ret = WOLFSSL_FATAL_ERROR;
  21297. }
  21298. }
  21299. }
  21300. printf(resultFmt, ret == 0 ? passed : failed);
  21301. fflush(stdout);
  21302. if (wc_FreeRng(&rng) && ret == 0) {
  21303. ret = WOLFSSL_FATAL_ERROR;
  21304. }
  21305. wc_ecc_free(&key);
  21306. wc_ecc_free(&pubKey);
  21307. #ifdef FP_ECC
  21308. wc_ecc_fp_free();
  21309. #endif
  21310. #endif
  21311. PRIVATE_KEY_LOCK();
  21312. return ret;
  21313. } /* END tests_wc_ecc_shared_secret */
  21314. /*
  21315. * testint wc_ecc_export_x963()
  21316. */
  21317. static int test_wc_ecc_export_x963(void)
  21318. {
  21319. int ret = 0;
  21320. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21321. ecc_key key;
  21322. WC_RNG rng;
  21323. byte out[ECC_ASN963_MAX_BUF_SZ];
  21324. word32 outlen = sizeof(out);
  21325. PRIVATE_KEY_UNLOCK();
  21326. /* Initialize variables. */
  21327. XMEMSET(out, 0, outlen);
  21328. XMEMSET(&rng, 0, sizeof(rng));
  21329. XMEMSET(&key, 0, sizeof(key));
  21330. ret = wc_InitRng(&rng);
  21331. if (ret == 0) {
  21332. ret = wc_ecc_init(&key);
  21333. if (ret == 0) {
  21334. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21335. #if defined(WOLFSSL_ASYNC_CRYPT)
  21336. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21337. #endif
  21338. }
  21339. }
  21340. printf(testingFmt, "wc_ecc_export_x963()");
  21341. if (ret == 0) {
  21342. ret = wc_ecc_export_x963(&key, out, &outlen);
  21343. }
  21344. /* Test bad args. */
  21345. if (ret == 0) {
  21346. ret = wc_ecc_export_x963(NULL, out, &outlen);
  21347. if (ret == ECC_BAD_ARG_E) {
  21348. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  21349. }
  21350. if (ret == LENGTH_ONLY_E) {
  21351. ret = wc_ecc_export_x963(&key, out, NULL);
  21352. }
  21353. if (ret == ECC_BAD_ARG_E) {
  21354. key.idx = -4;
  21355. ret = wc_ecc_export_x963(&key, out, &outlen);
  21356. }
  21357. if (ret == ECC_BAD_ARG_E) {
  21358. ret = 0;
  21359. } else {
  21360. ret = WOLFSSL_FATAL_ERROR;
  21361. }
  21362. }
  21363. printf(resultFmt, ret == 0 ? passed : failed);
  21364. fflush(stdout);
  21365. if (wc_FreeRng(&rng) && ret == 0) {
  21366. ret = WOLFSSL_FATAL_ERROR;
  21367. }
  21368. wc_ecc_free(&key);
  21369. #ifdef FP_ECC
  21370. wc_ecc_fp_free();
  21371. #endif
  21372. #endif
  21373. PRIVATE_KEY_LOCK();
  21374. return ret;
  21375. } /* END test_wc_ecc_export_x963 */
  21376. /*
  21377. * Testing wc_ecc_export_x963_ex()
  21378. * compile with --enable-compkey will use compression.
  21379. */
  21380. static int test_wc_ecc_export_x963_ex(void)
  21381. {
  21382. int ret = 0;
  21383. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21384. ecc_key key;
  21385. WC_RNG rng;
  21386. byte out[ECC_ASN963_MAX_BUF_SZ];
  21387. word32 outlen = sizeof(out);
  21388. #ifdef HAVE_COMP_KEY
  21389. word32 badOutLen = 5;
  21390. #endif
  21391. /* Init stack variables. */
  21392. XMEMSET(out, 0, outlen);
  21393. XMEMSET(&rng, 0, sizeof(rng));
  21394. XMEMSET(&key, 0, sizeof(key));
  21395. ret = wc_InitRng(&rng);
  21396. if (ret == 0) {
  21397. ret = wc_ecc_init(&key);
  21398. if (ret == 0) {
  21399. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21400. #if defined(WOLFSSL_ASYNC_CRYPT)
  21401. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21402. #endif
  21403. }
  21404. }
  21405. printf(testingFmt, "wc_ecc_export_x963_ex()");
  21406. #ifdef HAVE_COMP_KEY
  21407. if (ret == 0) {
  21408. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21409. }
  21410. #else
  21411. if (ret == 0) {
  21412. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21413. }
  21414. #endif
  21415. /* Test bad args. */
  21416. #ifdef HAVE_COMP_KEY
  21417. if (ret == 0) {
  21418. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21419. if (ret == BAD_FUNC_ARG) {
  21420. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21421. }
  21422. if (ret == BAD_FUNC_ARG) {
  21423. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21424. }
  21425. if (ret == BAD_FUNC_ARG) {
  21426. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21427. }
  21428. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21429. if (ret == BUFFER_E)
  21430. #else
  21431. if (ret == LENGTH_ONLY_E)
  21432. #endif
  21433. {
  21434. key.idx = -4;
  21435. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21436. }
  21437. if (ret == ECC_BAD_ARG_E) {
  21438. ret = 0;
  21439. } else {
  21440. ret = WOLFSSL_FATAL_ERROR;
  21441. }
  21442. }
  21443. #else
  21444. if (ret == 0) {
  21445. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  21446. if (ret == BAD_FUNC_ARG) {
  21447. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  21448. }
  21449. if (ret == BAD_FUNC_ARG) {
  21450. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  21451. }
  21452. if (ret == NOT_COMPILED_IN) {
  21453. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  21454. }
  21455. if (ret == BAD_FUNC_ARG) {
  21456. key.idx = -4;
  21457. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21458. }
  21459. if (ret == ECC_BAD_ARG_E) {
  21460. ret = 0;
  21461. } else if (ret == 0) {
  21462. ret = WOLFSSL_FATAL_ERROR;
  21463. }
  21464. }
  21465. #endif
  21466. printf(resultFmt, ret == 0 ? passed : failed);
  21467. fflush(stdout);
  21468. if (wc_FreeRng(&rng) && ret == 0) {
  21469. ret = WOLFSSL_FATAL_ERROR;
  21470. }
  21471. wc_ecc_free(&key);
  21472. #ifdef FP_ECC
  21473. wc_ecc_fp_free();
  21474. #endif
  21475. #endif
  21476. return ret;
  21477. } /* END test_wc_ecc_export_x963_ex */
  21478. /*
  21479. * testing wc_ecc_import_x963()
  21480. */
  21481. static int test_wc_ecc_import_x963(void)
  21482. {
  21483. int ret = 0;
  21484. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21485. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21486. ecc_key pubKey, key;
  21487. WC_RNG rng;
  21488. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21489. word32 x963Len = (word32)sizeof(x963);
  21490. /* Init stack variables. */
  21491. XMEMSET(x963, 0, x963Len);
  21492. XMEMSET(&rng, 0, sizeof(rng));
  21493. XMEMSET(&key, 0, sizeof(key));
  21494. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21495. ret = wc_InitRng(&rng);
  21496. if (ret == 0) {
  21497. ret = wc_ecc_init(&pubKey);
  21498. if (ret == 0) {
  21499. ret = wc_ecc_init(&key);
  21500. }
  21501. if (ret == 0) {
  21502. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21503. #if defined(WOLFSSL_ASYNC_CRYPT)
  21504. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21505. #endif
  21506. }
  21507. if (ret == 0) {
  21508. PRIVATE_KEY_UNLOCK();
  21509. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  21510. PRIVATE_KEY_LOCK();
  21511. }
  21512. }
  21513. printf(testingFmt, "wc_ecc_import_x963()");
  21514. if (ret == 0) {
  21515. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  21516. }
  21517. /* Test bad args. */
  21518. if (ret == 0) {
  21519. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  21520. if (ret == BAD_FUNC_ARG) {
  21521. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  21522. }
  21523. if (ret == BAD_FUNC_ARG) {
  21524. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  21525. }
  21526. if (ret == ECC_BAD_ARG_E) {
  21527. ret = 0;
  21528. } else if (ret == 0) {
  21529. ret = WOLFSSL_FATAL_ERROR;
  21530. }
  21531. }
  21532. printf(resultFmt, ret == 0 ? passed : failed);
  21533. fflush(stdout);
  21534. if (wc_FreeRng(&rng) && ret == 0) {
  21535. ret = WOLFSSL_FATAL_ERROR;
  21536. }
  21537. wc_ecc_free(&key);
  21538. wc_ecc_free(&pubKey);
  21539. #ifdef FP_ECC
  21540. wc_ecc_fp_free();
  21541. #endif
  21542. #endif
  21543. return ret;
  21544. } /* END wc_ecc_import_x963 */
  21545. /*
  21546. * testing wc_ecc_import_private_key()
  21547. */
  21548. static int ecc_import_private_key (void)
  21549. {
  21550. int ret = 0;
  21551. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21552. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21553. ecc_key key, keyImp;
  21554. WC_RNG rng;
  21555. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21556. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21557. word32 privKeySz = (word32)sizeof(privKey);
  21558. word32 x963KeySz = (word32)sizeof(x963Key);
  21559. /* Init stack variables. */
  21560. XMEMSET(privKey, 0, privKeySz);
  21561. XMEMSET(x963Key, 0, x963KeySz);
  21562. XMEMSET(&rng, 0, sizeof(rng));
  21563. XMEMSET(&key, 0, sizeof(key));
  21564. XMEMSET(&keyImp, 0, sizeof(keyImp));
  21565. ret = wc_InitRng(&rng);
  21566. if (ret == 0) {
  21567. ret = wc_ecc_init(&key);
  21568. if (ret == 0) {
  21569. ret = wc_ecc_init(&keyImp);
  21570. }
  21571. if (ret == 0) {
  21572. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21573. #if defined(WOLFSSL_ASYNC_CRYPT)
  21574. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21575. #endif
  21576. }
  21577. if (ret == 0) {
  21578. PRIVATE_KEY_UNLOCK();
  21579. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  21580. PRIVATE_KEY_LOCK();
  21581. }
  21582. if (ret == 0) {
  21583. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  21584. }
  21585. }
  21586. printf(testingFmt, "wc_ecc_import_private_key()");
  21587. if (ret == 0) {
  21588. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21589. x963KeySz, &keyImp);
  21590. }
  21591. /* Pass in bad args. */
  21592. if (ret == 0) {
  21593. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21594. x963KeySz, NULL);
  21595. if (ret == BAD_FUNC_ARG) {
  21596. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  21597. x963KeySz, &keyImp);
  21598. }
  21599. if (ret == BAD_FUNC_ARG) {
  21600. ret = 0;
  21601. } else if (ret == 0) {
  21602. ret = WOLFSSL_FATAL_ERROR;
  21603. }
  21604. }
  21605. printf(resultFmt, ret == 0 ? passed : failed);
  21606. fflush(stdout);
  21607. if (wc_FreeRng(&rng) && ret == 0) {
  21608. ret = WOLFSSL_FATAL_ERROR;
  21609. }
  21610. wc_ecc_free(&key);
  21611. wc_ecc_free(&keyImp);
  21612. #ifdef FP_ECC
  21613. wc_ecc_fp_free();
  21614. #endif
  21615. #endif
  21616. return ret;
  21617. } /* END wc_ecc_import_private_key */
  21618. /*
  21619. * Testing wc_ecc_export_private_only()
  21620. */
  21621. static int test_wc_ecc_export_private_only(void)
  21622. {
  21623. int ret = 0;
  21624. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21625. ecc_key key;
  21626. WC_RNG rng;
  21627. byte out[ECC_PRIV_KEY_BUF];
  21628. word32 outlen = sizeof(out);
  21629. /* Init stack variables. */
  21630. XMEMSET(out, 0, outlen);
  21631. XMEMSET(&rng, 0, sizeof(rng));
  21632. XMEMSET(&key, 0, sizeof(key));
  21633. ret = wc_InitRng(&rng);
  21634. if (ret == 0) {
  21635. ret = wc_ecc_init(&key);
  21636. if (ret == 0) {
  21637. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21638. #if defined(WOLFSSL_ASYNC_CRYPT)
  21639. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21640. #endif
  21641. }
  21642. }
  21643. printf(testingFmt, "wc_ecc_export_private_only()");
  21644. if (ret == 0) {
  21645. ret = wc_ecc_export_private_only(&key, out, &outlen);
  21646. }
  21647. /* Pass in bad args. */
  21648. if (ret == 0) {
  21649. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  21650. if (ret == BAD_FUNC_ARG) {
  21651. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  21652. }
  21653. if (ret == BAD_FUNC_ARG) {
  21654. ret = wc_ecc_export_private_only(&key, out, NULL);
  21655. }
  21656. if (ret == BAD_FUNC_ARG) {
  21657. ret = 0;
  21658. } else if (ret == 0) {
  21659. ret = WOLFSSL_FATAL_ERROR;
  21660. }
  21661. }
  21662. printf(resultFmt, ret == 0 ? passed : failed);
  21663. fflush(stdout);
  21664. if (wc_FreeRng(&rng) && ret == 0) {
  21665. ret = WOLFSSL_FATAL_ERROR;
  21666. }
  21667. wc_ecc_free(&key);
  21668. #ifdef FP_ECC
  21669. wc_ecc_fp_free();
  21670. #endif
  21671. #endif
  21672. return ret;
  21673. } /* END test_wc_ecc_export_private_only */
  21674. /*
  21675. * Testing wc_ecc_rs_to_sig()
  21676. */
  21677. static int test_wc_ecc_rs_to_sig(void)
  21678. {
  21679. int ret = 0;
  21680. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21681. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21682. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21683. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21684. const char* zeroStr = "0";
  21685. byte sig[ECC_MAX_SIG_SIZE];
  21686. word32 siglen = (word32)sizeof(sig);
  21687. /*R and S max size is the order of curve. 2^192.*/
  21688. int keySz = KEY24;
  21689. byte r[KEY24];
  21690. byte s[KEY24];
  21691. word32 rlen = (word32)sizeof(r);
  21692. word32 slen = (word32)sizeof(s);
  21693. /* Init stack variables. */
  21694. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21695. XMEMSET(r, 0, keySz);
  21696. XMEMSET(s, 0, keySz);
  21697. printf(testingFmt, "wc_ecc_rs_to_sig()");
  21698. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  21699. /* Test bad args. */
  21700. if (ret == 0) {
  21701. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  21702. if (ret == ECC_BAD_ARG_E) {
  21703. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  21704. }
  21705. if (ret == ECC_BAD_ARG_E) {
  21706. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  21707. }
  21708. if (ret == ECC_BAD_ARG_E) {
  21709. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  21710. }
  21711. if (ret == ECC_BAD_ARG_E) {
  21712. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  21713. }
  21714. if (ret == MP_ZERO_E) {
  21715. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  21716. }
  21717. if (ret == MP_ZERO_E) {
  21718. ret = 0;
  21719. } else {
  21720. ret = WOLFSSL_FATAL_ERROR;
  21721. }
  21722. }
  21723. printf(resultFmt, ret == 0 ? passed : failed);
  21724. fflush(stdout);
  21725. printf(testingFmt, "wc_ecc_sig_to_rs()");
  21726. if (ret == 0) {
  21727. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  21728. }
  21729. /* Test bad args. */
  21730. if (ret == 0) {
  21731. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  21732. if (ret == ECC_BAD_ARG_E) {
  21733. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  21734. }
  21735. if (ret == ECC_BAD_ARG_E) {
  21736. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  21737. }
  21738. if (ret == ECC_BAD_ARG_E) {
  21739. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  21740. }
  21741. if (ret == ECC_BAD_ARG_E) {
  21742. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  21743. }
  21744. if (ret == ECC_BAD_ARG_E) {
  21745. ret = 0;
  21746. } else if (ret == 0) {
  21747. ret = WOLFSSL_FATAL_ERROR;
  21748. }
  21749. }
  21750. printf(resultFmt, ret == 0 ? passed : failed);
  21751. fflush(stdout);
  21752. #endif
  21753. return ret;
  21754. } /* END test_wc_ecc_rs_to_sig */
  21755. static int test_wc_ecc_import_raw(void)
  21756. {
  21757. int ret = 0;
  21758. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21759. ecc_key key;
  21760. const char* qx =
  21761. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21762. const char* qy =
  21763. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21764. const char* d =
  21765. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21766. const char* curveName = "SECP256R1";
  21767. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21768. const char* kNullStr = "";
  21769. #endif
  21770. ret = wc_ecc_init(&key);
  21771. printf(testingFmt, "wc_ecc_import_raw()");
  21772. if (ret == 0) {
  21773. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21774. }
  21775. /* Test bad args. */
  21776. if (ret == 0) {
  21777. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  21778. if (ret == BAD_FUNC_ARG) {
  21779. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  21780. }
  21781. if (ret == BAD_FUNC_ARG) {
  21782. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  21783. }
  21784. if (ret == BAD_FUNC_ARG) {
  21785. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  21786. }
  21787. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21788. if (ret == BAD_FUNC_ARG) {
  21789. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21790. wc_ecc_free(&key);
  21791. #endif
  21792. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  21793. if (ret == ECC_INF_E)
  21794. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  21795. }
  21796. #endif
  21797. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21798. if (ret == BAD_FUNC_ARG) {
  21799. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21800. wc_ecc_free(&key);
  21801. #endif
  21802. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  21803. }
  21804. if (ret == BAD_FUNC_ARG) {
  21805. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21806. wc_ecc_free(&key);
  21807. #endif
  21808. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  21809. }
  21810. #endif
  21811. if (ret == BAD_FUNC_ARG) {
  21812. ret = 0;
  21813. }
  21814. }
  21815. printf(resultFmt, ret == 0 ? passed : failed);
  21816. fflush(stdout);
  21817. wc_ecc_free(&key);
  21818. #endif
  21819. return ret;
  21820. } /* END test_wc_ecc_import_raw */
  21821. static int test_wc_ecc_import_unsigned(void)
  21822. {
  21823. int ret = 0;
  21824. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21825. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  21826. ecc_key key;
  21827. const byte qx[] = {
  21828. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21829. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21830. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21831. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21832. };
  21833. const byte qy[] = {
  21834. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21835. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21836. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21837. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21838. };
  21839. const byte d[] = {
  21840. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21841. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21842. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21843. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21844. };
  21845. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21846. const byte nullBytes[32] = {0};
  21847. #endif
  21848. int curveId = ECC_SECP256R1;
  21849. ret = wc_ecc_init(&key);
  21850. printf(testingFmt, "wc_ecc_import_unsigned()");
  21851. if (ret == 0) {
  21852. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21853. curveId);
  21854. }
  21855. /* Test bad args. */
  21856. if (ret == 0) {
  21857. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21858. curveId);
  21859. if (ret == BAD_FUNC_ARG) {
  21860. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21861. curveId);
  21862. }
  21863. if (ret == BAD_FUNC_ARG) {
  21864. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21865. curveId);
  21866. }
  21867. if (ret == BAD_FUNC_ARG) {
  21868. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21869. ECC_CURVE_INVALID);
  21870. }
  21871. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21872. if (ret == BAD_FUNC_ARG) {
  21873. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21874. (byte*)nullBytes, (byte*)nullBytes, curveId);
  21875. }
  21876. #endif
  21877. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  21878. ret = 0;
  21879. }
  21880. }
  21881. printf(resultFmt, ret == 0 ? passed : failed);
  21882. fflush(stdout);
  21883. wc_ecc_free(&key);
  21884. #endif
  21885. return ret;
  21886. } /* END test_wc_ecc_import_unsigned */
  21887. /*
  21888. * Testing wc_ecc_sig_size()
  21889. */
  21890. static int test_wc_ecc_sig_size(void)
  21891. {
  21892. int ret = 0;
  21893. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21894. ecc_key key;
  21895. WC_RNG rng;
  21896. int keySz = KEY16;
  21897. XMEMSET(&rng, 0, sizeof(rng));
  21898. XMEMSET(&key, 0, sizeof(key));
  21899. ret = wc_InitRng(&rng);
  21900. if (ret == 0) {
  21901. ret = wc_ecc_init(&key);
  21902. if (ret == 0) {
  21903. ret = wc_ecc_make_key(&rng, keySz, &key);
  21904. #if defined(WOLFSSL_ASYNC_CRYPT)
  21905. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21906. #endif
  21907. }
  21908. }
  21909. printf(testingFmt, "wc_ecc_sig_size()");
  21910. if (ret == 0) {
  21911. ret = wc_ecc_sig_size(&key);
  21912. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  21913. ret = 0;
  21914. }
  21915. }
  21916. printf(resultFmt, ret == 0 ? passed : failed);
  21917. fflush(stdout);
  21918. if (wc_FreeRng(&rng) && ret == 0) {
  21919. ret = WOLFSSL_FATAL_ERROR;
  21920. }
  21921. wc_ecc_free(&key);
  21922. #endif
  21923. return ret;
  21924. } /* END test_wc_ecc_sig_size */
  21925. /*
  21926. * Testing wc_ecc_ctx_new()
  21927. */
  21928. static int test_wc_ecc_ctx_new(void)
  21929. {
  21930. int ret = 0;
  21931. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21932. WC_RNG rng;
  21933. ecEncCtx* cli = NULL;
  21934. ecEncCtx* srv = NULL;
  21935. ret = wc_InitRng(&rng);
  21936. printf(testingFmt, "wc_ecc_ctx_new()");
  21937. if (ret == 0) {
  21938. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  21939. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  21940. }
  21941. if (ret == 0 && (cli == NULL || srv == NULL)) {
  21942. ret = WOLFSSL_FATAL_ERROR;
  21943. }
  21944. wc_ecc_ctx_free(cli);
  21945. wc_ecc_ctx_free(srv);
  21946. /* Test bad args. */
  21947. if (ret == 0) {
  21948. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  21949. cli = wc_ecc_ctx_new(0, &rng);
  21950. if (cli != NULL) {
  21951. ret = WOLFSSL_FATAL_ERROR;
  21952. }
  21953. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  21954. if (cli != NULL) {
  21955. ret = WOLFSSL_FATAL_ERROR;
  21956. }
  21957. }
  21958. printf(resultFmt, ret == 0 ? passed : failed);
  21959. fflush(stdout);
  21960. if (wc_FreeRng(&rng) && ret == 0) {
  21961. ret = WOLFSSL_FATAL_ERROR;
  21962. }
  21963. wc_ecc_ctx_free(cli);
  21964. #endif
  21965. return ret;
  21966. } /* END test_wc_ecc_ctx_new */
  21967. /*
  21968. * Tesing wc_ecc_reset()
  21969. */
  21970. static int test_wc_ecc_ctx_reset(void)
  21971. {
  21972. int ret = 0;
  21973. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  21974. ecEncCtx* ctx = NULL;
  21975. WC_RNG rng;
  21976. ret = wc_InitRng(&rng);
  21977. if (ret == 0) {
  21978. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  21979. ret = WOLFSSL_FATAL_ERROR;
  21980. }
  21981. }
  21982. printf(testingFmt, "wc_ecc_ctx_reset()");
  21983. if (ret == 0) {
  21984. ret = wc_ecc_ctx_reset(ctx, &rng);
  21985. }
  21986. /* Pass in bad args. */
  21987. if (ret == 0) {
  21988. ret = wc_ecc_ctx_reset(NULL, &rng);
  21989. if (ret == BAD_FUNC_ARG) {
  21990. ret = wc_ecc_ctx_reset(ctx, NULL);
  21991. }
  21992. if (ret == BAD_FUNC_ARG) {
  21993. ret = 0;
  21994. } else if (ret == 0) {
  21995. ret = WOLFSSL_FATAL_ERROR;
  21996. }
  21997. }
  21998. printf(resultFmt, ret == 0 ? passed : failed);
  21999. fflush(stdout);
  22000. if (wc_FreeRng(&rng) && ret == 0) {
  22001. ret = WOLFSSL_FATAL_ERROR;
  22002. }
  22003. wc_ecc_ctx_free(ctx);
  22004. #endif
  22005. return ret;
  22006. } /* END test_wc_ecc_ctx_reset */
  22007. /*
  22008. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22009. */
  22010. static int test_wc_ecc_ctx_set_peer_salt(void)
  22011. {
  22012. int ret = 0;
  22013. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22014. WC_RNG rng;
  22015. ecEncCtx* cliCtx = NULL;
  22016. ecEncCtx* servCtx = NULL;
  22017. const byte* cliSalt = NULL;
  22018. const byte* servSalt = NULL;
  22019. ret = wc_InitRng(&rng);
  22020. if (ret == 0) {
  22021. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  22022. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  22023. ret = WOLFSSL_FATAL_ERROR;
  22024. }
  22025. }
  22026. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  22027. /* Test bad args. */
  22028. if (ret == 0) {
  22029. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  22030. if (cliSalt != NULL) {
  22031. ret = WOLFSSL_FATAL_ERROR;
  22032. }
  22033. }
  22034. if (ret == 0) {
  22035. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  22036. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  22037. if (cliSalt == NULL || servSalt == NULL) {
  22038. ret = WOLFSSL_FATAL_ERROR;
  22039. }
  22040. }
  22041. printf(resultFmt, ret == 0 ? passed : failed);
  22042. fflush(stdout);
  22043. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  22044. if (ret == 0) {
  22045. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  22046. }
  22047. /* Test bad args. */
  22048. if (ret == 0) {
  22049. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  22050. if (ret == BAD_FUNC_ARG) {
  22051. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  22052. }
  22053. if (ret == BAD_FUNC_ARG) {
  22054. ret = 0;
  22055. } else if (ret == 0) {
  22056. ret = WOLFSSL_FATAL_ERROR;
  22057. }
  22058. }
  22059. printf(resultFmt, ret == 0 ? passed : failed);
  22060. fflush(stdout);
  22061. if (wc_FreeRng(&rng) && ret == 0) {
  22062. ret = WOLFSSL_FATAL_ERROR;
  22063. }
  22064. wc_ecc_ctx_free(cliCtx);
  22065. wc_ecc_ctx_free(servCtx);
  22066. #endif
  22067. return ret;
  22068. } /* END test_wc_ecc_ctx_set_peer_salt */
  22069. /*
  22070. * Testing wc_ecc_ctx_set_info()
  22071. */
  22072. static int test_wc_ecc_ctx_set_info(void)
  22073. {
  22074. int ret = 0;
  22075. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22076. ecEncCtx* ctx = NULL;
  22077. WC_RNG rng;
  22078. const char* optInfo = "Optional Test Info.";
  22079. int optInfoSz = (int)XSTRLEN(optInfo);
  22080. const char* badOptInfo = NULL;
  22081. ret = wc_InitRng(&rng);
  22082. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  22083. ret = WOLFSSL_FATAL_ERROR;
  22084. }
  22085. printf(testingFmt, "wc_ecc_ctx_set_info()");
  22086. if (ret == 0) {
  22087. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  22088. }
  22089. /* Test bad args. */
  22090. if (ret == 0) {
  22091. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  22092. if (ret == BAD_FUNC_ARG) {
  22093. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  22094. }
  22095. if (ret == BAD_FUNC_ARG) {
  22096. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  22097. }
  22098. if (ret == BAD_FUNC_ARG) {
  22099. ret = 0;
  22100. } else if (ret == 0) {
  22101. ret = WOLFSSL_FATAL_ERROR;
  22102. }
  22103. }
  22104. printf(resultFmt, ret == 0 ? passed : failed);
  22105. fflush(stdout);
  22106. if (wc_FreeRng(&rng) && ret == 0) {
  22107. ret = WOLFSSL_FATAL_ERROR;
  22108. }
  22109. wc_ecc_ctx_free(ctx);
  22110. #endif
  22111. return ret;
  22112. } /* END test_wc_ecc_ctx_set_info */
  22113. /*
  22114. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22115. */
  22116. static int test_wc_ecc_encryptDecrypt(void)
  22117. {
  22118. int ret = 0;
  22119. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22120. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22121. ecc_key srvKey, cliKey, tmpKey;
  22122. WC_RNG rng;
  22123. const char* msg = "EccBlock Size 16";
  22124. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22125. #ifdef WOLFSSL_ECIES_OLD
  22126. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22127. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22128. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22129. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22130. #else
  22131. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22132. #endif
  22133. word32 outSz = (word32)sizeof(out);
  22134. byte plain[sizeof("EccBlock Size 16")];
  22135. word32 plainSz = (word32)sizeof(plain);
  22136. int keySz = KEY20;
  22137. /* Init stack variables. */
  22138. XMEMSET(out, 0, outSz);
  22139. XMEMSET(plain, 0, plainSz);
  22140. XMEMSET(&rng, 0, sizeof(rng));
  22141. XMEMSET(&srvKey, 0, sizeof(srvKey));
  22142. XMEMSET(&cliKey, 0, sizeof(cliKey));
  22143. ret = wc_InitRng(&rng);
  22144. if (ret == 0) {
  22145. ret = wc_ecc_init(&cliKey);
  22146. if (ret == 0) {
  22147. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22148. #if defined(WOLFSSL_ASYNC_CRYPT)
  22149. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22150. #endif
  22151. }
  22152. if (ret == 0) {
  22153. ret = wc_ecc_init(&srvKey);
  22154. }
  22155. if (ret == 0) {
  22156. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22157. #if defined(WOLFSSL_ASYNC_CRYPT)
  22158. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22159. #endif
  22160. }
  22161. if (ret == 0) {
  22162. ret = wc_ecc_init(&tmpKey);
  22163. }
  22164. }
  22165. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22166. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22167. !defined(HAVE_SELFTEST)
  22168. if (ret == 0) {
  22169. ret = wc_ecc_set_rng(&srvKey, &rng);
  22170. }
  22171. if (ret == 0) {
  22172. ret = wc_ecc_set_rng(&cliKey, &rng);
  22173. }
  22174. #endif
  22175. printf(testingFmt, "wc_ecc_encrypt()");
  22176. if (ret == 0) {
  22177. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22178. &outSz, NULL);
  22179. }
  22180. if (ret == 0) {
  22181. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  22182. &outSz, NULL);
  22183. if (ret == BAD_FUNC_ARG) {
  22184. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  22185. &outSz, NULL);
  22186. }
  22187. if (ret == BAD_FUNC_ARG) {
  22188. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  22189. &outSz, NULL);
  22190. }
  22191. if (ret == BAD_FUNC_ARG) {
  22192. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22193. &outSz, NULL);
  22194. }
  22195. if (ret == BAD_FUNC_ARG) {
  22196. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22197. NULL, NULL);
  22198. }
  22199. if (ret == BAD_FUNC_ARG) {
  22200. ret = 0;
  22201. } else if (ret == 0) {
  22202. ret = WOLFSSL_FATAL_ERROR;
  22203. }
  22204. }
  22205. printf(resultFmt, ret == 0 ? passed : failed);
  22206. fflush(stdout);
  22207. printf(testingFmt, "wc_ecc_decrypt()");
  22208. #ifdef WOLFSSL_ECIES_OLD
  22209. if (ret == 0) {
  22210. tmpKey.dp = cliKey.dp;
  22211. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  22212. }
  22213. #endif
  22214. if (ret == 0) {
  22215. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  22216. &plainSz, NULL);
  22217. }
  22218. if (ret == 0) {
  22219. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  22220. &plainSz, NULL);
  22221. #ifdef WOLFSSL_ECIES_OLD
  22222. /* NULL parameter allowed in new implementations - public key comes from
  22223. * the message. */
  22224. if (ret == BAD_FUNC_ARG) {
  22225. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  22226. &plainSz, NULL);
  22227. }
  22228. #endif
  22229. if (ret == BAD_FUNC_ARG) {
  22230. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  22231. &plainSz, NULL);
  22232. }
  22233. if (ret == BAD_FUNC_ARG) {
  22234. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  22235. &plainSz, NULL);
  22236. }
  22237. if (ret == BAD_FUNC_ARG) {
  22238. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  22239. plain, NULL, NULL);
  22240. }
  22241. if (ret == BAD_FUNC_ARG) {
  22242. ret = 0;
  22243. } else if (ret == 0) {
  22244. ret = WOLFSSL_FATAL_ERROR;
  22245. }
  22246. }
  22247. if (XMEMCMP(msg, plain, msgSz) != 0) {
  22248. ret = WOLFSSL_FATAL_ERROR;
  22249. }
  22250. printf(resultFmt, ret == 0 ? passed : failed);
  22251. fflush(stdout);
  22252. if (wc_FreeRng(&rng) && ret == 0) {
  22253. ret = WOLFSSL_FATAL_ERROR;
  22254. }
  22255. wc_ecc_free(&tmpKey);
  22256. wc_ecc_free(&cliKey);
  22257. wc_ecc_free(&srvKey);
  22258. #endif
  22259. return ret;
  22260. } /* END test_wc_ecc_encryptDecrypt */
  22261. /*
  22262. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22263. */
  22264. static int test_wc_ecc_del_point(void)
  22265. {
  22266. int ret = 0;
  22267. #if defined(HAVE_ECC)
  22268. ecc_point* pt;
  22269. printf(testingFmt, "wc_ecc_new_point()");
  22270. pt = wc_ecc_new_point();
  22271. if (!pt) {
  22272. ret = WOLFSSL_FATAL_ERROR;
  22273. }
  22274. printf(resultFmt, ret == 0 ? passed : failed);
  22275. fflush(stdout);
  22276. wc_ecc_del_point(pt);
  22277. #endif
  22278. return ret;
  22279. } /* END test_wc_ecc_del_point */
  22280. /*
  22281. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22282. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22283. * and wc_ecc_cmp_point()
  22284. */
  22285. static int test_wc_ecc_pointFns(void)
  22286. {
  22287. int ret = 0;
  22288. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22289. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22290. !defined(WOLFSSL_ATECC608A)
  22291. ecc_key key;
  22292. WC_RNG rng;
  22293. ecc_point* point = NULL;
  22294. ecc_point* cpypt = NULL;
  22295. int idx = 0;
  22296. int keySz = KEY32;
  22297. byte der[DER_SZ(KEY32)];
  22298. word32 derlenChk = 0;
  22299. word32 derSz = DER_SZ(KEY32);
  22300. /* Init stack variables. */
  22301. XMEMSET(der, 0, derSz);
  22302. XMEMSET(&rng, 0, sizeof(rng));
  22303. XMEMSET(&key, 0, sizeof(key));
  22304. ret = wc_InitRng(&rng);
  22305. if (ret == 0) {
  22306. ret = wc_ecc_init(&key);
  22307. if (ret == 0) {
  22308. ret = wc_ecc_make_key(&rng, keySz, &key);
  22309. #if defined(WOLFSSL_ASYNC_CRYPT)
  22310. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22311. #endif
  22312. }
  22313. }
  22314. if (ret == 0) {
  22315. point = wc_ecc_new_point();
  22316. if (!point) {
  22317. ret = WOLFSSL_FATAL_ERROR;
  22318. }
  22319. }
  22320. if (ret == 0) {
  22321. cpypt = wc_ecc_new_point();
  22322. if (!cpypt) {
  22323. ret = WOLFSSL_FATAL_ERROR;
  22324. }
  22325. }
  22326. /* Export */
  22327. printf(testingFmt, "wc_ecc_export_point_der()");
  22328. if (ret == 0) {
  22329. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22330. NULL, &derlenChk);
  22331. /* Check length value. */
  22332. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  22333. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22334. der, &derSz);
  22335. }
  22336. }
  22337. /* Test bad args. */
  22338. if (ret == 0) {
  22339. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  22340. if (ret == ECC_BAD_ARG_E) {
  22341. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  22342. }
  22343. if (ret == ECC_BAD_ARG_E) {
  22344. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22345. der, NULL);
  22346. }
  22347. if (ret == ECC_BAD_ARG_E) {
  22348. ret = 0;
  22349. } else if (ret == 0) {
  22350. ret = WOLFSSL_FATAL_ERROR;
  22351. }
  22352. }
  22353. printf(resultFmt, ret == 0 ? passed : failed);
  22354. fflush(stdout);
  22355. /* Import */
  22356. printf(testingFmt, "wc_ecc_import_point_der()");
  22357. if (ret == 0) {
  22358. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  22359. /* Condition double checks wc_ecc_cmp_point(). */
  22360. if (ret == 0 &&
  22361. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  22362. ret = wc_ecc_cmp_point(&key.pubkey, point);
  22363. }
  22364. }
  22365. /* Test bad args. */
  22366. if (ret == 0) {
  22367. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  22368. if (ret == ECC_BAD_ARG_E) {
  22369. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  22370. }
  22371. if (ret == ECC_BAD_ARG_E) {
  22372. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  22373. }
  22374. if (ret == ECC_BAD_ARG_E) {
  22375. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  22376. }
  22377. if (ret == ECC_BAD_ARG_E) {
  22378. ret = 0;
  22379. } else if (ret == 0) {
  22380. ret = WOLFSSL_FATAL_ERROR;
  22381. }
  22382. }
  22383. printf(resultFmt, ret == 0 ? passed : failed);
  22384. fflush(stdout);
  22385. /* Copy */
  22386. printf(testingFmt, "wc_ecc_copy_point()");
  22387. if (ret == 0) {
  22388. ret = wc_ecc_copy_point(point, cpypt);
  22389. }
  22390. /* Test bad args. */
  22391. if (ret == 0) {
  22392. ret = wc_ecc_copy_point(NULL, cpypt);
  22393. if (ret == ECC_BAD_ARG_E) {
  22394. ret = wc_ecc_copy_point(point, NULL);
  22395. }
  22396. if (ret == ECC_BAD_ARG_E) {
  22397. ret = 0;
  22398. } else if (ret == 0) {
  22399. ret = WOLFSSL_FATAL_ERROR;
  22400. }
  22401. }
  22402. printf(resultFmt, ret == 0 ? passed : failed);
  22403. fflush(stdout);
  22404. printf(testingFmt, "wc_ecc_cmp_point()");
  22405. /* Compare point */
  22406. if (ret == 0) {
  22407. ret = wc_ecc_cmp_point(point, cpypt);
  22408. }
  22409. /* Test bad args. */
  22410. if (ret == 0) {
  22411. ret = wc_ecc_cmp_point(NULL, cpypt);
  22412. if (ret == BAD_FUNC_ARG) {
  22413. ret = wc_ecc_cmp_point(point, NULL);
  22414. }
  22415. if (ret == BAD_FUNC_ARG) {
  22416. ret = 0;
  22417. } else if (ret == 0) {
  22418. ret = WOLFSSL_FATAL_ERROR;
  22419. }
  22420. }
  22421. printf(resultFmt, ret == 0 ? passed : failed);
  22422. fflush(stdout);
  22423. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  22424. /* At infinity if return == 1, otherwise return == 0. */
  22425. if (ret == 0) {
  22426. ret = wc_ecc_point_is_at_infinity(point);
  22427. }
  22428. /* Test bad args. */
  22429. if (ret == 0) {
  22430. ret = wc_ecc_point_is_at_infinity(NULL);
  22431. if (ret == BAD_FUNC_ARG) {
  22432. ret = 0;
  22433. } else if (ret == 0) {
  22434. ret = WOLFSSL_FATAL_ERROR;
  22435. }
  22436. }
  22437. printf(resultFmt, ret == 0 ? passed : failed);
  22438. fflush(stdout);
  22439. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  22440. #ifdef USE_ECC_B_PARAM
  22441. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  22442. /* On curve if ret == 0 */
  22443. if (ret == 0) {
  22444. ret = wc_ecc_point_is_on_curve(point, idx);
  22445. }
  22446. /* Test bad args. */
  22447. if (ret == 0) {
  22448. ret = wc_ecc_point_is_on_curve(NULL, idx);
  22449. if (ret == BAD_FUNC_ARG) {
  22450. ret = wc_ecc_point_is_on_curve(point, 1000);
  22451. }
  22452. if (ret == ECC_BAD_ARG_E) {
  22453. ret = 0;
  22454. } else if (ret == 0) {
  22455. ret = WOLFSSL_FATAL_ERROR;
  22456. }
  22457. }
  22458. printf(resultFmt, ret == 0 ? passed : failed);
  22459. fflush(stdout);
  22460. #endif /* USE_ECC_B_PARAM */
  22461. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  22462. /* Free */
  22463. wc_ecc_del_point(point);
  22464. wc_ecc_del_point(cpypt);
  22465. wc_ecc_free(&key);
  22466. if (wc_FreeRng(&rng) && ret == 0) {
  22467. ret = WOLFSSL_FATAL_ERROR;
  22468. }
  22469. #endif
  22470. return ret;
  22471. } /* END test_wc_ecc_pointFns */
  22472. /*
  22473. * Testing wc_ecc_sahred_secret_ssh()
  22474. */
  22475. static int test_wc_ecc_shared_secret_ssh(void)
  22476. {
  22477. int ret = 0;
  22478. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22479. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22480. !defined(WOLFSSL_ATECC608A)
  22481. ecc_key key, key2;
  22482. WC_RNG rng;
  22483. int keySz = KEY32;
  22484. int key2Sz = KEY24;
  22485. byte secret[KEY32];
  22486. word32 secretLen = keySz;
  22487. /* Init stack variables. */
  22488. XMEMSET(secret, 0, secretLen);
  22489. XMEMSET(&rng, 0, sizeof(rng));
  22490. XMEMSET(&key, 0, sizeof(key));
  22491. XMEMSET(&key2, 0, sizeof(key2));
  22492. /* Make keys */
  22493. ret = wc_InitRng(&rng);
  22494. if (ret == 0) {
  22495. ret = wc_ecc_init(&key);
  22496. if (ret == 0) {
  22497. ret = wc_ecc_make_key(&rng, keySz, &key);
  22498. #if defined(WOLFSSL_ASYNC_CRYPT)
  22499. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22500. #endif
  22501. }
  22502. if (wc_FreeRng(&rng) && ret == 0) {
  22503. ret = WOLFSSL_FATAL_ERROR;
  22504. }
  22505. }
  22506. if (ret == 0) {
  22507. ret = wc_InitRng(&rng);
  22508. if (ret == 0) {
  22509. ret = wc_ecc_init(&key2);
  22510. }
  22511. if (ret == 0) {
  22512. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22513. #if defined(WOLFSSL_ASYNC_CRYPT)
  22514. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22515. #endif
  22516. }
  22517. }
  22518. printf(testingFmt, "ecc_shared_secret_ssh()");
  22519. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22520. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22521. !defined(HAVE_SELFTEST)
  22522. if (ret == 0) {
  22523. ret = wc_ecc_set_rng(&key, &rng);
  22524. }
  22525. #endif
  22526. if (ret == 0) {
  22527. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22528. }
  22529. /* Pass in bad args. */
  22530. if (ret == 0) {
  22531. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  22532. if (ret == BAD_FUNC_ARG) {
  22533. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  22534. }
  22535. if (ret == BAD_FUNC_ARG) {
  22536. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  22537. }
  22538. if (ret == BAD_FUNC_ARG) {
  22539. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  22540. }
  22541. if (ret == BAD_FUNC_ARG) {
  22542. key.type = ECC_PUBLICKEY;
  22543. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22544. if (ret == ECC_BAD_ARG_E) {
  22545. ret = 0;
  22546. } else if (ret == 0) {
  22547. ret = WOLFSSL_FATAL_ERROR;
  22548. }
  22549. } else if (ret == 0) {
  22550. ret = WOLFSSL_FATAL_ERROR;
  22551. }
  22552. }
  22553. printf(resultFmt, ret == 0 ? passed : failed);
  22554. fflush(stdout);
  22555. if (wc_FreeRng(&rng) && ret == 0) {
  22556. ret = WOLFSSL_FATAL_ERROR;
  22557. }
  22558. wc_ecc_free(&key);
  22559. wc_ecc_free(&key2);
  22560. #ifdef FP_ECC
  22561. wc_ecc_fp_free();
  22562. #endif
  22563. #endif
  22564. return ret;
  22565. } /* END test_wc_ecc_shared_secret_ssh */
  22566. /*
  22567. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22568. */
  22569. static int test_wc_ecc_verify_hash_ex(void)
  22570. {
  22571. int ret = 0;
  22572. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22573. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22574. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22575. ecc_key key;
  22576. WC_RNG rng;
  22577. mp_int r;
  22578. mp_int s;
  22579. mp_int z;
  22580. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22581. unsigned char iHash[] = "Everyone gets Friday off.......";
  22582. unsigned char shortHash[] = TEST_STRING;
  22583. word32 hashlen = sizeof(hash);
  22584. word32 iHashLen = sizeof(iHash);
  22585. word32 shortHashLen = sizeof(shortHash);
  22586. int keySz = KEY32;
  22587. int sig = WOLFSSL_FATAL_ERROR;
  22588. int ver = WOLFSSL_FATAL_ERROR;
  22589. int verify_ok = 0;
  22590. /* Initialize r and s. */
  22591. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  22592. if (ret != MP_OKAY) {
  22593. return MP_INIT_E;
  22594. }
  22595. ret = wc_InitRng(&rng);
  22596. if (ret == 0) {
  22597. ret = wc_ecc_init(&key);
  22598. if (ret == 0) {
  22599. ret = wc_ecc_make_key(&rng, keySz, &key);
  22600. #if defined(WOLFSSL_ASYNC_CRYPT)
  22601. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22602. #endif
  22603. }
  22604. }
  22605. if (ret == 0) {
  22606. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  22607. if (ret == 0) {
  22608. /* verify_ok should be 1. */
  22609. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  22610. if (verify_ok != 1 && ret == 0) {
  22611. ret = WOLFSSL_FATAL_ERROR;
  22612. }
  22613. }
  22614. if (ret == 0) {
  22615. /* verify_ok should be 0 */
  22616. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  22617. &verify_ok, &key);
  22618. if (verify_ok != 0 && ret == 0) {
  22619. ret = WOLFSSL_FATAL_ERROR;
  22620. }
  22621. }
  22622. if (ret == 0) {
  22623. /* verify_ok should be 0. */
  22624. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22625. &verify_ok, &key);
  22626. if (verify_ok != 0 && ret == 0) {
  22627. ret = WOLFSSL_FATAL_ERROR;
  22628. }
  22629. }
  22630. }
  22631. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  22632. /* Test bad args. */
  22633. if (ret == 0) {
  22634. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  22635. == ECC_BAD_ARG_E) {
  22636. sig = 0;
  22637. }
  22638. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  22639. != ECC_BAD_ARG_E) {
  22640. sig = WOLFSSL_FATAL_ERROR;
  22641. }
  22642. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  22643. != ECC_BAD_ARG_E) {
  22644. sig = WOLFSSL_FATAL_ERROR;
  22645. }
  22646. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  22647. != ECC_BAD_ARG_E) {
  22648. sig = WOLFSSL_FATAL_ERROR;
  22649. }
  22650. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  22651. != ECC_BAD_ARG_E) {
  22652. sig = WOLFSSL_FATAL_ERROR;
  22653. }
  22654. }
  22655. printf(resultFmt, sig == 0 ? passed : failed);
  22656. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  22657. /* Test bad args. */
  22658. if (ret == 0) {
  22659. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  22660. == ECC_BAD_ARG_E) {
  22661. ver = 0;
  22662. }
  22663. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22664. &verify_ok, &key) != ECC_BAD_ARG_E) {
  22665. ver = WOLFSSL_FATAL_ERROR;
  22666. }
  22667. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  22668. != MP_ZERO_E) {
  22669. ver = WOLFSSL_FATAL_ERROR;
  22670. }
  22671. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  22672. != MP_ZERO_E) {
  22673. ver = WOLFSSL_FATAL_ERROR;
  22674. }
  22675. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  22676. != MP_ZERO_E) {
  22677. ver = WOLFSSL_FATAL_ERROR;
  22678. }
  22679. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22680. &key) != ECC_BAD_ARG_E) {
  22681. ver = WOLFSSL_FATAL_ERROR;
  22682. }
  22683. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22684. NULL, &key) != ECC_BAD_ARG_E) {
  22685. ver = WOLFSSL_FATAL_ERROR;
  22686. }
  22687. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22688. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  22689. ver = WOLFSSL_FATAL_ERROR;
  22690. }
  22691. }
  22692. printf(resultFmt, ver == 0 ? passed : failed);
  22693. wc_ecc_free(&key);
  22694. mp_free(&r);
  22695. mp_free(&s);
  22696. if (wc_FreeRng(&rng)) {
  22697. return WOLFSSL_FATAL_ERROR;
  22698. }
  22699. if (ret == 0 && (sig != 0 || ver != 0)) {
  22700. ret = WOLFSSL_FATAL_ERROR;
  22701. }
  22702. #endif
  22703. return ret;
  22704. } /* END test_wc_ecc_verify_hash_ex */
  22705. /*
  22706. * Testing wc_ecc_mulmod()
  22707. */
  22708. static int test_wc_ecc_mulmod(void)
  22709. {
  22710. int ret = 0;
  22711. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22712. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22713. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22714. ecc_key key1, key2, key3;
  22715. WC_RNG rng;
  22716. ret = wc_InitRng(&rng);
  22717. if (ret == 0) {
  22718. ret = wc_ecc_init(&key1);
  22719. if (ret == 0) {
  22720. ret = wc_ecc_init(&key2);
  22721. }
  22722. if (ret == 0) {
  22723. ret = wc_ecc_init(&key3);
  22724. }
  22725. if (ret == 0) {
  22726. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22727. #if defined(WOLFSSL_ASYNC_CRYPT)
  22728. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22729. #endif
  22730. }
  22731. wc_FreeRng(&rng);
  22732. }
  22733. if (ret == 0) {
  22734. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  22735. ECC_SECP256R1);
  22736. if (ret == 0) {
  22737. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22738. key1.dp->prime, ECC_SECP256R1);
  22739. }
  22740. }
  22741. printf(testingFmt, "wc_ecc_mulmod()");
  22742. if (ret == 0) {
  22743. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  22744. &key3.k, 1);
  22745. }
  22746. /* Test bad args. */
  22747. if (ret == 0) {
  22748. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  22749. &key3.k, 1);
  22750. if (ret == ECC_BAD_ARG_E) {
  22751. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  22752. &key3.k, 1);
  22753. }
  22754. if (ret == ECC_BAD_ARG_E) {
  22755. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  22756. &key3.k, 1);
  22757. }
  22758. if (ret == ECC_BAD_ARG_E) {
  22759. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  22760. &key2.k, NULL, 1);
  22761. }
  22762. if (ret == ECC_BAD_ARG_E) {
  22763. ret = 0;
  22764. } else if (ret == 0) {
  22765. ret = WOLFSSL_FATAL_ERROR;
  22766. }
  22767. }
  22768. printf(resultFmt, ret == 0 ? passed : failed);
  22769. fflush(stdout);
  22770. wc_ecc_free(&key1);
  22771. wc_ecc_free(&key2);
  22772. wc_ecc_free(&key3);
  22773. #ifdef FP_ECC
  22774. wc_ecc_fp_free();
  22775. #endif
  22776. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22777. return ret;
  22778. } /* END test_wc_ecc_mulmod */
  22779. /*
  22780. * Testing wc_ecc_is_valid_idx()
  22781. */
  22782. static int test_wc_ecc_is_valid_idx(void)
  22783. {
  22784. int ret = 0;
  22785. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22786. ecc_key key;
  22787. WC_RNG rng;
  22788. int iVal = -2;
  22789. int iVal2 = 3000;
  22790. XMEMSET(&rng, 0, sizeof(rng));
  22791. XMEMSET(&key, 0, sizeof(key));
  22792. ret = wc_InitRng(&rng);
  22793. if (ret == 0) {
  22794. ret = wc_ecc_init(&key);
  22795. if (ret == 0) {
  22796. ret = wc_ecc_make_key(&rng, 32, &key);
  22797. #if defined(WOLFSSL_ASYNC_CRYPT)
  22798. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22799. #endif
  22800. }
  22801. }
  22802. printf(testingFmt, "wc_ecc_is_valid_idx()");
  22803. if (ret == 0) {
  22804. ret = wc_ecc_is_valid_idx(key.idx);
  22805. if (ret == 1) {
  22806. ret = 0;
  22807. } else {
  22808. ret = WOLFSSL_FATAL_ERROR;
  22809. }
  22810. }
  22811. /* Test bad args. */
  22812. if (ret == 0) {
  22813. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  22814. if (ret == 0) {
  22815. ret = wc_ecc_is_valid_idx(iVal2);
  22816. }
  22817. if (ret != 0) {
  22818. ret = WOLFSSL_FATAL_ERROR;
  22819. }
  22820. }
  22821. printf(resultFmt, ret == 0 ? passed : failed);
  22822. fflush(stdout);
  22823. if (wc_FreeRng(&rng) && ret == 0) {
  22824. ret = WOLFSSL_FATAL_ERROR;
  22825. }
  22826. wc_ecc_free(&key);
  22827. #ifdef FP_ECC
  22828. wc_ecc_fp_free();
  22829. #endif
  22830. #endif
  22831. return ret;
  22832. } /* END test_wc_ecc_is_valid_idx */
  22833. /*
  22834. * Testing wc_ecc_get_curve_id_from_oid()
  22835. */
  22836. static int test_wc_ecc_get_curve_id_from_oid(void)
  22837. {
  22838. int ret = 0;
  22839. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22840. !defined(HAVE_FIPS)
  22841. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22842. word32 len = sizeof(oid);
  22843. printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
  22844. /* Bad Cases */
  22845. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  22846. if (ret == BAD_FUNC_ARG) {
  22847. ret = 0;
  22848. }
  22849. if (ret == 0) {
  22850. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  22851. if (ret == ECC_CURVE_INVALID) {
  22852. ret = 0;
  22853. }
  22854. }
  22855. /* Good Case */
  22856. if (ret == 0) {
  22857. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  22858. if (ret == ECC_SECP256R1) {
  22859. ret = 0;
  22860. }
  22861. }
  22862. printf(resultFmt, ret == 0 ? passed : failed);
  22863. fflush(stdout);
  22864. #endif
  22865. return ret;
  22866. }/* END test_wc_ecc_get_curve_id_from_oid */
  22867. /*
  22868. * Testing wc_ecc_sig_size_calc()
  22869. */
  22870. static int test_wc_ecc_sig_size_calc(void)
  22871. {
  22872. int ret = 0;
  22873. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22874. ecc_key key;
  22875. WC_RNG rng;
  22876. int sz = 0;
  22877. printf(testingFmt, "wc_ecc_sig_size_calc()");
  22878. ret = wc_InitRng(&rng);
  22879. if (ret == 0) {
  22880. ret = wc_ecc_init(&key);
  22881. if (ret == 0) {
  22882. ret = wc_ecc_make_key(&rng, 16, &key);
  22883. #if defined(WOLFSSL_ASYNC_CRYPT)
  22884. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22885. #endif
  22886. }
  22887. sz = key.dp->size;
  22888. }
  22889. if (ret == 0) {
  22890. ret = wc_ecc_sig_size_calc(sz);
  22891. if (ret > 0) {
  22892. ret = 0;
  22893. }
  22894. }
  22895. printf(resultFmt, ret == 0 ? passed : failed);
  22896. fflush(stdout);
  22897. wc_ecc_free(&key);
  22898. wc_FreeRng(&rng);
  22899. #endif
  22900. return ret;
  22901. } /* END test_wc_ecc_sig_size_calc */
  22902. /*
  22903. * Testing ToTraditional
  22904. */
  22905. static int test_ToTraditional(void)
  22906. {
  22907. int ret = 0;
  22908. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  22909. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  22910. defined(OPENSSL_EXTRA_X509_SMALL))
  22911. XFILE f;
  22912. byte input[TWOK_BUF];
  22913. word32 sz;
  22914. printf(testingFmt, "ToTraditional()");
  22915. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  22916. AssertTrue((f != XBADFILE));
  22917. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  22918. XFCLOSE(f);
  22919. /* Good case */
  22920. ret = ToTraditional(input, sz);
  22921. if (ret > 0) {
  22922. ret = 0;
  22923. }
  22924. /* Bad cases */
  22925. if (ret == 0) {
  22926. ret = ToTraditional(NULL, 0);
  22927. if (ret == BAD_FUNC_ARG) {
  22928. ret = 0;
  22929. }
  22930. }
  22931. if (ret == 0) {
  22932. ret = ToTraditional(NULL, sz);
  22933. if (ret == BAD_FUNC_ARG) {
  22934. ret = 0;
  22935. }
  22936. }
  22937. if (ret == 0) {
  22938. ret = ToTraditional(input, 0);
  22939. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  22940. ret = 0;
  22941. }
  22942. }
  22943. printf(resultFmt, ret == 0 ? passed : failed);
  22944. fflush(stdout);
  22945. #endif
  22946. return ret;
  22947. }/* End test_ToTraditional*/
  22948. /*
  22949. * Testing wc_EccPrivateKeyToDer
  22950. */
  22951. static int test_wc_EccPrivateKeyToDer(void)
  22952. {
  22953. int ret = 0;
  22954. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22955. byte output[ONEK_BUF];
  22956. ecc_key eccKey;
  22957. WC_RNG rng;
  22958. word32 inLen;
  22959. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  22960. ret = wc_InitRng(&rng);
  22961. if (ret == 0) {
  22962. ret = wc_ecc_init(&eccKey);
  22963. if (ret == 0) {
  22964. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  22965. #if defined(WOLFSSL_ASYNC_CRYPT)
  22966. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22967. #endif
  22968. }
  22969. inLen = (word32)sizeof(output);
  22970. /* Bad Cases */
  22971. if (ret == 0) {
  22972. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  22973. if (ret == BAD_FUNC_ARG) {
  22974. ret = 0;
  22975. }
  22976. }
  22977. if (ret == 0) {
  22978. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  22979. if (ret == BAD_FUNC_ARG) {
  22980. ret = 0;
  22981. }
  22982. }
  22983. if (ret == 0) {
  22984. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  22985. if (ret == LENGTH_ONLY_E) {
  22986. ret = 0;
  22987. }
  22988. }
  22989. if (ret == 0) {
  22990. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  22991. if (ret == BAD_FUNC_ARG) {
  22992. ret = 0;
  22993. }
  22994. }
  22995. /*Good Case */
  22996. if (ret == 0) {
  22997. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  22998. if (ret > 0) {
  22999. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23000. /* test importing private only into a PKEY struct */
  23001. EC_KEY* ec;
  23002. EVP_PKEY* pkey;
  23003. const unsigned char* der = output;
  23004. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  23005. AssertNotNull(pkey);
  23006. der = output;
  23007. ec = d2i_ECPrivateKey(NULL, &der, ret);
  23008. AssertNotNull(ec);
  23009. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23010. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23011. #endif
  23012. ret = 0;
  23013. }
  23014. }
  23015. wc_ecc_free(&eccKey);
  23016. }
  23017. wc_FreeRng(&rng);
  23018. printf(resultFmt, ret == 0 ? passed : failed);
  23019. fflush(stdout);
  23020. #endif
  23021. return ret;
  23022. }/* End test_wc_EccPrivateKeyToDer*/
  23023. /*
  23024. * Testing wc_DhPublicKeyDecode
  23025. */
  23026. static int test_wc_DhPublicKeyDecode(void)
  23027. {
  23028. int ret = 0;
  23029. #ifndef NO_DH
  23030. word32 inOutIdx;
  23031. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23032. DhKey key;
  23033. AssertIntEQ(wc_InitDhKey(&key), 0);
  23034. printf(testingFmt, "wc_DhPublicKeyDecode()");
  23035. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  23036. BAD_FUNC_ARG);
  23037. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23038. BAD_FUNC_ARG);
  23039. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23040. BAD_FUNC_ARG);
  23041. inOutIdx = 0;
  23042. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23043. BAD_FUNC_ARG);
  23044. inOutIdx = 0;
  23045. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23046. BAD_FUNC_ARG);
  23047. inOutIdx = 0;
  23048. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23049. sizeof_dh_pub_key_der_2048), 0);
  23050. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  23051. key.pub.used != 0 && key.priv.used == 0);
  23052. wc_FreeDhKey(&key);
  23053. printf(resultFmt, passed);
  23054. #endif
  23055. (void)inOutIdx;
  23056. #endif /* !NO_DH */
  23057. return ret;
  23058. }
  23059. /*
  23060. * Testing wc_Ed25519KeyToDer
  23061. */
  23062. static int test_wc_Ed25519KeyToDer(void)
  23063. {
  23064. int ret = 0;
  23065. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23066. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23067. byte output[ONEK_BUF];
  23068. ed25519_key ed25519Key;
  23069. WC_RNG rng;
  23070. word32 inLen;
  23071. printf(testingFmt, "wc_Ed25519KeyToDer()");
  23072. ret = wc_InitRng(&rng);
  23073. if (ret == 0) {
  23074. ret = wc_ed25519_init(&ed25519Key);
  23075. if (ret == 0) {
  23076. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23077. }
  23078. inLen = (word32)sizeof(output);
  23079. /* Bad Cases */
  23080. if (ret == 0) {
  23081. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  23082. if (ret == BAD_FUNC_ARG) {
  23083. ret = 0;
  23084. }
  23085. }
  23086. if (ret == 0) {
  23087. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  23088. if (ret == BAD_FUNC_ARG) {
  23089. ret = 0;
  23090. }
  23091. }
  23092. if (ret == 0) {
  23093. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  23094. if (ret == BAD_FUNC_ARG) {
  23095. ret = 0;
  23096. }
  23097. }
  23098. /* Good Cases */
  23099. if (ret == 0) {
  23100. /* length only */
  23101. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  23102. if (ret > 0) {
  23103. ret = 0;
  23104. }
  23105. }
  23106. if (ret == 0) {
  23107. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  23108. if (ret > 0) {
  23109. ret = 0;
  23110. }
  23111. }
  23112. wc_ed25519_free(&ed25519Key);
  23113. }
  23114. wc_FreeRng(&rng);
  23115. printf(resultFmt, ret == 0 ? passed : failed);
  23116. fflush(stdout);
  23117. #endif
  23118. return ret;
  23119. }/* End test_wc_Ed25519KeyToDer*/
  23120. /*
  23121. * Testing wc_Ed25519PrivateKeyToDer
  23122. */
  23123. static int test_wc_Ed25519PrivateKeyToDer(void)
  23124. {
  23125. int ret = 0;
  23126. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23127. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23128. byte output[ONEK_BUF];
  23129. ed25519_key ed25519PrivKey;
  23130. WC_RNG rng;
  23131. word32 inLen;
  23132. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  23133. ret = wc_InitRng(&rng);
  23134. if (ret == 0) {
  23135. ret = wc_ed25519_init(&ed25519PrivKey);
  23136. if (ret == 0) {
  23137. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  23138. }
  23139. inLen = (word32)sizeof(output);
  23140. /* Bad Cases */
  23141. if (ret == 0) {
  23142. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  23143. if (ret == BAD_FUNC_ARG) {
  23144. ret = 0;
  23145. }
  23146. }
  23147. if (ret == 0) {
  23148. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  23149. if (ret == BAD_FUNC_ARG) {
  23150. ret = 0;
  23151. }
  23152. }
  23153. if (ret == 0) {
  23154. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  23155. if (ret == BAD_FUNC_ARG) {
  23156. ret = 0;
  23157. }
  23158. }
  23159. /* Good Cases */
  23160. if (ret == 0) {
  23161. /* length only */
  23162. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  23163. if (ret > 0) {
  23164. ret = 0;
  23165. }
  23166. }
  23167. if (ret == 0) {
  23168. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  23169. if (ret > 0) {
  23170. ret = 0;
  23171. }
  23172. }
  23173. wc_ed25519_free(&ed25519PrivKey);
  23174. }
  23175. wc_FreeRng(&rng);
  23176. printf(resultFmt, ret == 0 ? passed : failed);
  23177. fflush(stdout);
  23178. #endif
  23179. return ret;
  23180. }/* End test_wc_Ed25519PrivateKeyToDer*/
  23181. /*
  23182. * Testing wc_Ed448KeyToDer
  23183. */
  23184. static int test_wc_Ed448KeyToDer(void)
  23185. {
  23186. int ret = 0;
  23187. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23188. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23189. byte output[ONEK_BUF];
  23190. ed448_key ed448Key;
  23191. WC_RNG rng;
  23192. word32 inLen;
  23193. printf(testingFmt, "wc_Ed448KeyToDer()");
  23194. ret = wc_InitRng(&rng);
  23195. if (ret == 0) {
  23196. ret = wc_ed448_init(&ed448Key);
  23197. if (ret == 0) {
  23198. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23199. }
  23200. inLen = sizeof(output);
  23201. /* Bad Cases */
  23202. if (ret == 0) {
  23203. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  23204. if (ret == BAD_FUNC_ARG) {
  23205. ret = 0;
  23206. }
  23207. }
  23208. if (ret == 0) {
  23209. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  23210. if (ret == BAD_FUNC_ARG) {
  23211. ret = 0;
  23212. }
  23213. }
  23214. if (ret == 0) {
  23215. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  23216. if (ret == BAD_FUNC_ARG) {
  23217. ret = 0;
  23218. }
  23219. }
  23220. /* Good Cases */
  23221. if (ret == 0) {
  23222. /* length only */
  23223. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  23224. if (ret > 0) {
  23225. ret = 0;
  23226. }
  23227. }
  23228. if (ret == 0) {
  23229. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  23230. if (ret > 0) {
  23231. ret = 0;
  23232. }
  23233. }
  23234. wc_ed448_free(&ed448Key);
  23235. }
  23236. wc_FreeRng(&rng);
  23237. printf(resultFmt, ret == 0 ? passed : failed);
  23238. fflush(stdout);
  23239. #endif
  23240. return ret;
  23241. }/* End test_wc_Ed448KeyToDer*/
  23242. /*
  23243. * Testing wc_Ed448PrivateKeyToDer
  23244. */
  23245. static int test_wc_Ed448PrivateKeyToDer(void)
  23246. {
  23247. int ret = 0;
  23248. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23249. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23250. byte output[ONEK_BUF];
  23251. ed448_key ed448PrivKey;
  23252. WC_RNG rng;
  23253. word32 inLen;
  23254. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  23255. ret = wc_InitRng(&rng);
  23256. if (ret == 0) {
  23257. ret = wc_ed448_init(&ed448PrivKey);
  23258. if (ret == 0) {
  23259. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  23260. }
  23261. inLen = sizeof(output);
  23262. /* Bad Cases */
  23263. if (ret == 0) {
  23264. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  23265. if (ret == BAD_FUNC_ARG) {
  23266. ret = 0;
  23267. }
  23268. }
  23269. if (ret == 0) {
  23270. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  23271. if (ret == BAD_FUNC_ARG) {
  23272. ret = 0;
  23273. }
  23274. }
  23275. if (ret == 0) {
  23276. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  23277. if (ret == BAD_FUNC_ARG) {
  23278. ret = 0;
  23279. }
  23280. }
  23281. /* Good cases */
  23282. if (ret == 0) {
  23283. /* length only */
  23284. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  23285. if (ret > 0) {
  23286. ret = 0;
  23287. }
  23288. }
  23289. if (ret == 0) {
  23290. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  23291. if (ret > 0) {
  23292. ret = 0;
  23293. }
  23294. }
  23295. wc_ed448_free(&ed448PrivKey);
  23296. }
  23297. wc_FreeRng(&rng);
  23298. printf(resultFmt, ret == 0 ? passed : failed);
  23299. fflush(stdout);
  23300. #endif
  23301. return ret;
  23302. }/* End test_wc_Ed448PrivateKeyToDer*/
  23303. /*
  23304. * Testing wc_SetSubjectBuffer
  23305. */
  23306. static int test_wc_SetSubjectBuffer(void)
  23307. {
  23308. int ret = 0;
  23309. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  23310. Cert cert;
  23311. FILE* file;
  23312. byte* der;
  23313. word32 derSz;
  23314. printf(testingFmt, "wc_SetSubjectBuffer()");
  23315. derSz = FOURK_BUF;
  23316. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23317. if (der == NULL) {
  23318. ret = -1;
  23319. }
  23320. if (ret == 0) {
  23321. file = XFOPEN("./certs/ca-cert.der", "rb");
  23322. if (file != NULL) {
  23323. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  23324. XFCLOSE(file);
  23325. }
  23326. else {
  23327. ret = -1;
  23328. }
  23329. }
  23330. if (ret == 0) {
  23331. ret = wc_InitCert(&cert);
  23332. }
  23333. if (ret == 0) {
  23334. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  23335. }
  23336. if (ret == 0) {
  23337. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  23338. if (ret == BAD_FUNC_ARG) {
  23339. ret = 0;
  23340. }
  23341. }
  23342. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23343. printf(resultFmt, ret == 0 ? passed : failed);
  23344. fflush(stdout);
  23345. #endif
  23346. return ret;
  23347. }/* End test_wc_SetSubjectBuffer*/
  23348. /*
  23349. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23350. */
  23351. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23352. {
  23353. int ret = 0;
  23354. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23355. WC_RNG rng;
  23356. Cert cert;
  23357. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23358. ed25519_key ed25519Key;
  23359. #endif
  23360. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23361. RsaKey rsaKey;
  23362. int bits = 2048;
  23363. #endif
  23364. #if defined(HAVE_ECC)
  23365. ecc_key eccKey;
  23366. #endif
  23367. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23368. ed448_key ed448Key;
  23369. #endif
  23370. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  23371. #ifndef HAVE_FIPS
  23372. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23373. #else
  23374. ret = wc_InitRng(&rng);
  23375. #endif
  23376. wc_InitCert(&cert);
  23377. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23378. if (ret == 0) { /*ED25519*/
  23379. ret = wc_ed25519_init(&ed25519Key);
  23380. if (ret == 0) {
  23381. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23382. }
  23383. if (ret == 0) {
  23384. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23385. &ed25519Key);
  23386. }
  23387. wc_ed25519_free(&ed25519Key);
  23388. }
  23389. #endif
  23390. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23391. if (ret == 0) { /*RSA*/
  23392. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23393. if (ret == 0) {
  23394. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23395. }
  23396. if (ret == 0) {
  23397. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23398. }
  23399. wc_FreeRsaKey(&rsaKey);
  23400. }
  23401. #endif
  23402. #if defined(HAVE_ECC)
  23403. if (ret == 0) { /*ECC*/
  23404. ret = wc_ecc_init(&eccKey);
  23405. if (ret == 0) {
  23406. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23407. #if defined(WOLFSSL_ASYNC_CRYPT)
  23408. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23409. #endif
  23410. }
  23411. if (ret == 0) {
  23412. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23413. }
  23414. wc_ecc_free(&eccKey);
  23415. }
  23416. #endif
  23417. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23418. if (ret == 0) { /*ED448*/
  23419. ret = wc_ed448_init(&ed448Key);
  23420. if (ret == 0) {
  23421. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23422. }
  23423. if (ret == 0) {
  23424. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23425. &ed448Key);
  23426. }
  23427. wc_ed448_free(&ed448Key);
  23428. }
  23429. #endif
  23430. printf(resultFmt, ret == 0 ? passed : failed);
  23431. fflush(stdout);
  23432. wc_FreeRng(&rng);
  23433. #endif
  23434. return ret;
  23435. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23436. /*
  23437. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23438. */
  23439. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23440. {
  23441. int ret = 0;
  23442. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23443. WC_RNG rng;
  23444. Cert cert;
  23445. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23446. ed25519_key ed25519Key;
  23447. #endif
  23448. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23449. RsaKey rsaKey;
  23450. int bits = 2048;
  23451. #endif
  23452. #if defined(HAVE_ECC)
  23453. ecc_key eccKey;
  23454. #endif
  23455. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23456. ed448_key ed448Key;
  23457. #endif
  23458. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  23459. #ifndef HAVE_FIPS
  23460. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23461. #else
  23462. ret = wc_InitRng(&rng);
  23463. #endif
  23464. wc_InitCert(&cert);
  23465. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23466. if (ret == 0) { /*ED25519*/
  23467. ret = wc_ed25519_init(&ed25519Key);
  23468. if (ret == 0) {
  23469. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23470. }
  23471. if (ret == 0) {
  23472. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23473. &ed25519Key);
  23474. }
  23475. wc_ed25519_free(&ed25519Key);
  23476. }
  23477. #endif
  23478. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23479. if (ret == 0) { /*RSA*/
  23480. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23481. if (ret == 0) {
  23482. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23483. }
  23484. if (ret == 0) {
  23485. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23486. }
  23487. wc_FreeRsaKey(&rsaKey);
  23488. }
  23489. #endif
  23490. #if defined(HAVE_ECC)
  23491. if (ret == 0) { /*ECC*/
  23492. ret = wc_ecc_init(&eccKey);
  23493. if (ret == 0) {
  23494. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23495. #if defined(WOLFSSL_ASYNC_CRYPT)
  23496. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23497. #endif
  23498. }
  23499. if (ret == 0) {
  23500. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23501. }
  23502. wc_ecc_free(&eccKey);
  23503. }
  23504. #endif
  23505. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23506. if (ret == 0) { /*ED448*/
  23507. ret = wc_ed448_init(&ed448Key);
  23508. if (ret == 0) {
  23509. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23510. }
  23511. if (ret == 0) {
  23512. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23513. &ed448Key);
  23514. }
  23515. wc_ed448_free(&ed448Key);
  23516. }
  23517. #endif
  23518. printf(resultFmt, ret == 0 ? passed : failed);
  23519. fflush(stdout);
  23520. wc_FreeRng(&rng);
  23521. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23522. return ret;
  23523. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23524. /*
  23525. * Testing wc_PKCS7_New()
  23526. */
  23527. static int test_wc_PKCS7_New (void)
  23528. {
  23529. #if defined(HAVE_PKCS7)
  23530. PKCS7* pkcs7;
  23531. void* heap = NULL;
  23532. printf(testingFmt, "wc_PKCS7_New()");
  23533. pkcs7 = wc_PKCS7_New(heap, testDevId);
  23534. AssertNotNull(pkcs7);
  23535. printf(resultFmt, passed);
  23536. wc_PKCS7_Free(pkcs7);
  23537. #endif
  23538. return 0;
  23539. } /* END test-wc_PKCS7_New */
  23540. /*
  23541. * Testing wc_PKCS7_Init()
  23542. */
  23543. static int test_wc_PKCS7_Init (void)
  23544. {
  23545. #if defined(HAVE_PKCS7)
  23546. PKCS7* pkcs7;
  23547. void* heap = NULL;
  23548. printf(testingFmt, "wc_PKCS7_Init()");
  23549. pkcs7 = wc_PKCS7_New(heap, testDevId);
  23550. AssertNotNull(pkcs7);
  23551. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23552. /* Pass in bad args. */
  23553. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23554. printf(resultFmt, passed);
  23555. wc_PKCS7_Free(pkcs7);
  23556. #endif
  23557. return 0;
  23558. } /* END test-wc_PKCS7_Init */
  23559. /*
  23560. * Testing wc_PKCS7_InitWithCert()
  23561. */
  23562. static int test_wc_PKCS7_InitWithCert (void)
  23563. {
  23564. #if defined(HAVE_PKCS7)
  23565. PKCS7* pkcs7;
  23566. #ifndef NO_RSA
  23567. #if defined(USE_CERT_BUFFERS_2048)
  23568. unsigned char cert[sizeof(client_cert_der_2048)];
  23569. int certSz = (int)sizeof(cert);
  23570. XMEMSET(cert, 0, certSz);
  23571. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23572. #elif defined(USE_CERT_BUFFERS_1024)
  23573. unsigned char cert[sizeof(client_cert_der_1024)];
  23574. int certSz = (int)sizeof(cert);
  23575. XMEMSET(cert, 0, certSz);
  23576. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23577. #else
  23578. unsigned char cert[ONEK_BUF];
  23579. XFILE fp;
  23580. int certSz;
  23581. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23582. AssertTrue(fp != XBADFILE);
  23583. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23584. XFCLOSE(fp);
  23585. #endif
  23586. #elif defined(HAVE_ECC)
  23587. #if defined(USE_CERT_BUFFERS_256)
  23588. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23589. int certSz = (int)sizeof(cert);
  23590. XMEMSET(cert, 0, certSz);
  23591. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23592. #else
  23593. unsigned char cert[ONEK_BUF];
  23594. XFILE fp;
  23595. int certSz;
  23596. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23597. AssertTrue(fp != XBADFILE);
  23598. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  23599. XFCLOSE(fp);
  23600. #endif
  23601. #else
  23602. #error PKCS7 requires ECC or RSA
  23603. #endif
  23604. #ifdef HAVE_ECC
  23605. {
  23606. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23607. static unsigned char certWithInvalidEccKey[] = {
  23608. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23609. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23610. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23611. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23612. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23613. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23614. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23615. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23616. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23617. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23618. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23619. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23620. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23621. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23622. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23623. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23624. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23625. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23626. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23627. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23628. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23629. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23630. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23631. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23632. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23633. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23634. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23635. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23636. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23637. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23638. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23639. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23640. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23641. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23642. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23643. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23644. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23645. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23646. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23647. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23648. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23649. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23650. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23651. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23652. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23653. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23654. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23655. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23656. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23657. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23658. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23659. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23660. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23661. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23662. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23663. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23664. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23665. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23666. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23667. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23668. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23669. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23670. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23671. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23672. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23673. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23674. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23675. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23676. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23677. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23678. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23679. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23680. 0x08, 0xA8, 0xB4};
  23681. #endif
  23682. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  23683. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23684. /* If initialization is not successful, it's free'd in init func. */
  23685. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  23686. wc_PKCS7_Free(pkcs7);
  23687. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23688. /* Valid initialization usage. */
  23689. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23690. /* Pass in bad args. No need free for null checks, free at end.*/
  23691. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23692. BAD_FUNC_ARG);
  23693. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23694. BAD_FUNC_ARG);
  23695. #ifdef HAVE_ECC
  23696. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23697. sizeof(certWithInvalidEccKey)), 0);
  23698. }
  23699. #endif
  23700. printf(resultFmt, passed);
  23701. wc_PKCS7_Free(pkcs7);
  23702. #endif
  23703. return 0;
  23704. } /* END test_wc_PKCS7_InitWithCert */
  23705. /*
  23706. * Testing wc_PKCS7_EncodeData()
  23707. */
  23708. static int test_wc_PKCS7_EncodeData (void)
  23709. {
  23710. #if defined(HAVE_PKCS7)
  23711. PKCS7* pkcs7;
  23712. byte output[FOURK_BUF];
  23713. byte data[] = "My encoded DER cert.";
  23714. #ifndef NO_RSA
  23715. #if defined(USE_CERT_BUFFERS_2048)
  23716. unsigned char cert[sizeof(client_cert_der_2048)];
  23717. unsigned char key[sizeof(client_key_der_2048)];
  23718. int certSz = (int)sizeof(cert);
  23719. int keySz = (int)sizeof(key);
  23720. XMEMSET(cert, 0, certSz);
  23721. XMEMSET(key, 0, keySz);
  23722. XMEMCPY(cert, client_cert_der_2048, certSz);
  23723. XMEMCPY(key, client_key_der_2048, keySz);
  23724. #elif defined(USE_CERT_BUFFERS_1024)
  23725. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23726. unsigned char key[sizeof_client_key_der_1024];
  23727. int certSz = (int)sizeof(cert);
  23728. int keySz = (int)sizeof(key);
  23729. XMEMSET(cert, 0, certSz);
  23730. XMEMSET(key, 0, keySz);
  23731. XMEMCPY(cert, client_cert_der_1024, certSz);
  23732. XMEMCPY(key, client_key_der_1024, keySz);
  23733. #else
  23734. unsigned char cert[ONEK_BUF];
  23735. unsigned char key[ONEK_BUF];
  23736. XFILE fp;
  23737. int certSz;
  23738. int keySz;
  23739. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23740. AssertTrue(fp != XBADFILE);
  23741. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23742. XFCLOSE(fp);
  23743. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23744. AssertTrue(fp != XBADFILE);
  23745. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23746. XFCLOSE(fp);
  23747. #endif
  23748. #elif defined(HAVE_ECC)
  23749. #if defined(USE_CERT_BUFFERS_256)
  23750. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23751. unsigned char key[sizeof(ecc_clikey_der_256)];
  23752. int certSz = (int)sizeof(cert);
  23753. int keySz = (int)sizeof(key);
  23754. XMEMSET(cert, 0, certSz);
  23755. XMEMSET(key, 0, keySz);
  23756. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23757. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23758. #else
  23759. unsigned char cert[ONEK_BUF];
  23760. unsigned char key[ONEK_BUF];
  23761. XFILE fp;
  23762. int certSz, keySz;
  23763. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  23764. AssertTrue(fp != XBADFILE);
  23765. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  23766. XFCLOSE(fp);
  23767. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23768. AssertTrue(fp != XBADFILE);
  23769. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  23770. XFCLOSE(fp);
  23771. #endif
  23772. #endif
  23773. XMEMSET(output, 0, sizeof(output));
  23774. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23775. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23776. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23777. printf(testingFmt, "wc_PKCS7_EncodeData()");
  23778. pkcs7->content = data;
  23779. pkcs7->contentSz = sizeof(data);
  23780. pkcs7->privateKey = key;
  23781. pkcs7->privateKeySz = keySz;
  23782. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23783. /* Test bad args. */
  23784. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23785. BAD_FUNC_ARG);
  23786. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23787. BAD_FUNC_ARG);
  23788. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23789. printf(resultFmt, passed);
  23790. wc_PKCS7_Free(pkcs7);
  23791. #endif
  23792. return 0;
  23793. } /* END test_wc_PKCS7_EncodeData */
  23794. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23795. !defined(NO_RSA) && !defined(NO_SHA256)
  23796. /* RSA sign raw digest callback */
  23797. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23798. byte* out, word32 outSz, byte* privateKey,
  23799. word32 privateKeySz, int devid, int hashOID)
  23800. {
  23801. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23802. byte digInfoEncoding[] = {
  23803. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23804. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23805. 0x00, 0x04, 0x20
  23806. };
  23807. int ret;
  23808. byte digestInfo[ONEK_BUF];
  23809. byte sig[FOURK_BUF];
  23810. word32 digestInfoSz = 0;
  23811. word32 idx = 0;
  23812. RsaKey rsa;
  23813. /* SHA-256 required only for this example callback due to above
  23814. * digInfoEncoding[] */
  23815. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23816. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23817. (hashOID != SHA256h)) {
  23818. return -1;
  23819. }
  23820. /* build DigestInfo */
  23821. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23822. digestInfoSz += sizeof(digInfoEncoding);
  23823. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23824. digestInfoSz += digestSz;
  23825. /* set up RSA key */
  23826. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23827. if (ret != 0) {
  23828. return ret;
  23829. }
  23830. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23831. /* sign DigestInfo */
  23832. if (ret == 0) {
  23833. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23834. &rsa, pkcs7->rng);
  23835. if (ret > 0) {
  23836. if (ret > (int)outSz) {
  23837. /* output buffer too small */
  23838. ret = -1;
  23839. } else {
  23840. /* success, ret holds sig size */
  23841. XMEMCPY(out, sig, ret);
  23842. }
  23843. }
  23844. }
  23845. wc_FreeRsaKey(&rsa);
  23846. return ret;
  23847. }
  23848. #endif
  23849. /*
  23850. * Testing wc_PKCS7_EncodeSignedData()
  23851. */
  23852. static int test_wc_PKCS7_EncodeSignedData(void)
  23853. {
  23854. #if defined(HAVE_PKCS7)
  23855. PKCS7* pkcs7;
  23856. WC_RNG rng;
  23857. byte output[FOURK_BUF];
  23858. byte badOut[1];
  23859. word32 outputSz = (word32)sizeof(output);
  23860. word32 badOutSz = 0;
  23861. byte data[] = "Test data to encode.";
  23862. #ifndef NO_RSA
  23863. #if defined(USE_CERT_BUFFERS_2048)
  23864. byte key[sizeof(client_key_der_2048)];
  23865. byte cert[sizeof(client_cert_der_2048)];
  23866. word32 keySz = (word32)sizeof(key);
  23867. word32 certSz = (word32)sizeof(cert);
  23868. XMEMSET(key, 0, keySz);
  23869. XMEMSET(cert, 0, certSz);
  23870. XMEMCPY(key, client_key_der_2048, keySz);
  23871. XMEMCPY(cert, client_cert_der_2048, certSz);
  23872. #elif defined(USE_CERT_BUFFERS_1024)
  23873. byte key[sizeof_client_key_der_1024];
  23874. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23875. word32 keySz = (word32)sizeof(key);
  23876. word32 certSz = (word32)sizeof(cert);
  23877. XMEMSET(key, 0, keySz);
  23878. XMEMSET(cert, 0, certSz);
  23879. XMEMCPY(key, client_key_der_1024, keySz);
  23880. XMEMCPY(cert, client_cert_der_1024, certSz);
  23881. #else
  23882. unsigned char cert[ONEK_BUF];
  23883. unsigned char key[ONEK_BUF];
  23884. XFILE fp;
  23885. int certSz;
  23886. int keySz;
  23887. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  23888. AssertTrue(fp != XBADFILE);
  23889. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  23890. XFCLOSE(fp);
  23891. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  23892. AssertTrue(fp != XBADFILE);
  23893. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  23894. XFCLOSE(fp);
  23895. #endif
  23896. #elif defined(HAVE_ECC)
  23897. #if defined(USE_CERT_BUFFERS_256)
  23898. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23899. unsigned char key[sizeof(ecc_clikey_der_256)];
  23900. int certSz = (int)sizeof(cert);
  23901. int keySz = (int)sizeof(key);
  23902. XMEMSET(cert, 0, certSz);
  23903. XMEMSET(key, 0, keySz);
  23904. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23905. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23906. #else
  23907. unsigned char cert[ONEK_BUF];
  23908. unsigned char key[ONEK_BUF];
  23909. XFILE fp;
  23910. int certSz, keySz;
  23911. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  23912. AssertTrue(fp != XBADFILE);
  23913. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  23914. XFCLOSE(fp);
  23915. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  23916. AssertTrue(fp != XBADFILE);
  23917. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  23918. XFCLOSE(fp);
  23919. #endif
  23920. #endif
  23921. XMEMSET(output, 0, outputSz);
  23922. AssertIntEQ(wc_InitRng(&rng), 0);
  23923. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23924. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23925. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23926. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  23927. pkcs7->content = data;
  23928. pkcs7->contentSz = (word32)sizeof(data);
  23929. pkcs7->privateKey = key;
  23930. pkcs7->privateKeySz = (word32)sizeof(key);
  23931. pkcs7->encryptOID = RSAk;
  23932. pkcs7->hashOID = SHAh;
  23933. pkcs7->rng = &rng;
  23934. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23935. wc_PKCS7_Free(pkcs7);
  23936. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23937. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23938. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  23939. /* Pass in bad args. */
  23940. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  23941. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  23942. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  23943. badOutSz), BAD_FUNC_ARG);
  23944. pkcs7->hashOID = 0; /* bad hashOID */
  23945. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  23946. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23947. !defined(NO_RSA) && !defined(NO_SHA256)
  23948. /* test RSA sign raw digest callback, if using RSA and compiled in.
  23949. * Example callback assumes SHA-256, so only run test if compiled in. */
  23950. wc_PKCS7_Free(pkcs7);
  23951. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23952. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  23953. pkcs7->content = data;
  23954. pkcs7->contentSz = (word32)sizeof(data);
  23955. pkcs7->privateKey = key;
  23956. pkcs7->privateKeySz = (word32)sizeof(key);
  23957. pkcs7->encryptOID = RSAk;
  23958. pkcs7->hashOID = SHA256h;
  23959. pkcs7->rng = &rng;
  23960. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  23961. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  23962. #endif
  23963. printf(resultFmt, passed);
  23964. wc_PKCS7_Free(pkcs7);
  23965. wc_FreeRng(&rng);
  23966. #endif
  23967. return 0;
  23968. } /* END test_wc_PKCS7_EncodeSignedData */
  23969. /*
  23970. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  23971. */
  23972. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  23973. {
  23974. #if defined(HAVE_PKCS7)
  23975. int ret, i;
  23976. PKCS7* pkcs7;
  23977. WC_RNG rng;
  23978. byte outputHead[FOURK_BUF/2];
  23979. byte outputFoot[FOURK_BUF/2];
  23980. word32 outputHeadSz = (word32)sizeof(outputHead);
  23981. word32 outputFootSz = (word32)sizeof(outputFoot);
  23982. byte data[FOURK_BUF];
  23983. wc_HashAlg hash;
  23984. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  23985. byte hashBuf[WC_MAX_DIGEST_SIZE];
  23986. word32 hashSz = wc_HashGetDigestSize(hashType);
  23987. #ifndef NO_RSA
  23988. #if defined(USE_CERT_BUFFERS_2048)
  23989. byte key[sizeof(client_key_der_2048)];
  23990. byte cert[sizeof(client_cert_der_2048)];
  23991. word32 keySz = (word32)sizeof(key);
  23992. word32 certSz = (word32)sizeof(cert);
  23993. XMEMSET(key, 0, keySz);
  23994. XMEMSET(cert, 0, certSz);
  23995. XMEMCPY(key, client_key_der_2048, keySz);
  23996. XMEMCPY(cert, client_cert_der_2048, certSz);
  23997. #elif defined(USE_CERT_BUFFERS_1024)
  23998. byte key[sizeof_client_key_der_1024];
  23999. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24000. word32 keySz = (word32)sizeof(key);
  24001. word32 certSz = (word32)sizeof(cert);
  24002. XMEMSET(key, 0, keySz);
  24003. XMEMSET(cert, 0, certSz);
  24004. XMEMCPY(key, client_key_der_1024, keySz);
  24005. XMEMCPY(cert, client_cert_der_1024, certSz);
  24006. #else
  24007. unsigned char cert[ONEK_BUF];
  24008. unsigned char key[ONEK_BUF];
  24009. XFILE fp;
  24010. int certSz;
  24011. int keySz;
  24012. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24013. AssertTrue((fp != XBADFILE));
  24014. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24015. XFCLOSE(fp);
  24016. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24017. AssertTrue(fp != XBADFILE);
  24018. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24019. XFCLOSE(fp);
  24020. #endif
  24021. #elif defined(HAVE_ECC)
  24022. #if defined(USE_CERT_BUFFERS_256)
  24023. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24024. unsigned char key[sizeof(ecc_clikey_der_256)];
  24025. int certSz = (int)sizeof(cert);
  24026. int keySz = (int)sizeof(key);
  24027. XMEMSET(cert, 0, certSz);
  24028. XMEMSET(key, 0, keySz);
  24029. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24030. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24031. #else
  24032. unsigned char cert[ONEK_BUF];
  24033. unsigned char key[ONEK_BUF];
  24034. XFILE fp;
  24035. int certSz, keySz;
  24036. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24037. AssertTrue(fp != XBADFILE);
  24038. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24039. XFCLOSE(fp);
  24040. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24041. AssertTrue(fp != XBADFILE);
  24042. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24043. XFCLOSE(fp);
  24044. #endif
  24045. #endif
  24046. /* initialize large data with sequence */
  24047. for (i=0; i<(int)sizeof(data); i++)
  24048. data[i] = i & 0xff;
  24049. XMEMSET(outputHead, 0, outputHeadSz);
  24050. XMEMSET(outputFoot, 0, outputFootSz);
  24051. AssertIntEQ(wc_InitRng(&rng), 0);
  24052. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24053. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24054. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24055. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  24056. pkcs7->content = NULL; /* not used for ex */
  24057. pkcs7->contentSz = (word32)sizeof(data);
  24058. pkcs7->privateKey = key;
  24059. pkcs7->privateKeySz = (word32)sizeof(key);
  24060. pkcs7->encryptOID = RSAk;
  24061. pkcs7->hashOID = SHAh;
  24062. pkcs7->rng = &rng;
  24063. /* calculate hash for content */
  24064. ret = wc_HashInit(&hash, hashType);
  24065. if (ret == 0) {
  24066. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24067. if (ret == 0) {
  24068. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24069. }
  24070. wc_HashFree(&hash, hashType);
  24071. }
  24072. AssertIntEQ(ret, 0);
  24073. /* Perform PKCS7 sign using hash directly */
  24074. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24075. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24076. AssertIntGT(outputHeadSz, 0);
  24077. AssertIntGT(outputFootSz, 0);
  24078. wc_PKCS7_Free(pkcs7);
  24079. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24080. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24081. /* required parameter even on verify when using _ex, if using outputHead
  24082. * and outputFoot */
  24083. pkcs7->contentSz = (word32)sizeof(data);
  24084. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24085. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24086. wc_PKCS7_Free(pkcs7);
  24087. /* assembly complete PKCS7 sign and use normal verify */
  24088. {
  24089. byte* output = (byte*)XMALLOC(outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24090. word32 outputSz = 0;
  24091. AssertNotNull(output);
  24092. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24093. outputSz += outputHeadSz;
  24094. XMEMCPY(&output[outputSz], data, sizeof(data));
  24095. outputSz += sizeof(data);
  24096. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24097. outputSz += outputFootSz;
  24098. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24099. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24100. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24101. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24102. }
  24103. /* Pass in bad args. */
  24104. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24105. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24106. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24107. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24108. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24109. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24110. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24111. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24112. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24113. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24114. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24115. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24116. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24117. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24118. pkcs7->hashOID = 0; /* bad hashOID */
  24119. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24120. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24121. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24122. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24123. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24124. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24125. #ifndef NO_PKCS7_STREAM
  24126. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24127. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24128. #else
  24129. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24130. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24131. #endif
  24132. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24133. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24134. #ifndef NO_PKCS7_STREAM
  24135. /* can pass in 0 buffer length with streaming API */
  24136. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24137. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24138. #else
  24139. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24140. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24141. #endif
  24142. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24143. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24144. #ifndef NO_PKCS7_STREAM
  24145. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24146. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24147. #else
  24148. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24149. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  24150. #endif
  24151. printf(resultFmt, passed);
  24152. wc_PKCS7_Free(pkcs7);
  24153. wc_FreeRng(&rng);
  24154. #endif
  24155. return 0;
  24156. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24157. #if defined(HAVE_PKCS7)
  24158. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24159. byte* data, word32 dataSz,
  24160. int withAttribs, int detachedSig)
  24161. {
  24162. PKCS7* pkcs7;
  24163. WC_RNG rng;
  24164. static byte messageTypeOid[] =
  24165. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24166. 0x09, 0x02 };
  24167. static byte messageType[] = { 0x13, 2, '1', '9' };
  24168. PKCS7Attrib attribs[] =
  24169. {
  24170. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24171. sizeof(messageType) }
  24172. };
  24173. #ifndef NO_RSA
  24174. #if defined(USE_CERT_BUFFERS_2048)
  24175. byte key[sizeof(client_key_der_2048)];
  24176. byte cert[sizeof(client_cert_der_2048)];
  24177. word32 keySz = (word32)sizeof(key);
  24178. word32 certSz = (word32)sizeof(cert);
  24179. XMEMSET(key, 0, keySz);
  24180. XMEMSET(cert, 0, certSz);
  24181. XMEMCPY(key, client_key_der_2048, keySz);
  24182. XMEMCPY(cert, client_cert_der_2048, certSz);
  24183. #elif defined(USE_CERT_BUFFERS_1024)
  24184. byte key[sizeof_client_key_der_1024];
  24185. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24186. word32 keySz = (word32)sizeof(key);
  24187. word32 certSz = (word32)sizeof(cert);
  24188. XMEMSET(key, 0, keySz);
  24189. XMEMSET(cert, 0, certSz);
  24190. XMEMCPY(key, client_key_der_1024, keySz);
  24191. XMEMCPY(cert, client_cert_der_1024, certSz);
  24192. #else
  24193. unsigned char cert[ONEK_BUF];
  24194. unsigned char key[ONEK_BUF];
  24195. FILE* fp;
  24196. int certSz;
  24197. int keySz;
  24198. fp = fopen("./certs/1024/client-cert.der", "rb");
  24199. AssertNotNull(fp);
  24200. certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
  24201. fclose(fp);
  24202. fp = fopen("./certs/1024/client-key.der", "rb");
  24203. AssertNotNull(fp);
  24204. keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
  24205. fclose(fp);
  24206. #endif
  24207. #elif defined(HAVE_ECC)
  24208. #if defined(USE_CERT_BUFFERS_256)
  24209. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24210. unsigned char key[sizeof(ecc_clikey_der_256)];
  24211. int certSz = (int)sizeof(cert);
  24212. int keySz = (int)sizeof(key);
  24213. XMEMSET(cert, 0, certSz);
  24214. XMEMSET(key, 0, keySz);
  24215. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24216. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24217. #else
  24218. unsigned char cert[ONEK_BUF];
  24219. unsigned char key[ONEK_BUF];
  24220. FILE* fp;
  24221. int certSz, keySz;
  24222. fp = fopen("./certs/client-ecc-cert.der", "rb");
  24223. AssertNotNull(fp);
  24224. certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24225. fclose(fp);
  24226. fp = fopen("./certs/client-ecc-key.der", "rb");
  24227. AssertNotNull(fp);
  24228. keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
  24229. fclose(fp);
  24230. #endif
  24231. #endif
  24232. XMEMSET(output, 0, outputSz);
  24233. AssertIntEQ(wc_InitRng(&rng), 0);
  24234. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24235. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24236. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24237. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  24238. pkcs7->content = data;
  24239. pkcs7->contentSz = dataSz;
  24240. pkcs7->privateKey = key;
  24241. pkcs7->privateKeySz = (word32)sizeof(key);
  24242. pkcs7->encryptOID = RSAk;
  24243. pkcs7->hashOID = SHAh;
  24244. pkcs7->rng = &rng;
  24245. if (withAttribs) {
  24246. /* include a signed attribute */
  24247. pkcs7->signedAttribs = attribs;
  24248. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24249. }
  24250. if (detachedSig) {
  24251. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24252. }
  24253. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24254. wc_PKCS7_Free(pkcs7);
  24255. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24256. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24257. if (detachedSig) {
  24258. pkcs7->content = data;
  24259. pkcs7->contentSz = dataSz;
  24260. }
  24261. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24262. wc_PKCS7_Free(pkcs7);
  24263. wc_FreeRng(&rng);
  24264. return outputSz;
  24265. }
  24266. #endif
  24267. /*
  24268. * Testing wc_PKCS_VerifySignedData()
  24269. */
  24270. static int test_wc_PKCS7_VerifySignedData(void)
  24271. {
  24272. #if defined(HAVE_PKCS7)
  24273. PKCS7* pkcs7;
  24274. byte output[FOURK_BUF];
  24275. word32 outputSz = sizeof(output);
  24276. byte data[] = "Test data to encode.";
  24277. byte badOut[1];
  24278. word32 badOutSz = 0;
  24279. byte badContent[] = "This is different content than was signed";
  24280. int ret;
  24281. wc_HashAlg hash;
  24282. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24283. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24284. word32 hashSz = wc_HashGetDigestSize(hashType);
  24285. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24286. (word32)sizeof(data),
  24287. 0, 0)), 0);
  24288. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24289. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24290. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24291. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24292. /* Test bad args. */
  24293. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  24294. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24295. #ifndef NO_PKCS7_STREAM
  24296. /* can pass in 0 buffer length with streaming API */
  24297. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24298. badOutSz), WC_PKCS7_WANT_READ_E);
  24299. #else
  24300. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24301. badOutSz), BAD_FUNC_ARG);
  24302. #endif
  24303. wc_PKCS7_Free(pkcs7);
  24304. /* Invalid content should error, use detached signature so we can
  24305. * easily change content */
  24306. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24307. (word32)sizeof(data),
  24308. 1, 1)), 0);
  24309. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24310. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24311. pkcs7->content = badContent;
  24312. pkcs7->contentSz = sizeof(badContent);
  24313. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), SIG_VERIFY_E);
  24314. wc_PKCS7_Free(pkcs7);
  24315. /* Test success case with detached signature and valid content */
  24316. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24317. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24318. pkcs7->content = data;
  24319. pkcs7->contentSz = sizeof(data);
  24320. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24321. wc_PKCS7_Free(pkcs7);
  24322. /* verify using pre-computed content digest only (no content) */
  24323. {
  24324. /* calculate hash for content */
  24325. ret = wc_HashInit(&hash, hashType);
  24326. if (ret == 0) {
  24327. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24328. if (ret == 0) {
  24329. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24330. }
  24331. wc_HashFree(&hash, hashType);
  24332. }
  24333. AssertIntEQ(ret, 0);
  24334. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24335. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24336. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24337. output, outputSz,
  24338. NULL, 0), 0);
  24339. wc_PKCS7_Free(pkcs7);
  24340. }
  24341. printf(resultFmt, passed);
  24342. #endif
  24343. return 0;
  24344. } /* END test_wc_PKCS7_VerifySignedData() */
  24345. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  24346. !defined(NO_AES_256)
  24347. static const byte defKey[] = {
  24348. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24349. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24350. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24351. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24352. };
  24353. static byte aesHandle[32]; /* simulated hardware key handle */
  24354. /* return 0 on success */
  24355. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  24356. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  24357. byte* in, int inSz, byte* out, void* usrCtx)
  24358. {
  24359. int ret;
  24360. Aes aes;
  24361. if (usrCtx == NULL) {
  24362. /* no simulated handle passed in */
  24363. return -1;
  24364. }
  24365. switch (encryptOID) {
  24366. case AES256CBCb:
  24367. if (ivSz != AES_BLOCK_SIZE)
  24368. return BAD_FUNC_ARG;
  24369. break;
  24370. default:
  24371. WOLFSSL_MSG("Unsupported content cipher type for test");
  24372. return ALGO_ID_E;
  24373. };
  24374. /* simulate using handle to get key */
  24375. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  24376. if (ret == 0) {
  24377. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  24378. if (ret == 0)
  24379. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  24380. wc_AesFree(&aes);
  24381. }
  24382. (void)aad;
  24383. (void)aadSz;
  24384. (void)authTag;
  24385. (void)authTagSz;
  24386. (void)pkcs7;
  24387. return ret;
  24388. }
  24389. /* returns key size on success */
  24390. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  24391. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  24392. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  24393. {
  24394. int ret = -1;
  24395. if (out == NULL)
  24396. return BAD_FUNC_ARG;
  24397. if (keyId[0] != 0x00) {
  24398. return -1;
  24399. }
  24400. if (type != (int)PKCS7_KEKRI) {
  24401. return -1;
  24402. }
  24403. switch (keyWrapAlgo) {
  24404. case AES256_WRAP:
  24405. /* simulate setting a handle for later decryption but use key
  24406. * as handle in the test case here */
  24407. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  24408. aesHandle, sizeof(aesHandle), NULL);
  24409. if (ret < 0)
  24410. return ret;
  24411. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  24412. if (ret < 0)
  24413. return ret;
  24414. /* return key size on success */
  24415. return sizeof(defKey);
  24416. default:
  24417. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  24418. return BAD_KEYWRAP_ALG_E;
  24419. };
  24420. (void)cekSz;
  24421. (void)cek;
  24422. (void)outSz;
  24423. (void)keyIdSz;
  24424. (void)direction;
  24425. (void)orginKey; /* used with KAKRI */
  24426. (void)orginKeySz;
  24427. return ret;
  24428. }
  24429. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  24430. /*
  24431. * Testing wc_PKCS7_EncodeEnvelopedData()
  24432. */
  24433. static int test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  24434. {
  24435. #if defined(HAVE_PKCS7)
  24436. PKCS7* pkcs7;
  24437. #ifdef ECC_TIMING_RESISTANT
  24438. WC_RNG rng;
  24439. #endif
  24440. word32 tempWrd32 = 0;
  24441. byte* tmpBytePtr = NULL;
  24442. const char input[] = "Test data to encode.";
  24443. int i;
  24444. int testSz = 0;
  24445. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  24446. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24447. byte* rsaCert = NULL;
  24448. byte* rsaPrivKey = NULL;
  24449. word32 rsaCertSz;
  24450. word32 rsaPrivKeySz;
  24451. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  24452. !defined(USE_CERT_BUFFERS_2048) )
  24453. static const char* rsaClientCert = "./certs/client-cert.der";
  24454. static const char* rsaClientKey = "./certs/client-key.der";
  24455. rsaCertSz = (word32)sizeof(rsaClientCert);
  24456. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  24457. #endif
  24458. #endif
  24459. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24460. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24461. byte* eccCert = NULL;
  24462. byte* eccPrivKey = NULL;
  24463. word32 eccCertSz;
  24464. word32 eccPrivKeySz;
  24465. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  24466. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  24467. static const char* eccClientKey = "./certs/ecc-client-key.der";
  24468. #endif
  24469. #endif
  24470. /* Generic buffer size. */
  24471. byte output[ONEK_BUF];
  24472. byte decoded[sizeof(input)/sizeof(char)];
  24473. int decodedSz = 0;
  24474. #ifndef NO_FILESYSTEM
  24475. XFILE certFile;
  24476. XFILE keyFile;
  24477. #endif
  24478. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24479. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24480. /* RSA certs and keys. */
  24481. #if defined(USE_CERT_BUFFERS_1024)
  24482. /* Allocate buffer space. */
  24483. AssertNotNull(rsaCert =
  24484. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24485. /* Init buffer. */
  24486. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  24487. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  24488. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  24489. DYNAMIC_TYPE_TMP_BUFFER));
  24490. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  24491. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  24492. #elif defined(USE_CERT_BUFFERS_2048)
  24493. /* Allocate buffer */
  24494. AssertNotNull(rsaCert =
  24495. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24496. /* Init buffer. */
  24497. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  24498. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  24499. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24500. DYNAMIC_TYPE_TMP_BUFFER));
  24501. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  24502. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  24503. #else
  24504. /* File system. */
  24505. certFile = XFOPEN(rsaClientCert, "rb");
  24506. AssertTrue(certFile != XBADFILE);
  24507. rsaCertSz = (word32)FOURK_BUF;
  24508. AssertNotNull(rsaCert =
  24509. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24510. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  24511. XFCLOSE(certFile);
  24512. keyFile = XFOPEN(rsaClientKey, "rb");
  24513. AssertTrue(keyFile != XBADFILE);
  24514. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24515. DYNAMIC_TYPE_TMP_BUFFER));
  24516. rsaPrivKeySz = (word32)FOURK_BUF;
  24517. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  24518. XFCLOSE(keyFile);
  24519. #endif /* USE_CERT_BUFFERS */
  24520. #endif /* NO_RSA */
  24521. /* ECC */
  24522. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  24523. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  24524. #ifdef USE_CERT_BUFFERS_256
  24525. AssertNotNull(eccCert =
  24526. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24527. /* Init buffer. */
  24528. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  24529. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  24530. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  24531. DYNAMIC_TYPE_TMP_BUFFER));
  24532. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  24533. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  24534. #else /* File system. */
  24535. certFile = XFOPEN(eccClientCert, "rb");
  24536. AssertTrue(certFile != XBADFILE);
  24537. eccCertSz = (word32)FOURK_BUF;
  24538. AssertNotNull(eccCert =
  24539. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24540. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  24541. XFCLOSE(certFile);
  24542. keyFile = XFOPEN(eccClientKey, "rb");
  24543. AssertTrue(keyFile != XBADFILE);
  24544. eccPrivKeySz = (word32)FOURK_BUF;
  24545. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  24546. DYNAMIC_TYPE_TMP_BUFFER));
  24547. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  24548. XFCLOSE(keyFile);
  24549. #endif /* USE_CERT_BUFFERS_256 */
  24550. #endif /* END HAVE_ECC */
  24551. /* Silence. */
  24552. (void)keyFile;
  24553. (void)certFile;
  24554. {
  24555. const pkcs7EnvelopedVector testVectors[] = {
  24556. /* DATA is a global variable defined in the makefile. */
  24557. #if !defined(NO_RSA)
  24558. #ifndef NO_DES3
  24559. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  24560. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24561. #endif /* NO_DES3 */
  24562. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24563. #ifndef NO_AES_128
  24564. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24565. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24566. #endif
  24567. #ifndef NO_AES_192
  24568. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  24569. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24570. #endif
  24571. #ifndef NO_AES_256
  24572. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24573. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  24574. #endif
  24575. #endif /* NO_AES && HAVE_AES_CBC */
  24576. #endif /* NO_RSA */
  24577. #if defined(HAVE_ECC)
  24578. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24579. #if !defined(NO_SHA) && !defined(NO_AES_128)
  24580. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  24581. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  24582. eccCertSz, eccPrivKey, eccPrivKeySz},
  24583. #endif
  24584. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  24585. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24586. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  24587. eccCertSz, eccPrivKey, eccPrivKeySz},
  24588. #endif
  24589. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  24590. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  24591. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  24592. eccCertSz, eccPrivKey, eccPrivKeySz},
  24593. #endif
  24594. #endif /* NO_AES && HAVE_AES_CBC*/
  24595. #endif /* END HAVE_ECC */
  24596. }; /* END pkcs7EnvelopedVector */
  24597. #ifdef ECC_TIMING_RESISTANT
  24598. AssertIntEQ(wc_InitRng(&rng), 0);
  24599. #endif
  24600. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  24601. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24602. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24603. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  24604. for (i = 0; i < testSz; i++) {
  24605. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  24606. (word32)(testVectors + i)->certSz), 0);
  24607. #ifdef ECC_TIMING_RESISTANT
  24608. pkcs7->rng = &rng;
  24609. #endif
  24610. pkcs7->content = (byte*)(testVectors + i)->content;
  24611. pkcs7->contentSz = (testVectors + i)->contentSz;
  24612. pkcs7->contentOID = (testVectors + i)->contentOID;
  24613. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  24614. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  24615. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  24616. pkcs7->privateKey = (testVectors + i)->privateKey;
  24617. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  24618. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24619. (word32)sizeof(output)), 0);
  24620. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24621. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  24622. AssertIntGE(decodedSz, 0);
  24623. /* Verify the size of each buffer. */
  24624. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  24625. /* Don't free the last time through the loop. */
  24626. if (i < testSz - 1 ){
  24627. wc_PKCS7_Free(pkcs7);
  24628. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24629. }
  24630. } /* END test loop. */
  24631. }
  24632. /* Test bad args. */
  24633. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  24634. (word32)sizeof(output)), BAD_FUNC_ARG);
  24635. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  24636. (word32)sizeof(output)), BAD_FUNC_ARG);
  24637. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  24638. printf(resultFmt, passed);
  24639. /* Decode. */
  24640. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  24641. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  24642. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24643. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24644. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24645. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24646. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  24647. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  24648. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24649. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  24650. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24651. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  24652. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  24653. /* only a failure for KARI test cases */
  24654. tempWrd32 = pkcs7->singleCertSz;
  24655. pkcs7->singleCertSz = 0;
  24656. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24657. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24658. pkcs7->singleCertSz = tempWrd32;
  24659. tmpBytePtr = pkcs7->singleCert;
  24660. pkcs7->singleCert = NULL;
  24661. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24662. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24663. pkcs7->singleCert = tmpBytePtr;
  24664. #endif
  24665. tempWrd32 = pkcs7->privateKeySz;
  24666. pkcs7->privateKeySz = 0;
  24667. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24668. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24669. pkcs7->privateKeySz = tempWrd32;
  24670. tmpBytePtr = pkcs7->privateKey;
  24671. pkcs7->privateKey = NULL;
  24672. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24673. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  24674. pkcs7->privateKey = tmpBytePtr;
  24675. wc_PKCS7_Free(pkcs7);
  24676. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  24677. /* test of decrypt callback with KEKRI enveloped data */
  24678. {
  24679. int envelopedSz;
  24680. const byte keyId[] = { 0x00 };
  24681. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24682. pkcs7->content = (byte*)input;
  24683. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  24684. pkcs7->contentOID = DATA;
  24685. pkcs7->encryptOID = AES256CBCb;
  24686. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  24687. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  24688. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  24689. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  24690. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  24691. (word32)sizeof(output))), 0);
  24692. wc_PKCS7_Free(pkcs7);
  24693. /* decode envelopedData */
  24694. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24695. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  24696. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  24697. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  24698. envelopedSz, decoded, sizeof(decoded))), 0);
  24699. wc_PKCS7_Free(pkcs7);
  24700. }
  24701. #endif /* !NO_AES && !NO_AES_256 */
  24702. printf(resultFmt, passed);
  24703. #ifndef NO_RSA
  24704. if (rsaCert) {
  24705. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24706. }
  24707. if (rsaPrivKey) {
  24708. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24709. }
  24710. #endif /*NO_RSA */
  24711. #ifdef HAVE_ECC
  24712. if (eccCert) {
  24713. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24714. }
  24715. if (eccPrivKey) {
  24716. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24717. }
  24718. #endif /* HAVE_ECC */
  24719. #ifdef ECC_TIMING_RESISTANT
  24720. wc_FreeRng(&rng);
  24721. #endif
  24722. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && !defined(NO_RSA)
  24723. {
  24724. byte out[7];
  24725. byte *cms;
  24726. word32 cmsSz;
  24727. XFILE cmsFile;
  24728. XMEMSET(out, 0, sizeof(out));
  24729. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24730. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  24731. AssertTrue(cmsFile != XBADFILE);
  24732. cmsSz = (word32)FOURK_BUF;
  24733. AssertNotNull(cms =
  24734. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  24735. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  24736. XFCLOSE(cmsFile);
  24737. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  24738. sizeof_client_cert_der_2048), 0);
  24739. pkcs7->privateKey = (byte*)client_key_der_2048;
  24740. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  24741. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24742. 2), 0);
  24743. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  24744. sizeof(out)), 0);
  24745. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24746. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  24747. wc_PKCS7_Free(pkcs7);
  24748. }
  24749. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  24750. #endif /* HAVE_PKCS7 */
  24751. return 0;
  24752. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  24753. /*
  24754. * Testing wc_PKCS7_EncodeEncryptedData()
  24755. */
  24756. static int test_wc_PKCS7_EncodeEncryptedData (void)
  24757. {
  24758. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  24759. PKCS7* pkcs7 = NULL;
  24760. byte* tmpBytePtr = NULL;
  24761. byte encrypted[TWOK_BUF];
  24762. byte decoded[TWOK_BUF];
  24763. word32 tmpWrd32 = 0;
  24764. int tmpInt = 0;
  24765. int decodedSz;
  24766. int encryptedSz;
  24767. int testSz;
  24768. int i;
  24769. const byte data[] = { /* Hello World */
  24770. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  24771. 0x72,0x6c,0x64
  24772. };
  24773. #ifndef NO_DES3
  24774. byte desKey[] = {
  24775. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  24776. };
  24777. byte des3Key[] = {
  24778. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  24779. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  24780. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  24781. };
  24782. #endif
  24783. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24784. #ifndef NO_AES_128
  24785. byte aes128Key[] = {
  24786. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24787. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24788. };
  24789. #endif
  24790. #ifndef NO_AES_192
  24791. byte aes192Key[] = {
  24792. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24793. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24794. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24795. };
  24796. #endif
  24797. #ifndef NO_AES_256
  24798. byte aes256Key[] = {
  24799. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24800. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24801. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  24802. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  24803. };
  24804. #endif
  24805. #endif /* !NO_AES && HAVE_AES_CBC */
  24806. const pkcs7EncryptedVector testVectors[] =
  24807. {
  24808. #ifndef NO_DES3
  24809. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  24810. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  24811. #endif /* !NO_DES3 */
  24812. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  24813. #ifndef NO_AES_128
  24814. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  24815. sizeof(aes128Key)},
  24816. #endif
  24817. #ifndef NO_AES_192
  24818. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  24819. sizeof(aes192Key)},
  24820. #endif
  24821. #ifndef NO_AES_256
  24822. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  24823. sizeof(aes256Key)},
  24824. #endif
  24825. #endif /* !NO_AES && HAVE_AES_CBC */
  24826. };
  24827. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  24828. for (i = 0; i < testSz; i++) {
  24829. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24830. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24831. pkcs7->content = (byte*)testVectors[i].content;
  24832. pkcs7->contentSz = testVectors[i].contentSz;
  24833. pkcs7->contentOID = testVectors[i].contentOID;
  24834. pkcs7->encryptOID = testVectors[i].encryptOID;
  24835. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  24836. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  24837. pkcs7->heap = HEAP_HINT;
  24838. /* encode encryptedData */
  24839. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24840. sizeof(encrypted));
  24841. AssertIntGT(encryptedSz, 0);
  24842. /* Decode encryptedData */
  24843. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24844. decoded, sizeof(decoded));
  24845. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  24846. /* Keep values for last itr. */
  24847. if (i < testSz - 1) {
  24848. wc_PKCS7_Free(pkcs7);
  24849. }
  24850. }
  24851. if (pkcs7 == NULL || testSz == 0) {
  24852. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24853. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  24854. }
  24855. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24856. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  24857. sizeof(encrypted)),BAD_FUNC_ARG);
  24858. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  24859. sizeof(encrypted)), BAD_FUNC_ARG);
  24860. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24861. 0), BAD_FUNC_ARG);
  24862. /* Testing the struct. */
  24863. tmpBytePtr = pkcs7->content;
  24864. pkcs7->content = NULL;
  24865. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24866. sizeof(encrypted)), BAD_FUNC_ARG);
  24867. pkcs7->content = tmpBytePtr;
  24868. tmpWrd32 = pkcs7->contentSz;
  24869. pkcs7->contentSz = 0;
  24870. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24871. sizeof(encrypted)), BAD_FUNC_ARG);
  24872. pkcs7->contentSz = tmpWrd32;
  24873. tmpInt = pkcs7->encryptOID;
  24874. pkcs7->encryptOID = 0;
  24875. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24876. sizeof(encrypted)), BAD_FUNC_ARG);
  24877. pkcs7->encryptOID = tmpInt;
  24878. tmpBytePtr = pkcs7->encryptionKey;
  24879. pkcs7->encryptionKey = NULL;
  24880. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24881. sizeof(encrypted)), BAD_FUNC_ARG);
  24882. pkcs7->encryptionKey = tmpBytePtr;
  24883. tmpWrd32 = pkcs7->encryptionKeySz;
  24884. pkcs7->encryptionKeySz = 0;
  24885. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  24886. sizeof(encrypted)), BAD_FUNC_ARG);
  24887. pkcs7->encryptionKeySz = tmpWrd32;
  24888. printf(resultFmt, passed);
  24889. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  24890. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  24891. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24892. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  24893. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24894. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  24895. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24896. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24897. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  24898. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24899. decoded, 0), BAD_FUNC_ARG);
  24900. /* Test struct fields */
  24901. tmpBytePtr = pkcs7->encryptionKey;
  24902. pkcs7->encryptionKey = NULL;
  24903. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24904. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24905. pkcs7->encryptionKey = tmpBytePtr;
  24906. pkcs7->encryptionKeySz = 0;
  24907. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  24908. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  24909. printf(resultFmt, passed);
  24910. wc_PKCS7_Free(pkcs7);
  24911. #endif
  24912. return 0;
  24913. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  24914. /*
  24915. * Testing wc_PKCS7_Degenerate()
  24916. */
  24917. static int test_wc_PKCS7_Degenerate(void)
  24918. {
  24919. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24920. PKCS7* pkcs7;
  24921. char fName[] = "./certs/test-degenerate.p7b";
  24922. XFILE f;
  24923. byte der[4096];
  24924. word32 derSz;
  24925. int ret;
  24926. printf(testingFmt, "wc_PKCS7_Degenerate()");
  24927. AssertNotNull(f = XFOPEN(fName, "rb"));
  24928. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  24929. derSz = (word32)ret;
  24930. XFCLOSE(f);
  24931. /* test degenerate success */
  24932. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24933. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24934. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24935. #ifndef NO_RSA
  24936. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24937. #else
  24938. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  24939. #endif
  24940. wc_PKCS7_Free(pkcs7);
  24941. /* test with turning off degenerate cases */
  24942. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24943. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24944. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24945. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  24946. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  24947. wc_PKCS7_Free(pkcs7);
  24948. printf(resultFmt, passed);
  24949. #endif
  24950. return 0;
  24951. } /* END test_wc_PKCS7_Degenerate() */
  24952. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  24953. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  24954. static byte berContent[] = {
  24955. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24956. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  24957. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  24958. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  24959. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  24960. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  24961. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  24962. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  24963. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  24964. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  24965. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  24966. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  24967. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  24968. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  24969. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  24970. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  24971. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  24972. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  24973. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24974. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24975. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  24976. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24977. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24978. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  24979. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  24980. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  24981. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  24982. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  24983. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  24984. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  24985. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  24986. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  24987. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  24988. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  24989. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  24990. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  24991. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  24992. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  24993. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  24994. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  24995. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  24996. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  24997. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  24998. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  24999. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25000. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25001. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25002. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25003. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25004. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25005. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25006. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25007. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25008. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25009. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25010. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25011. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25012. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25013. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25014. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25015. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25016. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25017. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25018. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25019. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25020. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25021. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25022. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25023. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25024. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25025. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25026. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25027. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25028. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25029. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25030. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25031. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25032. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25033. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25034. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25035. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25036. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25037. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25038. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25039. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25040. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25041. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25042. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25043. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25044. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25045. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25046. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25047. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25048. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25049. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25050. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25051. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25052. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25053. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25054. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25055. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25056. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25057. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25058. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25059. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25060. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25061. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25062. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25063. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25064. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25065. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25066. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25067. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25068. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25069. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25070. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25071. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25072. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25073. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25074. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25075. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25076. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25077. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25078. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25079. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25080. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25081. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25082. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25083. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25084. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25085. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25086. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25087. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25088. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25089. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25090. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25091. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25092. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25093. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25094. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25095. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25096. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25097. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25098. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25099. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25100. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25101. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25102. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25103. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25104. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25105. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25106. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25107. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25108. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25109. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25110. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25111. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25112. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25113. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25114. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25115. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25116. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25117. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25118. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25119. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25120. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25121. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25122. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25123. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25124. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25125. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25126. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25127. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25128. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25129. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25130. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25131. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25132. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25133. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25134. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25135. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25136. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25137. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25138. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25139. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25140. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25141. 0x00, 0x00, 0x00, 0x00, 0x00
  25142. };
  25143. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  25144. /*
  25145. * Testing wc_PKCS7_BER()
  25146. */
  25147. static int test_wc_PKCS7_BER(void)
  25148. {
  25149. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25150. defined(ASN_BER_TO_DER)
  25151. PKCS7* pkcs7;
  25152. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25153. XFILE f;
  25154. byte der[4096];
  25155. #ifndef NO_DES3
  25156. byte decoded[2048];
  25157. #endif
  25158. word32 derSz;
  25159. int ret;
  25160. printf(testingFmt, "wc_PKCS7_BER()");
  25161. AssertNotNull(f = XFOPEN(fName, "rb"));
  25162. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25163. derSz = (word32)ret;
  25164. XFCLOSE(f);
  25165. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25166. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25167. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25168. #ifndef NO_RSA
  25169. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25170. #else
  25171. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25172. #endif
  25173. wc_PKCS7_Free(pkcs7);
  25174. #ifndef NO_DES3
  25175. /* decode BER content */
  25176. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  25177. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25178. derSz = (word32)ret;
  25179. XFCLOSE(f);
  25180. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25181. #ifndef NO_RSA
  25182. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25183. #else
  25184. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25185. #endif
  25186. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  25187. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25188. derSz = (word32)ret;
  25189. XFCLOSE(f);
  25190. pkcs7->privateKey = der;
  25191. pkcs7->privateKeySz = derSz;
  25192. #ifndef NO_RSA
  25193. #ifdef WOLFSSL_SP_MATH
  25194. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25195. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25196. #else
  25197. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25198. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25199. #endif
  25200. #else
  25201. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25202. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25203. #endif
  25204. wc_PKCS7_Free(pkcs7);
  25205. #endif /* !NO_DES3 */
  25206. printf(resultFmt, passed);
  25207. #endif
  25208. return 0;
  25209. } /* END test_wc_PKCS7_BER() */
  25210. static int test_PKCS7_signed_enveloped(void)
  25211. {
  25212. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25213. !defined(NO_FILESYSTEM)
  25214. XFILE f;
  25215. PKCS7* pkcs7;
  25216. #ifdef HAVE_AES_CBC
  25217. PKCS7* inner;
  25218. #endif
  25219. void* pt;
  25220. WC_RNG rng;
  25221. unsigned char key[FOURK_BUF/2];
  25222. unsigned char cert[FOURK_BUF/2];
  25223. unsigned char env[FOURK_BUF];
  25224. int envSz = FOURK_BUF;
  25225. int keySz;
  25226. int certSz;
  25227. unsigned char sig[FOURK_BUF * 2];
  25228. int sigSz = FOURK_BUF * 2;
  25229. #ifdef HAVE_AES_CBC
  25230. unsigned char decoded[FOURK_BUF];
  25231. int decodedSz = FOURK_BUF;
  25232. #endif
  25233. printf(testingFmt, "PKCS7_signed_enveloped");
  25234. /* load cert */
  25235. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  25236. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25237. XFCLOSE(f);
  25238. /* load key */
  25239. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  25240. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25241. XFCLOSE(f);
  25242. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  25243. /* sign cert for envelope */
  25244. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25245. AssertIntEQ(wc_InitRng(&rng), 0);
  25246. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25247. pkcs7->content = cert;
  25248. pkcs7->contentSz = certSz;
  25249. pkcs7->contentOID = DATA;
  25250. pkcs7->privateKey = key;
  25251. pkcs7->privateKeySz = keySz;
  25252. pkcs7->encryptOID = RSAk;
  25253. pkcs7->hashOID = SHA256h;
  25254. pkcs7->rng = &rng;
  25255. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25256. wc_PKCS7_Free(pkcs7);
  25257. wc_FreeRng(&rng);
  25258. #ifdef HAVE_AES_CBC
  25259. /* create envelope */
  25260. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25261. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25262. pkcs7->content = sig;
  25263. pkcs7->contentSz = sigSz;
  25264. pkcs7->contentOID = DATA;
  25265. pkcs7->encryptOID = AES256CBCb;
  25266. pkcs7->privateKey = key;
  25267. pkcs7->privateKeySz = keySz;
  25268. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25269. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25270. wc_PKCS7_Free(pkcs7);
  25271. #endif
  25272. /* create bad signed enveloped data */
  25273. sigSz = FOURK_BUF * 2;
  25274. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25275. AssertIntEQ(wc_InitRng(&rng), 0);
  25276. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25277. pkcs7->content = env;
  25278. pkcs7->contentSz = envSz;
  25279. pkcs7->contentOID = DATA;
  25280. pkcs7->privateKey = key;
  25281. pkcs7->privateKeySz = keySz;
  25282. pkcs7->encryptOID = RSAk;
  25283. pkcs7->hashOID = SHA256h;
  25284. pkcs7->rng = &rng;
  25285. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25286. * free it later. */
  25287. pt = (void*)pkcs7->certList;
  25288. pkcs7->certList = NULL; /* no certs in bundle */
  25289. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25290. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  25291. wc_PKCS7_Free(pkcs7);
  25292. /* check verify fails */
  25293. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25294. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25295. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25296. PKCS7_SIGNEEDS_CHECK);
  25297. /* try verifying the signature manually */
  25298. {
  25299. RsaKey rKey;
  25300. word32 idx = 0;
  25301. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  25302. WC_MAX_DIGEST_SIZE];
  25303. int digestSz;
  25304. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  25305. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  25306. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  25307. digest, sizeof(digest), &rKey);
  25308. AssertIntGT(digestSz, 0);
  25309. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  25310. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  25311. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  25312. /* verify was success */
  25313. }
  25314. wc_PKCS7_Free(pkcs7);
  25315. /* initializing the PKCS7 struct with the signing certificate should pass */
  25316. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25317. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25318. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25319. wc_PKCS7_Free(pkcs7);
  25320. /* create valid degenerate bundle */
  25321. sigSz = FOURK_BUF * 2;
  25322. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25323. pkcs7->content = env;
  25324. pkcs7->contentSz = envSz;
  25325. pkcs7->contentOID = DATA;
  25326. pkcs7->privateKey = key;
  25327. pkcs7->privateKeySz = keySz;
  25328. pkcs7->encryptOID = RSAk;
  25329. pkcs7->hashOID = SHA256h;
  25330. pkcs7->rng = &rng;
  25331. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  25332. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25333. wc_PKCS7_Free(pkcs7);
  25334. wc_FreeRng(&rng);
  25335. /* check verify */
  25336. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25337. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25338. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  25339. AssertNotNull(pkcs7->content);
  25340. #ifdef HAVE_AES_CBC
  25341. /* check decode */
  25342. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  25343. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  25344. inner->privateKey = key;
  25345. inner->privateKeySz = keySz;
  25346. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  25347. pkcs7->contentSz, decoded, decodedSz)), 0);
  25348. wc_PKCS7_Free(inner);
  25349. #endif
  25350. wc_PKCS7_Free(pkcs7);
  25351. #ifdef HAVE_AES_CBC
  25352. /* check cert set */
  25353. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25354. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25355. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  25356. AssertNotNull(pkcs7->singleCert);
  25357. AssertIntNE(pkcs7->singleCertSz, 0);
  25358. wc_PKCS7_Free(pkcs7);
  25359. #endif
  25360. printf(resultFmt, passed);
  25361. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  25362. return 0;
  25363. }
  25364. static int test_wc_PKCS7_NoDefaultSignedAttribs (void)
  25365. {
  25366. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25367. && !defined(NO_AES)
  25368. PKCS7* pkcs7;
  25369. void* heap = NULL;
  25370. printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
  25371. pkcs7 = wc_PKCS7_New(heap, testDevId);
  25372. AssertNotNull(pkcs7);
  25373. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25374. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  25375. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  25376. wc_PKCS7_Free(pkcs7);
  25377. printf(resultFmt, passed);
  25378. #endif
  25379. return 0;
  25380. }
  25381. static int test_wc_PKCS7_SetOriEncryptCtx (void)
  25382. {
  25383. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25384. && !defined(NO_AES)
  25385. PKCS7* pkcs7;
  25386. void* heap = NULL;
  25387. WOLFSSL_CTX* ctx;
  25388. ctx = NULL;
  25389. printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
  25390. pkcs7 = wc_PKCS7_New(heap, testDevId);
  25391. AssertNotNull(pkcs7);
  25392. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25393. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25394. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  25395. wc_PKCS7_Free(pkcs7);
  25396. printf(resultFmt, passed);
  25397. #endif
  25398. return 0;
  25399. }
  25400. static int test_wc_PKCS7_SetOriDecryptCtx (void)
  25401. {
  25402. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25403. && !defined(NO_AES)
  25404. PKCS7* pkcs7;
  25405. void* heap = NULL;
  25406. WOLFSSL_CTX* ctx;
  25407. ctx = NULL;
  25408. printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
  25409. pkcs7 = wc_PKCS7_New(heap, testDevId);
  25410. AssertNotNull(pkcs7);
  25411. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  25412. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  25413. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  25414. wc_PKCS7_Free(pkcs7);
  25415. printf(resultFmt, passed);
  25416. #endif
  25417. return 0;
  25418. }
  25419. static int test_wc_PKCS7_DecodeCompressedData(void)
  25420. {
  25421. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25422. && !defined(NO_AES) && defined(HAVE_LIBZ)
  25423. PKCS7* pkcs7;
  25424. void* heap = NULL;
  25425. byte out[4096];
  25426. byte *decompressed;
  25427. int outSz, decompressedSz;
  25428. const char* cert = "./certs/client-cert.pem";
  25429. byte* cert_buf = NULL;
  25430. size_t cert_sz = 0;
  25431. printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
  25432. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  25433. AssertNotNull((decompressed =
  25434. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  25435. decompressedSz = (int)cert_sz;
  25436. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25437. pkcs7->content = (byte*)cert_buf;
  25438. pkcs7->contentSz = (word32)cert_sz;
  25439. pkcs7->contentOID = DATA;
  25440. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  25441. sizeof(out))), 0);
  25442. wc_PKCS7_Free(pkcs7);
  25443. /* compressed key should be smaller than when started */
  25444. AssertIntLT(outSz, cert_sz);
  25445. /* test decompression */
  25446. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  25447. AssertIntEQ(pkcs7->contentOID, 0);
  25448. /* fail case with out buffer too small */
  25449. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25450. decompressed, outSz), 0);
  25451. /* success case */
  25452. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  25453. decompressed, decompressedSz), cert_sz);
  25454. AssertIntEQ(pkcs7->contentOID, DATA);
  25455. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25456. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25457. decompressed = NULL;
  25458. /* test decompression function with different 'max' inputs */
  25459. outSz = sizeof(out);
  25460. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  25461. 0);
  25462. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  25463. out, outSz, 0, heap), 0);
  25464. AssertNull(decompressed);
  25465. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  25466. out, outSz, 0, heap), 0);
  25467. AssertNotNull(decompressed);
  25468. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25469. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25470. decompressed = NULL;
  25471. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  25472. out, outSz, 0, heap), 0);
  25473. AssertNotNull(decompressed);
  25474. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  25475. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  25476. if (cert_buf)
  25477. free(cert_buf);
  25478. wc_PKCS7_Free(pkcs7);
  25479. printf(resultFmt, passed);
  25480. #endif
  25481. return 0;
  25482. }
  25483. static int test_wc_i2d_PKCS12(void)
  25484. {
  25485. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  25486. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  25487. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  25488. WC_PKCS12* pkcs12 = NULL;
  25489. unsigned char der[FOURK_BUF * 2];
  25490. unsigned char* pt;
  25491. int derSz;
  25492. unsigned char out[FOURK_BUF * 2];
  25493. int outSz = FOURK_BUF * 2;
  25494. const char p12_f[] = "./certs/test-servercert.p12";
  25495. XFILE f;
  25496. printf(testingFmt, "wc_i2d_PKCS12");
  25497. f = XFOPEN(p12_f, "rb");
  25498. AssertNotNull(f);
  25499. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  25500. AssertIntGT(derSz, 0);
  25501. XFCLOSE(f);
  25502. AssertNotNull(pkcs12 = wc_PKCS12_new());
  25503. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  25504. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25505. AssertIntEQ(outSz, derSz);
  25506. outSz = derSz - 1;
  25507. pt = out;
  25508. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  25509. outSz = derSz;
  25510. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  25511. AssertIntEQ((pt == out), 0);
  25512. pt = NULL;
  25513. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  25514. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  25515. wc_PKCS12_free(pkcs12);
  25516. /* Run the same test but use wc_d2i_PKCS12_fp. */
  25517. AssertNotNull(pkcs12 = wc_PKCS12_new());
  25518. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25519. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25520. AssertIntEQ(outSz, derSz);
  25521. wc_PKCS12_free(pkcs12);
  25522. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  25523. pkcs12 = NULL;
  25524. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  25525. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  25526. AssertIntEQ(outSz, derSz);
  25527. wc_PKCS12_free(pkcs12);
  25528. printf(resultFmt, passed);
  25529. #endif
  25530. return 0;
  25531. }
  25532. /* Testing wc_SignatureGetSize() for signature type ECC */
  25533. static int test_wc_SignatureGetSize_ecc(void)
  25534. {
  25535. int ret = 0;
  25536. #ifndef NO_SIG_WRAPPER
  25537. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  25538. enum wc_SignatureType sig_type;
  25539. word32 key_len;
  25540. /* Initialize ECC Key */
  25541. ecc_key ecc;
  25542. const char* qx =
  25543. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  25544. const char* qy =
  25545. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  25546. const char* d =
  25547. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  25548. ret = wc_ecc_init(&ecc);
  25549. if (ret == 0) {
  25550. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  25551. }
  25552. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  25553. if (ret == 0) {
  25554. /* Input for signature type ECC */
  25555. sig_type = WC_SIGNATURE_TYPE_ECC;
  25556. key_len = sizeof(ecc_key);
  25557. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25558. /* Test bad args */
  25559. if (ret > 0) {
  25560. sig_type = (enum wc_SignatureType) 100;
  25561. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25562. if (ret == BAD_FUNC_ARG) {
  25563. sig_type = WC_SIGNATURE_TYPE_ECC;
  25564. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25565. }
  25566. if (ret >= 0) {
  25567. key_len = (word32) 0;
  25568. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  25569. }
  25570. if (ret == BAD_FUNC_ARG) {
  25571. ret = SIG_TYPE_E;
  25572. }
  25573. }
  25574. } else {
  25575. ret = WOLFSSL_FATAL_ERROR;
  25576. }
  25577. wc_ecc_free(&ecc);
  25578. #else
  25579. ret = SIG_TYPE_E;
  25580. #endif
  25581. if (ret == SIG_TYPE_E) {
  25582. ret = 0;
  25583. }
  25584. else {
  25585. ret = WOLFSSL_FATAL_ERROR;
  25586. }
  25587. printf(resultFmt, ret == 0 ? passed : failed);
  25588. fflush(stdout);
  25589. #endif /* NO_SIG_WRAPPER */
  25590. return ret;
  25591. }/* END test_wc_SignatureGetSize_ecc() */
  25592. /* Testing wc_SignatureGetSize() for signature type rsa */
  25593. static int test_wc_SignatureGetSize_rsa(void)
  25594. {
  25595. int ret = 0;
  25596. #ifndef NO_SIG_WRAPPER
  25597. #ifndef NO_RSA
  25598. enum wc_SignatureType sig_type;
  25599. word32 key_len;
  25600. word32 idx = 0;
  25601. /* Initialize RSA Key */
  25602. RsaKey rsa_key;
  25603. byte* tmp = NULL;
  25604. size_t bytes;
  25605. #ifdef USE_CERT_BUFFERS_1024
  25606. bytes = (size_t)sizeof_client_key_der_1024;
  25607. if (bytes < (size_t)sizeof_client_key_der_1024)
  25608. bytes = (size_t)sizeof_client_cert_der_1024;
  25609. #elif defined(USE_CERT_BUFFERS_2048)
  25610. bytes = (size_t)sizeof_client_key_der_2048;
  25611. if (bytes < (size_t)sizeof_client_cert_der_2048)
  25612. bytes = (size_t)sizeof_client_cert_der_2048;
  25613. #else
  25614. bytes = FOURK_BUF;
  25615. #endif
  25616. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25617. if (tmp != NULL) {
  25618. #ifdef USE_CERT_BUFFERS_1024
  25619. XMEMCPY(tmp, client_key_der_1024,
  25620. (size_t)sizeof_client_key_der_1024);
  25621. #elif defined(USE_CERT_BUFFERS_2048)
  25622. XMEMCPY(tmp, client_key_der_2048,
  25623. (size_t)sizeof_client_key_der_2048);
  25624. #elif !defined(NO_FILESYSTEM)
  25625. file = XFOPEN(clientKey, "rb");
  25626. if (file != XBADFILE) {
  25627. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  25628. XFCLOSE(file);
  25629. }
  25630. else {
  25631. ret = WOLFSSL_FATAL_ERROR;
  25632. }
  25633. #else
  25634. ret = WOLFSSL_FATAL_ERROR;
  25635. #endif
  25636. } else {
  25637. ret = WOLFSSL_FATAL_ERROR;
  25638. }
  25639. if (ret == 0) {
  25640. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  25641. }
  25642. if (ret == 0) {
  25643. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  25644. }
  25645. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  25646. if (ret == 0) {
  25647. /* Input for signature type RSA */
  25648. sig_type = WC_SIGNATURE_TYPE_RSA;
  25649. key_len = sizeof(RsaKey);
  25650. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25651. /* Test bad args */
  25652. if (ret > 0) {
  25653. sig_type = (enum wc_SignatureType) 100;
  25654. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25655. if (ret == BAD_FUNC_ARG) {
  25656. sig_type = WC_SIGNATURE_TYPE_RSA;
  25657. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  25658. }
  25659. #ifndef HAVE_USER_RSA
  25660. if (ret == BAD_FUNC_ARG) {
  25661. #else
  25662. if (ret == 0) {
  25663. #endif
  25664. key_len = (word32)0;
  25665. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  25666. }
  25667. if (ret == BAD_FUNC_ARG) {
  25668. ret = SIG_TYPE_E;
  25669. }
  25670. }
  25671. } else {
  25672. ret = WOLFSSL_FATAL_ERROR;
  25673. }
  25674. wc_FreeRsaKey(&rsa_key);
  25675. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25676. #else
  25677. ret = SIG_TYPE_E;
  25678. #endif
  25679. if (ret == SIG_TYPE_E) {
  25680. ret = 0;
  25681. }else {
  25682. ret = WOLFSSL_FATAL_ERROR;
  25683. }
  25684. printf(resultFmt, ret == 0 ? passed : failed);
  25685. #endif /* NO_SIG_WRAPPER */
  25686. return ret;
  25687. }/* END test_wc_SignatureGetSize_rsa(void) */
  25688. /*----------------------------------------------------------------------------*
  25689. | hash.h Tests
  25690. *----------------------------------------------------------------------------*/
  25691. static int test_wc_HashInit(void)
  25692. {
  25693. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  25694. wc_HashAlg hash;
  25695. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25696. enum wc_HashType enumArray[] = {
  25697. #ifndef NO_MD5
  25698. WC_HASH_TYPE_MD5,
  25699. #endif
  25700. #ifndef NO_SHA
  25701. WC_HASH_TYPE_SHA,
  25702. #endif
  25703. #ifndef WOLFSSL_SHA224
  25704. WC_HASH_TYPE_SHA224,
  25705. #endif
  25706. #ifndef NO_SHA256
  25707. WC_HASH_TYPE_SHA256,
  25708. #endif
  25709. #ifndef WOLFSSL_SHA384
  25710. WC_HASH_TYPE_SHA384,
  25711. #endif
  25712. #ifndef WOLFSSL_SHA512
  25713. WC_HASH_TYPE_SHA512,
  25714. #endif
  25715. };
  25716. /* dynamically finds the length */
  25717. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25718. /* For loop to test various arguments... */
  25719. for (i = 0; i < enumlen; i++) {
  25720. /* check for bad args */
  25721. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  25722. ret = 1;
  25723. break;
  25724. }
  25725. wc_HashFree(&hash, enumArray[i]);
  25726. /* check for null ptr */
  25727. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  25728. ret = 1;
  25729. break;
  25730. }
  25731. } /* end of for loop */
  25732. printf(testingFmt, "wc_HashInit()");
  25733. if (ret==0) { /* all tests have passed */
  25734. printf(resultFmt, passed);
  25735. }
  25736. else { /* a test has failed */
  25737. printf(resultFmt, failed);
  25738. }
  25739. return ret;
  25740. } /* end of test_wc_HashInit */
  25741. /*
  25742. * Unit test function for wc_HashSetFlags()
  25743. */
  25744. static int test_wc_HashSetFlags(void)
  25745. {
  25746. int ret = 0;
  25747. #ifdef WOLFSSL_HASH_FLAGS
  25748. wc_HashAlg hash;
  25749. word32 flags = 0;
  25750. int i, j;
  25751. int notSupportedLen;
  25752. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25753. enum wc_HashType enumArray[] = {
  25754. #ifndef NO_MD5
  25755. WC_HASH_TYPE_MD5,
  25756. #endif
  25757. #ifndef NO_SHA
  25758. WC_HASH_TYPE_SHA,
  25759. #endif
  25760. #ifdef WOLFSSL_SHA224
  25761. WC_HASH_TYPE_SHA224,
  25762. #endif
  25763. #ifndef NO_SHA256
  25764. WC_HASH_TYPE_SHA256,
  25765. #endif
  25766. #ifdef WOLFSSL_SHA384
  25767. WC_HASH_TYPE_SHA384,
  25768. #endif
  25769. #ifdef WOLFSSL_SHA512
  25770. WC_HASH_TYPE_SHA512,
  25771. #endif
  25772. #ifdef WOLFSSL_SHA3
  25773. WC_HASH_TYPE_SHA3_224,
  25774. #endif
  25775. };
  25776. enum wc_HashType notSupported[] = {
  25777. WC_HASH_TYPE_MD5_SHA,
  25778. WC_HASH_TYPE_MD2,
  25779. WC_HASH_TYPE_MD4,
  25780. WC_HASH_TYPE_BLAKE2B,
  25781. WC_HASH_TYPE_BLAKE2S,
  25782. WC_HASH_TYPE_NONE,
  25783. };
  25784. /* dynamically finds the length */
  25785. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25786. printf(testingFmt, "wc_HashSetFlags()");
  25787. /* For loop to test various arguments... */
  25788. for (i = 0; i < enumlen; i++) {
  25789. ret = wc_HashInit(&hash, enumArray[i]);
  25790. if (ret == 0) {
  25791. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  25792. }
  25793. if (ret == 0) {
  25794. if (flags & WC_HASH_FLAG_ISCOPY) {
  25795. ret = 0;
  25796. }
  25797. }
  25798. if (ret == 0) {
  25799. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  25800. if (ret == BAD_FUNC_ARG) {
  25801. ret = 0;
  25802. }
  25803. }
  25804. wc_HashFree(&hash, enumArray[i]);
  25805. }
  25806. /* For loop to test not supported cases */
  25807. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25808. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25809. ret = wc_HashInit(&hash, notSupported[j]);
  25810. if (ret == 0) {
  25811. ret = -1;
  25812. }
  25813. else if (ret == BAD_FUNC_ARG){
  25814. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  25815. if (ret == 0) {
  25816. ret = -1;
  25817. }
  25818. else if (ret == BAD_FUNC_ARG) {
  25819. ret = 0;
  25820. }
  25821. }
  25822. if (ret == 0) {
  25823. ret = wc_HashFree(&hash, notSupported[j]);
  25824. if (ret == 0) {
  25825. ret = -1;
  25826. }
  25827. else if (ret == BAD_FUNC_ARG) {
  25828. ret = 0;
  25829. }
  25830. }
  25831. }
  25832. printf(resultFmt, ret == 0 ? passed : failed);
  25833. fflush(stdout);
  25834. #endif
  25835. return ret;
  25836. } /* END test_wc_HashSetFlags */
  25837. /*
  25838. * Unit test function for wc_HashGetFlags()
  25839. */
  25840. static int test_wc_HashGetFlags(void)
  25841. {
  25842. int ret = 0;
  25843. #ifdef WOLFSSL_HASH_FLAGS
  25844. wc_HashAlg hash;
  25845. word32 flags = 0;
  25846. int i, j;
  25847. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  25848. enum wc_HashType enumArray[] = {
  25849. #ifndef NO_MD5
  25850. WC_HASH_TYPE_MD5,
  25851. #endif
  25852. #ifndef NO_SHA
  25853. WC_HASH_TYPE_SHA,
  25854. #endif
  25855. #ifdef WOLFSSL_SHA224
  25856. WC_HASH_TYPE_SHA224,
  25857. #endif
  25858. #ifndef NO_SHA256
  25859. WC_HASH_TYPE_SHA256,
  25860. #endif
  25861. #ifdef WOLFSSL_SHA384
  25862. WC_HASH_TYPE_SHA384,
  25863. #endif
  25864. #ifdef WOLFSSL_SHA512
  25865. WC_HASH_TYPE_SHA512,
  25866. #endif
  25867. #ifdef WOLFSSL_SHA3
  25868. WC_HASH_TYPE_SHA3_224,
  25869. #endif
  25870. };
  25871. enum wc_HashType notSupported[] = {
  25872. WC_HASH_TYPE_MD5_SHA,
  25873. WC_HASH_TYPE_MD2,
  25874. WC_HASH_TYPE_MD4,
  25875. WC_HASH_TYPE_BLAKE2B,
  25876. WC_HASH_TYPE_BLAKE2S,
  25877. WC_HASH_TYPE_NONE,
  25878. };
  25879. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  25880. int notSupportedLen;
  25881. printf(testingFmt, "wc_HashGetFlags()");
  25882. /* For loop to test various arguments... */
  25883. for (i = 0; i < enumlen; i++) {
  25884. ret = wc_HashInit(&hash, enumArray[i]);
  25885. if (ret == 0) {
  25886. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  25887. }
  25888. if (ret == 0) {
  25889. if (flags & WC_HASH_FLAG_ISCOPY) {
  25890. ret = 0;
  25891. }
  25892. }
  25893. if (ret == 0) {
  25894. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  25895. if (ret == BAD_FUNC_ARG) {
  25896. ret = 0;
  25897. }
  25898. }
  25899. wc_HashFree(&hash, enumArray[i]);
  25900. if (ret != 0) {
  25901. break;
  25902. }
  25903. }
  25904. /* For loop to test not supported cases */
  25905. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  25906. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  25907. ret = wc_HashInit(&hash, notSupported[j]);
  25908. if (ret == 0) {
  25909. ret = -1;
  25910. }
  25911. else if (ret == BAD_FUNC_ARG){
  25912. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  25913. if (ret == 0) {
  25914. ret = -1;
  25915. }
  25916. else if (ret == BAD_FUNC_ARG) {
  25917. ret = 0;
  25918. }
  25919. }
  25920. if (ret == 0) {
  25921. ret = wc_HashFree(&hash, notSupported[j]);
  25922. if (ret == 0) {
  25923. ret = -1;
  25924. }
  25925. if (ret == BAD_FUNC_ARG) {
  25926. ret = 0;
  25927. }
  25928. }
  25929. }
  25930. printf(resultFmt, ret == 0 ? passed : failed);
  25931. fflush(stdout);
  25932. #endif
  25933. return ret;
  25934. } /* END test_wc_HashGetFlags */
  25935. /*----------------------------------------------------------------------------*
  25936. | Compatibility Tests
  25937. *----------------------------------------------------------------------------*/
  25938. static int test_wolfSSL_lhash(void)
  25939. {
  25940. #ifdef OPENSSL_ALL
  25941. const char testStr[] = "Like a true nature's child\n"
  25942. "We were born\n"
  25943. "Born to be wild";
  25944. printf(testingFmt, "wolfSSL_LH_strhash()");
  25945. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  25946. printf(resultFmt, passed);
  25947. #endif
  25948. return 0;
  25949. }
  25950. static int test_wolfSSL_X509_NAME(void)
  25951. {
  25952. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  25953. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  25954. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  25955. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  25956. defined(OPENSSL_EXTRA))
  25957. X509* x509;
  25958. const unsigned char* c;
  25959. unsigned char buf[4096];
  25960. int bytes;
  25961. XFILE f;
  25962. const X509_NAME* a;
  25963. const X509_NAME* b;
  25964. X509_NAME* d2i_name = NULL;
  25965. int sz;
  25966. unsigned char* tmp;
  25967. char file[] = "./certs/ca-cert.der";
  25968. #ifndef OPENSSL_EXTRA_X509_SMALL
  25969. byte empty[] = { /* CN=empty emailAddress= */
  25970. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  25971. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  25972. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  25973. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25974. 0x01, 0x16, 0x00
  25975. };
  25976. #endif
  25977. printf(testingFmt, "wolfSSL_X509_NAME()");
  25978. #ifndef OPENSSL_EXTRA_X509_SMALL
  25979. /* test compile of deprecated function, returns 0 */
  25980. AssertIntEQ(CRYPTO_thread_id(), 0);
  25981. #endif
  25982. AssertNotNull(a = X509_NAME_new());
  25983. X509_NAME_free((X509_NAME*)a);
  25984. f = XFOPEN(file, "rb");
  25985. AssertTrue(f != XBADFILE);
  25986. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  25987. XFCLOSE(f);
  25988. c = buf;
  25989. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  25990. /* test cmp function */
  25991. AssertNotNull(a = X509_get_issuer_name(x509));
  25992. AssertNotNull(b = X509_get_subject_name(x509));
  25993. #ifndef OPENSSL_EXTRA_X509_SMALL
  25994. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  25995. #endif
  25996. tmp = buf;
  25997. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  25998. if (sz > 0 && tmp == buf) {
  25999. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__);
  26000. printf(" Expected pointer to be incremented\n");
  26001. abort();
  26002. }
  26003. #ifndef OPENSSL_EXTRA_X509_SMALL
  26004. tmp = buf;
  26005. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26006. #endif
  26007. /* if output parameter is NULL, should still return required size. */
  26008. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26009. /* retry but with the function creating a buffer */
  26010. tmp = NULL;
  26011. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26012. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26013. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26014. #ifndef OPENSSL_EXTRA_X509_SMALL
  26015. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  26016. #endif
  26017. X509_NAME_free((X509_NAME*)b);
  26018. X509_NAME_free(d2i_name);
  26019. X509_free(x509);
  26020. #ifndef OPENSSL_EXTRA_X509_SMALL
  26021. /* test with an empty domain component */
  26022. tmp = empty;
  26023. sz = sizeof(empty);
  26024. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26025. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26026. /* size of empty emailAddress will be 0 */
  26027. tmp = buf;
  26028. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26029. (char*)tmp, sizeof(buf)), 0);
  26030. /* should contain no organization name */
  26031. tmp = buf;
  26032. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26033. (char*)tmp, sizeof(buf)), -1);
  26034. X509_NAME_free(d2i_name);
  26035. #endif
  26036. printf(resultFmt, passed);
  26037. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26038. return 0;
  26039. }
  26040. static int test_wolfSSL_X509_NAME_hash(void)
  26041. {
  26042. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  26043. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26044. BIO* bio;
  26045. X509* x509 = NULL;
  26046. printf(testingFmt, "wolfSSL_X509_NAME_hash");
  26047. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26048. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26049. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26050. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26051. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26052. X509_free(x509);
  26053. BIO_free(bio);
  26054. printf(resultFmt, passed);
  26055. #endif
  26056. return 0;
  26057. }
  26058. static int test_wolfSSL_X509_NAME_print_ex(void)
  26059. {
  26060. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26061. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26062. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26063. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26064. !defined(NO_BIO) && !defined(NO_RSA)
  26065. int memSz;
  26066. byte* mem = NULL;
  26067. BIO* bio = NULL;
  26068. BIO* membio = NULL;
  26069. X509* x509 = NULL;
  26070. X509_NAME* name = NULL;
  26071. const char* expNormal = "C=US,CN=wolfssl.com";
  26072. const char* expReverse = "CN=wolfssl.com,C=US";
  26073. const char* expNotEscaped = "C= US,+\"\\ ,CN=#wolfssl.com<>;";
  26074. const char* expNotEscapedRev = "CN=#wolfssl.com<>;,C= US,+\"\\ ";
  26075. const char* expRFC5523 =
  26076. "CN=\\#wolfssl.com\\<\\>\\;,C=\\ US\\,\\+\\\"\\\\\\ ";
  26077. printf(testingFmt, "wolfSSL_X509_NAME_print_ex");
  26078. /* Test with real cert (svrCertFile) first */
  26079. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26080. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26081. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26082. AssertNotNull(name = X509_get_subject_name(x509));
  26083. /* Test without flags */
  26084. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26085. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26086. BIO_free(membio);
  26087. /* Test flag: XN_FLAG_RFC2253 */
  26088. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26089. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26090. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26091. BIO_free(membio);
  26092. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26093. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26094. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26095. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26096. BIO_free(membio);
  26097. X509_free(x509);
  26098. BIO_free(bio);
  26099. /* Test normal case without escaped characters */
  26100. {
  26101. /* Create name: "/C=US/CN=wolfssl.com" */
  26102. AssertNotNull(name = X509_NAME_new());
  26103. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26104. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26105. WOLFSSL_SUCCESS);
  26106. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26107. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26108. WOLFSSL_SUCCESS);
  26109. /* Test without flags */
  26110. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26111. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26112. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26113. AssertIntEQ(memSz, XSTRLEN(expNormal)+1);
  26114. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26115. BIO_free(membio);
  26116. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26117. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26118. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26119. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26120. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26121. AssertIntEQ(memSz, XSTRLEN(expReverse)+1);
  26122. BIO_free(membio);
  26123. /* Test flags: XN_FLAG_DN_REV - reversed */
  26124. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26125. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26126. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26127. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26128. AssertIntEQ(memSz, XSTRLEN(expReverse)+1);
  26129. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26130. BIO_free(membio);
  26131. X509_NAME_free(name);
  26132. }
  26133. /* Test RFC2253 characters are escaped with backslashes */
  26134. {
  26135. AssertNotNull(name = X509_NAME_new());
  26136. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26137. /* space at beginning and end, and: ,+"\ */
  26138. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26139. WOLFSSL_SUCCESS);
  26140. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26141. /* # at beginning, and: <>;*/
  26142. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26143. WOLFSSL_SUCCESS);
  26144. /* Test without flags */
  26145. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26146. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26147. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26148. AssertIntEQ(memSz, XSTRLEN(expNotEscaped)+1);
  26149. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26150. XSTRLEN(expNotEscaped)), 0);
  26151. BIO_free(membio);
  26152. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26153. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26154. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26155. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26156. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26157. AssertIntEQ(memSz, XSTRLEN(expRFC5523)+1);
  26158. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26159. BIO_free(membio);
  26160. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26161. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26162. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26163. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26164. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26165. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev)+1);
  26166. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26167. XSTRLEN(expNotEscapedRev)), 0);
  26168. BIO_free(membio);
  26169. X509_NAME_free(name);
  26170. }
  26171. printf(resultFmt, passed);
  26172. #endif
  26173. return 0;
  26174. }
  26175. #ifndef NO_BIO
  26176. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26177. {
  26178. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26179. STACK_OF(X509_INFO) *info_stack;
  26180. X509_INFO *info;
  26181. int len;
  26182. int i;
  26183. const char* files[] = {
  26184. cliCertFile,
  26185. cliKeyFile,
  26186. /* This needs to be the order as svrCertFile contains the
  26187. * intermediate cert as well. */
  26188. svrKeyFile,
  26189. svrCertFile,
  26190. NULL,
  26191. };
  26192. const char** curFile;
  26193. BIO *fileBIO;
  26194. BIO *concatBIO = NULL;
  26195. byte tmp[FOURK_BUF];
  26196. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26197. * to group objects together. */
  26198. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26199. for (curFile = files; *curFile != NULL; curFile++) {
  26200. int fileLen;
  26201. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26202. fileLen = wolfSSL_BIO_get_len(fileBIO);
  26203. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26204. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  26205. fileLen -= len;
  26206. }
  26207. /* Make sure we read the entire file */
  26208. AssertIntEQ(fileLen, 0);
  26209. BIO_free(fileBIO);
  26210. }
  26211. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  26212. NULL));
  26213. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  26214. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26215. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26216. AssertNotNull(info->x509);
  26217. AssertNull(info->crl);
  26218. if (i != 0) {
  26219. AssertNotNull(info->x_pkey);
  26220. AssertIntEQ(X509_check_private_key(info->x509,
  26221. info->x_pkey->dec_pkey), 1);
  26222. }
  26223. else {
  26224. AssertNull(info->x_pkey);
  26225. }
  26226. }
  26227. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26228. BIO_free(concatBIO);
  26229. #endif
  26230. return 0;
  26231. }
  26232. #endif
  26233. #ifndef NO_BIO
  26234. static int test_wolfSSL_X509_INFO(void)
  26235. {
  26236. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26237. STACK_OF(X509_INFO) *info_stack;
  26238. X509_INFO *info;
  26239. BIO *cert;
  26240. int i;
  26241. /* PEM in hex format to avoid null terminator */
  26242. byte data[] = {
  26243. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  26244. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  26245. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  26246. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  26247. 0x2d, 0x2d, 0x2d
  26248. };
  26249. /* PEM in hex format to avoid null terminator */
  26250. byte data2[] = {
  26251. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  26252. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  26253. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  26254. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  26255. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  26256. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  26257. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  26258. };
  26259. printf(testingFmt, "wolfSSL_X509_INFO");
  26260. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26261. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26262. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26263. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26264. AssertNotNull(info->x509);
  26265. AssertNull(info->crl);
  26266. AssertNull(info->x_pkey);
  26267. }
  26268. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26269. BIO_free(cert);
  26270. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26271. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26272. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26273. BIO_free(cert);
  26274. /* This case should fail due to invalid input. */
  26275. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  26276. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  26277. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26278. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26279. BIO_free(cert);
  26280. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  26281. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  26282. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26283. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26284. BIO_free(cert);
  26285. printf(resultFmt, passed);
  26286. #endif
  26287. return 0;
  26288. }
  26289. #endif
  26290. static int test_wolfSSL_X509_subject_name_hash(void)
  26291. {
  26292. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26293. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26294. X509* x509;
  26295. X509_NAME* subjectName = NULL;
  26296. unsigned long ret = 0;
  26297. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  26298. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26299. SSL_FILETYPE_PEM));
  26300. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  26301. ret = X509_subject_name_hash(x509);
  26302. AssertIntNE(ret, 0);
  26303. X509_free(x509);
  26304. printf(resultFmt, passed);
  26305. #endif
  26306. return 0;
  26307. }
  26308. static int test_wolfSSL_X509_issuer_name_hash(void)
  26309. {
  26310. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26311. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26312. X509* x509;
  26313. X509_NAME* issuertName = NULL;
  26314. unsigned long ret = 0;
  26315. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  26316. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26317. SSL_FILETYPE_PEM));
  26318. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  26319. ret = X509_issuer_name_hash(x509);
  26320. AssertIntNE(ret, 0);
  26321. X509_free(x509);
  26322. printf(resultFmt, passed);
  26323. #endif
  26324. return 0;
  26325. }
  26326. static int test_wolfSSL_X509_check_host(void)
  26327. {
  26328. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26329. && !defined(NO_SHA) && !defined(NO_RSA)
  26330. X509* x509;
  26331. const char altName[] = "example.com";
  26332. printf(testingFmt, "wolfSSL_X509_check_host()");
  26333. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26334. SSL_FILETYPE_PEM));
  26335. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  26336. WOLFSSL_SUCCESS);
  26337. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  26338. WOLFSSL_FAILURE);
  26339. X509_free(x509);
  26340. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  26341. WOLFSSL_FAILURE);
  26342. printf(resultFmt, passed);
  26343. #endif
  26344. return 0;
  26345. }
  26346. static int test_wolfSSL_X509_check_email(void)
  26347. {
  26348. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  26349. X509* x509;
  26350. const char goodEmail[] = "info@wolfssl.com";
  26351. const char badEmail[] = "disinfo@wolfssl.com";
  26352. printf(testingFmt, "wolfSSL_X509_check_email()");
  26353. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26354. SSL_FILETYPE_PEM));
  26355. /* Should fail on non-matching email address */
  26356. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  26357. WOLFSSL_FAILURE);
  26358. /* Should succeed on matching email address */
  26359. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  26360. WOLFSSL_SUCCESS);
  26361. /* Should compute length internally when not provided */
  26362. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  26363. WOLFSSL_SUCCESS);
  26364. /* Should fail when email address is NULL */
  26365. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  26366. WOLFSSL_FAILURE);
  26367. X509_free(x509);
  26368. /* Should fail when x509 is NULL */
  26369. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  26370. WOLFSSL_FAILURE);
  26371. printf(resultFmt, passed);
  26372. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  26373. return 0;
  26374. }
  26375. static int test_wolfSSL_DES(void)
  26376. {
  26377. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  26378. const_DES_cblock myDes;
  26379. DES_cblock iv;
  26380. DES_key_schedule key;
  26381. word32 i;
  26382. DES_LONG dl;
  26383. unsigned char msg[] = "hello wolfssl";
  26384. printf(testingFmt, "wolfSSL_DES()");
  26385. DES_check_key(1);
  26386. DES_set_key(&myDes, &key);
  26387. /* check, check of odd parity */
  26388. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  26389. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26390. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  26391. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  26392. /* set odd parity for success case */
  26393. DES_set_odd_parity(&myDes);
  26394. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  26395. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  26396. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  26397. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26398. AssertIntEQ(key[i], myDes[i]);
  26399. }
  26400. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  26401. /* check weak key */
  26402. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  26403. XMEMSET(key, 5, sizeof(DES_key_schedule));
  26404. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  26405. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  26406. /* now do unchecked copy of a weak key over */
  26407. DES_set_key_unchecked(&myDes, &key);
  26408. /* compare arrays, should be the same */
  26409. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26410. AssertIntEQ(key[i], myDes[i]);
  26411. }
  26412. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  26413. /* check DES_key_sched API */
  26414. XMEMSET(key, 1, sizeof(DES_key_schedule));
  26415. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  26416. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  26417. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  26418. /* compare arrays, should be the same */
  26419. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  26420. AssertIntEQ(key[i], myDes[i]);
  26421. }
  26422. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  26423. * DES_cbc_encrypt on the input */
  26424. XMEMSET(iv, 0, sizeof(DES_cblock));
  26425. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  26426. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  26427. AssertIntEQ(dl, 480052723);
  26428. printf(resultFmt, passed);
  26429. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26430. return 0;
  26431. }
  26432. static int test_wc_PemToDer(void)
  26433. {
  26434. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  26435. int ret;
  26436. DerBuffer* pDer = NULL;
  26437. const char* ca_cert = "./certs/server-cert.pem";
  26438. byte* cert_buf = NULL;
  26439. size_t cert_sz = 0;
  26440. int eccKey = 0;
  26441. EncryptedInfo info;
  26442. printf(testingFmt, "wc_PemToDer()");
  26443. XMEMSET(&info, 0, sizeof(info));
  26444. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26445. if (ret == 0) {
  26446. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  26447. &pDer, NULL, &info, &eccKey);
  26448. AssertIntEQ(ret, 0);
  26449. wc_FreeDer(&pDer);
  26450. }
  26451. if (cert_buf)
  26452. free(cert_buf);
  26453. #ifdef HAVE_ECC
  26454. {
  26455. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  26456. byte key_buf[256] = {0};
  26457. /* Test fail of loading a key with cert type */
  26458. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  26459. key_buf[0] = '\n';
  26460. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  26461. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  26462. &pDer, NULL, &info, &eccKey)), 0);
  26463. #ifdef OPENSSL_EXTRA
  26464. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  26465. &pDer, NULL, &info, &eccKey)), 0);
  26466. #endif
  26467. wc_FreeDer(&pDer);
  26468. if (cert_buf)
  26469. free(cert_buf);
  26470. }
  26471. #endif
  26472. printf(resultFmt, passed);
  26473. #endif
  26474. return 0;
  26475. }
  26476. static int test_wc_AllocDer(void)
  26477. {
  26478. #if !defined(NO_CERTS)
  26479. int ret;
  26480. DerBuffer* pDer = NULL;
  26481. word32 testSize = 1024;
  26482. printf(testingFmt, "wc_AllocDer()");
  26483. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  26484. AssertIntEQ(ret, 0);
  26485. AssertNotNull(pDer);
  26486. wc_FreeDer(&pDer);
  26487. printf(resultFmt, passed);
  26488. #endif
  26489. return 0;
  26490. }
  26491. static int test_wc_CertPemToDer(void)
  26492. {
  26493. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
  26494. int ret;
  26495. const char* ca_cert = "./certs/ca-cert.pem";
  26496. byte* cert_buf = NULL;
  26497. size_t cert_sz = 0, cert_dersz = 0;
  26498. byte* cert_der = NULL;
  26499. printf(testingFmt, "wc_CertPemToDer()");
  26500. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26501. if (ret == 0) {
  26502. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26503. cert_der = (byte*)malloc(cert_dersz);
  26504. if (cert_der) {
  26505. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  26506. cert_der, (int)cert_dersz, CERT_TYPE);
  26507. AssertIntGE(ret, 0);
  26508. }
  26509. }
  26510. if (cert_der)
  26511. free(cert_der);
  26512. if (cert_buf)
  26513. free(cert_buf);
  26514. printf(resultFmt, passed);
  26515. #endif
  26516. return 0;
  26517. }
  26518. static int test_wc_PubKeyPemToDer(void)
  26519. {
  26520. #ifdef WOLFSSL_PEM_TO_DER
  26521. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  26522. int ret;
  26523. const char* key = "./certs/ecc-client-keyPub.pem";
  26524. byte* cert_buf = NULL;
  26525. size_t cert_sz = 0, cert_dersz = 0;
  26526. byte* cert_der = NULL;
  26527. printf(testingFmt, "wc_PubKeyPemToDer()");
  26528. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26529. cert_der, (int)cert_dersz);
  26530. AssertIntGE(ret, BAD_FUNC_ARG);
  26531. ret = load_file(key, &cert_buf, &cert_sz);
  26532. if (ret == 0) {
  26533. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  26534. cert_der = (byte*)malloc(cert_dersz);
  26535. if (cert_der) {
  26536. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  26537. cert_der, (int)cert_dersz);
  26538. AssertIntGE(ret, 0);
  26539. }
  26540. }
  26541. if (cert_der)
  26542. free(cert_der);
  26543. if (cert_buf)
  26544. free(cert_buf);
  26545. printf(resultFmt, passed);
  26546. #endif
  26547. #endif
  26548. return 0;
  26549. }
  26550. static int test_wc_PemPubKeyToDer(void)
  26551. {
  26552. #if defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER)
  26553. int ret;
  26554. const char* key = "./certs/ecc-client-keyPub.pem";
  26555. size_t cert_dersz = 1024;
  26556. byte* cert_der = (byte*)malloc(cert_dersz);
  26557. printf(testingFmt, "wc_PemPubKeyToDer()");
  26558. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  26559. AssertIntGE(ret, BAD_FUNC_ARG);
  26560. if (cert_der) {
  26561. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  26562. AssertIntGE(ret, 0);
  26563. free(cert_der);
  26564. }
  26565. printf(resultFmt, passed);
  26566. #endif
  26567. return 0;
  26568. }
  26569. static int test_wc_GetPubKeyDerFromCert(void)
  26570. {
  26571. #if !defined(NO_RSA) || defined(HAVE_ECC)
  26572. int ret;
  26573. word32 idx = 0;
  26574. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26575. word32 keyDerSz = (word32)sizeof(keyDer);
  26576. DecodedCert decoded;
  26577. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
  26578. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  26579. word32 certBufSz = sizeof(certBuf);
  26580. #endif
  26581. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  26582. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA)
  26583. XFILE fp;
  26584. #endif
  26585. #ifndef NO_RSA
  26586. RsaKey rsaKey;
  26587. #if defined(USE_CERT_BUFFERS_2048)
  26588. byte* rsaCertDer = (byte*)client_cert_der_2048;
  26589. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  26590. #elif defined(USE_CERT_BUFFERS_1024)
  26591. byte* rsaCertDer = (byte*)client_cert_der_1024;
  26592. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  26593. #else
  26594. unsigned char rsaCertDer[TWOK_BUF];
  26595. word32 rsaCertDerSz;
  26596. #endif
  26597. #endif
  26598. #ifdef HAVE_ECC
  26599. ecc_key eccKey;
  26600. #if defined(USE_CERT_BUFFERS_256)
  26601. byte* eccCert = (byte*)cliecc_cert_der_256;
  26602. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  26603. #else
  26604. unsigned char eccCert[ONEK_BUF];
  26605. word32 eccCertSz;
  26606. XFILE fp2;
  26607. #endif
  26608. #endif
  26609. printf(testingFmt, "wc_GetPubKeyDerFromCert()");
  26610. #ifndef NO_RSA
  26611. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  26612. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  26613. AssertTrue((fp != XBADFILE));
  26614. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  26615. XFCLOSE(fp);
  26616. #endif
  26617. /* good test case - RSA DER cert */
  26618. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  26619. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26620. AssertIntEQ(ret, 0);
  26621. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26622. AssertIntEQ(ret, 0);
  26623. AssertIntGT(keyDerSz, 0);
  26624. /* sanity check, verify we can import DER public key */
  26625. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26626. AssertIntEQ(ret, 0);
  26627. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  26628. AssertIntEQ(ret, 0);
  26629. wc_FreeRsaKey(&rsaKey);
  26630. /* test LENGTH_ONLY_E case */
  26631. keyDerSz = 0;
  26632. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  26633. AssertIntEQ(ret, LENGTH_ONLY_E);
  26634. AssertIntGT(keyDerSz, 0);
  26635. /* bad args: DecodedCert NULL */
  26636. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  26637. AssertIntEQ(ret, BAD_FUNC_ARG);
  26638. /* bad args: output key buff size */
  26639. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  26640. AssertIntEQ(ret, BAD_FUNC_ARG);
  26641. /* bad args: zero size output key buffer */
  26642. keyDerSz = 0;
  26643. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26644. AssertIntEQ(ret, BAD_FUNC_ARG);
  26645. wc_FreeDecodedCert(&decoded);
  26646. /* Certificate Request Tests */
  26647. #ifdef WOLFSSL_CERT_REQ
  26648. {
  26649. XMEMSET(certBuf, 0, sizeof(certBuf));
  26650. fp = XFOPEN("./certs/csr.signed.der", "rb");
  26651. AssertTrue((fp != XBADFILE));
  26652. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  26653. XFCLOSE(fp);
  26654. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  26655. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  26656. AssertIntEQ(ret, 0);
  26657. /* good test case - RSA DER certificate request */
  26658. keyDerSz = sizeof(keyDer);
  26659. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26660. AssertIntEQ(ret, 0);
  26661. AssertIntGT(keyDerSz, 0);
  26662. /* sanity check, verify we can import DER public key */
  26663. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  26664. AssertIntEQ(ret, 0);
  26665. idx = 0;
  26666. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  26667. AssertIntEQ(ret, 0);
  26668. wc_FreeRsaKey(&rsaKey);
  26669. wc_FreeDecodedCert(&decoded);
  26670. }
  26671. #endif /* WOLFSSL_CERT_REQ */
  26672. #endif /* NO_RSA */
  26673. #ifdef HAVE_ECC
  26674. #ifndef USE_CERT_BUFFERS_256
  26675. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  26676. AssertTrue((fp2 != XBADFILE));
  26677. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  26678. XFCLOSE(fp2);
  26679. #endif
  26680. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  26681. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26682. AssertIntEQ(ret, 0);
  26683. /* good test case - ECC */
  26684. XMEMSET(keyDer, 0, sizeof(keyDer));
  26685. keyDerSz = sizeof(keyDer);
  26686. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26687. AssertIntEQ(ret, 0);
  26688. AssertIntGT(keyDerSz, 0);
  26689. /* sanity check, verify we can import DER public key */
  26690. ret = wc_ecc_init(&eccKey);
  26691. AssertIntEQ(ret, 0);
  26692. idx = 0; /* reset idx to 0, used above in RSA case */
  26693. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  26694. AssertIntEQ(ret, 0);
  26695. wc_ecc_free(&eccKey);
  26696. /* test LENGTH_ONLY_E case */
  26697. keyDerSz = 0;
  26698. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  26699. AssertIntEQ(ret, LENGTH_ONLY_E);
  26700. AssertIntGT(keyDerSz, 0);
  26701. wc_FreeDecodedCert(&decoded);
  26702. #endif
  26703. printf(resultFmt, passed);
  26704. #endif /* !NO_RSA || HAVE_ECC */
  26705. return 0;
  26706. }
  26707. static int test_wc_CheckCertSigPubKey(void)
  26708. {
  26709. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26710. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  26711. int ret;
  26712. const char* ca_cert = "./certs/ca-cert.pem";
  26713. byte* cert_buf = NULL;
  26714. size_t cert_sz = 0;
  26715. byte* cert_der = NULL;
  26716. word32 cert_dersz = 0;
  26717. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  26718. word32 keyDerSz = (word32)sizeof(keyDer);
  26719. DecodedCert decoded;
  26720. printf(testingFmt, "wc_CheckCertSigPubKey()");
  26721. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  26722. if (ret == 0) {
  26723. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  26724. cert_der = (byte*)malloc(cert_dersz);
  26725. if (cert_der) {
  26726. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  26727. cert_der, (int)cert_dersz, CERT_TYPE);
  26728. AssertIntGE(ret, 0);
  26729. }
  26730. }
  26731. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  26732. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  26733. AssertIntEQ(ret, 0);
  26734. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  26735. AssertIntEQ(ret, 0);
  26736. AssertIntGT(keyDerSz, 0);
  26737. /* Good test case. */
  26738. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  26739. RSAk);
  26740. AssertIntEQ(ret, 0);
  26741. /* No certificate. */
  26742. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  26743. ECDSAk);
  26744. AssertIntEQ(ret, BAD_FUNC_ARG);
  26745. /* Bad cert size. */
  26746. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  26747. RSAk);
  26748. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  26749. /* No public key. */
  26750. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  26751. RSAk);
  26752. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  26753. /* Bad public key size. */
  26754. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  26755. RSAk);
  26756. AssertIntEQ(ret, BAD_FUNC_ARG);
  26757. /* Wrong aglo. */
  26758. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  26759. ECDSAk);
  26760. AssertIntEQ(ret, ASN_PARSE_E);
  26761. wc_FreeDecodedCert(&decoded);
  26762. if (cert_der)
  26763. free(cert_der);
  26764. if (cert_buf)
  26765. free(cert_buf);
  26766. printf(resultFmt, passed);
  26767. #endif
  26768. return 0;
  26769. }
  26770. static int test_wolfSSL_certs(void)
  26771. {
  26772. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  26773. !defined(NO_RSA)
  26774. X509* x509ext;
  26775. #ifdef OPENSSL_ALL
  26776. X509* x509;
  26777. WOLFSSL_X509_EXTENSION* ext;
  26778. ASN1_OBJECT* obj;
  26779. #endif
  26780. WOLFSSL* ssl;
  26781. WOLFSSL_CTX* ctx;
  26782. STACK_OF(ASN1_OBJECT)* sk;
  26783. ASN1_STRING* asn1_str;
  26784. AUTHORITY_KEYID* akey;
  26785. BASIC_CONSTRAINTS* bc;
  26786. int crit;
  26787. printf(testingFmt, "wolfSSL_certs()");
  26788. #ifndef NO_WOLFSSL_SERVER
  26789. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  26790. #else
  26791. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  26792. #endif
  26793. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  26794. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26795. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  26796. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26797. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  26798. #endif
  26799. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  26800. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26801. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  26802. #endif
  26803. AssertNotNull(ssl = SSL_new(ctx));
  26804. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26805. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26806. #endif
  26807. #ifdef HAVE_PK_CALLBACKS
  26808. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  26809. #endif /* HAVE_PK_CALLBACKS */
  26810. /* create and use x509 */
  26811. #ifdef OPENSSL_ALL
  26812. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  26813. AssertNotNull(x509);
  26814. #endif
  26815. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  26816. AssertNotNull(x509ext);
  26817. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  26818. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  26819. /* with loading in a new cert the check on private key should now fail */
  26820. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  26821. #endif
  26822. #if defined(USE_CERT_BUFFERS_2048)
  26823. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  26824. (unsigned char*)server_cert_der_2048,
  26825. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  26826. #endif
  26827. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  26828. /************* Get Digest of Certificate ******************/
  26829. {
  26830. byte digest[64]; /* max digest size */
  26831. word32 digestSz;
  26832. XMEMSET(digest, 0, sizeof(digest));
  26833. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  26834. WOLFSSL_SUCCESS);
  26835. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  26836. WOLFSSL_SUCCESS);
  26837. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  26838. WOLFSSL_FAILURE);
  26839. }
  26840. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  26841. /* test and checkout X509 extensions */
  26842. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  26843. &crit, NULL);
  26844. AssertNotNull(bc);
  26845. AssertIntEQ(crit, 0);
  26846. #ifdef OPENSSL_ALL
  26847. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  26848. AssertNotNull(ext);
  26849. X509_EXTENSION_free(ext);
  26850. AssertNotNull(ext = X509_EXTENSION_new());
  26851. X509_EXTENSION_set_critical(ext, 1);
  26852. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  26853. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  26854. ASN1_OBJECT_free(obj);
  26855. X509_EXTENSION_free(ext);
  26856. AssertNotNull(ext = X509_EXTENSION_new());
  26857. X509_EXTENSION_set_critical(ext, 0);
  26858. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  26859. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  26860. NULL);
  26861. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  26862. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  26863. * and X509_get_ext_d2i has created new */
  26864. X509_EXTENSION_free(ext);
  26865. #endif
  26866. BASIC_CONSTRAINTS_free(bc);
  26867. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  26868. AssertNotNull(asn1_str);
  26869. AssertIntEQ(crit, 1);
  26870. AssertIntEQ(asn1_str->type, NID_key_usage);
  26871. #ifdef OPENSSL_ALL
  26872. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  26873. AssertNotNull(ext);
  26874. X509_EXTENSION_free(ext);
  26875. #endif
  26876. ASN1_STRING_free(asn1_str);
  26877. #ifdef OPENSSL_ALL
  26878. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  26879. &crit, NULL);
  26880. AssertNotNull(sk);
  26881. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  26882. AssertNotNull(ext);
  26883. X509_EXTENSION_free(ext);
  26884. sk_ASN1_OBJECT_pop_free(sk, NULL);
  26885. #else
  26886. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  26887. &crit, NULL);
  26888. AssertNull(sk);
  26889. #endif
  26890. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  26891. NID_authority_key_identifier, &crit, NULL);
  26892. AssertNotNull(akey);
  26893. #ifdef OPENSSL_ALL
  26894. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  26895. AssertNotNull(ext);
  26896. X509_EXTENSION_free(ext);
  26897. #endif
  26898. wolfSSL_AUTHORITY_KEYID_free(akey);
  26899. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  26900. NID_private_key_usage_period, &crit, NULL);
  26901. /* AssertNotNull(sk); NID not yet supported */
  26902. AssertIntEQ(crit, -1);
  26903. sk_ASN1_OBJECT_free(sk);
  26904. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  26905. &crit, NULL);
  26906. /* AssertNotNull(sk); no alt names set */
  26907. sk_GENERAL_NAME_free(sk);
  26908. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  26909. &crit, NULL);
  26910. /* AssertNotNull(sk); NID not yet supported */
  26911. AssertIntEQ(crit, -1);
  26912. sk_ASN1_OBJECT_free(sk);
  26913. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  26914. NULL);
  26915. /* AssertNotNull(sk); no auth info set */
  26916. sk_ASN1_OBJECT_free(sk);
  26917. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  26918. &crit, NULL);
  26919. /* AssertNotNull(sk); NID not yet supported */
  26920. AssertIntEQ(crit, -1);
  26921. sk_ASN1_OBJECT_free(sk);
  26922. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  26923. &crit, NULL);
  26924. /* AssertNotNull(sk); NID not yet supported */
  26925. AssertIntEQ(crit, -1);
  26926. sk_ASN1_OBJECT_free(sk);
  26927. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  26928. NID_certificate_policies, &crit, NULL);
  26929. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  26930. AssertNull(sk);
  26931. #else
  26932. /* AssertNotNull(sk); no cert policy set */
  26933. #endif
  26934. sk_ASN1_OBJECT_free(sk);
  26935. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  26936. &crit, NULL);
  26937. /* AssertNotNull(sk); NID not yet supported */
  26938. AssertIntEQ(crit, -1);
  26939. sk_ASN1_OBJECT_free(sk);
  26940. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  26941. &crit, NULL);
  26942. /* AssertNotNull(sk); NID not yet supported */
  26943. AssertIntEQ(crit, -1);
  26944. sk_ASN1_OBJECT_free(sk);
  26945. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  26946. &crit, NULL);
  26947. /* AssertNotNull(sk); NID not yet supported */
  26948. AssertIntEQ(crit, -1);
  26949. sk_ASN1_OBJECT_free(sk);
  26950. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  26951. NULL);
  26952. /* AssertNotNull(sk); NID not yet supported */
  26953. AssertIntEQ(crit, -1);
  26954. sk_ASN1_OBJECT_free(sk);
  26955. /* test invalid cases */
  26956. crit = 0;
  26957. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  26958. AssertNull(sk);
  26959. AssertIntEQ(crit, -1);
  26960. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  26961. NULL, NULL);
  26962. AssertNull(sk);
  26963. AssertIntEQ(SSL_get_hit(ssl), 0);
  26964. #ifdef OPENSSL_ALL
  26965. X509_free(x509);
  26966. #endif
  26967. X509_free(x509ext);
  26968. SSL_free(ssl);
  26969. SSL_CTX_free(ctx);
  26970. printf(resultFmt, passed);
  26971. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  26972. return 0;
  26973. }
  26974. static int test_wolfSSL_X509_check_private_key(void)
  26975. {
  26976. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  26977. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  26978. X509* x509;
  26979. EVP_PKEY* pkey = NULL;
  26980. const byte* key;
  26981. printf(testingFmt, "wolfSSL_X509_check_private_key()");
  26982. /* Check with correct key */
  26983. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  26984. SSL_FILETYPE_PEM)));
  26985. key = client_key_der_2048;
  26986. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26987. &key, (long)sizeof_client_key_der_2048));
  26988. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  26989. EVP_PKEY_free(pkey);
  26990. pkey = NULL;
  26991. /* Check with wrong key */
  26992. key = server_key_der_2048;
  26993. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  26994. &key, (long)sizeof_server_key_der_2048));
  26995. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  26996. /* test for incorrect parameter */
  26997. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  26998. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  26999. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  27000. EVP_PKEY_free(pkey);
  27001. X509_free(x509);
  27002. printf(resultFmt, passed);
  27003. #endif
  27004. return 0;
  27005. }
  27006. static int test_wolfSSL_ASN1_TIME_print(void)
  27007. {
  27008. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  27009. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27010. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  27011. !defined(NO_BIO)
  27012. BIO* bio;
  27013. X509* x509;
  27014. const unsigned char* der = client_cert_der_2048;
  27015. ASN1_TIME* t;
  27016. unsigned char buf[25];
  27017. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  27018. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27019. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27020. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27021. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  27022. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27023. AssertIntEQ(XMEMCMP(buf, "Feb 15 12:50:24 2022 GMT", sizeof(buf) - 1), 0);
  27024. /* create a bad time and test results */
  27025. AssertNotNull(t = X509_get_notAfter(x509));
  27026. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  27027. t->data[8] = 0;
  27028. t->data[3] = 0;
  27029. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  27030. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27031. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27032. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  27033. BIO_free(bio);
  27034. X509_free(x509);
  27035. printf(resultFmt, passed);
  27036. #endif
  27037. return 0;
  27038. }
  27039. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27040. {
  27041. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27042. BIO* bio;
  27043. ASN1_UTCTIME* utc = NULL;
  27044. unsigned char buf[25];
  27045. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27046. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27047. printf(testingFmt, "ASN1_UTCTIME_print()");
  27048. /* NULL parameter check */
  27049. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27050. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27051. BIO_free(bio);
  27052. /* Valid date */
  27053. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27054. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27055. DYNAMIC_TYPE_ASN1));
  27056. utc->type = ASN_UTC_TIME;
  27057. utc->length = ASN_UTC_TIME_SIZE;
  27058. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27059. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27060. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27061. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27062. XMEMSET(buf, 0, sizeof(buf));
  27063. BIO_free(bio);
  27064. /* Invalid format */
  27065. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27066. utc->type = ASN_UTC_TIME;
  27067. utc->length = ASN_UTC_TIME_SIZE;
  27068. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27069. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27070. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27071. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27072. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27073. BIO_free(bio);
  27074. printf(resultFmt, passed);
  27075. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27076. return 0;
  27077. }
  27078. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27079. {
  27080. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27081. ASN1_TIME* fromTime;
  27082. ASN1_TIME* toTime;
  27083. int daysDiff;
  27084. int secsDiff;
  27085. printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
  27086. AssertNotNull((fromTime = ASN1_TIME_new()));
  27087. /* Feb 22, 2003, 21:15:15 */
  27088. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  27089. AssertNotNull((toTime = ASN1_TIME_new()));
  27090. /* Dec 19, 2010, 18:10:11 */
  27091. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  27092. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  27093. /* Error conditions. */
  27094. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  27095. WOLFSSL_FAILURE);
  27096. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  27097. WOLFSSL_FAILURE);
  27098. /* If both times are NULL, difference is 0. */
  27099. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  27100. WOLFSSL_SUCCESS);
  27101. AssertIntEQ(daysDiff, 0);
  27102. AssertIntEQ(secsDiff, 0);
  27103. /* If one time is NULL, it defaults to the current time. */
  27104. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  27105. WOLFSSL_SUCCESS);
  27106. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  27107. WOLFSSL_SUCCESS);
  27108. /* Normal operation. Both times non-NULL. */
  27109. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27110. WOLFSSL_SUCCESS);
  27111. AssertIntEQ(daysDiff, 2856);
  27112. AssertIntEQ(secsDiff, 75296);
  27113. /* Swapping the times should return negative values. */
  27114. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  27115. WOLFSSL_SUCCESS);
  27116. AssertIntEQ(daysDiff, -2856);
  27117. AssertIntEQ(secsDiff, -75296);
  27118. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27119. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27120. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27121. /* Compare regression test: No seconds difference, just difference in days.
  27122. */
  27123. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27124. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27125. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27126. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27127. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27128. /* Edge case with Unix epoch. */
  27129. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27130. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27131. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27132. WOLFSSL_SUCCESS);
  27133. AssertIntEQ(daysDiff, 3652);
  27134. AssertIntEQ(secsDiff, 0);
  27135. /* Edge case with year > 2038 (year 2038 problem). */
  27136. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27137. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27138. WOLFSSL_SUCCESS);
  27139. AssertIntEQ(daysDiff, 2932896);
  27140. AssertIntEQ(secsDiff, 86399);
  27141. ASN1_TIME_free(fromTime);
  27142. ASN1_TIME_free(toTime);
  27143. printf(resultFmt, passed);
  27144. #endif
  27145. return 0;
  27146. }
  27147. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27148. {
  27149. #if defined(OPENSSL_EXTRA)
  27150. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  27151. unsigned char nullstr[32];
  27152. printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
  27153. XMEMSET(nullstr, 0, 32);
  27154. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27155. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  27156. DYNAMIC_TYPE_TMP_BUFFER);
  27157. if (asn1_gtime) {
  27158. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27159. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27160. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27161. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27162. }
  27163. printf(resultFmt, passed);
  27164. #endif /* OPENSSL_EXTRA */
  27165. return 0;
  27166. }
  27167. static int test_wolfSSL_private_keys(void)
  27168. {
  27169. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27170. !defined(NO_FILESYSTEM)
  27171. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27172. WOLFSSL* ssl;
  27173. WOLFSSL_CTX* ctx;
  27174. EVP_PKEY* pkey = NULL;
  27175. printf(testingFmt, "wolfSSL_private_keys()");
  27176. OpenSSL_add_all_digests();
  27177. OpenSSL_add_all_algorithms();
  27178. #ifndef NO_RSA
  27179. #ifndef NO_WOLFSSL_SERVER
  27180. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27181. #else
  27182. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27183. #endif
  27184. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27185. /* Have to load a cert before you can check the private key against that
  27186. * certificates public key! */
  27187. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27188. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  27189. #endif
  27190. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27191. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27192. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27193. #endif
  27194. AssertNotNull(ssl = SSL_new(ctx));
  27195. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27196. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27197. #endif
  27198. #ifdef USE_CERT_BUFFERS_2048
  27199. {
  27200. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27201. unsigned char buf[FOURK_BUF];
  27202. word32 bufSz;
  27203. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  27204. (unsigned char*)client_key_der_2048,
  27205. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27206. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27207. /* Should mismatch now that a different private key loaded */
  27208. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27209. #endif
  27210. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  27211. (unsigned char*)server_key,
  27212. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27213. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27214. /* After loading back in DER format of original key, should match */
  27215. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27216. #endif
  27217. /* test loading private key to the WOLFSSL_CTX */
  27218. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27219. (unsigned char*)client_key_der_2048,
  27220. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27221. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27222. /* Should mismatch now that a different private key loaded */
  27223. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27224. #endif
  27225. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27226. (unsigned char*)server_key,
  27227. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27228. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27229. /* After loading back in DER format of original key, should match */
  27230. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27231. #endif
  27232. /* pkey not set yet, expecting to fail */
  27233. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  27234. /* set PKEY and test again */
  27235. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27236. &server_key, (long)sizeof_server_key_der_2048));
  27237. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27238. /* reuse PKEY structure and test
  27239. * this should be checked with a memory management sanity checker */
  27240. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  27241. server_key = (const unsigned char*)server_key_der_2048;
  27242. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27243. &server_key, (long)sizeof_server_key_der_2048));
  27244. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27245. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  27246. bufSz = FOURK_BUF;
  27247. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  27248. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  27249. RSAk, NULL, 0)), 0);
  27250. server_key = (const unsigned char*)buf;
  27251. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  27252. (long)bufSz));
  27253. }
  27254. #endif
  27255. EVP_PKEY_free(pkey);
  27256. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  27257. SSL_CTX_free(ctx);
  27258. #endif /* end of RSA private key match tests */
  27259. #ifdef HAVE_ECC
  27260. #ifndef NO_WOLFSSL_SERVER
  27261. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27262. #else
  27263. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27264. #endif
  27265. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  27266. WOLFSSL_FILETYPE_PEM));
  27267. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  27268. WOLFSSL_FILETYPE_PEM));
  27269. AssertNotNull(ssl = SSL_new(ctx));
  27270. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27271. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27272. #endif
  27273. SSL_free(ssl);
  27274. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  27275. WOLFSSL_FILETYPE_PEM));
  27276. AssertNotNull(ssl = SSL_new(ctx));
  27277. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  27278. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27279. #endif
  27280. SSL_free(ssl);
  27281. SSL_CTX_free(ctx);
  27282. #endif /* end of ECC private key match tests */
  27283. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  27284. #ifndef NO_WOLFSSL_SERVER
  27285. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27286. #else
  27287. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27288. #endif
  27289. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  27290. WOLFSSL_FILETYPE_PEM));
  27291. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  27292. WOLFSSL_FILETYPE_PEM));
  27293. AssertNotNull(ssl = SSL_new(ctx));
  27294. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27295. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27296. #endif
  27297. SSL_free(ssl);
  27298. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  27299. WOLFSSL_FILETYPE_PEM));
  27300. AssertNotNull(ssl = SSL_new(ctx));
  27301. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27302. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27303. #endif
  27304. SSL_free(ssl);
  27305. SSL_CTX_free(ctx);
  27306. #endif /* end of Ed25519 private key match tests */
  27307. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  27308. #ifndef NO_WOLFSSL_SERVER
  27309. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27310. #else
  27311. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27312. #endif
  27313. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  27314. WOLFSSL_FILETYPE_PEM));
  27315. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  27316. WOLFSSL_FILETYPE_PEM));
  27317. AssertNotNull(ssl = SSL_new(ctx));
  27318. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27319. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27320. #endif
  27321. SSL_free(ssl);
  27322. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  27323. WOLFSSL_FILETYPE_PEM));
  27324. AssertNotNull(ssl = SSL_new(ctx));
  27325. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27326. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27327. #endif
  27328. SSL_free(ssl);
  27329. SSL_CTX_free(ctx);
  27330. #endif /* end of Ed448 private key match tests */
  27331. EVP_cleanup();
  27332. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  27333. CONF_modules_free();
  27334. ENGINE_cleanup();
  27335. CONF_modules_unload();
  27336. (void)ssl;
  27337. (void)ctx;
  27338. (void)pkey;
  27339. printf(resultFmt, passed);
  27340. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  27341. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27342. return 0;
  27343. }
  27344. static int test_wolfSSL_PEM_read_PrivateKey(void)
  27345. {
  27346. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27347. && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  27348. XFILE file;
  27349. const char* fname = "./certs/server-key.pem";
  27350. EVP_PKEY* pkey;
  27351. RSA* rsa;
  27352. WOLFSSL_EVP_PKEY_CTX* ctx;
  27353. unsigned char* sig;
  27354. size_t sigLen;
  27355. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  27356. size_t tbsLen = sizeof(tbs);
  27357. printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
  27358. /* Check error case. */
  27359. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  27360. /* Read in an RSA key. */
  27361. file = XFOPEN(fname, "rb");
  27362. AssertTrue(file != XBADFILE);
  27363. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  27364. XFCLOSE(file);
  27365. /* Make sure the key is usable by signing some data with it. */
  27366. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  27367. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  27368. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  27369. DYNAMIC_TYPE_TMP_BUFFER));
  27370. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  27371. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  27372. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  27373. WOLFSSL_SUCCESS);
  27374. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  27375. EVP_PKEY_CTX_free(ctx);
  27376. EVP_PKEY_free(pkey);
  27377. printf(resultFmt, passed);
  27378. #endif
  27379. return 0;
  27380. }
  27381. static int test_wolfSSL_PEM_read_PUBKEY(void)
  27382. {
  27383. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  27384. && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  27385. XFILE file;
  27386. const char* fname = "./certs/client-keyPub.pem";
  27387. EVP_PKEY* pkey;
  27388. printf(testingFmt, "test_wolfSSL_PEM_read_PUBKEY()");
  27389. /* Check error case. */
  27390. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  27391. /* Read in an RSA key. */
  27392. file = XFOPEN(fname, "rb");
  27393. AssertTrue(file != XBADFILE);
  27394. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  27395. EVP_PKEY_free(pkey);
  27396. XFCLOSE(file);
  27397. #endif
  27398. return 0;
  27399. }
  27400. static int test_wolfSSL_PEM_PrivateKey(void)
  27401. {
  27402. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27403. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  27404. #ifndef NO_BIO
  27405. BIO* bio = NULL;
  27406. #endif
  27407. EVP_PKEY* pkey = NULL;
  27408. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27409. #ifndef NO_BIO
  27410. /* test creating new EVP_PKEY with bad arg */
  27411. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  27412. /* test loading RSA key using BIO */
  27413. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  27414. {
  27415. XFILE file;
  27416. const char* fname = "./certs/server-key.pem";
  27417. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  27418. size_t sz;
  27419. byte* buf;
  27420. EVP_PKEY* pkey2;
  27421. EVP_PKEY* pkey3;
  27422. RSA* rsa_key = NULL;
  27423. file = XFOPEN(fname, "rb");
  27424. AssertTrue((file != XBADFILE));
  27425. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27426. sz = XFTELL(file);
  27427. XREWIND(file);
  27428. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27429. if (buf) {
  27430. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27431. }
  27432. XFCLOSE(file);
  27433. /* Test using BIO new mem and loading PEM private key */
  27434. bio = BIO_new_mem_buf(buf, (int)sz);
  27435. AssertNotNull(bio);
  27436. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27437. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27438. BIO_free(bio);
  27439. bio = NULL;
  27440. AssertNotNull(pkey2 = EVP_PKEY_new());
  27441. pkey2->type = EVP_PKEY_RSA;
  27442. /* Test parameter copy */
  27443. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  27444. EVP_PKEY_free(pkey2);
  27445. EVP_PKEY_free(pkey);
  27446. pkey = NULL;
  27447. /* Qt unit test case : rsa pkcs8 key */
  27448. file = XFOPEN(fname_rsa_p8, "rb");
  27449. AssertTrue((file != XBADFILE));
  27450. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27451. sz = XFTELL(file);
  27452. XREWIND(file);
  27453. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27454. if (buf)
  27455. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27456. XFCLOSE(file);
  27457. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27458. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27459. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27460. BIO_free(bio);
  27461. bio = NULL;
  27462. AssertNotNull(pkey3 = EVP_PKEY_new());
  27463. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  27464. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  27465. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27466. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27467. #else
  27468. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27469. #endif
  27470. RSA_free(rsa_key);
  27471. EVP_PKEY_free(pkey3);
  27472. EVP_PKEY_free(pkey);
  27473. pkey = NULL;
  27474. }
  27475. #endif
  27476. /* test loading ECC key using BIO */
  27477. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27478. {
  27479. XFILE file;
  27480. const char* fname = "./certs/ecc-key.pem";
  27481. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  27482. size_t sz;
  27483. byte* buf;
  27484. EVP_PKEY* pkey2;
  27485. EVP_PKEY* pkey3;
  27486. EC_KEY* ec_key;
  27487. int nid = 0;
  27488. file = XFOPEN(fname, "rb");
  27489. AssertTrue((file != XBADFILE));
  27490. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27491. sz = XFTELL(file);
  27492. XREWIND(file);
  27493. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27494. if (buf)
  27495. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27496. XFCLOSE(file);
  27497. /* Test using BIO new mem and loading PEM private key */
  27498. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27499. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27500. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27501. BIO_free(bio);
  27502. bio = NULL;
  27503. AssertNotNull(pkey2 = EVP_PKEY_new());
  27504. AssertNotNull(pkey3 = EVP_PKEY_new());
  27505. pkey2->type = EVP_PKEY_EC;
  27506. /* Test parameter copy */
  27507. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  27508. /* Qt unit test case 1*/
  27509. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27510. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27511. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27512. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27513. #else
  27514. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27515. #endif
  27516. /* Test default digest */
  27517. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  27518. AssertIntEQ(nid, NID_sha256);
  27519. EC_KEY_free(ec_key);
  27520. EVP_PKEY_free(pkey3);
  27521. EVP_PKEY_free(pkey2);
  27522. EVP_PKEY_free(pkey);
  27523. pkey = NULL;
  27524. /* Qt unit test case ec pkcs8 key */
  27525. file = XFOPEN(fname_ecc_p8, "rb");
  27526. AssertTrue((file != XBADFILE));
  27527. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  27528. sz = XFTELL(file);
  27529. XREWIND(file);
  27530. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27531. if (buf)
  27532. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27533. XFCLOSE(file);
  27534. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27535. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27536. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27537. BIO_free(bio);
  27538. bio = NULL;
  27539. AssertNotNull(pkey3 = EVP_PKEY_new());
  27540. /* Qt unit test case */
  27541. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27542. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  27543. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27544. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  27545. #else
  27546. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  27547. #endif
  27548. EC_KEY_free(ec_key);
  27549. EVP_PKEY_free(pkey3);
  27550. EVP_PKEY_free(pkey);
  27551. pkey = NULL;
  27552. }
  27553. #endif
  27554. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  27555. defined(WOLFSSL_CERT_GEN))
  27556. {
  27557. #define BIO_PEM_TEST_CHAR 'a'
  27558. EVP_PKEY* pkey2 = NULL;
  27559. unsigned char extra[10];
  27560. int i;
  27561. BIO* pub_bio = NULL;
  27562. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  27563. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  27564. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27565. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  27566. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27567. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  27568. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  27569. &server_key, (long)sizeof_server_key_der_2048));
  27570. AssertNull(pkey);
  27571. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27572. &server_key, (long)sizeof_server_key_der_2048));
  27573. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27574. WOLFSSL_SUCCESS);
  27575. AssertIntGT(BIO_pending(bio), 0);
  27576. AssertIntEQ(BIO_pending(bio), 1679);
  27577. /* Check if the pubkey API writes only the public key */
  27578. #ifdef WOLFSSL_KEY_GEN
  27579. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  27580. AssertIntGT(BIO_pending(pub_bio), 0);
  27581. /* Previously both the private key and the pubkey calls would write
  27582. * out the private key and the PEM header was the only difference.
  27583. * The public PEM should be significantly shorter than the
  27584. * private key versison. */
  27585. AssertIntEQ(BIO_pending(pub_bio), 451);
  27586. #endif
  27587. /* test creating new EVP_PKEY with good args */
  27588. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  27589. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  27590. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  27591. /* test of reuse of EVP_PKEY */
  27592. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27593. AssertIntEQ(BIO_pending(bio), 0);
  27594. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  27595. SSL_SUCCESS);
  27596. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  27597. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  27598. AssertNotNull(pkey);
  27599. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  27600. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  27601. }
  27602. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  27603. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  27604. for (i = 0; i < 10; i++) {
  27605. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  27606. }
  27607. BIO_free(pub_bio);
  27608. BIO_free(bio);
  27609. bio = NULL;
  27610. EVP_PKEY_free(pkey);
  27611. pkey = NULL;
  27612. EVP_PKEY_free(pkey2);
  27613. }
  27614. #endif
  27615. /* key is DES encrypted */
  27616. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  27617. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  27618. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  27619. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  27620. {
  27621. XFILE f;
  27622. wc_pem_password_cb* passwd_cb;
  27623. void* passwd_cb_userdata;
  27624. SSL_CTX* ctx;
  27625. char passwd[] = "bad password";
  27626. #ifndef WOLFSSL_NO_TLS12
  27627. #ifndef NO_WOLFSSL_SERVER
  27628. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27629. #else
  27630. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27631. #endif
  27632. #else
  27633. #ifndef NO_WOLFSSL_SERVER
  27634. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27635. #else
  27636. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27637. #endif
  27638. #endif
  27639. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27640. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  27641. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  27642. AssertNull(passwd_cb_userdata =
  27643. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  27644. /* fail case with password call back */
  27645. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  27646. (void*)passwd));
  27647. BIO_free(bio);
  27648. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  27649. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27650. (void*)passwd));
  27651. BIO_free(bio);
  27652. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  27653. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  27654. /* use callback that works */
  27655. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  27656. (void*)"yassl123"));
  27657. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27658. EVP_PKEY_free(pkey);
  27659. pkey = NULL;
  27660. BIO_free(bio);
  27661. bio = NULL;
  27662. SSL_CTX_free(ctx);
  27663. }
  27664. #endif /* !defined(NO_DES3) */
  27665. #endif /* !NO_BIO */
  27666. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27667. {
  27668. unsigned char buf[2048];
  27669. size_t bytes;
  27670. XFILE f;
  27671. SSL_CTX* ctx;
  27672. #ifndef WOLFSSL_NO_TLS12
  27673. #ifndef NO_WOLFSSL_SERVER
  27674. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  27675. #else
  27676. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  27677. #endif
  27678. #else
  27679. #ifndef NO_WOLFSSL_SERVER
  27680. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  27681. #else
  27682. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  27683. #endif
  27684. #endif
  27685. f = XFOPEN("./certs/ecc-key.der", "rb");
  27686. AssertTrue((f != XBADFILE));
  27687. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  27688. XFCLOSE(f);
  27689. server_key = buf;
  27690. pkey = NULL;
  27691. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  27692. AssertNull(pkey);
  27693. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  27694. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  27695. EVP_PKEY_free(pkey);
  27696. pkey = NULL;
  27697. SSL_CTX_free(ctx);
  27698. }
  27699. #endif
  27700. printf(resultFmt, passed);
  27701. #ifndef NO_BIO
  27702. (void)bio;
  27703. #endif
  27704. (void)pkey;
  27705. (void)server_key;
  27706. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  27707. return 0;
  27708. }
  27709. #ifndef NO_BIO
  27710. static int test_wolfSSL_PEM_bio_RSAKey(void)
  27711. {
  27712. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27713. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  27714. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  27715. RSA* rsa = NULL;
  27716. BIO* bio = NULL;
  27717. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  27718. /* PrivateKey */
  27719. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27720. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  27721. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  27722. AssertNotNull(rsa);
  27723. AssertIntEQ(RSA_size(rsa), 256);
  27724. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27725. NULL), WOLFSSL_FAILURE);
  27726. BIO_free(bio);
  27727. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27728. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  27729. NULL), WOLFSSL_SUCCESS);
  27730. BIO_free(bio);
  27731. RSA_free(rsa);
  27732. /* PUBKEY */
  27733. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  27734. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27735. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27736. AssertIntEQ(RSA_size(rsa), 256);
  27737. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27738. BIO_free(bio);
  27739. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27740. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  27741. BIO_free(bio);
  27742. /* Same test as above, but with a file pointer rather than a BIO. */
  27743. AssertIntEQ(PEM_write_RSAPublicKey(NULL, rsa), WOLFSSL_FAILURE);
  27744. AssertIntEQ(PEM_write_RSAPublicKey(stdout, NULL), WOLFSSL_FAILURE);
  27745. AssertIntEQ(PEM_write_RSAPublicKey(stdout, rsa), WOLFSSL_SUCCESS);
  27746. RSA_free(rsa);
  27747. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  27748. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  27749. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27750. BIO_free(bio);
  27751. RSA_free(rsa);
  27752. #ifdef HAVE_ECC
  27753. /* ensure that non-rsa keys do not work */
  27754. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27755. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27756. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  27757. BIO_free(bio);
  27758. RSA_free(rsa);
  27759. #endif /* HAVE_ECC */
  27760. printf(resultFmt, passed);
  27761. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27762. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  27763. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  27764. return 0;
  27765. }
  27766. static int test_wolfSSL_PEM_RSAPrivateKey(void)
  27767. {
  27768. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27769. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27770. RSA* rsa = NULL;
  27771. RSA* rsa_dup = NULL;
  27772. BIO* bio = NULL;
  27773. XFILE f = NULL;
  27774. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  27775. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  27776. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27777. AssertIntEQ(RSA_size(rsa), 256);
  27778. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  27779. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  27780. /* Test duplicating empty key. */
  27781. rsa_dup = RSA_new();
  27782. AssertNull(RSAPublicKey_dup(rsa_dup));
  27783. RSA_free(rsa_dup);
  27784. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  27785. AssertPtrNE(rsa_dup, rsa);
  27786. #endif
  27787. /* test if valgrind complains about unreleased memory */
  27788. RSA_up_ref(rsa);
  27789. RSA_free(rsa);
  27790. BIO_free(bio);
  27791. RSA_free(rsa);
  27792. RSA_free(rsa_dup);
  27793. f = XFOPEN(svrKeyFile, "r");
  27794. AssertTrue((f != XBADFILE));
  27795. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  27796. AssertIntEQ(RSA_size(rsa), 256);
  27797. RSA_free(rsa);
  27798. XFCLOSE(f);
  27799. #ifdef HAVE_ECC
  27800. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  27801. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  27802. BIO_free(bio);
  27803. #endif /* HAVE_ECC */
  27804. printf(resultFmt, passed);
  27805. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27806. return 0;
  27807. }
  27808. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  27809. {
  27810. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27811. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  27812. XFILE file;
  27813. const char* fname = "./certs/client-keyPub.pem";
  27814. RSA *rsa;
  27815. file = XFOPEN(fname, "rb");
  27816. AssertTrue((file != XBADFILE));
  27817. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  27818. AssertIntEQ(RSA_size(rsa), 256);
  27819. RSA_free(rsa);
  27820. XFCLOSE(file);
  27821. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27822. return 0;
  27823. }
  27824. static int test_wolfSSL_PEM_bio_DSAKey(void)
  27825. {
  27826. #ifndef HAVE_SELFTEST
  27827. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  27828. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  27829. DSA* dsa = NULL;
  27830. BIO* bio = NULL;
  27831. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  27832. /* PrivateKey */
  27833. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  27834. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  27835. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  27836. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  27837. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  27838. WOLFSSL_FAILURE);
  27839. BIO_free(bio);
  27840. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27841. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  27842. WOLFSSL_SUCCESS);
  27843. BIO_free(bio);
  27844. DSA_free(dsa);
  27845. /* PUBKEY */
  27846. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  27847. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  27848. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  27849. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  27850. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27851. BIO_free(bio);
  27852. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27853. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  27854. BIO_free(bio);
  27855. DSA_free(dsa);
  27856. #ifdef HAVE_ECC
  27857. /* ensure that non-dsa keys do not work */
  27858. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  27859. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  27860. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  27861. BIO_free(bio);
  27862. DSA_free(dsa);
  27863. #endif /* HAVE_ECC */
  27864. printf(resultFmt, passed);
  27865. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  27866. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  27867. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  27868. #endif /* HAVE_SELFTEST */
  27869. return 0;
  27870. }
  27871. static int test_wolfSSL_PEM_bio_ECKey(void)
  27872. {
  27873. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  27874. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  27875. EC_KEY* ec = NULL;
  27876. BIO* bio = NULL;
  27877. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  27878. /* PrivateKey */
  27879. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  27880. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  27881. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  27882. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  27883. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  27884. NULL),WOLFSSL_FAILURE);
  27885. BIO_free(bio);
  27886. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27887. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  27888. NULL), WOLFSSL_SUCCESS);
  27889. BIO_free(bio);
  27890. EC_KEY_free(ec);
  27891. /* PUBKEY */
  27892. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  27893. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  27894. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  27895. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  27896. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  27897. BIO_free(bio);
  27898. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  27899. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  27900. BIO_free(bio);
  27901. /* Same test as above, but with a file pointer rather than a BIO. */
  27902. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  27903. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, NULL), WOLFSSL_FAILURE);
  27904. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, ec), WOLFSSL_SUCCESS);
  27905. EC_KEY_free(ec);
  27906. #ifndef NO_RSA
  27907. /* ensure that non-ec keys do not work */
  27908. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  27909. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  27910. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  27911. BIO_free(bio);
  27912. EC_KEY_free(ec);
  27913. #endif /* HAVE_ECC */
  27914. printf(resultFmt, passed);
  27915. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  27916. return 0;
  27917. }
  27918. static int test_wolfSSL_PEM_PUBKEY(void)
  27919. {
  27920. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  27921. BIO* bio = NULL;
  27922. EVP_PKEY* pkey = NULL;
  27923. /* test creating new EVP_PKEY with bad arg */
  27924. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  27925. /* test loading ECC key using BIO */
  27926. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  27927. {
  27928. XFILE file;
  27929. const char* fname = "./certs/ecc-client-keyPub.pem";
  27930. size_t sz;
  27931. byte* buf;
  27932. EVP_PKEY* pkey2;
  27933. EC_KEY* ec_key;
  27934. file = XFOPEN(fname, "rb");
  27935. AssertTrue((file != XBADFILE));
  27936. AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
  27937. sz = XFTELL(file);
  27938. XREWIND(file);
  27939. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  27940. if (buf)
  27941. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  27942. XFCLOSE(file);
  27943. /* Test using BIO new mem and loading PEM private key */
  27944. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  27945. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  27946. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  27947. BIO_free(bio);
  27948. bio = NULL;
  27949. /* Qt unit test case*/
  27950. AssertNotNull(pkey2 = EVP_PKEY_new());
  27951. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  27952. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  27953. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  27954. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  27955. #else
  27956. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  27957. #endif
  27958. EC_KEY_free(ec_key);
  27959. EVP_PKEY_free(pkey2);
  27960. EVP_PKEY_free(pkey);
  27961. pkey = NULL;
  27962. }
  27963. #endif
  27964. (void)bio;
  27965. (void)pkey;
  27966. #endif
  27967. return 0;
  27968. }
  27969. #endif /* !NO_BIO */
  27970. static int test_DSA_do_sign_verify(void)
  27971. {
  27972. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  27973. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  27974. !defined(NO_DSA)
  27975. unsigned char digest[WC_SHA_DIGEST_SIZE];
  27976. DSA_SIG* sig;
  27977. DSA* dsa;
  27978. word32 bytes;
  27979. byte sigBin[DSA_SIG_SIZE];
  27980. int dsacheck;
  27981. #ifdef USE_CERT_BUFFERS_1024
  27982. byte tmp[ONEK_BUF];
  27983. XMEMSET(tmp, 0, sizeof(tmp));
  27984. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  27985. bytes = sizeof_dsa_key_der_1024;
  27986. #elif defined(USE_CERT_BUFFERS_2048)
  27987. byte tmp[TWOK_BUF];
  27988. XMEMSET(tmp, 0, sizeof(tmp));
  27989. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  27990. bytes = sizeof_dsa_key_der_2048;
  27991. #else
  27992. byte tmp[TWOK_BUF];
  27993. XMEMSET(tmp, 0, sizeof(tmp));
  27994. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  27995. if (fp == XBADFILE) {
  27996. return WOLFSSL_BAD_FILE;
  27997. }
  27998. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  27999. XFCLOSE(fp);
  28000. #endif /* END USE_CERT_BUFFERS_1024 */
  28001. printf(testingFmt, "DSA_do_sign_verify()");
  28002. XMEMSET(digest, 202, sizeof(digest));
  28003. AssertNotNull(dsa = DSA_new());
  28004. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28005. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28006. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28007. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28008. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28009. DSA_SIG_free(sig);
  28010. DSA_free(dsa);
  28011. #endif
  28012. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28013. return 0;
  28014. }
  28015. static int test_wolfSSL_tmp_dh(void)
  28016. {
  28017. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28018. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28019. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28020. byte buff[6000];
  28021. char file[] = "./certs/dsaparams.pem";
  28022. XFILE f;
  28023. int bytes;
  28024. DSA* dsa;
  28025. DH* dh;
  28026. #if defined(WOLFSSL_DH_EXTRA) && \
  28027. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28028. DH* dh2;
  28029. #endif
  28030. BIO* bio;
  28031. SSL* ssl;
  28032. SSL_CTX* ctx;
  28033. printf(testingFmt, "wolfSSL_tmp_dh()");
  28034. #ifndef NO_WOLFSSL_SERVER
  28035. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28036. #else
  28037. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28038. #endif
  28039. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28040. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28041. AssertNotNull(ssl = SSL_new(ctx));
  28042. f = XFOPEN(file, "rb");
  28043. AssertTrue((f != XBADFILE));
  28044. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  28045. XFCLOSE(f);
  28046. bio = BIO_new_mem_buf((void*)buff, bytes);
  28047. AssertNotNull(bio);
  28048. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  28049. AssertNotNull(dsa);
  28050. dh = wolfSSL_DSA_dup_DH(dsa);
  28051. AssertNotNull(dh);
  28052. #if defined(WOLFSSL_DH_EXTRA) && \
  28053. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28054. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  28055. #endif
  28056. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  28057. #ifndef NO_WOLFSSL_SERVER
  28058. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  28059. #else
  28060. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  28061. #endif
  28062. BIO_free(bio);
  28063. DSA_free(dsa);
  28064. DH_free(dh);
  28065. #if defined(WOLFSSL_DH_EXTRA) && \
  28066. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28067. DH_free(dh2);
  28068. #endif
  28069. SSL_free(ssl);
  28070. SSL_CTX_free(ctx);
  28071. printf(resultFmt, passed);
  28072. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28073. #endif
  28074. return 0;
  28075. }
  28076. static int test_wolfSSL_ctrl(void)
  28077. {
  28078. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  28079. byte buff[6000];
  28080. BIO* bio;
  28081. int bytes;
  28082. BUF_MEM* ptr = NULL;
  28083. printf(testingFmt, "wolfSSL_crtl()");
  28084. bytes = sizeof(buff);
  28085. bio = BIO_new_mem_buf((void*)buff, bytes);
  28086. AssertNotNull(bio);
  28087. AssertNotNull(BIO_s_socket());
  28088. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  28089. /* needs tested after stubs filled out @TODO
  28090. SSL_ctrl
  28091. SSL_CTX_ctrl
  28092. */
  28093. BIO_free(bio);
  28094. printf(resultFmt, passed);
  28095. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28096. return 0;
  28097. }
  28098. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28099. {
  28100. #ifdef OPENSSL_EXTRA
  28101. static const unsigned char pw[] = "password";
  28102. static const int pwSz = sizeof(pw) - 1;
  28103. size_t checkPwSz = 0;
  28104. const unsigned char* checkPw = NULL;
  28105. WOLFSSL_EVP_PKEY* key = NULL;
  28106. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  28107. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28108. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28109. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  28110. if (key) {
  28111. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  28112. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  28113. AssertIntEQ(key->pkey_sz, pwSz);
  28114. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28115. }
  28116. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28117. AssertIntEQ((int)checkPwSz, pwSz);
  28118. if (checkPw) {
  28119. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  28120. }
  28121. wolfSSL_EVP_PKEY_free(key);
  28122. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  28123. if (key) {
  28124. AssertIntEQ(key->pkey_sz, 0);
  28125. }
  28126. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28127. (void)checkPw;
  28128. AssertIntEQ((int)checkPwSz, 0);
  28129. wolfSSL_EVP_PKEY_free(key);
  28130. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  28131. if (key) {
  28132. AssertIntEQ(key->pkey_sz, 0);
  28133. }
  28134. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28135. (void)checkPw;
  28136. AssertIntEQ((int)checkPwSz, 0);
  28137. wolfSSL_EVP_PKEY_free(key);
  28138. printf(resultFmt, passed);
  28139. #endif /* OPENSSL_EXTRA */
  28140. return 0;
  28141. }
  28142. static int test_wolfSSL_EVP_Digest(void)
  28143. {
  28144. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28145. const char* in = "abc";
  28146. int inLen = (int)XSTRLEN(in);
  28147. byte out[WC_SHA256_DIGEST_SIZE];
  28148. unsigned int outLen;
  28149. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  28150. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  28151. "\x15\xAD";
  28152. printf(testingFmt, "wolfSSL_EVP_Digest()");
  28153. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  28154. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  28155. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  28156. printf(resultFmt, passed);
  28157. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  28158. return 0;
  28159. }
  28160. static int test_wolfSSL_EVP_Digest_all(void)
  28161. {
  28162. #ifdef OPENSSL_EXTRA
  28163. const char* digests[] = {
  28164. #ifndef NO_MD5
  28165. "MD5",
  28166. #endif
  28167. #ifndef NO_SHA
  28168. "SHA",
  28169. #endif
  28170. #ifdef WOLFSSL_SHA224
  28171. "SHA224",
  28172. #endif
  28173. #ifndef NO_SHA256
  28174. "SHA256",
  28175. #endif
  28176. #ifdef WOLFSSL_SHA384
  28177. "SHA384",
  28178. #endif
  28179. #ifdef WOLFSSL_SHA512
  28180. "SHA512",
  28181. #endif
  28182. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  28183. "SHA512_224",
  28184. #endif
  28185. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  28186. "SHA512_256",
  28187. #endif
  28188. #ifdef WOLFSSL_SHA3
  28189. #ifndef WOLFSSL_NOSHA3_224
  28190. "SHA3_224",
  28191. #endif
  28192. #ifndef WOLFSSL_NOSHA3_256
  28193. "SHA3_256",
  28194. #endif
  28195. "SHA3_384",
  28196. #ifndef WOLFSSL_NOSHA3_512
  28197. "SHA3_512",
  28198. #endif
  28199. #endif /* WOLFSSL_SHA3 */
  28200. NULL
  28201. };
  28202. const char** d;
  28203. const unsigned char in[] = "abc";
  28204. int inLen = XSTR_SIZEOF(in);
  28205. byte out[WC_MAX_DIGEST_SIZE];
  28206. unsigned int outLen;
  28207. printf(testingFmt, "wolfSSL_EVP_Digest_all");
  28208. for (d = digests; *d != NULL; d++) {
  28209. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  28210. AssertIntGT(outLen, 0);
  28211. AssertIntEQ(EVP_MD_size(*d), outLen);
  28212. }
  28213. printf(resultFmt, passed);
  28214. #endif
  28215. return 0;
  28216. }
  28217. static int test_wolfSSL_EVP_MD_size(void)
  28218. {
  28219. #ifdef OPENSSL_EXTRA
  28220. WOLFSSL_EVP_MD_CTX mdCtx;
  28221. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  28222. #ifdef WOLFSSL_SHA3
  28223. #ifndef WOLFSSL_NOSHA3_224
  28224. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28225. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  28226. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  28227. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  28228. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28229. #endif
  28230. #ifndef WOLFSSL_NOSHA3_256
  28231. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28232. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  28233. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  28234. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  28235. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28236. #endif
  28237. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28238. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  28239. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  28240. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  28241. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28242. #ifndef WOLFSSL_NOSHA3_512
  28243. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28244. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  28245. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  28246. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  28247. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28248. #endif
  28249. #endif /* WOLFSSL_SHA3 */
  28250. #ifndef NO_SHA256
  28251. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28252. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  28253. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  28254. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  28255. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  28256. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  28257. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28258. #endif
  28259. #ifndef NO_MD5
  28260. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28261. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  28262. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  28263. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  28264. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  28265. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  28266. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28267. #endif
  28268. #ifdef WOLFSSL_SHA224
  28269. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28270. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  28271. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  28272. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  28273. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  28274. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  28275. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28276. #endif
  28277. #ifdef WOLFSSL_SHA384
  28278. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28279. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  28280. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  28281. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  28282. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  28283. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  28284. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28285. #endif
  28286. #ifdef WOLFSSL_SHA512
  28287. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28288. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  28289. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  28290. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  28291. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  28292. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  28293. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28294. #endif
  28295. #ifndef NO_SHA
  28296. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28297. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  28298. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  28299. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  28300. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28301. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28302. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28303. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28304. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  28305. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  28306. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  28307. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  28308. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  28309. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28310. #endif
  28311. /* error case */
  28312. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28313. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  28314. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  28315. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  28316. /* Cleanup is valid on uninit'ed struct */
  28317. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28318. printf(resultFmt, passed);
  28319. #endif /* OPENSSL_EXTRA */
  28320. return 0;
  28321. }
  28322. static int test_wolfSSL_EVP_MD_pkey_type(void)
  28323. {
  28324. #ifdef OPENSSL_EXTRA
  28325. const WOLFSSL_EVP_MD* md;
  28326. printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
  28327. #ifndef NO_MD5
  28328. AssertNotNull(md = EVP_md5());
  28329. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  28330. #endif
  28331. #ifndef NO_SHA
  28332. AssertNotNull(md = EVP_sha1());
  28333. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  28334. #endif
  28335. #ifdef WOLFSSL_SHA224
  28336. AssertNotNull(md = EVP_sha224());
  28337. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  28338. #endif
  28339. AssertNotNull(md = EVP_sha256());
  28340. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  28341. #ifdef WOLFSSL_SHA384
  28342. AssertNotNull(md = EVP_sha384());
  28343. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  28344. #endif
  28345. #ifdef WOLFSSL_SHA512
  28346. AssertNotNull(md = EVP_sha512());
  28347. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  28348. #endif
  28349. printf(resultFmt, passed);
  28350. #endif
  28351. return 0;
  28352. }
  28353. #ifdef OPENSSL_EXTRA
  28354. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  28355. size_t testKeySz, const char* testData, size_t testDataSz,
  28356. const byte* testResult, size_t testResultSz)
  28357. {
  28358. unsigned char check[WC_MAX_DIGEST_SIZE];
  28359. size_t checkSz = -1;
  28360. WOLFSSL_EVP_PKEY* key;
  28361. WOLFSSL_EVP_MD_CTX mdCtx;
  28362. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  28363. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  28364. testKey, (int)testKeySz));
  28365. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28366. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28367. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28368. (unsigned int)testDataSz), 1);
  28369. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28370. AssertIntEQ((int)checkSz, (int)testResultSz);
  28371. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28372. AssertIntEQ((int)checkSz,(int)testResultSz);
  28373. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28374. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28375. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28376. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28377. (unsigned int)testDataSz), 1);
  28378. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28379. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28380. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28381. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  28382. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28383. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28384. AssertIntEQ((int)checkSz, (int)testResultSz);
  28385. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28386. AssertIntEQ((int)checkSz,(int)testResultSz);
  28387. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28388. (unsigned int)testDataSz - 4), 1);
  28389. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28390. AssertIntEQ((int)checkSz,(int)testResultSz);
  28391. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  28392. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28393. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  28394. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28395. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28396. (unsigned int)testDataSz - 4), 1);
  28397. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  28398. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28399. wolfSSL_EVP_PKEY_free(key);
  28400. }
  28401. #endif
  28402. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  28403. {
  28404. #ifdef OPENSSL_EXTRA
  28405. static const unsigned char testKey[] =
  28406. {
  28407. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28408. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  28409. 0x0b, 0x0b, 0x0b, 0x0b
  28410. };
  28411. static const char testData[] = "Hi There";
  28412. #ifdef WOLFSSL_SHA224
  28413. static const unsigned char testResultSha224[] =
  28414. {
  28415. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  28416. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  28417. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  28418. 0x53, 0x68, 0x4b, 0x22
  28419. };
  28420. #endif
  28421. #ifndef NO_SHA256
  28422. static const unsigned char testResultSha256[] =
  28423. {
  28424. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  28425. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  28426. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  28427. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  28428. };
  28429. #endif
  28430. #ifdef WOLFSSL_SHA384
  28431. static const unsigned char testResultSha384[] =
  28432. {
  28433. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  28434. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  28435. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  28436. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  28437. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  28438. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  28439. };
  28440. #endif
  28441. #ifdef WOLFSSL_SHA512
  28442. static const unsigned char testResultSha512[] =
  28443. {
  28444. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  28445. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  28446. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  28447. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  28448. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  28449. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  28450. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  28451. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  28452. };
  28453. #endif
  28454. #ifdef WOLFSSL_SHA3
  28455. #ifndef WOLFSSL_NOSHA3_224
  28456. static const unsigned char testResultSha3_224[] =
  28457. {
  28458. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  28459. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  28460. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  28461. 0xf3, 0xc8, 0x60, 0xf7
  28462. };
  28463. #endif
  28464. #ifndef WOLFSSL_NOSHA3_256
  28465. static const unsigned char testResultSha3_256[] =
  28466. {
  28467. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  28468. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  28469. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  28470. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  28471. };
  28472. #endif
  28473. #ifndef WOLFSSL_NOSHA3_384
  28474. static const unsigned char testResultSha3_384[] =
  28475. {
  28476. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  28477. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  28478. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  28479. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  28480. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  28481. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  28482. };
  28483. #endif
  28484. #ifndef WOLFSSL_NOSHA3_512
  28485. static const unsigned char testResultSha3_512[] =
  28486. {
  28487. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  28488. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  28489. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  28490. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  28491. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  28492. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  28493. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  28494. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  28495. };
  28496. #endif
  28497. #endif
  28498. #ifndef NO_SHA256
  28499. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  28500. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  28501. #endif
  28502. #ifdef WOLFSSL_SHA224
  28503. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  28504. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  28505. #endif
  28506. #ifdef WOLFSSL_SHA384
  28507. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  28508. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  28509. #endif
  28510. #ifdef WOLFSSL_SHA512
  28511. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  28512. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  28513. #endif
  28514. #ifdef WOLFSSL_SHA3
  28515. #ifndef WOLFSSL_NOSHA3_224
  28516. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  28517. testData, XSTRLEN(testData), testResultSha3_224,
  28518. sizeof(testResultSha3_224));
  28519. #endif
  28520. #ifndef WOLFSSL_NOSHA3_256
  28521. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  28522. testData, XSTRLEN(testData), testResultSha3_256,
  28523. sizeof(testResultSha3_256));
  28524. #endif
  28525. #ifndef WOLFSSL_NOSHA3_384
  28526. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  28527. testData, XSTRLEN(testData), testResultSha3_384,
  28528. sizeof(testResultSha3_384));
  28529. #endif
  28530. #ifndef WOLFSSL_NOSHA3_512
  28531. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  28532. testData, XSTRLEN(testData), testResultSha3_512,
  28533. sizeof(testResultSha3_512));
  28534. #endif
  28535. #endif
  28536. printf(resultFmt, passed);
  28537. #endif /* OPENSSL_EXTRA */
  28538. return 0;
  28539. }
  28540. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  28541. {
  28542. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  28543. defined(USE_CERT_BUFFERS_2048)
  28544. WOLFSSL_EVP_PKEY* privKey;
  28545. WOLFSSL_EVP_PKEY* pubKey;
  28546. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  28547. const char testData[] = "Hi There";
  28548. WOLFSSL_EVP_MD_CTX mdCtx;
  28549. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  28550. size_t checkSz = -1;
  28551. int sz = 2048 / 8;
  28552. const unsigned char* cp;
  28553. const unsigned char* p;
  28554. unsigned char check[2048/8];
  28555. size_t i;
  28556. int paddings[] = {
  28557. RSA_PKCS1_PADDING,
  28558. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  28559. RSA_PKCS1_PSS_PADDING,
  28560. #endif
  28561. };
  28562. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  28563. cp = client_key_der_2048;
  28564. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  28565. sizeof_client_key_der_2048)));
  28566. p = client_keypub_der_2048;
  28567. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28568. sizeof_client_keypub_der_2048)));
  28569. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28570. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  28571. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28572. NULL, privKey), 1);
  28573. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28574. (unsigned int)XSTRLEN(testData)), 1);
  28575. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28576. AssertIntEQ((int)checkSz, sz);
  28577. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28578. AssertIntEQ((int)checkSz,sz);
  28579. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  28580. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  28581. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  28582. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28583. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28584. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28585. NULL, pubKey), 1);
  28586. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28587. (unsigned int)XSTRLEN(testData)),
  28588. 1);
  28589. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28590. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28591. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28592. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28593. NULL, privKey), 1);
  28594. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28595. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28596. AssertIntEQ((int)checkSz, sz);
  28597. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28598. AssertIntEQ((int)checkSz, sz);
  28599. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28600. (unsigned int)XSTRLEN(testData) - 4), 1);
  28601. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28602. AssertIntEQ((int)checkSz, sz);
  28603. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28604. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28605. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28606. NULL, pubKey), 1);
  28607. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28608. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28609. (unsigned int)XSTRLEN(testData) - 4),
  28610. 1);
  28611. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28612. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28613. /* Check all signing padding types */
  28614. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  28615. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28616. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  28617. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  28618. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28619. paddings[i]), 1);
  28620. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28621. (unsigned int)XSTRLEN(testData)), 1);
  28622. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28623. AssertIntEQ((int)checkSz, sz);
  28624. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28625. AssertIntEQ((int)checkSz,sz);
  28626. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28627. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28628. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  28629. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  28630. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  28631. paddings[i]), 1);
  28632. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28633. (unsigned int)XSTRLEN(testData)), 1);
  28634. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28635. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28636. }
  28637. wolfSSL_EVP_PKEY_free(pubKey);
  28638. wolfSSL_EVP_PKEY_free(privKey);
  28639. printf(resultFmt, passed);
  28640. #endif
  28641. return 0;
  28642. }
  28643. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  28644. {
  28645. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28646. WOLFSSL_EVP_PKEY* privKey;
  28647. WOLFSSL_EVP_PKEY* pubKey;
  28648. const char testData[] = "Hi There";
  28649. WOLFSSL_EVP_MD_CTX mdCtx;
  28650. size_t checkSz = -1;
  28651. const unsigned char* cp;
  28652. const unsigned char* p;
  28653. unsigned char check[2048/8];
  28654. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  28655. cp = ecc_clikey_der_256;
  28656. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  28657. sizeof_ecc_clikey_der_256);
  28658. AssertNotNull(privKey);
  28659. p = ecc_clikeypub_der_256;
  28660. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  28661. sizeof_ecc_clikeypub_der_256)));
  28662. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28663. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28664. NULL, privKey), 1);
  28665. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  28666. (unsigned int)XSTRLEN(testData)), 1);
  28667. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28668. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28669. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28670. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28671. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28672. NULL, pubKey), 1);
  28673. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  28674. (unsigned int)XSTRLEN(testData)),
  28675. 1);
  28676. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28677. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28678. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28679. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28680. NULL, privKey), 1);
  28681. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  28682. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  28683. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28684. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  28685. (unsigned int)XSTRLEN(testData) - 4), 1);
  28686. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  28687. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28688. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  28689. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  28690. NULL, pubKey), 1);
  28691. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  28692. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  28693. (unsigned int)XSTRLEN(testData) - 4),
  28694. 1);
  28695. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  28696. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  28697. wolfSSL_EVP_PKEY_free(pubKey);
  28698. wolfSSL_EVP_PKEY_free(privKey);
  28699. printf(resultFmt, passed);
  28700. #endif
  28701. return 0;
  28702. }
  28703. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  28704. {
  28705. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28706. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  28707. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28708. char caFile[] = "./certs/client-ca.pem";
  28709. char clientFile[] = "./certs/client-cert.pem";
  28710. SSL_CTX* ctx;
  28711. X509* x509;
  28712. BIO *bio = NULL;
  28713. X509 *cert = NULL;
  28714. X509 *ca;
  28715. STACK_OF(X509) *chain = NULL;
  28716. STACK_OF(X509) *chain2 = NULL;
  28717. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  28718. #ifndef NO_WOLFSSL_SERVER
  28719. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28720. #else
  28721. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28722. #endif
  28723. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  28724. AssertNotNull(x509);
  28725. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  28726. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  28727. AssertNotNull(x509);
  28728. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  28729. /* additional test of getting EVP_PKEY key size from X509
  28730. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  28731. * allowed with user RSA */
  28732. {
  28733. EVP_PKEY* pkey;
  28734. #if defined(HAVE_ECC)
  28735. X509* ecX509;
  28736. #endif /* HAVE_ECC */
  28737. AssertNotNull(pkey = X509_get_pubkey(x509));
  28738. /* current RSA key is 2048 bit (256 bytes) */
  28739. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  28740. EVP_PKEY_free(pkey);
  28741. #if defined(HAVE_ECC)
  28742. #if defined(USE_CERT_BUFFERS_256)
  28743. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  28744. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  28745. SSL_FILETYPE_ASN1));
  28746. #else
  28747. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  28748. SSL_FILETYPE_PEM));
  28749. #endif
  28750. pkey = X509_get_pubkey(ecX509);
  28751. AssertNotNull(pkey);
  28752. /* current ECC key is 256 bit (32 bytes) */
  28753. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  28754. X509_free(ecX509);
  28755. EVP_PKEY_free(pkey);
  28756. #endif /* HAVE_ECC */
  28757. }
  28758. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  28759. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  28760. #ifdef WOLFSSL_ENCRYPTED_KEYS
  28761. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  28762. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28763. #endif
  28764. SSL_CTX_free(ctx);
  28765. #ifndef NO_WOLFSSL_SERVER
  28766. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28767. #else
  28768. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28769. #endif
  28770. /* Test haproxy use case */
  28771. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  28772. /* Read Certificate */
  28773. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  28774. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  28775. AssertNotNull(chain = sk_X509_new_null());
  28776. AssertIntEQ(sk_X509_push(chain, ca), 1);
  28777. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  28778. AssertNotNull(ca = sk_X509_shift(chain2));
  28779. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  28780. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  28781. BIO_free(bio);
  28782. X509_free(cert);
  28783. sk_X509_pop_free(chain, X509_free);
  28784. sk_X509_pop_free(chain2, X509_free);
  28785. SSL_CTX_free(ctx);
  28786. printf(resultFmt, passed);
  28787. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28788. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28789. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  28790. return 0;
  28791. }
  28792. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  28793. static int test_wolfSSL_ERR_peek_last_error_line(void)
  28794. {
  28795. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28796. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  28797. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  28798. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  28799. tcp_ready ready;
  28800. func_args client_args;
  28801. func_args server_args;
  28802. #ifndef SINGLE_THREADED
  28803. THREAD_TYPE serverThread;
  28804. #endif
  28805. callback_functions client_cb;
  28806. callback_functions server_cb;
  28807. int line = 0;
  28808. int flag = ERR_TXT_STRING;
  28809. const char* file = NULL;
  28810. const char* data = NULL;
  28811. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  28812. /* create a failed connection and inspect the error */
  28813. #ifdef WOLFSSL_TIRTOS
  28814. fdOpenSession(Task_self());
  28815. #endif
  28816. XMEMSET(&client_args, 0, sizeof(func_args));
  28817. XMEMSET(&server_args, 0, sizeof(func_args));
  28818. StartTCP();
  28819. InitTcpReady(&ready);
  28820. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  28821. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  28822. client_cb.method = wolfTLSv1_1_client_method;
  28823. server_cb.method = wolfTLSv1_2_server_method;
  28824. server_args.signal = &ready;
  28825. server_args.callbacks = &server_cb;
  28826. client_args.signal = &ready;
  28827. client_args.callbacks = &client_cb;
  28828. #ifndef SINGLE_THREADED
  28829. start_thread(test_server_nofail, &server_args, &serverThread);
  28830. wait_tcp_ready(&server_args);
  28831. test_client_nofail(&client_args, NULL);
  28832. join_thread(serverThread);
  28833. #endif
  28834. FreeTcpReady(&ready);
  28835. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  28836. AssertNotNull(data);
  28837. /* check clearing error state */
  28838. ERR_remove_state(0);
  28839. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  28840. ERR_peek_last_error_line(NULL, &line);
  28841. AssertIntEQ(line, 0);
  28842. ERR_peek_last_error_line(&file, NULL);
  28843. AssertNull(file);
  28844. /* retry connection to fill error queue */
  28845. XMEMSET(&client_args, 0, sizeof(func_args));
  28846. XMEMSET(&server_args, 0, sizeof(func_args));
  28847. StartTCP();
  28848. InitTcpReady(&ready);
  28849. client_cb.method = wolfTLSv1_1_client_method;
  28850. server_cb.method = wolfTLSv1_2_server_method;
  28851. server_args.signal = &ready;
  28852. server_args.callbacks = &server_cb;
  28853. client_args.signal = &ready;
  28854. client_args.callbacks = &client_cb;
  28855. start_thread(test_server_nofail, &server_args, &serverThread);
  28856. wait_tcp_ready(&server_args);
  28857. test_client_nofail(&client_args, NULL);
  28858. join_thread(serverThread);
  28859. FreeTcpReady(&ready);
  28860. /* check that error code was stored */
  28861. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  28862. ERR_peek_last_error_line(NULL, &line);
  28863. AssertIntNE(line, 0);
  28864. ERR_peek_last_error_line(&file, NULL);
  28865. AssertNotNull(file);
  28866. #ifdef WOLFSSL_TIRTOS
  28867. fdOpenSession(Task_self());
  28868. #endif
  28869. printf(resultFmt, passed);
  28870. printf("\nTesting error print out\n");
  28871. ERR_print_errors_fp(stdout);
  28872. printf("Done testing print out\n\n");
  28873. fflush(stdout);
  28874. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28875. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  28876. return 0;
  28877. }
  28878. #endif
  28879. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28880. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28881. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  28882. {
  28883. (void) ok;
  28884. (void) ctx;
  28885. printf("ENTER verify_cb\n");
  28886. return SSL_SUCCESS;
  28887. }
  28888. #endif
  28889. static int test_wolfSSL_X509_Name_canon(void)
  28890. {
  28891. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  28892. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  28893. defined(WOLFSSL_CERT_GEN) && \
  28894. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  28895. const long ex_hash1 = 0x0fdb2da4;
  28896. const long ex_hash2 = 0x9f3e8c9e;
  28897. X509_NAME *name = NULL;
  28898. X509 *x509 = NULL;
  28899. FILE* file = NULL;
  28900. unsigned long hash = 0;
  28901. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  28902. byte *pbuf = NULL;
  28903. word32 len = 0;
  28904. (void) ex_hash2;
  28905. printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
  28906. file = XFOPEN(caCertFile, "rb");
  28907. AssertNotNull(file);
  28908. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  28909. AssertNotNull(name = X509_get_issuer_name(x509));
  28910. /* When output buffer is NULL, should return necessary output buffer
  28911. * length.*/
  28912. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  28913. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  28914. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  28915. hash = (((unsigned long)digest[3] << 24) |
  28916. ((unsigned long)digest[2] << 16) |
  28917. ((unsigned long)digest[1] << 8) |
  28918. ((unsigned long)digest[0]));
  28919. AssertIntEQ(hash, ex_hash1);
  28920. XFCLOSE(file);
  28921. X509_free(x509);
  28922. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  28923. pbuf = NULL;
  28924. file = XFOPEN(cliCertFile, "rb");
  28925. AssertNotNull(file);
  28926. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  28927. AssertNotNull(name = X509_get_issuer_name(x509));
  28928. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  28929. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  28930. hash = (((unsigned long)digest[3] << 24) |
  28931. ((unsigned long)digest[2] << 16) |
  28932. ((unsigned long)digest[1] << 8) |
  28933. ((unsigned long)digest[0]));
  28934. AssertIntEQ(hash, ex_hash2);
  28935. XFCLOSE(file);
  28936. X509_free(x509);
  28937. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  28938. printf(resultFmt, passed);
  28939. #endif
  28940. return 0;
  28941. }
  28942. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  28943. {
  28944. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  28945. const int MAX_DIR = 4;
  28946. const char paths[][32] = {
  28947. "./certs/ed25519",
  28948. "./certs/ecc",
  28949. "./certs/crl",
  28950. "./certs/",
  28951. };
  28952. char CertCrl_path[MAX_FILENAME_SZ];
  28953. char *p;
  28954. X509_STORE* str;
  28955. X509_LOOKUP* lookup;
  28956. WOLFSSL_STACK* sk = NULL;
  28957. int len, total_len, i;
  28958. (void) sk;
  28959. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
  28960. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  28961. /* illegal string */
  28962. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28963. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28964. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  28965. SSL_FILETYPE_PEM,NULL), 0);
  28966. /* free store */
  28967. X509_STORE_free(str);
  28968. /* short folder string */
  28969. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28970. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28971. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  28972. SSL_FILETYPE_PEM,NULL), 1);
  28973. #if defined(WOLFSSL_INT_H)
  28974. /* only available when including internal.h */
  28975. AssertNotNull(sk = lookup->dirs->dir_entry);
  28976. #endif
  28977. /* free store */
  28978. X509_STORE_free(str);
  28979. /* typical function check */
  28980. p = &CertCrl_path[0];
  28981. total_len = 0;
  28982. for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  28983. len = (int)XSTRLEN((const char*)&paths[i]);
  28984. total_len += len;
  28985. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  28986. p += len;
  28987. if (i != 0) *(p++) = SEPARATOR_CHAR;
  28988. }
  28989. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  28990. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  28991. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  28992. SSL_FILETYPE_PEM,NULL), 1);
  28993. #if defined(WOLFSSL_INT_H)
  28994. /* only available when including internal.h */
  28995. AssertNotNull(sk = lookup->dirs->dir_entry);
  28996. #endif
  28997. X509_STORE_free(str);
  28998. printf(resultFmt, passed);
  28999. #endif
  29000. return 0;
  29001. }
  29002. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29003. {
  29004. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29005. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29006. defined(WOLFSSL_SIGNER_DER_CERT)
  29007. X509_STORE_CTX* ctx;
  29008. X509_STORE* str;
  29009. X509_LOOKUP* lookup;
  29010. X509* cert1;
  29011. X509* x509Ca;
  29012. X509* x509Svr;
  29013. X509* issuer;
  29014. WOLFSSL_STACK* sk = NULL;
  29015. X509_NAME* caName;
  29016. X509_NAME* issuerName;
  29017. FILE* file1 = NULL;
  29018. int i, cert_count, cmp;
  29019. char der[] = "certs/ca-cert.der";
  29020. #ifdef HAVE_CRL
  29021. char pem[][100] = {
  29022. "./certs/crl/crl.pem",
  29023. "./certs/crl/crl2.pem",
  29024. "./certs/crl/caEccCrl.pem",
  29025. "./certs/crl/eccCliCRL.pem",
  29026. "./certs/crl/eccSrvCRL.pem",
  29027. ""
  29028. };
  29029. #endif
  29030. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
  29031. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  29032. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  29033. fclose(file1);
  29034. AssertNotNull(ctx = X509_STORE_CTX_new());
  29035. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29036. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29037. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29038. SSL_FILETYPE_PEM,NULL), 1);
  29039. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29040. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29041. /* check if CA cert is loaded into the store */
  29042. for (i = 0; i < cert_count; i++) {
  29043. x509Ca = sk_X509_value(sk, i);
  29044. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29045. }
  29046. AssertNotNull((x509Svr =
  29047. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29048. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29049. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29050. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29051. AssertNotNull(issuer);
  29052. caName = X509_get_subject_name(x509Ca);
  29053. AssertNotNull(caName);
  29054. issuerName = X509_get_subject_name(issuer);
  29055. AssertNotNull(issuerName);
  29056. cmp = X509_NAME_cmp(caName, issuerName);
  29057. AssertIntEQ(cmp, 0);
  29058. /* load der format */
  29059. X509_free(issuer);
  29060. X509_STORE_CTX_free(ctx);
  29061. X509_STORE_free(str);
  29062. sk_X509_pop_free(sk, NULL);
  29063. X509_free(x509Svr);
  29064. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29065. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29066. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  29067. SSL_FILETYPE_ASN1,NULL), 1);
  29068. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29069. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29070. /* check if CA cert is loaded into the store */
  29071. for (i = 0; i < cert_count; i++) {
  29072. x509Ca = sk_X509_value(sk, i);
  29073. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29074. }
  29075. X509_STORE_free(str);
  29076. sk_X509_pop_free(sk, NULL);
  29077. X509_free(cert1);
  29078. #ifdef HAVE_CRL
  29079. AssertNotNull(str = wolfSSL_X509_STORE_new());
  29080. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29081. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29082. SSL_FILETYPE_PEM,NULL), 1);
  29083. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  29084. "certs/server-revoked-cert.pem",
  29085. SSL_FILETYPE_PEM,NULL), 1);
  29086. if (str) {
  29087. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  29088. WOLFSSL_FILETYPE_PEM), 1);
  29089. /* since store hasn't yet known the revoked cert*/
  29090. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29091. "certs/server-revoked-cert.pem",
  29092. WOLFSSL_FILETYPE_PEM), 1);
  29093. }
  29094. for (i = 0; pem[i][0] != '\0'; i++)
  29095. {
  29096. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  29097. SSL_FILETYPE_PEM, NULL), 1);
  29098. }
  29099. if (str) {
  29100. /* since store knows crl list */
  29101. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29102. "certs/server-revoked-cert.pem",
  29103. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  29104. }
  29105. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  29106. X509_STORE_free(str);
  29107. #endif
  29108. printf(resultFmt, passed);
  29109. #endif
  29110. return 0;
  29111. }
  29112. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  29113. {
  29114. #if defined(OPENSSL_EXTRA)
  29115. printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
  29116. X509_STORE_CTX_cleanup(NULL);
  29117. X509_STORE_CTX_trusted_stack(NULL, NULL);
  29118. AssertTrue(1); /* to confirm previous call gives no harm */
  29119. printf(resultFmt, passed);
  29120. #endif
  29121. return 0;
  29122. }
  29123. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  29124. {
  29125. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29126. #ifdef WOLFSSL_SIGNER_DER_CERT
  29127. int cmp;
  29128. #endif
  29129. X509_STORE_CTX* ctx;
  29130. X509_STORE* str;
  29131. X509* x509Ca;
  29132. X509* x509Svr;
  29133. X509* issuer;
  29134. X509_NAME* caName;
  29135. X509_NAME* issuerName;
  29136. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  29137. AssertNotNull(ctx = X509_STORE_CTX_new());
  29138. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29139. AssertNotNull((x509Ca =
  29140. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  29141. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  29142. AssertNotNull((x509Svr =
  29143. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29144. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29145. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29146. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29147. AssertNotNull(issuer);
  29148. caName = X509_get_subject_name(x509Ca);
  29149. AssertNotNull(caName);
  29150. issuerName = X509_get_subject_name(issuer);
  29151. AssertNotNull(issuerName);
  29152. #ifdef WOLFSSL_SIGNER_DER_CERT
  29153. cmp = X509_NAME_cmp(caName, issuerName);
  29154. AssertIntEQ(cmp, 0);
  29155. #endif
  29156. X509_free(issuer);
  29157. X509_STORE_CTX_free(ctx);
  29158. X509_free(x509Svr);
  29159. X509_STORE_free(str);
  29160. X509_free(x509Ca);
  29161. printf(resultFmt, passed);
  29162. #endif
  29163. return 0;
  29164. }
  29165. static int test_wolfSSL_PKCS7_certs(void)
  29166. {
  29167. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  29168. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  29169. STACK_OF(X509)* sk = NULL;
  29170. STACK_OF(X509_INFO)* info_sk = NULL;
  29171. PKCS7 *p7 = NULL;
  29172. BIO* bio;
  29173. const byte* p = NULL;
  29174. int buflen = 0;
  29175. int i;
  29176. printf(testingFmt, "wolfSSL_PKCS7_certs()");
  29177. /* Test twice. Once with d2i and once without to test
  29178. * that everything is free'd correctly. */
  29179. for (i = 0; i < 2; i++) {
  29180. AssertNotNull(p7 = PKCS7_new());
  29181. p7->version = 1;
  29182. p7->hashOID = SHAh;
  29183. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29184. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29185. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29186. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  29187. AssertNotNull(sk = sk_X509_new_null());
  29188. while (sk_X509_INFO_num(info_sk)) {
  29189. X509_INFO* info;
  29190. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  29191. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  29192. info->x509 = NULL;
  29193. X509_INFO_free(info);
  29194. }
  29195. sk_X509_INFO_free(info_sk);
  29196. BIO_free(bio);
  29197. bio = BIO_new(BIO_s_mem());
  29198. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  29199. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  29200. if (i == 0) {
  29201. PKCS7_free(p7);
  29202. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  29203. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  29204. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  29205. /* PKCS7_free free's the certs */
  29206. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  29207. }
  29208. BIO_free(bio);
  29209. PKCS7_free(p7);
  29210. }
  29211. printf(resultFmt, passed);
  29212. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29213. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  29214. return 0;
  29215. }
  29216. static int test_wolfSSL_X509_STORE_CTX(void)
  29217. {
  29218. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29219. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29220. X509_STORE_CTX* ctx;
  29221. X509_STORE* str;
  29222. X509* x509;
  29223. #ifdef OPENSSL_ALL
  29224. X509* x5092;
  29225. STACK_OF(X509) *sk, *sk2, *sk3;
  29226. #endif
  29227. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  29228. AssertNotNull(ctx = X509_STORE_CTX_new());
  29229. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29230. AssertNotNull((x509 =
  29231. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29232. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  29233. #ifdef OPENSSL_ALL
  29234. /* sk_X509_new only in OPENSSL_ALL */
  29235. sk = sk_X509_new();
  29236. AssertNotNull(sk);
  29237. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  29238. #else
  29239. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  29240. #endif
  29241. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  29242. X509_STORE_CTX_set_error(ctx, -5);
  29243. X509_STORE_CTX_set_error(NULL, -5);
  29244. X509_STORE_CTX_free(ctx);
  29245. #ifdef OPENSSL_ALL
  29246. sk_X509_pop_free(sk, NULL);
  29247. #endif
  29248. X509_STORE_free(str);
  29249. X509_free(x509);
  29250. AssertNotNull(ctx = X509_STORE_CTX_new());
  29251. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  29252. X509_STORE_CTX_free(ctx);
  29253. #ifdef OPENSSL_ALL
  29254. /* test X509_STORE_CTX_get(1)_chain */
  29255. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  29256. SSL_FILETYPE_PEM)));
  29257. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  29258. SSL_FILETYPE_PEM)));
  29259. AssertNotNull((sk = sk_X509_new()));
  29260. AssertIntEQ(sk_X509_push(sk, x509), 1);
  29261. AssertNotNull((str = X509_STORE_new()));
  29262. AssertNotNull((ctx = X509_STORE_CTX_new()));
  29263. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  29264. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  29265. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  29266. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  29267. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  29268. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  29269. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  29270. X509_STORE_CTX_free(ctx);
  29271. X509_STORE_free(str);
  29272. /* CTX certs not freed yet */
  29273. X509_free(x5092);
  29274. sk_X509_pop_free(sk, NULL);
  29275. /* sk3 is dup so free here */
  29276. sk_X509_pop_free(sk3, NULL);
  29277. #endif
  29278. /* test X509_STORE_CTX_get/set_ex_data */
  29279. {
  29280. int i = 0, tmpData = 5;
  29281. void* tmpDataRet;
  29282. AssertNotNull(ctx = X509_STORE_CTX_new());
  29283. #ifdef HAVE_EX_DATA
  29284. for (i = 0; i < MAX_EX_DATA; i++) {
  29285. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29286. WOLFSSL_SUCCESS);
  29287. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29288. AssertNotNull(tmpDataRet);
  29289. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  29290. }
  29291. #else
  29292. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  29293. WOLFSSL_FAILURE);
  29294. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  29295. AssertNull(tmpDataRet);
  29296. #endif
  29297. X509_STORE_CTX_free(ctx);
  29298. }
  29299. /* test X509_STORE_get/set_ex_data */
  29300. {
  29301. int i = 0, tmpData = 99;
  29302. void* tmpDataRet;
  29303. AssertNotNull(str = X509_STORE_new());
  29304. #ifdef HAVE_EX_DATA
  29305. for (i = 0; i < MAX_EX_DATA; i++) {
  29306. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29307. WOLFSSL_SUCCESS);
  29308. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29309. AssertNotNull(tmpDataRet);
  29310. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  29311. }
  29312. #else
  29313. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  29314. WOLFSSL_FAILURE);
  29315. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  29316. AssertNull(tmpDataRet);
  29317. #endif
  29318. X509_STORE_free(str);
  29319. }
  29320. printf(resultFmt, passed);
  29321. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29322. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29323. return 0;
  29324. }
  29325. static int test_wolfSSL_X509_STORE_set_flags(void)
  29326. {
  29327. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29328. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29329. X509_STORE* store;
  29330. X509* x509;
  29331. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  29332. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  29333. AssertNotNull((x509 =
  29334. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  29335. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  29336. #ifdef HAVE_CRL
  29337. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  29338. #else
  29339. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  29340. NOT_COMPILED_IN);
  29341. #endif
  29342. wolfSSL_X509_free(x509);
  29343. wolfSSL_X509_STORE_free(store);
  29344. printf(resultFmt, passed);
  29345. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29346. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29347. return 0;
  29348. }
  29349. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  29350. {
  29351. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  29352. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29353. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  29354. WOLFSSL_X509_STORE* store;
  29355. WOLFSSL_X509_LOOKUP* lookup;
  29356. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  29357. AssertNotNull(store = wolfSSL_X509_STORE_new());
  29358. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  29359. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  29360. X509_FILETYPE_PEM), 1);
  29361. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  29362. X509_FILETYPE_PEM), 1);
  29363. if (store) {
  29364. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  29365. WOLFSSL_FILETYPE_PEM), 1);
  29366. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29367. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  29368. }
  29369. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  29370. X509_FILETYPE_PEM), 1);
  29371. if (store) {
  29372. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  29373. WOLFSSL_FILETYPE_PEM), 1);
  29374. }
  29375. wolfSSL_X509_STORE_free(store);
  29376. printf(resultFmt, passed);
  29377. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  29378. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  29379. return 0;
  29380. }
  29381. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  29382. {
  29383. #if defined(OPENSSL_EXTRA)
  29384. WOLFSSL_X509_STORE_CTX* ctx;
  29385. time_t c_time;
  29386. printf(testingFmt, "wolfSSL_X509_set_time()");
  29387. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  29388. c_time = 365*24*60*60;
  29389. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  29390. AssertTrue(
  29391. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  29392. AssertTrue(ctx->param->check_time == c_time);
  29393. wolfSSL_X509_STORE_CTX_free(ctx);
  29394. printf(resultFmt, passed);
  29395. #endif /* OPENSSL_EXTRA */
  29396. return 0;
  29397. }
  29398. static int test_wolfSSL_CTX_get0_set1_param(void)
  29399. {
  29400. #if defined(OPENSSL_EXTRA)
  29401. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29402. int ret;
  29403. SSL_CTX* ctx;
  29404. WOLFSSL_X509_VERIFY_PARAM* pParam;
  29405. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  29406. char testIPv4[] = "127.0.0.1";
  29407. char testhostName[] = "foo.hoge.com";
  29408. printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
  29409. #ifndef NO_WOLFSSL_SERVER
  29410. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29411. #else
  29412. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29413. #endif
  29414. AssertNull(SSL_CTX_get0_param(NULL));
  29415. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  29416. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  29417. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  29418. AssertNotNull(pvpm);
  29419. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29420. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  29421. (int)XSTRLEN(testhostName));
  29422. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  29423. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  29424. ret = SSL_CTX_set1_param(ctx, pvpm);
  29425. AssertIntEQ(1, ret);
  29426. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  29427. (int)XSTRLEN(testhostName)));
  29428. AssertIntEQ(0x01, pParam->hostFlags);
  29429. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  29430. /* test for incorrect patameter */
  29431. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  29432. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  29433. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  29434. SSL_CTX_free(ctx);
  29435. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  29436. printf(resultFmt, passed);
  29437. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29438. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29439. return 0;
  29440. }
  29441. static int test_wolfSSL_get0_param(void)
  29442. {
  29443. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29444. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29445. SSL_CTX* ctx;
  29446. SSL* ssl;
  29447. WOLFSSL_X509_VERIFY_PARAM* pParam;
  29448. printf(testingFmt, "wolfSSL_get0_param()");
  29449. #ifndef NO_WOLFSSL_SERVER
  29450. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29451. #else
  29452. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29453. #endif
  29454. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29455. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29456. AssertNotNull(ssl = SSL_new(ctx));
  29457. pParam = SSL_get0_param(ssl);
  29458. (void)pParam;
  29459. SSL_free(ssl);
  29460. SSL_CTX_free(ctx);
  29461. printf(resultFmt, passed);
  29462. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29463. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  29464. return 0;
  29465. }
  29466. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  29467. {
  29468. #if defined(OPENSSL_EXTRA)
  29469. const char host[] = "www.example.com";
  29470. WOLFSSL_X509_VERIFY_PARAM* pParam;
  29471. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  29472. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  29473. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  29474. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  29475. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29476. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  29477. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29478. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  29479. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  29480. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29481. printf(resultFmt, passed);
  29482. #endif /* OPENSSL_EXTRA */
  29483. return 0;
  29484. }
  29485. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  29486. {
  29487. #if defined(OPENSSL_EXTRA)
  29488. unsigned char buf[16] = {0};
  29489. WOLFSSL_X509_VERIFY_PARAM* param;
  29490. printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
  29491. AssertNotNull(param = X509_VERIFY_PARAM_new());
  29492. /* test 127.0.0.1 */
  29493. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  29494. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  29495. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  29496. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  29497. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29498. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  29499. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  29500. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  29501. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29502. AssertIntEQ(XSTRNCMP(param->ipasc,
  29503. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  29504. /* test 2001:db8:: */
  29505. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29506. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29507. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29508. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  29509. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29510. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  29511. /* test ::1234:5678 */
  29512. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  29513. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29514. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29515. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29516. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29517. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  29518. /* test 2001:db8::1234:5678 */
  29519. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29520. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  29521. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  29522. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  29523. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29524. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  29525. sizeof(param->ipasc)), 0);
  29526. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  29527. /* 2001:db8:1::ab9:c0a8:102 */
  29528. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  29529. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  29530. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  29531. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  29532. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  29533. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  29534. sizeof(param->ipasc)), 0);
  29535. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  29536. printf(resultFmt, passed);
  29537. #endif /* OPENSSL_EXTRA */
  29538. return 0;
  29539. }
  29540. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  29541. {
  29542. #if defined(OPENSSL_EXTRA)
  29543. X509_STORE* store;
  29544. X509_STORE_CTX* ctx;
  29545. X509_STORE_CTX* ctx_no_init;
  29546. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  29547. AssertNotNull((store = X509_STORE_new()));
  29548. AssertNotNull(ctx = X509_STORE_CTX_new());
  29549. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  29550. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  29551. AssertNull(X509_STORE_CTX_get0_store(NULL));
  29552. /* should return NULL if ctx has not bee initialized */
  29553. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  29554. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  29555. wolfSSL_X509_STORE_CTX_free(ctx);
  29556. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  29557. X509_STORE_free(store);
  29558. printf(resultFmt, passed);
  29559. #endif /* OPENSSL_EXTRA */
  29560. return 0;
  29561. }
  29562. static int test_wolfSSL_CTX_set_client_CA_list(void)
  29563. {
  29564. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29565. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  29566. WOLFSSL_CTX* ctx;
  29567. WOLFSSL* ssl;
  29568. X509_NAME* name = NULL;
  29569. STACK_OF(X509_NAME)* names = NULL;
  29570. STACK_OF(X509_NAME)* ca_list = NULL;
  29571. int i, names_len;
  29572. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  29573. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  29574. /* Send two X501 names in cert request */
  29575. names = SSL_load_client_CA_file(cliCertFile);
  29576. AssertNotNull(names);
  29577. ca_list = SSL_load_client_CA_file(caCertFile);
  29578. AssertNotNull(ca_list);
  29579. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  29580. SSL_CTX_set_client_CA_list(ctx, names);
  29581. /* This should only free the stack structure */
  29582. sk_X509_NAME_free(ca_list);
  29583. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  29584. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29585. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29586. for (i=0; i<names_len; i++) {
  29587. AssertNotNull(name = sk_X509_NAME_value(names, i));
  29588. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  29589. }
  29590. /* Needed to be able to create ssl object */
  29591. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  29592. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  29593. AssertNotNull(ssl = wolfSSL_new(ctx));
  29594. /* load again as old names are responsibility of ctx to free*/
  29595. names = SSL_load_client_CA_file(cliCertFile);
  29596. AssertNotNull(names);
  29597. SSL_set_client_CA_list(ssl, names);
  29598. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  29599. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  29600. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  29601. for (i=0; i<names_len; i++) {
  29602. AssertNotNull(name = sk_X509_NAME_value(names, i));
  29603. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  29604. }
  29605. printf(resultFmt, passed);
  29606. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  29607. {
  29608. tcp_ready ready;
  29609. func_args server_args;
  29610. callback_functions server_cb;
  29611. THREAD_TYPE serverThread;
  29612. WOLFSSL* ssl_client;
  29613. WOLFSSL_CTX* ctx_client;
  29614. SOCKET_T sockfd = 0;
  29615. printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
  29616. StartTCP();
  29617. InitTcpReady(&ready);
  29618. XMEMSET(&server_args, 0, sizeof(func_args));
  29619. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29620. server_args.signal = &ready;
  29621. server_args.callbacks = &server_cb;
  29622. /* we are responsible for free'ing WOLFSSL_CTX */
  29623. server_cb.ctx = ctx;
  29624. server_cb.isSharedCtx = 1;
  29625. AssertIntEQ(WOLFSSL_SUCCESS,
  29626. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  29627. start_thread(test_server_nofail, &server_args, &serverThread);
  29628. wait_tcp_ready(&server_args);
  29629. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29630. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  29631. AssertIntEQ(WOLFSSL_SUCCESS,
  29632. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  29633. AssertIntEQ(WOLFSSL_SUCCESS,
  29634. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  29635. AssertIntEQ(WOLFSSL_SUCCESS,
  29636. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  29637. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  29638. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  29639. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  29640. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  29641. /* We are expecting two cert names to be sent */
  29642. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  29643. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  29644. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  29645. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  29646. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  29647. }
  29648. wolfSSL_shutdown(ssl_client);
  29649. wolfSSL_free(ssl_client);
  29650. wolfSSL_CTX_free(ctx_client);
  29651. join_thread(serverThread);
  29652. FreeTcpReady(&ready);
  29653. printf(resultFmt, passed);
  29654. }
  29655. #endif
  29656. wolfSSL_free(ssl);
  29657. wolfSSL_CTX_free(ctx);
  29658. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
  29659. return 0;
  29660. }
  29661. static int test_wolfSSL_CTX_add_client_CA(void)
  29662. {
  29663. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  29664. !defined(NO_WOLFSSL_CLIENT)
  29665. WOLFSSL_CTX* ctx;
  29666. WOLFSSL_X509* x509;
  29667. WOLFSSL_X509* x509_a;
  29668. STACK_OF(X509_NAME)* ca_list;
  29669. int ret = 0;
  29670. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  29671. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29672. /* Add client cert */
  29673. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  29674. AssertNotNull(x509);
  29675. ret = SSL_CTX_add_client_CA(ctx, x509);
  29676. AssertIntEQ(ret, SSL_SUCCESS);
  29677. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  29678. /* Add another client cert */
  29679. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  29680. SSL_FILETYPE_PEM));
  29681. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  29682. /* test for incorrect parameter */
  29683. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  29684. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  29685. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  29686. X509_free(x509);
  29687. X509_free(x509_a);
  29688. SSL_CTX_free(ctx);
  29689. printf(resultFmt, passed);
  29690. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  29691. return 0;
  29692. }
  29693. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29694. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  29695. {
  29696. callback_functions* callbacks = ((func_args*)args)->callbacks;
  29697. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  29698. WOLFSSL* ssl = NULL;
  29699. SOCKET_T sfd = 0;
  29700. SOCKET_T cfd = 0;
  29701. word16 port;
  29702. char msg[] = "I hear you fa shizzle!";
  29703. int len = (int) XSTRLEN(msg);
  29704. char input[1024];
  29705. int idx;
  29706. int ret, err = 0;
  29707. #ifdef WOLFSSL_TIRTOS
  29708. fdOpenSession(Task_self());
  29709. #endif
  29710. ((func_args*)args)->return_code = TEST_FAIL;
  29711. port = ((func_args*)args)->signal->port;
  29712. AssertIntEQ(WOLFSSL_SUCCESS,
  29713. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  29714. AssertIntEQ(WOLFSSL_SUCCESS,
  29715. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  29716. WOLFSSL_FILETYPE_PEM));
  29717. AssertIntEQ(WOLFSSL_SUCCESS,
  29718. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  29719. WOLFSSL_FILETYPE_PEM));
  29720. if (callbacks->ctx_ready)
  29721. callbacks->ctx_ready(ctx);
  29722. ssl = wolfSSL_new(ctx);
  29723. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  29724. CloseSocket(sfd);
  29725. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  29726. if (callbacks->ssl_ready)
  29727. callbacks->ssl_ready(ssl);
  29728. do {
  29729. err = 0; /* Reset error */
  29730. ret = wolfSSL_accept(ssl);
  29731. if (ret != WOLFSSL_SUCCESS) {
  29732. err = wolfSSL_get_error(ssl, 0);
  29733. }
  29734. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  29735. if (ret != WOLFSSL_SUCCESS) {
  29736. char buff[WOLFSSL_MAX_ERROR_SZ];
  29737. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  29738. }
  29739. else {
  29740. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  29741. input[idx] = 0;
  29742. printf("Client message: %s\n", input);
  29743. }
  29744. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  29745. #ifdef WOLFSSL_TIRTOS
  29746. Task_yield();
  29747. #endif
  29748. ((func_args*)args)->return_code = TEST_SUCCESS;
  29749. }
  29750. if (callbacks->on_result)
  29751. callbacks->on_result(ssl);
  29752. wolfSSL_shutdown(ssl);
  29753. wolfSSL_free(ssl);
  29754. wolfSSL_CTX_free(ctx);
  29755. CloseSocket(cfd);
  29756. #ifdef WOLFSSL_TIRTOS
  29757. fdCloseSession(Task_self());
  29758. #endif
  29759. #ifndef WOLFSSL_TIRTOS
  29760. return 0;
  29761. #endif
  29762. }
  29763. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  29764. {
  29765. AssertNotNull(ssl);
  29766. AssertNotNull(line);
  29767. XFILE fp;
  29768. const byte lf = '\n';
  29769. fp = XFOPEN("./MyKeyLog.txt", "a");
  29770. XFWRITE( line, 1, strlen(line),fp);
  29771. XFWRITE( (void*)&lf,1,1,fp);
  29772. XFCLOSE(fp);
  29773. }
  29774. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29775. static int test_wolfSSL_CTX_set_keylog_callback(void)
  29776. {
  29777. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  29778. !defined(NO_WOLFSSL_CLIENT)
  29779. SSL_CTX* ctx;
  29780. printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
  29781. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29782. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  29783. SSL_CTX_free(ctx);
  29784. SSL_CTX_set_keylog_callback(NULL, NULL);
  29785. printf(resultFmt, passed);
  29786. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  29787. return 0;
  29788. }
  29789. static int test_wolfSSL_CTX_get_keylog_callback(void)
  29790. {
  29791. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  29792. !defined(NO_WOLFSSL_CLIENT)
  29793. SSL_CTX* ctx;
  29794. printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
  29795. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29796. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  29797. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  29798. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  29799. SSL_CTX_set_keylog_callback(ctx, NULL );
  29800. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  29801. SSL_CTX_free(ctx);
  29802. printf(resultFmt, passed);
  29803. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  29804. return 0;
  29805. }
  29806. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  29807. {
  29808. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  29809. /* This test is intended for checking whether keylog callback is called
  29810. * in client during TLS handshake between the client and a server.
  29811. */
  29812. tcp_ready ready;
  29813. func_args client_args;
  29814. func_args server_args;
  29815. THREAD_TYPE serverThread;
  29816. callback_functions server_cbf;
  29817. callback_functions client_cbf;
  29818. SOCKET_T sockfd = 0;
  29819. WOLFSSL_CTX* ctx;
  29820. WOLFSSL* ssl;
  29821. XFILE fp;
  29822. char msg[64] = "hello wolfssl!";
  29823. char reply[1024];
  29824. int msgSz = (int)XSTRLEN(msg);
  29825. printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
  29826. #ifdef WOLFSSL_TIRTOS
  29827. fdOpenSession(Task_self());
  29828. #endif
  29829. InitTcpReady(&ready);
  29830. ready.port = 22222;
  29831. XMEMSET(&client_args, 0, sizeof(func_args));
  29832. XMEMSET(&server_args, 0, sizeof(func_args));
  29833. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29834. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29835. server_cbf.method = wolfTLSv1_2_server_method;
  29836. server_args.callbacks = &server_cbf;
  29837. server_args.signal = &ready;
  29838. /* clean up keylog file */
  29839. fp = XFOPEN("./MyKeyLog.txt", "w");
  29840. XFCLOSE(fp);
  29841. /* start server task */
  29842. start_thread(server_task, &server_args, &serverThread);
  29843. wait_tcp_ready(&server_args);
  29844. /* run as a TLS1.2 client */
  29845. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  29846. AssertIntEQ(WOLFSSL_SUCCESS,
  29847. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  29848. AssertIntEQ(WOLFSSL_SUCCESS,
  29849. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  29850. AssertIntEQ(WOLFSSL_SUCCESS,
  29851. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29852. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29853. /* set keylog callback */
  29854. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  29855. /* get connected the server task */
  29856. AssertNotNull(ssl = wolfSSL_new(ctx));
  29857. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  29858. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  29859. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  29860. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  29861. wolfSSL_shutdown(ssl);
  29862. wolfSSL_free(ssl);
  29863. wolfSSL_CTX_free(ctx);
  29864. CloseSocket(sockfd);
  29865. join_thread(serverThread);
  29866. FreeTcpReady(&ready);
  29867. #ifdef WOLFSSL_TIRTOS
  29868. fdOpenSession(Task_self());
  29869. #endif
  29870. /* check if the keylog file exists */
  29871. char buff[300] = {0};
  29872. int found = 0;
  29873. fp = XFOPEN("./MyKeyLog.txt", "r");
  29874. AssertNotNull(fp);
  29875. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  29876. if(0 == strncmp(buff,"CLIENT_RANDOM ",
  29877. sizeof("CLIENT_RANDOM ")-1)) {
  29878. found = 1;
  29879. break;
  29880. }
  29881. }
  29882. XFCLOSE(fp);
  29883. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  29884. AssertNotNull( found );
  29885. printf(resultFmt, passed);
  29886. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  29887. return 0;
  29888. }
  29889. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  29890. {
  29891. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  29892. defined(HAVE_SECRET_CALLBACK)
  29893. /* This test is intended for checking whether keylog callback is called
  29894. * in client during TLS handshake between the client and a server.
  29895. */
  29896. tcp_ready ready;
  29897. func_args client_args;
  29898. func_args server_args;
  29899. THREAD_TYPE serverThread;
  29900. callback_functions server_cbf;
  29901. callback_functions client_cbf;
  29902. SOCKET_T sockfd = 0;
  29903. WOLFSSL_CTX* ctx;
  29904. WOLFSSL* ssl;
  29905. XFILE fp;
  29906. char msg[64] = "hello wolfssl!";
  29907. char reply[1024];
  29908. int msgSz = (int)XSTRLEN(msg);
  29909. printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
  29910. #ifdef WOLFSSL_TIRTOS
  29911. fdOpenSession(Task_self());
  29912. #endif
  29913. InitTcpReady(&ready);
  29914. ready.port = 22222;
  29915. XMEMSET(&client_args, 0, sizeof(func_args));
  29916. XMEMSET(&server_args, 0, sizeof(func_args));
  29917. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  29918. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  29919. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  29920. server_args.callbacks = &server_cbf;
  29921. server_args.signal = &ready;
  29922. /* clean up keylog file */
  29923. fp = XFOPEN("./MyKeyLog.txt", "w");
  29924. XFCLOSE(fp);
  29925. /* start server task */
  29926. start_thread(server_task, &server_args, &serverThread);
  29927. wait_tcp_ready(&server_args);
  29928. /* run as a TLS1.3 client */
  29929. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  29930. AssertIntEQ(WOLFSSL_SUCCESS,
  29931. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  29932. AssertIntEQ(WOLFSSL_SUCCESS,
  29933. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  29934. AssertIntEQ(WOLFSSL_SUCCESS,
  29935. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  29936. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  29937. /* set keylog callback */
  29938. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  29939. /* get connected the server task */
  29940. AssertNotNull(ssl = wolfSSL_new(ctx));
  29941. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  29942. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  29943. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  29944. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  29945. wolfSSL_free(ssl);
  29946. wolfSSL_CTX_free(ctx);
  29947. join_thread(serverThread);
  29948. FreeTcpReady(&ready);
  29949. #ifdef WOLFSSL_TIRTOS
  29950. fdOpenSession(Task_self());
  29951. #endif
  29952. /* check if the keylog file exists */
  29953. {
  29954. char buff[300] = {0};
  29955. int found[4] = {0};
  29956. int numfnd = 0;
  29957. int i;
  29958. fp = XFOPEN("./MyKeyLog.txt", "r");
  29959. AssertNotNull(fp);
  29960. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  29961. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  29962. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  29963. found[0] = 1;
  29964. continue;
  29965. }
  29966. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  29967. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  29968. found[1] = 1;
  29969. continue;
  29970. }
  29971. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  29972. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  29973. found[2] = 1;
  29974. continue;
  29975. }
  29976. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  29977. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  29978. found[3] = 1;
  29979. continue;
  29980. }
  29981. }
  29982. XFCLOSE(fp);
  29983. for (i = 0; i < 4; i++) {
  29984. if( found[i] != 0)
  29985. numfnd++;
  29986. }
  29987. AssertIntEQ(numfnd, 4);
  29988. }
  29989. printf(resultFmt, passed);
  29990. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  29991. return 0;
  29992. }
  29993. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  29994. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29995. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  29996. static void post_auth_version_cb(WOLFSSL* ssl)
  29997. {
  29998. /* do handshake and then test version error */
  29999. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30000. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30001. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  30002. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  30003. /* check was added to error queue */
  30004. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  30005. /* check the string matches expected string */
  30006. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  30007. "WRONG_SSL_VERSION");
  30008. #endif
  30009. }
  30010. static void post_auth_cb(WOLFSSL* ssl)
  30011. {
  30012. WOLFSSL_X509* x509;
  30013. /* do handshake and then test version error */
  30014. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30015. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  30016. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  30017. wolfSSL_X509_free(x509);
  30018. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  30019. }
  30020. static void set_post_auth_cb(WOLFSSL* ssl)
  30021. {
  30022. if (!wolfSSL_is_server(ssl)) {
  30023. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  30024. }
  30025. else {
  30026. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  30027. }
  30028. }
  30029. #endif
  30030. static int test_wolfSSL_Tls13_postauth(void)
  30031. {
  30032. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30033. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30034. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30035. tcp_ready ready;
  30036. func_args client_args;
  30037. func_args server_args;
  30038. callback_functions server_cbf;
  30039. callback_functions client_cbf;
  30040. THREAD_TYPE serverThread;
  30041. printf(testingFmt, "wolfSSL_Tls13_postauth()");
  30042. XMEMSET(&client_args, 0, sizeof(func_args));
  30043. XMEMSET(&server_args, 0, sizeof(func_args));
  30044. StartTCP();
  30045. InitTcpReady(&ready);
  30046. #if defined(USE_WINDOWS_API)
  30047. /* use RNG to get random port if using windows */
  30048. ready.port = GetRandomPort();
  30049. #endif
  30050. server_args.signal = &ready;
  30051. client_args.signal = &ready;
  30052. /* test version failure doing post auth with TLS 1.2 connection */
  30053. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30054. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30055. server_cbf.method = wolfTLSv1_2_server_method;
  30056. server_cbf.ssl_ready = set_post_auth_cb;
  30057. client_cbf.ssl_ready = set_post_auth_cb;
  30058. server_cbf.on_result = post_auth_version_cb;
  30059. server_args.callbacks = &server_cbf;
  30060. client_args.callbacks = &client_cbf;
  30061. start_thread(test_server_nofail, &server_args, &serverThread);
  30062. wait_tcp_ready(&server_args);
  30063. test_client_nofail(&client_args, NULL);
  30064. join_thread(serverThread);
  30065. /* tests on post auth with TLS 1.3 */
  30066. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30067. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30068. server_cbf.method = wolfTLSv1_3_server_method;
  30069. server_cbf.ssl_ready = set_post_auth_cb;
  30070. client_cbf.ssl_ready = set_post_auth_cb;
  30071. server_cbf.on_result = post_auth_cb;
  30072. server_args.callbacks = &server_cbf;
  30073. client_args.callbacks = &client_cbf;
  30074. start_thread(test_server_nofail, &server_args, &serverThread);
  30075. wait_tcp_ready(&server_args);
  30076. test_client_nofail(&client_args, NULL);
  30077. join_thread(serverThread);
  30078. FreeTcpReady(&ready);
  30079. printf(resultFmt, passed);
  30080. #endif
  30081. return 0;
  30082. }
  30083. static int test_wolfSSL_X509_NID(void)
  30084. {
  30085. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30086. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  30087. int sigType;
  30088. int nameSz;
  30089. X509* cert;
  30090. EVP_PKEY* pubKeyTmp;
  30091. X509_NAME* name;
  30092. char commonName[80];
  30093. char countryName[80];
  30094. char localityName[80];
  30095. char stateName[80];
  30096. char orgName[80];
  30097. char orgUnit[80];
  30098. printf(testingFmt, "wolfSSL_X509_NID()");
  30099. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  30100. /* convert cert from DER to internal WOLFSSL_X509 struct */
  30101. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  30102. sizeof_client_cert_der_2048));
  30103. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  30104. /* extract PUBLIC KEY from cert */
  30105. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  30106. /* extract signatureType */
  30107. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  30108. /* extract subjectName info */
  30109. AssertNotNull(name = X509_get_subject_name(cert));
  30110. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  30111. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30112. NULL, 0)), 0);
  30113. AssertIntEQ(nameSz, 15);
  30114. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30115. commonName, sizeof(commonName))), 0);
  30116. AssertIntEQ(nameSz, 15);
  30117. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  30118. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30119. commonName, 9)), 0);
  30120. AssertIntEQ(nameSz, 8);
  30121. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  30122. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  30123. countryName, sizeof(countryName))), 0);
  30124. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  30125. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  30126. localityName, sizeof(localityName))), 0);
  30127. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  30128. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  30129. stateName, sizeof(stateName))), 0);
  30130. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  30131. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  30132. orgName, sizeof(orgName))), 0);
  30133. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  30134. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  30135. orgUnit, sizeof(orgUnit))), 0);
  30136. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  30137. EVP_PKEY_free(pubKeyTmp);
  30138. X509_free(cert);
  30139. printf(resultFmt, passed);
  30140. #endif
  30141. return 0;
  30142. }
  30143. static int test_wolfSSL_CTX_set_srp_username(void)
  30144. {
  30145. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30146. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30147. WOLFSSL_CTX* ctx;
  30148. WOLFSSL* ssl;
  30149. const char *username = "TESTUSER";
  30150. const char *password = "TESTPASSWORD";
  30151. int r;
  30152. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  30153. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30154. AssertNotNull(ctx);
  30155. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30156. AssertIntEQ(r,SSL_SUCCESS);
  30157. wolfSSL_CTX_free(ctx);
  30158. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30159. AssertNotNull(ctx);
  30160. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  30161. AssertIntEQ(r,SSL_SUCCESS);
  30162. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30163. AssertIntEQ(r,SSL_SUCCESS);
  30164. AssertNotNull(ssl = SSL_new(ctx));
  30165. AssertNotNull(SSL_get_srp_username(ssl));
  30166. AssertStrEQ(SSL_get_srp_username(ssl), username);
  30167. wolfSSL_free(ssl);
  30168. wolfSSL_CTX_free(ctx);
  30169. printf(resultFmt, passed);
  30170. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30171. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30172. return 0;
  30173. }
  30174. static int test_wolfSSL_CTX_set_srp_password(void)
  30175. {
  30176. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30177. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30178. WOLFSSL_CTX* ctx;
  30179. const char *username = "TESTUSER";
  30180. const char *password = "TESTPASSWORD";
  30181. int r;
  30182. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  30183. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30184. AssertNotNull(ctx);
  30185. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  30186. AssertIntEQ(r,SSL_SUCCESS);
  30187. wolfSSL_CTX_free(ctx);
  30188. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30189. AssertNotNull(ctx);
  30190. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30191. AssertIntEQ(r,SSL_SUCCESS);
  30192. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  30193. AssertIntEQ(r,SSL_SUCCESS);
  30194. wolfSSL_CTX_free(ctx);
  30195. printf(resultFmt, passed);
  30196. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  30197. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  30198. return 0;
  30199. }
  30200. static int test_wolfSSL_X509_STORE(void)
  30201. {
  30202. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30203. X509_STORE *store;
  30204. #ifdef HAVE_CRL
  30205. X509_STORE_CTX *storeCtx;
  30206. X509_CRL *crl;
  30207. X509 *ca, *cert;
  30208. const char crlPem[] = "./certs/crl/crl.revoked";
  30209. const char srvCert[] = "./certs/server-revoked-cert.pem";
  30210. const char caCert[] = "./certs/ca-cert.pem";
  30211. XFILE fp;
  30212. printf(testingFmt, "test_wolfSSL_X509_STORE");
  30213. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30214. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30215. SSL_FILETYPE_PEM)));
  30216. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30217. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30218. SSL_FILETYPE_PEM)));
  30219. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  30220. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30221. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30222. X509_STORE_free(store);
  30223. X509_STORE_CTX_free(storeCtx);
  30224. X509_free(cert);
  30225. X509_free(ca);
  30226. /* should fail to verify now after adding in CRL */
  30227. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30228. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  30229. SSL_FILETYPE_PEM)));
  30230. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  30231. fp = XFOPEN(crlPem, "rb");
  30232. AssertTrue((fp != XBADFILE));
  30233. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  30234. NULL, NULL));
  30235. XFCLOSE(fp);
  30236. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  30237. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  30238. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  30239. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  30240. SSL_FILETYPE_PEM)));
  30241. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  30242. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  30243. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  30244. X509_CRL_free(crl);
  30245. X509_STORE_free(store);
  30246. X509_STORE_CTX_free(storeCtx);
  30247. X509_free(cert);
  30248. X509_free(ca);
  30249. #endif /* HAVE_CRL */
  30250. #ifndef WOLFCRYPT_ONLY
  30251. {
  30252. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30253. SSL_CTX* ctx;
  30254. SSL* ssl;
  30255. int i;
  30256. for (i = 0; i < 2; i++) {
  30257. #ifndef NO_WOLFSSL_SERVER
  30258. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30259. #else
  30260. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30261. #endif
  30262. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30263. SSL_CTX_set_cert_store(ctx, store);
  30264. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30265. SSL_CTX_set_cert_store(ctx, store);
  30266. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  30267. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  30268. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30269. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30270. SSL_FILETYPE_PEM), SSL_SUCCESS);
  30271. AssertNotNull(ssl = SSL_new(ctx));
  30272. if (i == 0) {
  30273. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  30274. }
  30275. else {
  30276. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  30277. X509_STORE_free(store);
  30278. }
  30279. SSL_free(ssl);
  30280. SSL_CTX_free(ctx);
  30281. }
  30282. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30283. }
  30284. #endif
  30285. printf(resultFmt, passed);
  30286. #endif
  30287. return 0;
  30288. }
  30289. static int test_wolfSSL_X509_STORE_load_locations(void)
  30290. {
  30291. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  30292. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30293. SSL_CTX *ctx;
  30294. X509_STORE *store;
  30295. const char ca_file[] = "./certs/ca-cert.pem";
  30296. const char client_pem_file[] = "./certs/client-cert.pem";
  30297. const char client_der_file[] = "./certs/client-cert.der";
  30298. const char ecc_file[] = "./certs/ecc-key.pem";
  30299. const char certs_path[] = "./certs/";
  30300. const char bad_path[] = "./bad-path/";
  30301. #ifdef HAVE_CRL
  30302. const char crl_path[] = "./certs/crl/";
  30303. const char crl_file[] = "./certs/crl/crl.pem";
  30304. #endif
  30305. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  30306. #ifndef NO_WOLFSSL_SERVER
  30307. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30308. #else
  30309. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30310. #endif
  30311. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  30312. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  30313. /* Test bad arguments */
  30314. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  30315. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  30316. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  30317. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  30318. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  30319. #ifdef HAVE_CRL
  30320. /* Test with CRL */
  30321. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  30322. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  30323. #endif
  30324. /* Test with CA */
  30325. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  30326. /* Test with client_cert and certs path */
  30327. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  30328. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  30329. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  30330. /* Clear nodes */
  30331. ERR_clear_error();
  30332. #endif
  30333. SSL_CTX_free(ctx);
  30334. printf(resultFmt, passed);
  30335. #endif
  30336. return 0;
  30337. }
  30338. static int test_X509_STORE_get0_objects(void)
  30339. {
  30340. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  30341. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  30342. X509_STORE *store;
  30343. X509_STORE *store_cpy;
  30344. SSL_CTX *ctx;
  30345. X509_OBJECT *obj;
  30346. STACK_OF(X509_OBJECT) *objs;
  30347. int i;
  30348. printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
  30349. /* Setup store */
  30350. #ifndef NO_WOLFSSL_SERVER
  30351. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30352. #else
  30353. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30354. #endif
  30355. AssertNotNull(store_cpy = X509_STORE_new());
  30356. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  30357. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  30358. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  30359. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  30360. #ifdef HAVE_CRL
  30361. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  30362. #endif
  30363. /* Store ready */
  30364. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  30365. AssertNotNull(objs = X509_STORE_get0_objects(store));
  30366. #ifdef HAVE_CRL
  30367. #ifdef WOLFSSL_SIGNER_DER_CERT
  30368. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  30369. #else
  30370. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  30371. #endif
  30372. #else
  30373. #ifdef WOLFSSL_SIGNER_DER_CERT
  30374. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  30375. #else
  30376. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  30377. #endif
  30378. #endif
  30379. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  30380. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  30381. switch (X509_OBJECT_get_type(obj)) {
  30382. case X509_LU_X509:
  30383. AssertNotNull(X509_OBJECT_get0_X509(obj));
  30384. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  30385. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  30386. break;
  30387. case X509_LU_CRL:
  30388. #ifdef HAVE_CRL
  30389. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  30390. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  30391. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  30392. break;
  30393. #endif
  30394. case X509_LU_NONE:
  30395. default:
  30396. Fail(("X509_OBJECT_get_type should return x509 or crl "
  30397. "(when built with crl support)"),
  30398. ("Unrecognized X509_OBJECT type or none"));
  30399. }
  30400. }
  30401. X509_STORE_free(store_cpy);
  30402. SSL_CTX_free(ctx);
  30403. printf(resultFmt, passed);
  30404. #endif
  30405. return 0;
  30406. }
  30407. static int test_wolfSSL_BN(void)
  30408. {
  30409. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  30410. BIGNUM* a;
  30411. BIGNUM* b;
  30412. BIGNUM* c;
  30413. BIGNUM* d;
  30414. ASN1_INTEGER* ai;
  30415. printf(testingFmt, "wolfSSL_BN()");
  30416. AssertNotNull(b = BN_new());
  30417. AssertNotNull(c = BN_new());
  30418. AssertNotNull(d = BN_new());
  30419. ai = ASN1_INTEGER_new();
  30420. AssertNotNull(ai);
  30421. /* at the moment hard setting since no set function */
  30422. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  30423. ai->data[1] = 0x01; /* length of integer */
  30424. ai->data[2] = 0x03;
  30425. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  30426. ASN1_INTEGER_free(ai);
  30427. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  30428. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  30429. /* a + 3 = */
  30430. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  30431. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  30432. /* check result 3 + 3*/
  30433. AssertIntEQ(BN_get_word(a), 6);
  30434. /* set a back to 3 */
  30435. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30436. /* a - 3 = */
  30437. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  30438. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  30439. /* check result 3 - 3*/
  30440. AssertIntEQ(BN_get_word(a), 0);
  30441. /* set a back to 3 */
  30442. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30443. /* a^b mod c = */
  30444. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  30445. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  30446. /* check result 3^2 mod 5 */
  30447. AssertIntEQ(BN_get_word(d), 4);
  30448. /* a*b = */
  30449. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  30450. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  30451. /* check result 3*2 */
  30452. AssertIntEQ(BN_get_word(d), 6);
  30453. /* c/b => db + a */
  30454. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  30455. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  30456. /* check result 5/2 */
  30457. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  30458. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  30459. /* set a back to 3 */
  30460. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  30461. /* a*b mod c = */
  30462. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  30463. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  30464. /* check result 3*2 mod 5 */
  30465. AssertIntEQ(BN_get_word(d), 1);
  30466. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  30467. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  30468. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  30469. /* gcd of a and b */
  30470. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  30471. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  30472. /* check result gcd(16, 24) */
  30473. AssertIntEQ(BN_get_word(d), 8);
  30474. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  30475. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  30476. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  30477. AssertIntEQ(BN_is_zero(b), 0);
  30478. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  30479. AssertIntEQ(BN_is_zero(b), 1);
  30480. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  30481. AssertIntEQ(BN_is_zero(b), 0);
  30482. /* set b back to 2 */
  30483. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  30484. {
  30485. /* BN_mod_inverse test */
  30486. BIGNUM *r = BN_new();
  30487. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  30488. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  30489. BN_free(val);
  30490. }
  30491. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  30492. defined(WOLFSSL_SP_INT_NEGATIVE))
  30493. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  30494. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  30495. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  30496. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  30497. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  30498. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  30499. {
  30500. char* ret;
  30501. AssertNotNull(ret = BN_bn2dec(c));
  30502. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  30503. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  30504. }
  30505. #endif
  30506. AssertIntEQ(BN_get_word(c), 4);
  30507. #endif
  30508. BN_free(a);
  30509. BN_free(b);
  30510. BN_free(c);
  30511. BN_clear_free(d);
  30512. /* check that converting NULL and the null string returns an error */
  30513. a = NULL;
  30514. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  30515. AssertIntLE(BN_hex2bn(&a, ""), 0);
  30516. AssertNull(a);
  30517. /* check that getting a string and a bin of the same number are equal,
  30518. * and that the comparison works EQ, LT and GT */
  30519. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  30520. AssertNotNull(b = BN_new());
  30521. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  30522. AssertNotNull(c = BN_new());
  30523. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  30524. AssertIntEQ(BN_cmp(a, b), 0);
  30525. AssertIntLT(BN_cmp(a, c), 0);
  30526. AssertIntGT(BN_cmp(c, b), 0);
  30527. AssertIntEQ(BN_set_word(a, 0), 1);
  30528. AssertIntEQ(BN_is_zero(a), 1);
  30529. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  30530. AssertIntEQ(BN_is_zero(a), 0);
  30531. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  30532. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  30533. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  30534. AssertIntEQ(BN_is_zero(a), 1);
  30535. BN_free(a);
  30536. BN_free(b);
  30537. BN_free(c);
  30538. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  30539. {
  30540. BIGNUM *ap;
  30541. BIGNUM bv;
  30542. BIGNUM cv;
  30543. BIGNUM dv;
  30544. AssertNotNull(ap = BN_new());
  30545. BN_init(&bv);
  30546. BN_init(&cv);
  30547. BN_init(&dv);
  30548. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  30549. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  30550. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  30551. /* a^b mod c = */
  30552. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  30553. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  30554. /* check result 3^2 mod 5 */
  30555. AssertIntEQ(BN_get_word(&dv), 4);
  30556. /* a*b mod c = */
  30557. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  30558. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  30559. /* check result 3*2 mod 5 */
  30560. AssertIntEQ(BN_get_word(&dv), 1);
  30561. BN_free(ap);
  30562. }
  30563. #endif
  30564. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  30565. AssertNotNull(a = BN_new());
  30566. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  30567. SSL_SUCCESS);
  30568. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  30569. BN_free(a);
  30570. #endif
  30571. printf(resultFmt, passed);
  30572. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  30573. return 0;
  30574. }
  30575. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30576. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30577. #define TEST_ARG 0x1234
  30578. static void msg_cb(int write_p, int version, int content_type,
  30579. const void *buf, size_t len, SSL *ssl, void *arg)
  30580. {
  30581. (void)write_p;
  30582. (void)version;
  30583. (void)content_type;
  30584. (void)buf;
  30585. (void)len;
  30586. (void)ssl;
  30587. AssertTrue(arg == (void*)TEST_ARG);
  30588. }
  30589. #endif
  30590. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30591. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30592. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  30593. !defined(NO_WOLFSSL_SERVER)
  30594. #ifndef SINGLE_THREADED
  30595. #if defined(SESSION_CERTS)
  30596. #include "wolfssl/internal.h"
  30597. #endif
  30598. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  30599. {
  30600. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  30601. STACK_OF(X509)* sk;
  30602. X509* x509;
  30603. int i, num;
  30604. BIO* bio;
  30605. #endif
  30606. (void) ctx;
  30607. printf("\n===== msgcb called ====\n");
  30608. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  30609. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  30610. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  30611. AssertNotNull(SSL_get0_verified_chain(ssl));
  30612. #else
  30613. (void) ssl;
  30614. #endif
  30615. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  30616. bio = BIO_new(BIO_s_file());
  30617. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  30618. sk = SSL_get_peer_cert_chain(ssl);
  30619. AssertNotNull(sk);
  30620. if (!sk) {
  30621. BIO_free(bio);
  30622. return SSL_FAILURE;
  30623. }
  30624. num = sk_X509_num(sk);
  30625. AssertTrue(num > 0);
  30626. for (i = 0; i < num; i++) {
  30627. x509 = sk_X509_value(sk,i);
  30628. AssertNotNull(x509);
  30629. if (!x509)
  30630. break;
  30631. printf("Certificate at index [%d] = :\n",i);
  30632. X509_print(bio,x509);
  30633. printf("\n\n");
  30634. }
  30635. BIO_free(bio);
  30636. #endif
  30637. return SSL_SUCCESS;
  30638. }
  30639. #endif
  30640. #endif
  30641. static int test_wolfSSL_msgCb(void)
  30642. {
  30643. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30644. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  30645. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  30646. !defined(NO_WOLFSSL_SERVER)
  30647. tcp_ready ready;
  30648. func_args client_args;
  30649. func_args server_args;
  30650. #ifndef SINGLE_THREADED
  30651. THREAD_TYPE serverThread;
  30652. #endif
  30653. callback_functions client_cb;
  30654. callback_functions server_cb;
  30655. printf(testingFmt, "test_wolfSSL_msgCb");
  30656. /* create a failed connection and inspect the error */
  30657. #ifdef WOLFSSL_TIRTOS
  30658. fdOpenSession(Task_self());
  30659. #endif
  30660. XMEMSET(&client_args, 0, sizeof(func_args));
  30661. XMEMSET(&server_args, 0, sizeof(func_args));
  30662. StartTCP();
  30663. InitTcpReady(&ready);
  30664. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30665. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30666. #ifndef WOLFSSL_NO_TLS12
  30667. client_cb.method = wolfTLSv1_2_client_method;
  30668. server_cb.method = wolfTLSv1_2_server_method;
  30669. #else
  30670. client_cb.method = wolfTLSv1_3_client_method;
  30671. server_cb.method = wolfTLSv1_3_server_method;
  30672. #endif
  30673. server_args.signal = &ready;
  30674. server_args.callbacks = &server_cb;
  30675. client_args.signal = &ready;
  30676. client_args.callbacks = &client_cb;
  30677. client_args.return_code = TEST_FAIL;
  30678. #ifndef SINGLE_THREADED
  30679. start_thread(test_server_nofail, &server_args, &serverThread);
  30680. wait_tcp_ready(&server_args);
  30681. test_client_nofail(&client_args, msgCb);
  30682. join_thread(serverThread);
  30683. #endif
  30684. FreeTcpReady(&ready);
  30685. #ifndef SINGLE_THREADED
  30686. AssertTrue(client_args.return_code);
  30687. AssertTrue(server_args.return_code);
  30688. #endif
  30689. #ifdef WOLFSSL_TIRTOS
  30690. fdOpenSession(Task_self());
  30691. #endif
  30692. printf(resultFmt, passed);
  30693. #endif
  30694. return 0;
  30695. }
  30696. static int test_wolfSSL_either_side(void)
  30697. {
  30698. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  30699. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30700. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  30701. tcp_ready ready;
  30702. func_args client_args;
  30703. func_args server_args;
  30704. #ifndef SINGLE_THREADED
  30705. THREAD_TYPE serverThread;
  30706. #endif
  30707. callback_functions client_cb;
  30708. callback_functions server_cb;
  30709. printf(testingFmt, "test_wolfSSL_either_side");
  30710. /* create a failed connection and inspect the error */
  30711. #ifdef WOLFSSL_TIRTOS
  30712. fdOpenSession(Task_self());
  30713. #endif
  30714. XMEMSET(&client_args, 0, sizeof(func_args));
  30715. XMEMSET(&server_args, 0, sizeof(func_args));
  30716. StartTCP();
  30717. InitTcpReady(&ready);
  30718. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30719. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30720. /* Use different CTX for client and server */
  30721. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  30722. AssertNotNull(client_cb.ctx);
  30723. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  30724. AssertNotNull(server_cb.ctx);
  30725. /* we are responsible for free'ing WOLFSSL_CTX */
  30726. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  30727. server_args.signal = &ready;
  30728. server_args.callbacks = &server_cb;
  30729. client_args.signal = &ready;
  30730. client_args.callbacks = &client_cb;
  30731. client_args.return_code = TEST_FAIL;
  30732. #ifndef SINGLE_THREADED
  30733. start_thread(test_server_nofail, &server_args, &serverThread);
  30734. wait_tcp_ready(&server_args);
  30735. test_client_nofail(&client_args, NULL);
  30736. join_thread(serverThread);
  30737. #endif
  30738. wolfSSL_CTX_free(client_cb.ctx);
  30739. wolfSSL_CTX_free(server_cb.ctx);
  30740. FreeTcpReady(&ready);
  30741. #ifndef SINGLE_THREADED
  30742. AssertTrue(client_args.return_code);
  30743. AssertTrue(server_args.return_code);
  30744. #endif
  30745. #ifdef WOLFSSL_TIRTOS
  30746. fdOpenSession(Task_self());
  30747. #endif
  30748. printf(resultFmt, passed);
  30749. #endif
  30750. return 0;
  30751. }
  30752. static int test_wolfSSL_DTLS_either_side(void)
  30753. {
  30754. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  30755. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30756. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  30757. defined(WOLFSSL_DTLS)
  30758. tcp_ready ready;
  30759. func_args client_args;
  30760. func_args server_args;
  30761. #ifndef SINGLE_THREADED
  30762. THREAD_TYPE serverThread;
  30763. #endif
  30764. callback_functions client_cb;
  30765. callback_functions server_cb;
  30766. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  30767. /* create a failed connection and inspect the error */
  30768. #ifdef WOLFSSL_TIRTOS
  30769. fdOpenSession(Task_self());
  30770. #endif
  30771. XMEMSET(&client_args, 0, sizeof(func_args));
  30772. XMEMSET(&server_args, 0, sizeof(func_args));
  30773. StartTCP();
  30774. InitTcpReady(&ready);
  30775. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  30776. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30777. /* Use different CTX for client and server */
  30778. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  30779. AssertNotNull(client_cb.ctx);
  30780. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  30781. AssertNotNull(server_cb.ctx);
  30782. /* we are responsible for free'ing WOLFSSL_CTX */
  30783. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  30784. server_args.signal = &ready;
  30785. server_args.callbacks = &server_cb;
  30786. client_args.signal = &ready;
  30787. client_args.callbacks = &client_cb;
  30788. client_args.return_code = TEST_FAIL;
  30789. #ifndef SINGLE_THREADED
  30790. start_thread(test_server_nofail, &server_args, &serverThread);
  30791. wait_tcp_ready(&server_args);
  30792. test_client_nofail(&client_args, NULL);
  30793. join_thread(serverThread);
  30794. #endif
  30795. wolfSSL_CTX_free(client_cb.ctx);
  30796. wolfSSL_CTX_free(server_cb.ctx);
  30797. FreeTcpReady(&ready);
  30798. #ifndef SINGLE_THREADED
  30799. AssertTrue(client_args.return_code);
  30800. AssertTrue(server_args.return_code);
  30801. #endif
  30802. #ifdef WOLFSSL_TIRTOS
  30803. fdOpenSession(Task_self());
  30804. #endif
  30805. printf(resultFmt, passed);
  30806. #endif
  30807. return 0;
  30808. }
  30809. static int test_generate_cookie(void)
  30810. {
  30811. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  30812. SSL_CTX* ctx;
  30813. SSL* ssl;
  30814. byte buf[FOURK_BUF] = {0};
  30815. printf(testingFmt, "test_generate_cookie");
  30816. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  30817. AssertNotNull(ssl = SSL_new(ctx));
  30818. /* Test unconnected */
  30819. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  30820. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  30821. wolfSSL_SetCookieCtx(ssl, ctx);
  30822. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  30823. AssertNull(wolfSSL_GetCookieCtx(NULL));
  30824. SSL_free(ssl);
  30825. SSL_CTX_free(ctx);
  30826. printf(resultFmt, passed);
  30827. #endif
  30828. return 0;
  30829. }
  30830. static int test_wolfSSL_set_options(void)
  30831. {
  30832. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30833. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30834. WOLFSSL* ssl;
  30835. WOLFSSL_CTX* ctx;
  30836. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  30837. char appData[] = "extra msg";
  30838. #endif
  30839. #ifdef OPENSSL_EXTRA
  30840. unsigned char protos[] = {
  30841. 7, 't', 'l', 's', '/', '1', '.', '2',
  30842. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  30843. };
  30844. unsigned int len = sizeof(protos);
  30845. void *arg = (void *)TEST_ARG;
  30846. #endif
  30847. printf(testingFmt, "wolfSSL_set_options()");
  30848. #ifndef NO_WOLFSSL_SERVER
  30849. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30850. #else
  30851. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  30852. #endif
  30853. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30854. WOLFSSL_FILETYPE_PEM));
  30855. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30856. WOLFSSL_FILETYPE_PEM));
  30857. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  30858. == WOLFSSL_OP_NO_TLSv1);
  30859. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  30860. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  30861. WOLFSSL_OP_NO_SSLv2)), 0);
  30862. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  30863. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  30864. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  30865. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  30866. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  30867. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  30868. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  30869. WOLFSSL_OP_NO_COMPRESSION));
  30870. wolfSSL_CTX_free(ctx);
  30871. #ifndef NO_WOLFSSL_SERVER
  30872. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  30873. AssertNotNull(ctx);
  30874. #else
  30875. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30876. AssertNotNull(ctx);
  30877. #endif
  30878. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30879. WOLFSSL_FILETYPE_PEM));
  30880. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30881. WOLFSSL_FILETYPE_PEM));
  30882. #ifdef OPENSSL_EXTRA
  30883. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  30884. #endif
  30885. AssertNotNull(ssl = wolfSSL_new(ctx));
  30886. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  30887. #ifdef HAVE_EX_DATA
  30888. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  30889. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  30890. if (ssl) {
  30891. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  30892. appData, sizeof(appData)), 0);
  30893. }
  30894. #else
  30895. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  30896. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  30897. #endif
  30898. #endif
  30899. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  30900. WOLFSSL_OP_NO_TLSv1);
  30901. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  30902. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  30903. WOLFSSL_OP_NO_SSLv2)), 0);
  30904. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  30905. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  30906. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  30907. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  30908. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  30909. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  30910. #ifdef OPENSSL_EXTRA
  30911. AssertNull((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  30912. WOLFSSL_OP_NO_COMPRESSION));
  30913. #endif
  30914. #ifdef OPENSSL_EXTRA
  30915. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  30916. wolfSSL_set_msg_callback_arg(ssl, arg);
  30917. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30918. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  30919. #else
  30920. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  30921. #endif
  30922. #endif
  30923. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  30924. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  30925. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  30926. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  30927. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30928. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  30929. #else
  30930. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  30931. #endif
  30932. #endif /* HAVE_ALPN && !NO_BIO */
  30933. #endif
  30934. wolfSSL_free(ssl);
  30935. wolfSSL_CTX_free(ctx);
  30936. printf(resultFmt, passed);
  30937. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30938. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30939. return 0;
  30940. }
  30941. static int test_wolfSSL_sk_SSL_CIPHER(void)
  30942. {
  30943. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30944. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30945. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30946. SSL* ssl;
  30947. SSL_CTX* ctx;
  30948. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  30949. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  30950. #ifndef NO_WOLFSSL_SERVER
  30951. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30952. #else
  30953. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30954. #endif
  30955. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30956. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30957. AssertNotNull(ssl = SSL_new(ctx));
  30958. AssertNotNull(sk = SSL_get_ciphers(ssl));
  30959. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  30960. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  30961. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  30962. /* error case because connection has not been established yet */
  30963. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  30964. sk_SSL_CIPHER_free(dupSk);
  30965. /* sk is pointer to internal struct that should be free'd in SSL_free */
  30966. SSL_free(ssl);
  30967. SSL_CTX_free(ctx);
  30968. printf(resultFmt, passed);
  30969. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30970. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30971. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30972. return 0;
  30973. }
  30974. static int test_wolfSSL_set1_curves_list(void)
  30975. {
  30976. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  30977. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30978. SSL* ssl = NULL;
  30979. SSL_CTX* ctx = NULL;
  30980. #ifndef NO_WOLFSSL_SERVER
  30981. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30982. #else
  30983. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30984. #endif
  30985. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30986. SSL_FILETYPE_PEM));
  30987. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  30988. AssertNotNull(ssl = SSL_new(ctx));
  30989. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  30990. #ifdef HAVE_ECC
  30991. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  30992. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  30993. #endif
  30994. #ifdef HAVE_CURVE25519
  30995. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  30996. #else
  30997. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  30998. #endif
  30999. #ifdef HAVE_CURVE448
  31000. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  31001. #else
  31002. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  31003. #endif
  31004. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  31005. #ifdef HAVE_ECC
  31006. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  31007. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  31008. #endif
  31009. #ifdef HAVE_CURVE25519
  31010. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  31011. #else
  31012. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  31013. #endif
  31014. #ifdef HAVE_CURVE448
  31015. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  31016. #else
  31017. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  31018. #endif
  31019. SSL_free(ssl);
  31020. SSL_CTX_free(ctx);
  31021. printf(resultFmt, passed);
  31022. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31023. #endif
  31024. return 0;
  31025. }
  31026. static int test_wolfSSL_set1_sigalgs_list(void)
  31027. {
  31028. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  31029. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31030. SSL* ssl;
  31031. SSL_CTX* ctx;
  31032. #ifndef NO_WOLFSSL_SERVER
  31033. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31034. #else
  31035. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31036. #endif
  31037. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31038. SSL_FILETYPE_PEM));
  31039. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31040. AssertNotNull(ssl = SSL_new(ctx));
  31041. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31042. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  31043. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31044. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  31045. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  31046. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  31047. #ifndef NO_RSA
  31048. #ifndef NO_SHA256
  31049. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  31050. WOLFSSL_FAILURE);
  31051. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  31052. WOLFSSL_FAILURE);
  31053. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  31054. WOLFSSL_SUCCESS);
  31055. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  31056. WOLFSSL_SUCCESS);
  31057. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  31058. WOLFSSL_FAILURE);
  31059. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  31060. WOLFSSL_FAILURE);
  31061. #ifdef WC_RSA_PSS
  31062. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  31063. WOLFSSL_SUCCESS);
  31064. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  31065. WOLFSSL_SUCCESS);
  31066. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  31067. WOLFSSL_SUCCESS);
  31068. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  31069. WOLFSSL_SUCCESS);
  31070. #endif
  31071. #ifdef WOLFSSL_SHA512
  31072. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31073. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  31074. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31075. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  31076. #elif defined(WOLFSSL_SHA384)
  31077. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31078. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  31079. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31080. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  31081. #endif
  31082. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  31083. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  31084. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  31085. WOLFSSL_FAILURE);
  31086. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  31087. WOLFSSL_FAILURE);
  31088. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  31089. WOLFSSL_FAILURE);
  31090. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  31091. WOLFSSL_FAILURE);
  31092. #endif
  31093. #endif
  31094. #ifdef HAVE_ECC
  31095. #ifndef NO_SHA256
  31096. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  31097. WOLFSSL_SUCCESS);
  31098. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  31099. #ifdef WOLFSSL_SHA512
  31100. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31101. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  31102. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31103. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  31104. #elif defined(WOLFSSL_SHA384)
  31105. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31106. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  31107. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31108. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  31109. #endif
  31110. #endif
  31111. #endif
  31112. #ifdef HAVE_ED25519
  31113. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  31114. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  31115. #endif
  31116. #ifdef HAVE_ED448
  31117. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  31118. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  31119. #endif
  31120. #ifndef NO_DSA
  31121. #ifndef NO_SHA256
  31122. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  31123. WOLFSSL_SUCCESS);
  31124. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  31125. WOLFSSL_SUCCESS);
  31126. #endif
  31127. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  31128. defined(WOLFSSL_ALLOW_TLS_SHA1))
  31129. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  31130. WOLFSSL_SUCCESS);
  31131. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  31132. WOLFSSL_SUCCESS);
  31133. #endif
  31134. #endif
  31135. SSL_free(ssl);
  31136. SSL_CTX_free(ctx);
  31137. printf(resultFmt, passed);
  31138. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31139. #endif
  31140. return 0;
  31141. }
  31142. /* Testing wolfSSL_set_tlsext_status_type function.
  31143. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  31144. */
  31145. static int test_wolfSSL_set_tlsext_status_type(void){
  31146. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  31147. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  31148. SSL* ssl;
  31149. SSL_CTX* ctx;
  31150. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  31151. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31152. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31153. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31154. AssertNotNull(ssl = SSL_new(ctx));
  31155. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  31156. SSL_SUCCESS);
  31157. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  31158. SSL_free(ssl);
  31159. SSL_CTX_free(ctx);
  31160. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  31161. return 0;
  31162. }
  31163. #ifndef NO_BIO
  31164. static int test_wolfSSL_PEM_read_bio(void)
  31165. {
  31166. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31167. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31168. byte buff[6000];
  31169. XFILE f;
  31170. int bytes;
  31171. X509* x509;
  31172. BIO* bio = NULL;
  31173. BUF_MEM* buf;
  31174. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  31175. f = XFOPEN(cliCertFile, "rb");
  31176. AssertTrue((f != XBADFILE));
  31177. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  31178. XFCLOSE(f);
  31179. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31180. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31181. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  31182. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  31183. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  31184. /* BIO should return the set EOF value */
  31185. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  31186. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  31187. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  31188. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  31189. BIO_free(bio);
  31190. BUF_MEM_free(buf);
  31191. X509_free(x509);
  31192. printf(resultFmt, passed);
  31193. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31194. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31195. return 0;
  31196. }
  31197. #if defined(OPENSSL_EXTRA)
  31198. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  31199. long argl, long ret)
  31200. {
  31201. (void)bio;
  31202. (void)cmd;
  31203. (void)argp;
  31204. (void)argi;
  31205. (void)argl;
  31206. return ret;
  31207. }
  31208. #endif
  31209. static int test_wolfSSL_BIO(void)
  31210. {
  31211. #if defined(OPENSSL_EXTRA)
  31212. const unsigned char* p;
  31213. byte buff[20];
  31214. BIO* bio1;
  31215. BIO* bio2;
  31216. BIO* bio3;
  31217. char* bufPt;
  31218. int i;
  31219. printf(testingFmt, "wolfSSL_BIO()");
  31220. for (i = 0; i < 20; i++) {
  31221. buff[i] = i;
  31222. }
  31223. /* test BIO_free with NULL */
  31224. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  31225. /* Creating and testing type BIO_s_bio */
  31226. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  31227. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  31228. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  31229. /* read/write before set up */
  31230. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  31231. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  31232. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  31233. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  31234. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  31235. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  31236. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  31237. XMEMCPY(bufPt, buff, 10);
  31238. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  31239. /* write buffer full */
  31240. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  31241. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  31242. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  31243. /* write the other direction with pair */
  31244. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  31245. XMEMCPY(bufPt, buff, 8);
  31246. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  31247. /* try read */
  31248. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  31249. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  31250. /* try read using ctrl function */
  31251. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  31252. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  31253. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  31254. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  31255. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  31256. for (i = 0; i < 20; i++) {
  31257. AssertIntEQ((int)bufPt[i], i);
  31258. }
  31259. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  31260. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  31261. for (i = 0; i < 8; i++) {
  31262. AssertIntEQ((int)bufPt[i], i);
  31263. }
  31264. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  31265. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  31266. /* new pair */
  31267. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  31268. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  31269. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  31270. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  31271. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  31272. /* test wrap around... */
  31273. AssertIntEQ(BIO_reset(bio1), 0);
  31274. AssertIntEQ(BIO_reset(bio3), 0);
  31275. /* fill write buffer, read only small amount then write again */
  31276. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  31277. XMEMCPY(bufPt, buff, 20);
  31278. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  31279. for (i = 0; i < 4; i++) {
  31280. AssertIntEQ(bufPt[i], i);
  31281. }
  31282. /* try writing over read index */
  31283. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  31284. XMEMSET(bufPt, 0, 4);
  31285. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  31286. /* read and write 0 bytes */
  31287. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  31288. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  31289. /* should read only to end of write buffer then need to read again */
  31290. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  31291. for (i = 0; i < 16; i++) {
  31292. AssertIntEQ(bufPt[i], buff[4 + i]);
  31293. }
  31294. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  31295. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  31296. for (i = 0; i < 4; i++) {
  31297. AssertIntEQ(bufPt[i], 0);
  31298. }
  31299. /* read index should not have advanced with nread0 */
  31300. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  31301. for (i = 0; i < 4; i++) {
  31302. AssertIntEQ(bufPt[i], 0);
  31303. }
  31304. /* write and fill up buffer checking reset of index state */
  31305. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  31306. XMEMCPY(bufPt, buff, 20);
  31307. /* test reset on data in bio1 write buffer */
  31308. AssertIntEQ(BIO_reset(bio1), 0);
  31309. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  31310. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  31311. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  31312. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  31313. AssertNotNull(p);
  31314. XMEMCPY(bufPt, buff, 20);
  31315. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  31316. for (i = 0; i < 6; i++) {
  31317. AssertIntEQ(bufPt[i], i);
  31318. }
  31319. /* test case of writing twice with offset read index */
  31320. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  31321. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  31322. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  31323. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  31324. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  31325. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  31326. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  31327. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  31328. BIO_free(bio1);
  31329. BIO_free(bio3);
  31330. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  31331. {
  31332. BIO* bioA = NULL;
  31333. BIO* bioB = NULL;
  31334. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  31335. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  31336. BIO_free(bioA);
  31337. bioA = NULL;
  31338. BIO_free(bioB);
  31339. bioB = NULL;
  31340. }
  31341. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  31342. /* BIOs with file pointers */
  31343. #if !defined(NO_FILESYSTEM)
  31344. {
  31345. XFILE f1;
  31346. XFILE f2;
  31347. BIO* f_bio1;
  31348. BIO* f_bio2;
  31349. unsigned char cert[300];
  31350. char testFile[] = "tests/bio_write_test.txt";
  31351. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  31352. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  31353. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  31354. /* Failure due to wrong BIO type */
  31355. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  31356. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  31357. f1 = XFOPEN(svrCertFile, "rwb");
  31358. AssertTrue((f1 != XBADFILE));
  31359. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  31360. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  31361. WOLFSSL_SUCCESS);
  31362. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  31363. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  31364. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  31365. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  31366. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  31367. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  31368. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  31369. AssertIntEQ(BIO_reset(f_bio2), 0);
  31370. AssertIntEQ(BIO_tell(NULL),-1);
  31371. AssertIntEQ(BIO_tell(f_bio2),0);
  31372. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  31373. AssertIntEQ(BIO_tell(f_bio2),4);
  31374. BIO_free(f_bio1);
  31375. BIO_free(f_bio2);
  31376. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  31377. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  31378. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  31379. BIO_free(f_bio1);
  31380. }
  31381. #endif /* !defined(NO_FILESYSTEM) */
  31382. /* BIO info callback */
  31383. {
  31384. const char* testArg = "test";
  31385. BIO* cb_bio;
  31386. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  31387. BIO_set_callback(cb_bio, bioCallback);
  31388. AssertNotNull(BIO_get_callback(cb_bio));
  31389. BIO_set_callback(cb_bio, NULL);
  31390. AssertNull(BIO_get_callback(cb_bio));
  31391. BIO_set_callback_arg(cb_bio, (char*)testArg);
  31392. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  31393. AssertNull(BIO_get_callback_arg(NULL));
  31394. BIO_free(cb_bio);
  31395. }
  31396. /* BIO_vfree */
  31397. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  31398. BIO_vfree(NULL);
  31399. BIO_vfree(bio1);
  31400. printf(resultFmt, passed);
  31401. #endif
  31402. return 0;
  31403. }
  31404. #endif /* !NO_BIO */
  31405. static int test_wolfSSL_ASN1_STRING(void)
  31406. {
  31407. #if defined(OPENSSL_EXTRA)
  31408. ASN1_STRING* str = NULL;
  31409. const char data[] = "hello wolfSSL";
  31410. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  31411. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  31412. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  31413. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  31414. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  31415. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  31416. ASN1_STRING_free(str);
  31417. printf(resultFmt, passed);
  31418. #endif
  31419. return 0;
  31420. }
  31421. static int test_wolfSSL_ASN1_BIT_STRING(void)
  31422. {
  31423. #ifdef OPENSSL_ALL
  31424. ASN1_BIT_STRING* str;
  31425. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  31426. AssertNotNull(str = ASN1_BIT_STRING_new());
  31427. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  31428. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  31429. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  31430. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  31431. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  31432. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  31433. ASN1_BIT_STRING_free(str);
  31434. printf(resultFmt, passed);
  31435. #endif
  31436. return 0;
  31437. }
  31438. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  31439. {
  31440. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  31441. BIO *bio, *out;
  31442. ASN1_INTEGER* ai;
  31443. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  31444. char tmp[1024];
  31445. int tmpSz;
  31446. const char expected1[] = "123456";
  31447. const char expected2[] = "112345678912345678901234567890";
  31448. printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
  31449. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  31450. AssertNotNull(out = BIO_new(BIO_s_mem()));
  31451. AssertNotNull(ai = ASN1_INTEGER_new());
  31452. /* read first line */
  31453. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  31454. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  31455. XMEMSET(tmp, 0, 1024);
  31456. tmpSz = BIO_read(out, tmp, 1024);
  31457. AssertIntEQ(tmpSz, 6);
  31458. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  31459. /* fail on second line (not % 2) */
  31460. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  31461. /* read 3rd long line */
  31462. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  31463. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  31464. XMEMSET(tmp, 0, 1024);
  31465. tmpSz = BIO_read(out, tmp, 1024);
  31466. AssertIntEQ(tmpSz, 30);
  31467. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  31468. BIO_free(out);
  31469. BIO_free(bio);
  31470. ASN1_INTEGER_free(ai);
  31471. printf(resultFmt, passed);
  31472. #endif
  31473. return 0;
  31474. }
  31475. static int test_wolfSSL_a2i_IPADDRESS(void)
  31476. {
  31477. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  31478. const unsigned char* data;
  31479. int dataSz = 0;
  31480. ASN1_OCTET_STRING *st;
  31481. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  31482. const unsigned char ipv6_exp[] = {
  31483. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  31484. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  31485. };
  31486. const unsigned char ipv6_home[] = {
  31487. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  31488. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  31489. };
  31490. printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
  31491. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  31492. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  31493. data = ASN1_STRING_get0_data(st);
  31494. dataSz = ASN1_STRING_length(st);
  31495. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  31496. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  31497. ASN1_STRING_free(st);
  31498. AssertNotNull(st = a2i_IPADDRESS("::1"));
  31499. data = ASN1_STRING_get0_data(st);
  31500. dataSz = ASN1_STRING_length(st);
  31501. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  31502. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  31503. ASN1_STRING_free(st);
  31504. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  31505. data = ASN1_STRING_get0_data(st);
  31506. dataSz = ASN1_STRING_length(st);
  31507. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  31508. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  31509. ASN1_STRING_free(st);
  31510. printf(resultFmt, passed);
  31511. #endif
  31512. return 0;
  31513. }
  31514. static int test_wolfSSL_DES_ecb_encrypt(void)
  31515. {
  31516. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  31517. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  31518. WOLFSSL_DES_key_schedule key;
  31519. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  31520. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  31521. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  31522. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  31523. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  31524. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  31525. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  31526. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  31527. /* Encrypt messages */
  31528. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  31529. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  31530. {
  31531. /* Decrypt messages */
  31532. int ret1 = 0;
  31533. int ret2 = 0;
  31534. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  31535. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  31536. AssertIntEQ(ret1,0);
  31537. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  31538. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  31539. AssertIntEQ(ret2,0);
  31540. }
  31541. printf(resultFmt, passed);
  31542. #endif
  31543. return 0;
  31544. }
  31545. static int test_wolfSSL_ASN1_TIME_adj(void)
  31546. {
  31547. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  31548. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  31549. const int year = 365*24*60*60;
  31550. const int day = 24*60*60;
  31551. const int hour = 60*60;
  31552. const int mini = 60;
  31553. const byte asn_utc_time = ASN_UTC_TIME;
  31554. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  31555. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  31556. #endif
  31557. WOLFSSL_ASN1_TIME *asn_time, *s;
  31558. int offset_day;
  31559. long offset_sec;
  31560. char date_str[CTC_DATE_SIZE + 1];
  31561. time_t t;
  31562. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  31563. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  31564. /* UTC notation test */
  31565. /* 2000/2/15 20:30:00 */
  31566. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  31567. offset_day = 7;
  31568. offset_sec = 45 * mini;
  31569. /* offset_sec = -45 * min;*/
  31570. AssertNotNull(asn_time =
  31571. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  31572. AssertTrue(asn_time->type == asn_utc_time);
  31573. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31574. date_str[CTC_DATE_SIZE] = '\0';
  31575. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  31576. /* negative offset */
  31577. offset_sec = -45 * mini;
  31578. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  31579. AssertNotNull(asn_time);
  31580. AssertTrue(asn_time->type == asn_utc_time);
  31581. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31582. date_str[CTC_DATE_SIZE] = '\0';
  31583. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  31584. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  31585. XMEMSET(date_str, 0, sizeof(date_str));
  31586. /* Generalized time will overflow time_t if not long */
  31587. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  31588. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  31589. DYNAMIC_TYPE_OPENSSL);
  31590. /* GeneralizedTime notation test */
  31591. /* 2055/03/01 09:00:00 */
  31592. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  31593. offset_day = 12;
  31594. offset_sec = 10 * mini;
  31595. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  31596. AssertTrue(asn_time->type == asn_gen_time);
  31597. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31598. date_str[CTC_DATE_SIZE] = '\0';
  31599. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  31600. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  31601. XMEMSET(date_str, 0, sizeof(date_str));
  31602. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  31603. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  31604. s = NULL;
  31605. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  31606. offset_day = 7;
  31607. offset_sec = 45 * mini;
  31608. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  31609. AssertTrue(asn_time->type == asn_utc_time);
  31610. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31611. date_str[CTC_DATE_SIZE] = '\0';
  31612. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  31613. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  31614. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  31615. AssertTrue(asn_time->type == asn_utc_time);
  31616. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  31617. date_str[CTC_DATE_SIZE] = '\0';
  31618. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  31619. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  31620. printf(resultFmt, passed);
  31621. #endif
  31622. return 0;
  31623. }
  31624. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  31625. {
  31626. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  31627. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  31628. && !defined(NO_ASN_TIME)
  31629. ASN1_TIME asnTime;
  31630. struct tm tm;
  31631. printf(testingFmt, "wolfSSL_ASN1_TIME_to_tm()");
  31632. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  31633. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  31634. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  31635. AssertIntEQ(tm.tm_sec, 15);
  31636. AssertIntEQ(tm.tm_min, 15);
  31637. AssertIntEQ(tm.tm_hour, 21);
  31638. AssertIntEQ(tm.tm_mday, 22);
  31639. AssertIntEQ(tm.tm_mon, 1);
  31640. AssertIntEQ(tm.tm_year, 100);
  31641. AssertIntEQ(tm.tm_isdst, 0);
  31642. #ifdef XMKTIME
  31643. AssertIntEQ(tm.tm_wday, 2);
  31644. AssertIntEQ(tm.tm_yday, 52);
  31645. #endif
  31646. printf(resultFmt, passed);
  31647. #endif
  31648. return 0;
  31649. }
  31650. static int test_wolfSSL_X509_cmp_time(void)
  31651. {
  31652. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  31653. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  31654. WOLFSSL_ASN1_TIME asn_time;
  31655. time_t t;
  31656. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  31657. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  31658. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  31659. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  31660. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  31661. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  31662. printf(resultFmt, passed);
  31663. #endif
  31664. return 0;
  31665. }
  31666. static int test_wolfSSL_X509_time_adj(void)
  31667. {
  31668. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  31669. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  31670. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  31671. !defined(NO_ASN_TIME)
  31672. X509* x509;
  31673. time_t t, not_before, not_after;
  31674. printf(testingFmt, "wolfSSL_X509_time_adj()");
  31675. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  31676. client_cert_der_2048, sizeof_client_cert_der_2048,
  31677. WOLFSSL_FILETYPE_ASN1));
  31678. t = 0;
  31679. not_before = wc_Time(0);
  31680. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  31681. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  31682. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  31683. /* Check X509_gmtime_adj, too. */
  31684. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  31685. X509_free(x509);
  31686. printf(resultFmt, passed);
  31687. #endif
  31688. return 0;
  31689. }
  31690. static int test_wolfSSL_X509(void)
  31691. {
  31692. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  31693. && !defined(NO_RSA)
  31694. X509* x509;
  31695. #ifndef NO_BIO
  31696. BIO* bio;
  31697. X509_STORE_CTX* ctx;
  31698. X509_STORE* store;
  31699. #endif
  31700. char der[] = "certs/ca-cert.der";
  31701. XFILE fp;
  31702. printf(testingFmt, "wolfSSL_X509()");
  31703. AssertNotNull(x509 = X509_new());
  31704. X509_free(x509);
  31705. #ifndef NO_BIO
  31706. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  31707. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  31708. #ifdef WOLFSSL_CERT_GEN
  31709. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  31710. #endif
  31711. AssertNotNull(ctx = X509_STORE_CTX_new());
  31712. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  31713. AssertNotNull(store = X509_STORE_new());
  31714. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  31715. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  31716. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  31717. X509_STORE_CTX_free(ctx);
  31718. X509_STORE_free(store);
  31719. X509_free(x509);
  31720. BIO_free(bio);
  31721. #endif
  31722. /** d2i_X509_fp test **/
  31723. fp = XFOPEN(der, "rb");
  31724. AssertTrue((fp != XBADFILE));
  31725. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  31726. AssertNotNull(x509);
  31727. X509_free(x509);
  31728. XFCLOSE(fp);
  31729. fp = XFOPEN(der, "rb");
  31730. AssertTrue((fp != XBADFILE));
  31731. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  31732. AssertNotNull(x509);
  31733. X509_free(x509);
  31734. XFCLOSE(fp);
  31735. /* X509_up_ref test */
  31736. AssertIntEQ(X509_up_ref(NULL), 0);
  31737. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  31738. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  31739. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  31740. X509_free(x509); /* refCount = 2 */
  31741. X509_free(x509); /* refCount = 1 */
  31742. X509_free(x509); /* refCount = 0, free */
  31743. printf(resultFmt, passed);
  31744. #endif
  31745. return 0;
  31746. }
  31747. static int test_wolfSSL_X509_get_ext_count(void)
  31748. {
  31749. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31750. !defined(NO_RSA)
  31751. int ret = 0;
  31752. WOLFSSL_X509* x509;
  31753. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  31754. FILE* f;
  31755. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  31756. /* NULL parameter check */
  31757. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  31758. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  31759. SSL_FILETYPE_PEM));
  31760. AssertIntEQ(X509_get_ext_count(x509), 5);
  31761. wolfSSL_X509_free(x509);
  31762. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  31763. SSL_FILETYPE_PEM));
  31764. AssertIntEQ(X509_get_ext_count(x509), 5);
  31765. wolfSSL_X509_free(x509);
  31766. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  31767. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  31768. fclose(f);
  31769. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  31770. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  31771. printf(resultFmt, ret == 4 ? passed : failed);
  31772. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  31773. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  31774. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  31775. wolfSSL_X509_free(x509);
  31776. printf(resultFmt, passed);
  31777. #endif
  31778. return 0;
  31779. }
  31780. static int test_wolfSSL_X509_sign2(void)
  31781. {
  31782. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  31783. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  31784. defined(WOLFSSL_CERT_EXT) && \
  31785. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  31786. WOLFSSL_X509 *x509, *ca;
  31787. const unsigned char *der;
  31788. const unsigned char *pt;
  31789. WOLFSSL_EVP_PKEY *priv;
  31790. WOLFSSL_X509_NAME *name;
  31791. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  31792. int derSz;
  31793. const int year = 365*24*60*60;
  31794. const int day = 24*60*60;
  31795. const int hour = 60*60;
  31796. const int mini = 60;
  31797. time_t t;
  31798. const unsigned char expected[] = {
  31799. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xfb, 0xa0, 0x03, 0x02, 0x01,
  31800. 0x02, 0x02, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3, 0xa6,
  31801. 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c, 0x30,
  31802. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  31803. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  31804. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
  31805. 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61,
  31806. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42,
  31807. 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
  31808. 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74,
  31809. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0a,
  31810. 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18,
  31811. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  31812. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  31813. 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  31814. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f,
  31815. 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17,
  31816. 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  31817. 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  31818. 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  31819. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  31820. 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  31821. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07,
  31822. 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15, 0x30,
  31823. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c, 0x66,
  31824. 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  31825. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67, 0x72,
  31826. 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38, 0x31,
  31827. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  31828. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  31829. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  31830. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  31831. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82,
  31832. 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  31833. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
  31834. 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b, 0xfe,
  31835. 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c, 0x74,
  31836. 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2, 0x07,
  31837. 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4, 0x81,
  31838. 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8, 0x36,
  31839. 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef, 0xec,
  31840. 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc, 0x7f,
  31841. 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7, 0xc5,
  31842. 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01, 0x56,
  31843. 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c, 0xef,
  31844. 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1, 0xa3,
  31845. 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e, 0x97,
  31846. 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43, 0x14,
  31847. 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88, 0x40,
  31848. 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52, 0x65,
  31849. 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c, 0x7b,
  31850. 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3, 0x3b,
  31851. 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a, 0xd9,
  31852. 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4, 0xd1,
  31853. 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5, 0x72,
  31854. 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15, 0xf7,
  31855. 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3, 0x02,
  31856. 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x4f, 0x30, 0x82, 0x01, 0x4b,
  31857. 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  31858. 0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30,
  31859. 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
  31860. 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03,
  31861. 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  31862. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  31863. 0x85, 0x65, 0xc0, 0x30, 0x81, 0xde, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04,
  31864. 0x81, 0xd6, 0x30, 0x81, 0xd3, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  31865. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  31866. 0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81, 0x9e,
  31867. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  31868. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07,
  31869. 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  31870. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61,
  31871. 0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c,
  31872. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38,
  31873. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50,
  31874. 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32,
  31875. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  31876. 0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73,
  31877. 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  31878. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e,
  31879. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  31880. 0x6f, 0x6d, 0x82, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3,
  31881. 0xa6, 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c,
  31882. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  31883. 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b,
  31884. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  31885. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  31886. 0x01, 0x01, 0x00, 0xa3, 0x41, 0x43, 0x93, 0x30, 0x92, 0x98, 0xfe, 0x57,
  31887. 0xd0, 0x39, 0x7c, 0x50, 0x06, 0x50, 0x20, 0x80, 0x0e, 0x28, 0x95, 0x79,
  31888. 0xb4, 0xf1, 0x6b, 0x6a, 0xab, 0x78, 0x30, 0x93, 0x49, 0x0a, 0x6a, 0x19,
  31889. 0x09, 0xae, 0x31, 0xc6, 0x8e, 0xcc, 0x69, 0x26, 0x89, 0x37, 0xc1, 0x57,
  31890. 0x58, 0x75, 0xae, 0xbf, 0x13, 0xc8, 0xd6, 0xad, 0xd0, 0x0f, 0x57, 0xcd,
  31891. 0x32, 0xa8, 0xda, 0xa8, 0x1b, 0xbf, 0xb5, 0xcd, 0x16, 0x14, 0x56, 0x86,
  31892. 0x84, 0xb4, 0xab, 0x93, 0x52, 0x74, 0xfd, 0x96, 0x9f, 0x6d, 0xbe, 0xdb,
  31893. 0x75, 0x5e, 0x76, 0xfe, 0xa6, 0x37, 0xe5, 0x5f, 0xcb, 0x62, 0x77, 0xc7,
  31894. 0xd6, 0xcb, 0xb4, 0xf6, 0x43, 0xc8, 0x47, 0xdf, 0x12, 0x16, 0x28, 0x29,
  31895. 0x61, 0xd1, 0xdc, 0x9d, 0x37, 0x9f, 0xe5, 0x71, 0x52, 0xae, 0xb8, 0x12,
  31896. 0xec, 0x32, 0x9f, 0x03, 0x1a, 0x66, 0x98, 0xd8, 0xb0, 0x40, 0x71, 0x4c,
  31897. 0xee, 0x64, 0x15, 0x48, 0x0c, 0x5c, 0x8a, 0x47, 0x20, 0xbd, 0x07, 0xc0,
  31898. 0x30, 0xf8, 0x84, 0xe6, 0x29, 0x6d, 0xa9, 0x32, 0x53, 0x02, 0x4d, 0x3c,
  31899. 0x99, 0x6e, 0x63, 0xfe, 0x39, 0x9c, 0x05, 0xa6, 0xa0, 0x0c, 0x1e, 0x11,
  31900. 0xa4, 0x86, 0x6a, 0x89, 0x76, 0x54, 0x17, 0x68, 0x5d, 0x35, 0x9a, 0xd7,
  31901. 0x5e, 0x27, 0x0e, 0xbb, 0xba, 0x67, 0x4d, 0x62, 0x12, 0xa8, 0x46, 0x1f,
  31902. 0x0e, 0xd8, 0x7d, 0xc0, 0xae, 0x30, 0xc2, 0x45, 0x71, 0xab, 0xb1, 0xc1,
  31903. 0xfb, 0xdc, 0x03, 0x7a, 0x52, 0xe6, 0x57, 0xf9, 0x7f, 0x65, 0x6b, 0x4e,
  31904. 0x44, 0x64, 0xe8, 0x77, 0x82, 0x1c, 0xc8, 0xfa, 0x09, 0xc7, 0x2f, 0xa9,
  31905. 0x40, 0x87, 0x8e, 0x0e, 0x49, 0xc2, 0x7d, 0x97, 0x27, 0x79, 0x90, 0xc2,
  31906. 0x90, 0x13, 0xa7, 0x49, 0xb7, 0xd7, 0xc5, 0x02, 0x32, 0x4f, 0x1e, 0x34,
  31907. 0x4a, 0xa6, 0xe4, 0xbd, 0xa5, 0xc6, 0xec
  31908. };
  31909. printf(testingFmt, "wolfSSL_X509_sign2");
  31910. pt = ca_key_der_2048;
  31911. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  31912. sizeof_ca_key_der_2048));
  31913. pt = client_cert_der_2048;
  31914. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  31915. sizeof_client_cert_der_2048));
  31916. pt = ca_cert_der_2048;
  31917. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  31918. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  31919. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  31920. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  31921. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  31922. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  31923. AssertIntEQ(notAfter->length, 13);
  31924. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  31925. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  31926. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  31927. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  31928. AssertIntEQ(derSz, sizeof(expected));
  31929. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  31930. wolfSSL_X509_free(ca);
  31931. wolfSSL_X509_free(x509);
  31932. wolfSSL_EVP_PKEY_free(priv);
  31933. wolfSSL_ASN1_TIME_free(notBefore);
  31934. wolfSSL_ASN1_TIME_free(notAfter);
  31935. printf(resultFmt, passed);
  31936. #endif
  31937. return 0;
  31938. }
  31939. static int test_wolfSSL_X509_sign(void)
  31940. {
  31941. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31942. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  31943. int ret;
  31944. char *cn;
  31945. word32 cnSz;
  31946. X509_NAME *name;
  31947. X509 *x509, *ca;
  31948. DecodedCert dCert;
  31949. EVP_PKEY *pub;
  31950. EVP_PKEY *priv;
  31951. EVP_MD_CTX *mctx;
  31952. #if defined(USE_CERT_BUFFERS_1024)
  31953. const unsigned char* rsaPriv = client_key_der_1024;
  31954. const unsigned char* rsaPub = client_keypub_der_1024;
  31955. const unsigned char* certIssuer = client_cert_der_1024;
  31956. long clientKeySz = (long)sizeof_client_key_der_1024;
  31957. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  31958. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  31959. #elif defined(USE_CERT_BUFFERS_2048)
  31960. const unsigned char* rsaPriv = client_key_der_2048;
  31961. const unsigned char* rsaPub = client_keypub_der_2048;
  31962. const unsigned char* certIssuer = client_cert_der_2048;
  31963. long clientKeySz = (long)sizeof_client_key_der_2048;
  31964. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  31965. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  31966. #endif
  31967. byte sn[16];
  31968. int snSz = sizeof(sn);
  31969. printf(testingFmt, "wolfSSL_X509_sign");
  31970. /* Set X509_NAME fields */
  31971. AssertNotNull(name = X509_NAME_new());
  31972. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  31973. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  31974. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  31975. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  31976. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  31977. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  31978. /* Get private and public keys */
  31979. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  31980. clientKeySz));
  31981. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  31982. AssertNotNull(x509 = X509_new());
  31983. /* Set version 3 */
  31984. AssertIntNE(X509_set_version(x509, 2L), 0);
  31985. /* Set subject name, add pubkey, and sign certificate */
  31986. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  31987. X509_NAME_free(name);
  31988. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  31989. #ifdef WOLFSSL_ALT_NAMES
  31990. /* Add some subject alt names */
  31991. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  31992. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  31993. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31994. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  31995. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31996. "sphygmomanometer",
  31997. ASN_DNS_TYPE), SSL_SUCCESS);
  31998. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  31999. "supercalifragilisticexpialidocious",
  32000. ASN_DNS_TYPE), SSL_SUCCESS);
  32001. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32002. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  32003. ASN_DNS_TYPE), SSL_SUCCESS);
  32004. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32005. {
  32006. unsigned char ip4_type[] = {127,128,0,255};
  32007. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  32008. 0xff, 0xee, 0x99, 0x88,
  32009. 0x77, 0x66, 0x55, 0x44,
  32010. 0x00, 0x33, 0x22, 0x11};
  32011. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  32012. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  32013. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  32014. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  32015. }
  32016. #endif
  32017. #endif /* WOLFSSL_ALT_NAMES */
  32018. /* test valid sign case */
  32019. ret = X509_sign(x509, priv, EVP_sha256());
  32020. /* test valid X509_sign_ctx case */
  32021. AssertNotNull(mctx = EVP_MD_CTX_new());
  32022. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  32023. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  32024. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  32025. AssertIntEQ(X509_get_ext_count(x509), 1);
  32026. #endif
  32027. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32028. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  32029. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  32030. #endif
  32031. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  32032. WOLFSSL_SUCCESS);
  32033. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  32034. /* Variation in size depends on ASN.1 encoding when MSB is set.
  32035. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  32036. * with the MSB set. See GenerateInteger in asn.c */
  32037. #ifndef USE_CERT_BUFFERS_1024
  32038. #ifndef WOLFSSL_ALT_NAMES
  32039. /* Valid case - size should be 798-797 with 16 byte serial number */
  32040. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  32041. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32042. /* Valid case - size should be 955-956 with 16 byte serial number */
  32043. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  32044. #else
  32045. /* Valid case - size should be 926-927 with 16 byte serial number */
  32046. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  32047. #endif
  32048. #else
  32049. #ifndef WOLFSSL_ALT_NAMES
  32050. /* Valid case - size should be 537-538 with 16 byte serial number */
  32051. AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
  32052. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32053. /* Valid case - size should be 695-696 with 16 byte serial number */
  32054. AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
  32055. #else
  32056. /* Valid case - size should be 666-667 with 16 byte serial number */
  32057. AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
  32058. #endif
  32059. #endif
  32060. /* check that issuer name is as expected after signature */
  32061. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  32062. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  32063. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  32064. AssertNotNull(name = X509_get_subject_name(ca));
  32065. cnSz = X509_NAME_get_sz(name);
  32066. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32067. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  32068. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  32069. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32070. #ifdef WOLFSSL_MULTI_ATTRIB
  32071. /* test adding multiple OU's to the signer */
  32072. AssertNotNull(name = X509_get_subject_name(ca));
  32073. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32074. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  32075. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32076. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  32077. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  32078. #endif
  32079. AssertNotNull(name = X509_get_subject_name(ca));
  32080. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  32081. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  32082. AssertNotNull(name = X509_get_issuer_name(x509));
  32083. cnSz = X509_NAME_get_sz(name);
  32084. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32085. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  32086. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  32087. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  32088. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32089. FreeDecodedCert(&dCert);
  32090. /* Test invalid parameters */
  32091. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  32092. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  32093. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  32094. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  32095. EVP_MD_CTX_free(mctx);
  32096. AssertNotNull(mctx = EVP_MD_CTX_new());
  32097. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  32098. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  32099. /* test invalid version number */
  32100. #if defined(OPENSSL_ALL)
  32101. AssertIntNE(X509_set_version(x509, 6L), 0);
  32102. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  32103. /* uses ParseCert which fails on bad version number */
  32104. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  32105. #endif
  32106. EVP_MD_CTX_free(mctx);
  32107. EVP_PKEY_free(priv);
  32108. EVP_PKEY_free(pub);
  32109. X509_free(x509);
  32110. X509_free(ca);
  32111. printf(resultFmt, passed);
  32112. #endif
  32113. return 0;
  32114. }
  32115. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  32116. {
  32117. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  32118. X509* x509 = NULL;
  32119. const X509_ALGOR* alg;
  32120. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  32121. AssertNotNull(x509 = X509_new());
  32122. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  32123. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  32124. X509_free(x509);
  32125. printf(resultFmt, passed);
  32126. #endif
  32127. return 0;
  32128. }
  32129. static int test_wolfSSL_X509_ALGOR_get0(void)
  32130. {
  32131. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32132. !defined(NO_SHA256) && !defined(NO_RSA)
  32133. X509* x509 = NULL;
  32134. const ASN1_OBJECT* obj = NULL;
  32135. const X509_ALGOR* alg;
  32136. int pptype = 0;
  32137. const void *ppval = NULL;
  32138. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  32139. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  32140. SSL_FILETYPE_PEM));
  32141. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  32142. /* Invalid case */
  32143. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  32144. AssertNull(obj);
  32145. /* Valid case */
  32146. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  32147. AssertNotNull(obj);
  32148. AssertNull(ppval);
  32149. AssertIntNE(pptype, 0);
  32150. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  32151. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  32152. X509_free(x509);
  32153. printf(resultFmt, passed);
  32154. #endif
  32155. return 0;
  32156. }
  32157. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  32158. {
  32159. #if defined(OPENSSL_EXTRA)
  32160. X509_VERIFY_PARAM *paramTo;
  32161. X509_VERIFY_PARAM *paramFrom;
  32162. int ret;
  32163. char testIPv4[] = "127.0.0.1";
  32164. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  32165. char testhostName1[] = "foo.hoge.com";
  32166. char testhostName2[] = "foobar.hoge.com";
  32167. printf(testingFmt, "wolfSSL_X509()");
  32168. paramTo = X509_VERIFY_PARAM_new();
  32169. AssertNotNull(paramTo);
  32170. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  32171. paramFrom = X509_VERIFY_PARAM_new();
  32172. AssertNotNull(paramFrom);
  32173. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  32174. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  32175. (int)XSTRLEN(testhostName1));
  32176. AssertIntEQ(1, ret);
  32177. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  32178. (int)XSTRLEN(testhostName1)));
  32179. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  32180. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  32181. AssertIntEQ(0x01, paramFrom->hostFlags);
  32182. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  32183. AssertIntEQ(0, ret);
  32184. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  32185. AssertIntEQ(1, ret);
  32186. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  32187. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  32188. AssertIntEQ(1, ret);
  32189. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  32190. AssertIntEQ(1, ret);
  32191. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32192. /* null pointer */
  32193. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  32194. AssertIntEQ(WOLFSSL_FAILURE, ret);
  32195. /* in the case of "from" null, returns success */
  32196. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  32197. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  32198. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  32199. AssertIntEQ(WOLFSSL_FAILURE, ret);
  32200. /* inherit flags test : VPARAM_DEFAULT */
  32201. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32202. AssertIntEQ(1, ret);
  32203. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  32204. (int)XSTRLEN(testhostName1)));
  32205. AssertIntEQ(0x01, paramTo->hostFlags);
  32206. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32207. /* inherit flags test : VPARAM OVERWRITE */
  32208. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  32209. (int)XSTRLEN(testhostName2));
  32210. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  32211. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  32212. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  32213. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32214. AssertIntEQ(1, ret);
  32215. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  32216. (int)XSTRLEN(testhostName1)));
  32217. AssertIntEQ(0x01, paramTo->hostFlags);
  32218. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32219. /* inherit flags test : VPARAM_RESET_FLAGS */
  32220. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  32221. (int)XSTRLEN(testhostName2));
  32222. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  32223. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  32224. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  32225. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32226. AssertIntEQ(1, ret);
  32227. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  32228. (int)XSTRLEN(testhostName1)));
  32229. AssertIntEQ(0x01, paramTo->hostFlags);
  32230. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  32231. /* inherit flags test : VPARAM_LOCKED */
  32232. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  32233. (int)XSTRLEN(testhostName2));
  32234. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  32235. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  32236. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  32237. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  32238. AssertIntEQ(1, ret);
  32239. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  32240. (int)XSTRLEN(testhostName2)));
  32241. AssertIntEQ(0x00, paramTo->hostFlags);
  32242. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  32243. /* test for incorrect parameters */
  32244. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  32245. AssertIntEQ(0, ret);
  32246. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  32247. AssertIntEQ(0, ret);
  32248. /* inherit flags test : VPARAM_ONCE, not testable yet */
  32249. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  32250. AssertIntEQ(1, ret);
  32251. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  32252. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  32253. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  32254. AssertIntEQ(1, ret);
  32255. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  32256. AssertIntEQ(0, ret);
  32257. X509_VERIFY_PARAM_free(paramTo);
  32258. X509_VERIFY_PARAM_free(paramFrom);
  32259. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  32260. printf(resultFmt, passed);
  32261. #endif
  32262. return 0;
  32263. }
  32264. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  32265. static int test_wolfSSL_check_domain_verify_count = 0;
  32266. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  32267. WOLFSSL_X509_STORE_CTX* store)
  32268. {
  32269. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  32270. AssertIntEQ(preverify, 1);
  32271. test_wolfSSL_check_domain_verify_count++;
  32272. return 1;
  32273. }
  32274. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  32275. {
  32276. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  32277. /* Domain check should only be done on the leaf cert */
  32278. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  32279. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  32280. "wolfSSL Server Chain", 0), 1);
  32281. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  32282. test_wolfSSL_check_domain_verify_cb);
  32283. }
  32284. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  32285. {
  32286. /* Use a cert with different domains in chain */
  32287. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  32288. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  32289. }
  32290. static int test_wolfSSL_check_domain(void)
  32291. {
  32292. tcp_ready ready;
  32293. func_args client_args;
  32294. func_args server_args;
  32295. THREAD_TYPE serverThread;
  32296. callback_functions func_cb_client;
  32297. callback_functions func_cb_server;
  32298. printf(testingFmt, "wolfSSL_check_domain");
  32299. XMEMSET(&client_args, 0, sizeof(func_args));
  32300. XMEMSET(&server_args, 0, sizeof(func_args));
  32301. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  32302. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  32303. #ifdef WOLFSSL_TIRTOS
  32304. fdOpenSession(Task_self());
  32305. #endif
  32306. StartTCP();
  32307. InitTcpReady(&ready);
  32308. #if defined(USE_WINDOWS_API)
  32309. /* use RNG to get random port if using windows */
  32310. ready.port = GetRandomPort();
  32311. #endif
  32312. server_args.signal = &ready;
  32313. client_args.signal = &ready;
  32314. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  32315. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  32316. client_args.callbacks = &func_cb_client;
  32317. server_args.callbacks = &func_cb_server;
  32318. start_thread(test_server_nofail, &server_args, &serverThread);
  32319. wait_tcp_ready(&server_args);
  32320. test_client_nofail(&client_args, NULL);
  32321. join_thread(serverThread);
  32322. AssertTrue(client_args.return_code);
  32323. AssertTrue(server_args.return_code);
  32324. FreeTcpReady(&ready);
  32325. /* Should have been called once for each cert in sent chain */
  32326. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  32327. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  32328. #else
  32329. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  32330. #endif
  32331. printf(resultFmt, passed);
  32332. return 0;
  32333. }
  32334. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  32335. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  32336. {
  32337. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  32338. X509* x509 = NULL;
  32339. X509_PUBKEY* pubKey;
  32340. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  32341. AssertNotNull(x509 = X509_new());
  32342. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  32343. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  32344. X509_free(x509);
  32345. printf(resultFmt, passed);
  32346. #endif
  32347. return 0;
  32348. }
  32349. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  32350. {
  32351. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32352. !defined(NO_SHA256) && !defined(NO_RSA)
  32353. X509* x509 = NULL;
  32354. ASN1_OBJECT* obj = NULL;
  32355. const ASN1_OBJECT* pa_oid = NULL;
  32356. X509_PUBKEY* pubKey;
  32357. X509_PUBKEY* pubKey2;
  32358. EVP_PKEY* evpKey;
  32359. const unsigned char *pk;
  32360. int ppklen, pptype;
  32361. X509_ALGOR *pa;
  32362. const void *pval;
  32363. printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
  32364. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  32365. SSL_FILETYPE_PEM));
  32366. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  32367. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  32368. AssertNotNull(pk);
  32369. AssertNotNull(pa);
  32370. AssertNotNull(pubKey);
  32371. AssertIntGT(ppklen, 0);
  32372. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  32373. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  32374. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  32375. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  32376. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  32377. AssertNotNull(pk);
  32378. AssertNotNull(pa);
  32379. AssertIntGT(ppklen, 0);
  32380. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  32381. AssertNotNull(pa_oid);
  32382. AssertNull(pval);
  32383. AssertIntEQ(pptype, V_ASN1_NULL);
  32384. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  32385. X509_PUBKEY_free(pubKey2);
  32386. X509_free(x509);
  32387. EVP_PKEY_free(evpKey);
  32388. printf(resultFmt, passed);
  32389. #endif
  32390. return 0;
  32391. }
  32392. static int test_wolfSSL_X509_PUBKEY_EC(void)
  32393. {
  32394. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  32395. X509* x509 = NULL;
  32396. ASN1_OBJECT* obj = NULL;
  32397. ASN1_OBJECT* poid;
  32398. const ASN1_OBJECT* pa_oid = NULL;
  32399. X509_PUBKEY* pubKey;
  32400. X509_PUBKEY* pubKey2;
  32401. EVP_PKEY* evpKey;
  32402. const unsigned char *pk;
  32403. int ppklen, pptype;
  32404. X509_ALGOR *pa;
  32405. const void *pval;
  32406. char buf[50];
  32407. printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
  32408. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  32409. SSL_FILETYPE_PEM));
  32410. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  32411. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  32412. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  32413. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  32414. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  32415. AssertNotNull(pk);
  32416. AssertNotNull(pa);
  32417. AssertIntGT(ppklen, 0);
  32418. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  32419. AssertNotNull(pa_oid);
  32420. AssertNotNull(pval);
  32421. AssertIntEQ(pptype, V_ASN1_OBJECT);
  32422. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  32423. poid = (ASN1_OBJECT *)pval;
  32424. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  32425. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  32426. X509_PUBKEY_free(pubKey2);
  32427. X509_free(x509);
  32428. EVP_PKEY_free(evpKey);
  32429. printf(resultFmt, passed);
  32430. #endif
  32431. return 0;
  32432. }
  32433. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  32434. {
  32435. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  32436. word32 bytes;
  32437. #ifdef USE_CERT_BUFFERS_1024
  32438. byte tmp[ONEK_BUF];
  32439. #elif defined(USE_CERT_BUFFERS_2048)
  32440. byte tmp[TWOK_BUF];
  32441. #else
  32442. byte tmp[TWOK_BUF];
  32443. #endif /* END USE_CERT_BUFFERS_1024 */
  32444. const unsigned char* dsaKeyDer = tmp;
  32445. ASN1_OBJECT* obj = NULL;
  32446. ASN1_STRING* str;
  32447. const ASN1_OBJECT* pa_oid = NULL;
  32448. X509_PUBKEY* pubKey = NULL;
  32449. EVP_PKEY* evpKey = NULL;
  32450. const unsigned char *pk;
  32451. int ppklen, pptype;
  32452. X509_ALGOR *pa;
  32453. const void *pval;
  32454. printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
  32455. #ifdef USE_CERT_BUFFERS_1024
  32456. XMEMSET(tmp, 0, sizeof(tmp));
  32457. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  32458. bytes = sizeof_dsa_key_der_1024;
  32459. #elif defined(USE_CERT_BUFFERS_2048)
  32460. XMEMSET(tmp, 0, sizeof(tmp));
  32461. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  32462. bytes = sizeof_dsa_key_der_2048;
  32463. #else
  32464. {
  32465. XFILE fp;
  32466. XMEMSET(tmp, 0, sizeof(tmp));
  32467. fp = XFOPEN("./certs/dsa2048.der", "rb");
  32468. if (fp == XBADFILE) {
  32469. return WOLFSSL_BAD_FILE;
  32470. }
  32471. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  32472. XFCLOSE(fp);
  32473. }
  32474. #endif
  32475. /* Initialize pkey with der format dsa key */
  32476. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  32477. AssertNotNull(pubKey = X509_PUBKEY_new());
  32478. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  32479. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  32480. AssertNotNull(pk);
  32481. AssertNotNull(pa);
  32482. AssertIntGT(ppklen, 0);
  32483. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  32484. AssertNotNull(pa_oid);
  32485. AssertNotNull(pval);
  32486. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  32487. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  32488. str = (ASN1_STRING *)pval;
  32489. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  32490. #ifdef USE_CERT_BUFFERS_1024
  32491. AssertIntEQ(ASN1_STRING_length(str), 291);
  32492. #else
  32493. AssertIntEQ(ASN1_STRING_length(str), 549);
  32494. #endif /* END USE_CERT_BUFFERS_1024 */
  32495. X509_PUBKEY_free(pubKey);
  32496. EVP_PKEY_free(evpKey);
  32497. printf(resultFmt, passed);
  32498. #endif
  32499. return 0;
  32500. }
  32501. static int test_wolfSSL_RAND(void)
  32502. {
  32503. #if defined(OPENSSL_EXTRA)
  32504. byte seed[16];
  32505. printf(testingFmt, "wolfSSL_RAND()");
  32506. RAND_seed(seed, sizeof(seed));
  32507. AssertIntEQ(RAND_poll(), 1);
  32508. RAND_cleanup();
  32509. AssertIntEQ(RAND_egd(NULL), -1);
  32510. #ifndef NO_FILESYSTEM
  32511. {
  32512. char fname[100];
  32513. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  32514. AssertIntEQ(RAND_write_file(NULL), 0);
  32515. }
  32516. #endif
  32517. printf(resultFmt, passed);
  32518. #endif
  32519. return 0;
  32520. }
  32521. static int test_wolfSSL_BUF(void)
  32522. {
  32523. #if defined(OPENSSL_EXTRA)
  32524. BUF_MEM* buf;
  32525. AssertNotNull(buf = BUF_MEM_new());
  32526. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  32527. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  32528. BUF_MEM_free(buf);
  32529. #endif /* OPENSSL_EXTRA */
  32530. return 0;
  32531. }
  32532. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  32533. static int stub_rand_seed(const void *buf, int num)
  32534. {
  32535. (void)buf;
  32536. (void)num;
  32537. return 123;
  32538. }
  32539. static int stub_rand_bytes(unsigned char *buf, int num)
  32540. {
  32541. (void)buf;
  32542. (void)num;
  32543. return 456;
  32544. }
  32545. static byte* was_stub_rand_cleanup_called(void)
  32546. {
  32547. static byte was_called = 0;
  32548. return &was_called;
  32549. }
  32550. static void stub_rand_cleanup(void)
  32551. {
  32552. byte* was_called = was_stub_rand_cleanup_called();
  32553. *was_called = 1;
  32554. return;
  32555. }
  32556. static byte* was_stub_rand_add_called(void)
  32557. {
  32558. static byte was_called = 0;
  32559. return &was_called;
  32560. }
  32561. static int stub_rand_add(const void *buf, int num, double entropy)
  32562. {
  32563. byte* was_called = was_stub_rand_add_called();
  32564. (void)buf;
  32565. (void)num;
  32566. (void)entropy;
  32567. *was_called = 1;
  32568. return 0;
  32569. }
  32570. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  32571. {
  32572. (void)buf;
  32573. (void)num;
  32574. return 9876;
  32575. }
  32576. static int stub_rand_status(void)
  32577. {
  32578. return 5432;
  32579. }
  32580. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  32581. static int test_wolfSSL_RAND_set_rand_method(void)
  32582. {
  32583. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  32584. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  32585. unsigned char* buf = NULL;
  32586. int num = 0;
  32587. double entropy = 0;
  32588. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  32589. byte* was_add_called = was_stub_rand_add_called();
  32590. printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
  32591. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  32592. DYNAMIC_TYPE_TMP_BUFFER);
  32593. AssertIntNE(wolfSSL_RAND_status(), 5432);
  32594. AssertIntEQ(*was_cleanup_called, 0);
  32595. RAND_cleanup();
  32596. AssertIntEQ(*was_cleanup_called, 0);
  32597. rand_methods.seed = &stub_rand_seed;
  32598. rand_methods.bytes = &stub_rand_bytes;
  32599. rand_methods.cleanup = &stub_rand_cleanup;
  32600. rand_methods.add = &stub_rand_add;
  32601. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  32602. rand_methods.status = &stub_rand_status;
  32603. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  32604. AssertIntEQ(RAND_seed(buf, num), 123);
  32605. AssertIntEQ(RAND_bytes(buf, num), 456);
  32606. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  32607. AssertIntEQ(RAND_status(), 5432);
  32608. AssertIntEQ(*was_add_called, 0);
  32609. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  32610. RAND_add(buf, num, entropy);
  32611. AssertIntEQ(*was_add_called, 1);
  32612. was_add_called = 0;
  32613. AssertIntEQ(*was_cleanup_called, 0);
  32614. RAND_cleanup();
  32615. AssertIntEQ(*was_cleanup_called, 1);
  32616. *was_cleanup_called = 0;
  32617. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  32618. AssertIntNE(RAND_status(), 5432);
  32619. AssertIntEQ(*was_cleanup_called, 0);
  32620. RAND_cleanup();
  32621. AssertIntEQ(*was_cleanup_called, 0);
  32622. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32623. printf(resultFmt, passed);
  32624. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  32625. return 0;
  32626. }
  32627. static int test_wolfSSL_RAND_bytes(void)
  32628. {
  32629. #if defined(OPENSSL_EXTRA)
  32630. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  32631. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  32632. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  32633. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  32634. int max_bufsize;
  32635. byte *my_buf;
  32636. printf(testingFmt, "test_wolfSSL_RAND_bytes()");
  32637. /* sanity check */
  32638. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  32639. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  32640. max_bufsize = size4;
  32641. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  32642. DYNAMIC_TYPE_TMP_BUFFER);
  32643. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  32644. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  32645. AssertNotNull(my_buf);
  32646. XMEMSET(my_buf, 0, max_bufsize);
  32647. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  32648. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  32649. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  32650. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  32651. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  32652. printf(resultFmt, passed);
  32653. #endif
  32654. return 0;
  32655. }
  32656. static int test_wolfSSL_BN_rand(void)
  32657. {
  32658. #if defined(OPENSSL_EXTRA)
  32659. BIGNUM* bn;
  32660. BIGNUM* range;
  32661. printf(testingFmt, "wolfSSL_BN_rand()");
  32662. /* Error conditions. */
  32663. /* NULL BN. */
  32664. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  32665. AssertNotNull(bn = BN_new());
  32666. /* Negative bits. */
  32667. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  32668. /* 0 bits and top is not -1. */
  32669. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  32670. /* 0 bits and bottom is not 0. */
  32671. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  32672. /* 1 bit and top is 1. */
  32673. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  32674. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  32675. AssertIntEQ(BN_num_bits(bn), 0);
  32676. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  32677. AssertIntEQ(BN_num_bits(bn), 8);
  32678. /* When top is 0, top bit should be 1. */
  32679. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  32680. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  32681. /* When top is 1, top 2 bits should be 1. */
  32682. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  32683. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  32684. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  32685. /* When bottom is 1, bottom bit should be 1. */
  32686. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  32687. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  32688. * requested number of bits up to the nearest multiple of 8. E.g. in this
  32689. * case, requesting a 13-bit random number would actually return a 16-bit
  32690. * random number. */
  32691. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  32692. AssertIntEQ(BN_num_bits(bn), 13);
  32693. AssertNotNull(range = BN_new());
  32694. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  32695. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  32696. BN_free(bn);
  32697. BN_free(range);
  32698. printf(resultFmt, passed);
  32699. #endif
  32700. return 0;
  32701. }
  32702. static int test_wolfSSL_pseudo_rand(void)
  32703. {
  32704. #if defined(OPENSSL_EXTRA)
  32705. BIGNUM* bn;
  32706. unsigned char bin[8];
  32707. int i;
  32708. printf(testingFmt, "wolfSSL_pseudo_rand()");
  32709. /* BN_pseudo_rand returns 1 on success 0 on failure
  32710. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  32711. for (i = 0; i < 10; i++) {
  32712. AssertNotNull(bn = BN_new());
  32713. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  32714. AssertIntGT(BN_bn2bin(bn, bin),0);
  32715. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  32716. BN_free(bn);
  32717. }
  32718. for (i = 0; i < 10; i++) {
  32719. AssertNotNull(bn = BN_new());
  32720. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  32721. AssertIntGT(BN_bn2bin(bn, bin),0);
  32722. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  32723. BN_free(bn);
  32724. }
  32725. printf(resultFmt, passed);
  32726. #endif
  32727. return 0;
  32728. }
  32729. static int test_wolfSSL_PKCS8_Compat(void)
  32730. {
  32731. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  32732. #ifndef NO_BIO
  32733. PKCS8_PRIV_KEY_INFO* pt;
  32734. BIO* bio;
  32735. XFILE f;
  32736. int bytes;
  32737. char pkcs8_buffer[512];
  32738. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  32739. EVP_PKEY *pkey = NULL;
  32740. #endif
  32741. printf(testingFmt, "wolfSSL_pkcs8()");
  32742. /* file from wolfssl/certs/ directory */
  32743. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  32744. AssertTrue(f != XBADFILE);
  32745. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  32746. XFCLOSE(f);
  32747. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32748. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  32749. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  32750. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  32751. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  32752. /* gets PKCS8 pointer to pkey */
  32753. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  32754. EVP_PKEY_free(pkey);
  32755. #endif
  32756. BIO_free(bio);
  32757. PKCS8_PRIV_KEY_INFO_free(pt);
  32758. printf(resultFmt, passed);
  32759. #endif
  32760. #endif
  32761. return 0;
  32762. }
  32763. static int test_wolfSSL_PKCS8_d2i(void)
  32764. {
  32765. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  32766. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  32767. * requires a 12 byte password in FIPS mode. This test ends up
  32768. * trying to use an 8 byte password. */
  32769. #ifndef NO_FILESYSTEM
  32770. unsigned char pkcs8_buffer[2048];
  32771. const unsigned char* p;
  32772. int bytes;
  32773. XFILE file;
  32774. WOLFSSL_EVP_PKEY* pkey = NULL;
  32775. #ifndef NO_BIO
  32776. BIO* bio;
  32777. #if defined(OPENSSL_ALL) && \
  32778. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  32779. defined(HAVE_ECC)) && \
  32780. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32781. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  32782. #endif
  32783. #endif
  32784. #ifndef NO_RSA
  32785. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  32786. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  32787. #ifndef NO_DES3
  32788. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  32789. #endif
  32790. #endif /* NO_RSA */
  32791. #ifdef HAVE_ECC
  32792. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  32793. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  32794. #ifndef NO_DES3
  32795. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  32796. #endif
  32797. #endif /* HAVE_ECC */
  32798. #endif /* !NO_FILESYSTEM */
  32799. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  32800. #ifndef NO_RSA
  32801. #ifdef USE_CERT_BUFFERS_1024
  32802. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  32803. int rsaSz = sizeof_server_key_der_1024;
  32804. #else
  32805. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  32806. int rsaSz = sizeof_server_key_der_2048;
  32807. #endif
  32808. #endif
  32809. #ifdef HAVE_ECC
  32810. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  32811. int ecSz = sizeof_ecc_key_der_256;
  32812. #endif
  32813. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  32814. #ifndef NO_FILESYSTEM
  32815. (void)pkcs8_buffer;
  32816. (void)p;
  32817. (void)bytes;
  32818. (void)file;
  32819. #ifndef NO_BIO
  32820. (void)bio;
  32821. #endif
  32822. #endif
  32823. #ifdef OPENSSL_ALL
  32824. #ifndef NO_RSA
  32825. /* Try to auto-detect normal RSA private key */
  32826. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  32827. EVP_PKEY_free(pkey);
  32828. #endif
  32829. #ifdef HAVE_ECC
  32830. /* Try to auto-detect normal EC private key */
  32831. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  32832. EVP_PKEY_free(pkey);
  32833. #endif
  32834. #endif /* OPENSSL_ALL */
  32835. #ifndef NO_FILESYSTEM
  32836. #ifndef NO_RSA
  32837. /* Get DER encoded RSA PKCS#8 data. */
  32838. file = XFOPEN(rsaDerPkcs8File, "rb");
  32839. AssertTrue(file != XBADFILE);
  32840. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32841. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32842. file)), 0);
  32843. XFCLOSE(file);
  32844. p = pkcs8_buffer;
  32845. #ifdef OPENSSL_ALL
  32846. /* Try to decode - auto-detect key type. */
  32847. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  32848. #else
  32849. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  32850. #endif
  32851. /* Get PEM encoded RSA PKCS#8 data. */
  32852. file = XFOPEN(rsaPemPkcs8File, "rb");
  32853. AssertTrue(file != XBADFILE);
  32854. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32855. file)), 0);
  32856. XFCLOSE(file);
  32857. #if defined(OPENSSL_ALL) && \
  32858. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32859. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32860. /* Write PKCS#8 PEM to BIO. */
  32861. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32862. NULL), bytes);
  32863. /* Compare file and written data */
  32864. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  32865. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  32866. BIO_free(bio);
  32867. #ifndef NO_DES3
  32868. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32869. /* Write Encrypted PKCS#8 PEM to BIO. */
  32870. bytes = 1834;
  32871. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  32872. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  32873. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  32874. (void*)"yassl123"));
  32875. EVP_PKEY_free(evpPkey);
  32876. BIO_free(bio);
  32877. #endif /* !NO_DES3 */
  32878. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32879. EVP_PKEY_free(pkey);
  32880. /* PKCS#8 encrypted RSA key */
  32881. #ifndef NO_DES3
  32882. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  32883. AssertTrue(file != XBADFILE);
  32884. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32885. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32886. file)), 0);
  32887. XFCLOSE(file);
  32888. #if defined(OPENSSL_ALL) && \
  32889. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32890. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32891. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  32892. (void*)"yassl123"));
  32893. EVP_PKEY_free(pkey);
  32894. BIO_free(bio);
  32895. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32896. #endif /* !NO_DES3 */
  32897. #endif /* NO_RSA */
  32898. #ifdef HAVE_ECC
  32899. /* PKCS#8 encode EC key */
  32900. file = XFOPEN(ecDerPkcs8File, "rb");
  32901. AssertTrue(file != XBADFILE);
  32902. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32903. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32904. file)), 0);
  32905. XFCLOSE(file);
  32906. p = pkcs8_buffer;
  32907. #ifdef OPENSSL_ALL
  32908. /* Try to decode - auto-detect key type. */
  32909. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  32910. #else
  32911. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  32912. #endif
  32913. /* Get PEM encoded RSA PKCS#8 data. */
  32914. file = XFOPEN(ecPemPkcs8File, "rb");
  32915. AssertTrue(file != XBADFILE);
  32916. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32917. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32918. file)), 0);
  32919. XFCLOSE(file);
  32920. #if defined(OPENSSL_ALL) && \
  32921. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  32922. defined(HAVE_AES_CBC)
  32923. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32924. /* Write PKCS#8 PEM to BIO. */
  32925. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  32926. NULL), bytes);
  32927. /* Compare file and written data */
  32928. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  32929. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  32930. BIO_free(bio);
  32931. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32932. /* Write Encrypted PKCS#8 PEM to BIO. */
  32933. bytes = 379;
  32934. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  32935. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  32936. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  32937. (void*)"yassl123"));
  32938. EVP_PKEY_free(evpPkey);
  32939. BIO_free(bio);
  32940. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  32941. EVP_PKEY_free(pkey);
  32942. /* PKCS#8 encrypted EC key */
  32943. #ifndef NO_DES3
  32944. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  32945. AssertTrue(file != XBADFILE);
  32946. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  32947. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  32948. file)), 0);
  32949. XFCLOSE(file);
  32950. #if defined(OPENSSL_ALL) && \
  32951. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  32952. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  32953. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  32954. (void*)"yassl123"));
  32955. EVP_PKEY_free(pkey);
  32956. BIO_free(bio);
  32957. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  32958. #endif /* !NO_DES3 */
  32959. #endif /* HAVE_ECC */
  32960. #endif /* !NO_FILESYSTEM */
  32961. printf(resultFmt, passed);
  32962. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  32963. return 0;
  32964. }
  32965. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  32966. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  32967. #define LOGGING_THREADS 5
  32968. #define ERROR_COUNT 10
  32969. static volatile int loggingThreadsReady;
  32970. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  32971. {
  32972. const char* file;
  32973. int line;
  32974. int err;
  32975. int errorCount = 0;
  32976. int i;
  32977. (void)args;
  32978. while (!loggingThreadsReady);
  32979. for (i = 0; i < ERROR_COUNT; i++)
  32980. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  32981. while ((err = ERR_get_error_line(&file, &line))) {
  32982. AssertIntEQ(err, 990 + errorCount);
  32983. errorCount++;
  32984. }
  32985. AssertIntEQ(errorCount, ERROR_COUNT);
  32986. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  32987. errorCount = 0;
  32988. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  32989. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  32990. while ((err = ERR_get_error_line(&file, &line))) {
  32991. AssertIntEQ(err, 990 + errorCount);
  32992. errorCount++;
  32993. }
  32994. /* test that the 3 errors over the max were dropped */
  32995. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  32996. return 0;
  32997. }
  32998. #endif
  32999. static int test_error_queue_per_thread(void)
  33000. {
  33001. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33002. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33003. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  33004. int i;
  33005. printf(testingFmt, "error_queue_per_thread()");
  33006. ERR_clear_error(); /* clear out any error nodes */
  33007. loggingThreadsReady = 0;
  33008. for (i = 0; i < LOGGING_THREADS; i++)
  33009. start_thread(test_logging, NULL, &loggingThreads[i]);
  33010. loggingThreadsReady = 1;
  33011. for (i = 0; i < LOGGING_THREADS; i++)
  33012. join_thread(loggingThreads[i]);
  33013. printf(resultFmt, passed);
  33014. #endif
  33015. return 0;
  33016. }
  33017. static int test_wolfSSL_ERR_put_error(void)
  33018. {
  33019. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33020. defined(DEBUG_WOLFSSL)
  33021. const char* file;
  33022. int line;
  33023. printf(testingFmt, "wolfSSL_ERR_put_error()");
  33024. ERR_clear_error(); /* clear out any error nodes */
  33025. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33026. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33027. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  33028. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  33029. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  33030. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  33031. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  33032. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  33033. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  33034. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  33035. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  33036. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  33037. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  33038. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  33039. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  33040. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  33041. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  33042. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  33043. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  33044. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  33045. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  33046. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  33047. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  33048. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  33049. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  33050. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  33051. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  33052. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  33053. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  33054. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  33055. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  33056. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  33057. #ifdef WOLFSSL_PYTHON
  33058. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  33059. "this file", 100);
  33060. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  33061. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33062. AssertIntEQ(line, 100);
  33063. AssertIntEQ(wolfSSL_ERR_peek_error(),
  33064. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33065. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  33066. #endif
  33067. /* try reading past end of error queue */
  33068. file = NULL;
  33069. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33070. AssertNull(file);
  33071. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  33072. PEMerr(4,4);
  33073. AssertIntEQ(ERR_get_error(), 4);
  33074. /* Empty and free up all error nodes */
  33075. ERR_clear_error();
  33076. /* Verify all nodes are cleared */
  33077. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33078. ERR_clear_error();
  33079. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33080. printf(resultFmt, passed);
  33081. #endif
  33082. return 0;
  33083. }
  33084. /*
  33085. * This is a regression test for a bug where the peek/get error functions were
  33086. * drawing from the end of the queue rather than the front.
  33087. */
  33088. static int test_wolfSSL_ERR_get_error_order(void)
  33089. {
  33090. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  33091. printf(testingFmt, "test_wolfSSL_ERR_get_error_order");
  33092. /* Empty the queue. */
  33093. wolfSSL_ERR_clear_error();
  33094. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  33095. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  33096. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  33097. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  33098. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  33099. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  33100. printf(resultFmt, passed);
  33101. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  33102. return 0;
  33103. }
  33104. #ifndef NO_BIO
  33105. static int test_wolfSSL_ERR_print_errors(void)
  33106. {
  33107. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33108. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  33109. BIO* bio;
  33110. char buf[1024];
  33111. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  33112. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33113. ERR_clear_error(); /* clear out any error nodes */
  33114. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33115. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  33116. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  33117. ERR_print_errors(bio);
  33118. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  33119. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  33120. buf, 55), 0);
  33121. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  33122. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  33123. buf, 56), 0);
  33124. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  33125. AssertIntEQ(buf[0], '\0');
  33126. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  33127. BIO_free(bio);
  33128. printf(resultFmt, passed);
  33129. #endif
  33130. return 0;
  33131. }
  33132. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33133. defined(DEBUG_WOLFSSL)
  33134. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  33135. {
  33136. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  33137. return 0;
  33138. }
  33139. #endif
  33140. static int test_wolfSSL_ERR_print_errors_cb(void)
  33141. {
  33142. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33143. defined(DEBUG_WOLFSSL)
  33144. BIO* bio;
  33145. char buf[1024];
  33146. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  33147. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33148. ERR_clear_error(); /* clear out any error nodes */
  33149. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33150. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  33151. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  33152. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  33153. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  33154. buf, 53), 0);
  33155. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  33156. buf + 53, 55), 0);
  33157. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  33158. BIO_free(bio);
  33159. printf(resultFmt, passed);
  33160. #endif
  33161. return 0;
  33162. }
  33163. /*
  33164. * Testing WOLFSSL_ERROR_MSG
  33165. */
  33166. static int test_WOLFSSL_ERROR_MSG(void)
  33167. {
  33168. int ret = 0;
  33169. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  33170. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  33171. const char* msg = TEST_STRING;
  33172. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  33173. WOLFSSL_ERROR_MSG(msg);
  33174. printf(resultFmt, ret == 0 ? passed : failed);
  33175. fflush(stdout);
  33176. #endif
  33177. return ret;
  33178. }/*End test_WOLFSSL_ERROR_MSG*/
  33179. /*
  33180. * Testing wc_ERR_remove_state
  33181. */
  33182. static int test_wc_ERR_remove_state(void)
  33183. {
  33184. int ret = 0;
  33185. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  33186. printf(testingFmt, "wc_ERR_remove_state()");
  33187. wc_ERR_remove_state();
  33188. printf(resultFmt, ret == 0 ? passed : failed);
  33189. fflush(stdout);
  33190. #endif
  33191. return ret;
  33192. }/*End test_wc_ERR_remove_state*/
  33193. /*
  33194. * Testing wc_ERR_print_errors_fp
  33195. */
  33196. static int test_wc_ERR_print_errors_fp(void)
  33197. {
  33198. int ret = 0;
  33199. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  33200. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  33201. long sz;
  33202. XFILE fp;
  33203. printf(testingFmt, "wc_ERR_print_errors_fp()");
  33204. WOLFSSL_ERROR(BAD_FUNC_ARG);
  33205. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  33206. wc_ERR_print_errors_fp(fp);
  33207. #if defined(DEBUG_WOLFSSL)
  33208. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  33209. sz = XFTELL(fp);
  33210. #ifdef NO_ERROR_QUEUE
  33211. /* File should be empty when NO_ERROR_QUEUE is defined */
  33212. if (sz != 0) {
  33213. ret = BAD_FUNC_ARG;
  33214. }
  33215. #else
  33216. if (sz == 0) {
  33217. ret = BAD_FUNC_ARG;
  33218. }
  33219. #endif
  33220. #endif
  33221. printf(resultFmt, ret == 0 ? passed : failed);
  33222. fflush(stdout);
  33223. XFCLOSE(fp);
  33224. (void)sz;
  33225. #endif
  33226. return ret;
  33227. }/*End test_wc_ERR_print_errors_fp*/
  33228. #ifdef DEBUG_WOLFSSL
  33229. static void Logging_cb(const int logLevel, const char *const logMessage)
  33230. {
  33231. (void)logLevel;
  33232. (void)logMessage;
  33233. }
  33234. #endif
  33235. /*
  33236. * Testing wolfSSL_GetLoggingCb
  33237. */
  33238. static int test_wolfSSL_GetLoggingCb(void)
  33239. {
  33240. int ret = 0;
  33241. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  33242. #ifdef DEBUG_WOLFSSL
  33243. /* Testing without wolfSSL_SetLoggingCb() */
  33244. if (ret == 0) {
  33245. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  33246. ret = 0;
  33247. }
  33248. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  33249. ret = -1;
  33250. }
  33251. }
  33252. /* Testing with wolfSSL_SetLoggingCb() */
  33253. if (ret == 0) {
  33254. ret = wolfSSL_SetLoggingCb(Logging_cb);
  33255. if (ret == 0){
  33256. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  33257. ret = -1;
  33258. }
  33259. if (ret == 0) {
  33260. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  33261. ret = 0;
  33262. }
  33263. }
  33264. /* reset logging callback */
  33265. wolfSSL_SetLoggingCb(NULL);
  33266. }
  33267. }
  33268. #endif
  33269. if (ret == 0) {
  33270. if (wolfSSL_GetLoggingCb() != NULL) {
  33271. ret = -1;
  33272. }
  33273. }
  33274. printf(resultFmt, ret == 0 ? passed : failed);
  33275. fflush(stdout);
  33276. return ret;
  33277. }/*End test_wolfSSL_GetLoggingCb*/
  33278. #endif /* !NO_BIO */
  33279. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  33280. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  33281. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  33282. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  33283. {
  33284. static const unsigned char key[] = "simple test key";
  33285. HMAC_CTX* hmac;
  33286. ENGINE* e = NULL;
  33287. unsigned char hash[WC_MAX_DIGEST_SIZE];
  33288. unsigned int len;
  33289. AssertNotNull(hmac = HMAC_CTX_new());
  33290. HMAC_CTX_init(hmac);
  33291. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  33292. SSL_SUCCESS);
  33293. /* re-using test key as data to hash */
  33294. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  33295. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  33296. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  33297. AssertIntEQ(len, md_len);
  33298. AssertIntEQ(HMAC_size(hmac), md_len);
  33299. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  33300. HMAC_cleanup(hmac);
  33301. HMAC_CTX_free(hmac);
  33302. len = 0;
  33303. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  33304. AssertIntEQ(len, md_len);
  33305. return 0;
  33306. }
  33307. #endif
  33308. static int test_wolfSSL_HMAC(void)
  33309. {
  33310. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  33311. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  33312. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  33313. printf(testingFmt, "wolfSSL_HMAC()");
  33314. #ifndef NO_SHA256
  33315. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  33316. #endif
  33317. #ifdef WOLFSSL_SHA224
  33318. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  33319. #endif
  33320. #ifdef WOLFSSL_SHA384
  33321. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  33322. #endif
  33323. #ifdef WOLFSSL_SHA512
  33324. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  33325. #endif
  33326. #ifdef WOLFSSL_SHA3
  33327. #ifndef WOLFSSL_NOSHA3_224
  33328. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  33329. #endif
  33330. #ifndef WOLFSSL_NOSHA3_256
  33331. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  33332. #endif
  33333. #ifndef WOLFSSL_NOSHA3_384
  33334. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  33335. #endif
  33336. #ifndef WOLFSSL_NOSHA3_512
  33337. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  33338. #endif
  33339. #endif
  33340. printf(resultFmt, passed);
  33341. #endif
  33342. return 0;
  33343. }
  33344. static int test_wolfSSL_CMAC(void)
  33345. {
  33346. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  33347. defined(WOLFSSL_AES_DIRECT)
  33348. int i;
  33349. byte key[AES_128_KEY_SIZE];
  33350. CMAC_CTX* cmacCtx = NULL;
  33351. byte out[AES_BLOCK_SIZE];
  33352. size_t outLen = AES_BLOCK_SIZE;
  33353. printf(testingFmt, "test_wolfSSL_CMAC()");
  33354. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  33355. key[i] = i;
  33356. }
  33357. AssertNotNull(cmacCtx = CMAC_CTX_new());
  33358. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  33359. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  33360. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  33361. NULL), SSL_SUCCESS);
  33362. /* re-using test key as data to hash */
  33363. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  33364. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  33365. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  33366. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  33367. CMAC_CTX_free(cmacCtx);
  33368. printf(resultFmt, passed);
  33369. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  33370. return 0;
  33371. }
  33372. static int test_wolfSSL_OBJ(void)
  33373. {
  33374. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  33375. * mode
  33376. */
  33377. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  33378. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  33379. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  33380. ASN1_OBJECT *obj = NULL;
  33381. ASN1_OBJECT *obj2 = NULL;
  33382. char buf[50];
  33383. XFILE fp;
  33384. X509 *x509 = NULL;
  33385. X509_NAME *x509Name;
  33386. X509_NAME_ENTRY *x509NameEntry;
  33387. ASN1_OBJECT *asn1Name = NULL;
  33388. int numNames;
  33389. BIO *bio = NULL;
  33390. int nid;
  33391. int i, j;
  33392. const char *f[] = {
  33393. #ifndef NO_RSA
  33394. "./certs/ca-cert.der",
  33395. #endif
  33396. #ifdef HAVE_ECC
  33397. "./certs/ca-ecc-cert.der",
  33398. "./certs/ca-ecc384-cert.der",
  33399. #endif
  33400. NULL};
  33401. ASN1_OBJECT *field_name_obj = NULL;
  33402. int lastpos = -1;
  33403. int tmp = -1;
  33404. ASN1_STRING *asn1 = NULL;
  33405. unsigned char *buf_dyn = NULL;
  33406. printf(testingFmt, "wolfSSL_OBJ()");
  33407. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  33408. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  33409. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  33410. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  33411. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  33412. ASN1_OBJECT_free(obj);
  33413. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  33414. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  33415. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  33416. #ifdef WOLFSSL_CERT_EXT
  33417. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  33418. #endif
  33419. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  33420. AssertNotNull(obj2 = OBJ_dup(obj));
  33421. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  33422. ASN1_OBJECT_free(obj);
  33423. ASN1_OBJECT_free(obj2);
  33424. for (i = 0; f[i] != NULL; i++)
  33425. {
  33426. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  33427. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  33428. XFCLOSE(fp);
  33429. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  33430. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  33431. /* Get the Common Name by using OBJ_txt2obj */
  33432. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  33433. do
  33434. {
  33435. lastpos = tmp;
  33436. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  33437. } while (tmp > -1);
  33438. AssertIntNE(lastpos, -1);
  33439. ASN1_OBJECT_free(field_name_obj);
  33440. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  33441. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  33442. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  33443. /*
  33444. * All Common Names should be www.wolfssl.com
  33445. * This makes testing easier as we can test for the expected value.
  33446. */
  33447. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  33448. OPENSSL_free(buf_dyn);
  33449. bio = BIO_new(BIO_s_mem());
  33450. AssertTrue(bio != NULL);
  33451. for (j = 0; j < numNames; j++)
  33452. {
  33453. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  33454. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  33455. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  33456. }
  33457. BIO_free(bio);
  33458. X509_free(x509);
  33459. }
  33460. #ifdef HAVE_PKCS12
  33461. {
  33462. PKCS12 *p12;
  33463. int boolRet;
  33464. EVP_PKEY *pkey = NULL;
  33465. const char *p12_f[] = {
  33466. #if !defined(NO_DES3) && !defined(NO_RSA)
  33467. "./certs/test-servercert.p12",
  33468. #endif
  33469. NULL};
  33470. for (i = 0; p12_f[i] != NULL; i++)
  33471. {
  33472. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  33473. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  33474. XFCLOSE(fp);
  33475. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  33476. &pkey, &x509, NULL)) > 0);
  33477. wc_PKCS12_free(p12);
  33478. EVP_PKEY_free(pkey);
  33479. x509Name = X509_get_issuer_name(x509);
  33480. AssertNotNull(x509Name);
  33481. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  33482. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  33483. for (j = 0; j < numNames; j++)
  33484. {
  33485. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  33486. AssertNotNull(asn1Name =
  33487. X509_NAME_ENTRY_get_object(x509NameEntry));
  33488. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  33489. }
  33490. BIO_free(bio);
  33491. X509_free(x509);
  33492. }
  33493. }
  33494. #endif /* HAVE_PKCS12 */
  33495. printf(resultFmt, passed);
  33496. #endif
  33497. return 0;
  33498. }
  33499. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  33500. {
  33501. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  33502. ASN1_OBJECT *obj = NULL;
  33503. BIO *bio = NULL;
  33504. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  33505. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  33506. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  33507. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  33508. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  33509. BIO_free(bio);
  33510. ASN1_OBJECT_free(obj);
  33511. #endif
  33512. return 0;
  33513. }
  33514. static int test_wolfSSL_OBJ_cmp(void)
  33515. {
  33516. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  33517. ASN1_OBJECT *obj = NULL;
  33518. ASN1_OBJECT *obj2 = NULL;
  33519. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  33520. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  33521. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  33522. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  33523. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  33524. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  33525. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  33526. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  33527. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  33528. ASN1_OBJECT_free(obj);
  33529. ASN1_OBJECT_free(obj2);
  33530. printf(resultFmt, passed);
  33531. #endif
  33532. return 0;
  33533. }
  33534. static int test_wolfSSL_OBJ_txt2nid(void)
  33535. {
  33536. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  33537. int i;
  33538. static const struct {
  33539. const char* sn;
  33540. const char* ln;
  33541. const char* oid;
  33542. int nid;
  33543. } testVals[] = {
  33544. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  33545. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  33546. NID_id_on_dnsSRV },
  33547. { "msUPN", "Microsoft User Principal Name",
  33548. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  33549. { NULL, NULL, NULL, NID_undef }
  33550. };
  33551. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  33552. /* Invalid cases */
  33553. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  33554. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  33555. /* Valid cases */
  33556. for (i = 0; testVals[i].sn != NULL; i++) {
  33557. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  33558. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  33559. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  33560. }
  33561. printf(resultFmt, passed);
  33562. #endif
  33563. return 0;
  33564. }
  33565. static int test_wolfSSL_OBJ_txt2obj(void)
  33566. {
  33567. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  33568. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  33569. int i;
  33570. char buf[50];
  33571. ASN1_OBJECT* obj;
  33572. static const struct {
  33573. const char* oidStr;
  33574. const char* sn;
  33575. const char* ln;
  33576. } objs_list[] = {
  33577. #if defined(WOLFSSL_APACHE_HTTPD)
  33578. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  33579. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  33580. #endif
  33581. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  33582. { NULL, NULL, NULL }
  33583. };
  33584. static const struct {
  33585. const char* numeric;
  33586. const char* name;
  33587. } objs_named[] = {
  33588. /* In dictionary but not in normal list. */
  33589. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  33590. /* Made up OID. */
  33591. { "1.3.5.7", "1.3.5.7" },
  33592. { NULL, NULL }
  33593. };
  33594. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  33595. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  33596. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  33597. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  33598. /* Test numerical value of oid (oidStr) */
  33599. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  33600. /* Convert object back to text to confirm oid is correct */
  33601. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33602. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  33603. ASN1_OBJECT_free(obj);
  33604. XMEMSET(buf, 0, sizeof(buf));
  33605. /* Test short name (sn) */
  33606. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  33607. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  33608. /* Convert object back to text to confirm oid is correct */
  33609. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33610. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  33611. ASN1_OBJECT_free(obj);
  33612. XMEMSET(buf, 0, sizeof(buf));
  33613. /* Test long name (ln) - should fail when no_name = 1 */
  33614. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  33615. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  33616. /* Convert object back to text to confirm oid is correct */
  33617. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33618. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  33619. ASN1_OBJECT_free(obj);
  33620. XMEMSET(buf, 0, sizeof(buf));
  33621. }
  33622. for (i = 0; objs_named[i].numeric != NULL; i++) {
  33623. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  33624. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  33625. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  33626. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  33627. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  33628. ASN1_OBJECT_free(obj);
  33629. }
  33630. printf(resultFmt, passed);
  33631. #endif
  33632. return 0;
  33633. }
  33634. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  33635. {
  33636. #if defined(OPENSSL_EXTRA) && \
  33637. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  33638. char buf[50] = {0};
  33639. ASN1_OBJECT* obj;
  33640. const char* oid = "2.5.29.19";
  33641. const char* ln = "X509v3 Basic Constraints";
  33642. printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
  33643. obj = NULL;
  33644. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  33645. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  33646. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  33647. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  33648. XMEMSET(buf, 0, sizeof(buf));
  33649. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  33650. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  33651. ASN1_OBJECT_free(obj);
  33652. printf(resultFmt, passed);
  33653. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  33654. return 0;
  33655. }
  33656. static int test_wolfSSL_PEM_write_bio_X509(void)
  33657. {
  33658. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
  33659. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
  33660. !defined(NO_BIO) && !defined(NO_RSA)
  33661. /* This test contains the hard coded expected
  33662. * lengths. Update if necessary */
  33663. BIO* input;
  33664. BIO* output;
  33665. X509* x509a = NULL;
  33666. X509* x509b = NULL;
  33667. ASN1_TIME* notBeforeA = NULL;
  33668. ASN1_TIME* notAfterA = NULL;
  33669. ASN1_TIME* notBeforeB = NULL;
  33670. ASN1_TIME* notAfterB = NULL;
  33671. int expectedLen;
  33672. printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
  33673. AssertNotNull(input = BIO_new_file(
  33674. "certs/test/cert-ext-multiple.pem", "rb"));
  33675. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  33676. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  33677. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33678. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  33679. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  33680. /* write X509 back to PEM BIO */
  33681. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33682. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33683. /* compare length against expected */
  33684. expectedLen = 2000;
  33685. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  33686. /* read exported X509 PEM back into struct, sanity check on export,
  33687. * make sure notBefore/notAfter are the same. */
  33688. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33689. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  33690. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  33691. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  33692. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  33693. X509_free(x509b);
  33694. /* Reset output buffer */
  33695. BIO_free(output);
  33696. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33697. /* Test forcing the AKID to be generated just from KeyIdentifier */
  33698. if (x509a->authKeyIdSrc != NULL) {
  33699. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  33700. x509a->authKeyId = x509a->authKeyIdSrc;
  33701. x509a->authKeyIdSrc = NULL;
  33702. x509a->authKeyIdSrcSz = 0;
  33703. }
  33704. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33705. /* Check that we generate a smaller output since the AKID will
  33706. * only contain the KeyIdentifier without any additional
  33707. * information */
  33708. /* Here we copy the validity struct from the original */
  33709. expectedLen = 1688;
  33710. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  33711. /* Reset buffers and x509 */
  33712. BIO_free(input);
  33713. BIO_free(output);
  33714. X509_free(x509a);
  33715. /* test CA and basicConstSet values are encoded when
  33716. * the cert is a CA */
  33717. AssertNotNull(input = BIO_new_file(
  33718. "certs/server-cert.pem", "rb"));
  33719. /* read PEM into X509 struct */
  33720. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33721. /* write X509 back to PEM BIO */
  33722. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33723. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33724. /* read exported X509 PEM back into struct, ensure isCa and
  33725. * basicConstSet values are maintained */
  33726. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33727. AssertIntEQ(x509b->isCa, 1);
  33728. AssertIntEQ(x509b->basicConstSet, 1);
  33729. X509_free(x509a);
  33730. X509_free(x509b);
  33731. BIO_free(input);
  33732. BIO_free(output);
  33733. /* test CA and basicConstSet values are encoded when
  33734. * the cert is not CA */
  33735. AssertNotNull(input = BIO_new_file(
  33736. "certs/client-uri-cert.pem", "rb"));
  33737. /* read PEM into X509 struct */
  33738. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  33739. /* write X509 back to PEM BIO */
  33740. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  33741. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  33742. /* read exported X509 PEM back into struct, ensure isCa and
  33743. * basicConstSet values are maintained */
  33744. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  33745. AssertIntEQ(x509b->isCa, 0);
  33746. AssertIntEQ(x509b->basicConstSet, 1);
  33747. X509_free(x509a);
  33748. X509_free(x509b);
  33749. BIO_free(input);
  33750. BIO_free(output);
  33751. printf(resultFmt, passed);
  33752. #endif
  33753. return 0;
  33754. }
  33755. static int test_wolfSSL_X509_NAME_ENTRY(void)
  33756. {
  33757. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  33758. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  33759. X509* x509;
  33760. #ifndef NO_BIO
  33761. BIO* bio;
  33762. #endif
  33763. X509_NAME* nm;
  33764. X509_NAME_ENTRY* entry;
  33765. unsigned char cn[] = "another name to add";
  33766. #ifdef OPENSSL_ALL
  33767. int i, names_len;
  33768. #endif
  33769. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  33770. AssertNotNull(x509 =
  33771. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  33772. #ifndef NO_BIO
  33773. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33774. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  33775. #endif
  33776. #ifdef WOLFSSL_CERT_REQ
  33777. {
  33778. X509_REQ* req;
  33779. #ifndef NO_BIO
  33780. BIO* bReq;
  33781. #endif
  33782. AssertNotNull(req =
  33783. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  33784. #ifndef NO_BIO
  33785. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  33786. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  33787. BIO_free(bReq);
  33788. #endif
  33789. X509_free(req);
  33790. }
  33791. #endif
  33792. AssertNotNull(nm = X509_get_subject_name(x509));
  33793. /* Test add entry */
  33794. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  33795. 0x0c, cn, (int)sizeof(cn)));
  33796. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  33797. #ifdef WOLFSSL_CERT_EXT
  33798. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  33799. (byte*)"support@wolfssl.com", 19, -1,
  33800. 1), WOLFSSL_SUCCESS);
  33801. #endif
  33802. X509_NAME_ENTRY_free(entry);
  33803. #ifdef WOLFSSL_CERT_REQ
  33804. {
  33805. unsigned char srv_pkcs9p[] = "Server";
  33806. char* subject;
  33807. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  33808. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  33809. subject = X509_NAME_oneline(nm, 0, 0);
  33810. #ifdef DEBUG_WOLFSSL
  33811. printf("\n\t%s\n", subject);
  33812. #endif
  33813. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  33814. }
  33815. #endif
  33816. /* Test add entry by text */
  33817. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  33818. 0x0c, cn, (int)sizeof(cn)));
  33819. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  33820. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  33821. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  33822. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  33823. #endif
  33824. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  33825. X509_NAME_ENTRY_free(entry);
  33826. /* Test add entry by NID */
  33827. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  33828. cn, -1, -1, 0), SSL_SUCCESS);
  33829. #ifdef OPENSSL_ALL
  33830. /* stack of name entry */
  33831. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  33832. for (i=0; i<names_len; i++) {
  33833. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  33834. }
  33835. #endif
  33836. #ifndef NO_BIO
  33837. BIO_free(bio);
  33838. #endif
  33839. X509_free(x509); /* free's nm */
  33840. printf(resultFmt, passed);
  33841. #endif
  33842. return 0;
  33843. }
  33844. static int test_wolfSSL_X509_set_name(void)
  33845. {
  33846. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33847. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  33848. X509* x509;
  33849. X509_NAME* name;
  33850. printf(testingFmt, "wolfSSL_X509_set_name()");
  33851. AssertNotNull(name = X509_NAME_new());
  33852. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  33853. (byte*)"wolfssl.com", 11, 0, 1),
  33854. WOLFSSL_SUCCESS);
  33855. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  33856. (byte*)"support@wolfssl.com", 19, -1,
  33857. 1), WOLFSSL_SUCCESS);
  33858. AssertNotNull(x509 = X509_new());
  33859. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  33860. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  33861. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  33862. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  33863. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  33864. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  33865. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  33866. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  33867. X509_free(x509);
  33868. X509_NAME_free(name);
  33869. printf(resultFmt, passed);
  33870. #endif /* OPENSSL_ALL && !NO_CERTS */
  33871. return 0;
  33872. }
  33873. static int test_wolfSSL_X509_set_notAfter(void)
  33874. {
  33875. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  33876. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  33877. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  33878. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  33879. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  33880. /* Generalized time will overflow time_t if not long */
  33881. X509* x;
  33882. BIO* bio;
  33883. ASN1_TIME *asn_time, *time_check;
  33884. const int year = 365*24*60*60;
  33885. const int day = 24*60*60;
  33886. const int hour = 60*60;
  33887. const int mini = 60;
  33888. int offset_day;
  33889. unsigned char buf[25];
  33890. time_t t;
  33891. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  33892. /*
  33893. * Setup asn_time. APACHE HTTPD uses time(NULL)
  33894. */
  33895. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  33896. offset_day = 7;
  33897. /*
  33898. * Free these.
  33899. */
  33900. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  33901. AssertNotNull(asn_time);
  33902. AssertNotNull(x = X509_new());
  33903. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33904. /*
  33905. * Tests
  33906. */
  33907. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  33908. /* time_check is simply (ANS1_TIME*)x->notAfter */
  33909. AssertNotNull(time_check = X509_get_notAfter(x));
  33910. /* ANS1_TIME_check validates by checking if argument can be parsed */
  33911. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  33912. /* Convert to human readable format and compare to intended date */
  33913. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  33914. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  33915. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  33916. /*
  33917. * Cleanup
  33918. */
  33919. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  33920. X509_free(x);
  33921. BIO_free(bio);
  33922. printf(resultFmt, passed);
  33923. #endif
  33924. return 0;
  33925. }
  33926. static int test_wolfSSL_X509_set_notBefore(void)
  33927. {
  33928. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  33929. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  33930. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  33931. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  33932. X509* x;
  33933. BIO* bio;
  33934. ASN1_TIME *asn_time, *time_check;
  33935. const int year = 365*24*60*60;
  33936. const int day = 24*60*60;
  33937. const int hour = 60*60;
  33938. const int mini = 60;
  33939. int offset_day;
  33940. unsigned char buf[25];
  33941. time_t t;
  33942. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  33943. /*
  33944. * Setup asn_time. APACHE HTTPD uses time(NULL)
  33945. */
  33946. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  33947. offset_day = 7;
  33948. /*
  33949. * Free these.
  33950. */
  33951. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  33952. AssertNotNull(asn_time);
  33953. AssertNotNull(x = X509_new());
  33954. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33955. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  33956. /*
  33957. * Main Tests
  33958. */
  33959. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  33960. /* time_check == (ANS1_TIME*)x->notBefore */
  33961. AssertNotNull(time_check = X509_get_notBefore(x));
  33962. /* ANS1_TIME_check validates by checking if argument can be parsed */
  33963. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  33964. /* Convert to human readable format and compare to intended date */
  33965. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  33966. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  33967. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  33968. /*
  33969. * Cleanup
  33970. */
  33971. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  33972. X509_free(x);
  33973. BIO_free(bio);
  33974. printf(resultFmt, passed);
  33975. #endif
  33976. return 0;
  33977. }
  33978. static int test_wolfSSL_X509_set_version(void)
  33979. {
  33980. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33981. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  33982. X509* x509;
  33983. long v = 2L;
  33984. long maxInt = INT_MAX;
  33985. AssertNotNull(x509 = X509_new());
  33986. /* These should pass. */
  33987. AssertTrue(wolfSSL_X509_set_version(x509, v));
  33988. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  33989. /* Fail Case: When v(long) is greater than x509->version(int). */
  33990. v = maxInt+1;
  33991. AssertFalse(wolfSSL_X509_set_version(x509, v));
  33992. /* Cleanup */
  33993. X509_free(x509);
  33994. printf(resultFmt, passed);
  33995. #endif
  33996. return 0;
  33997. }
  33998. #ifndef NO_BIO
  33999. static int test_wolfSSL_BIO_gets(void)
  34000. {
  34001. #if defined(OPENSSL_EXTRA)
  34002. BIO* bio;
  34003. BIO* bio2;
  34004. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  34005. char emp[] = "";
  34006. char bio_buffer[20];
  34007. int bufferSz = 20;
  34008. printf(testingFmt, "wolfSSL_BIO_gets()");
  34009. /* try with bad args */
  34010. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  34011. /* try with real msg */
  34012. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  34013. XMEMSET(bio_buffer, 0, bufferSz);
  34014. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  34015. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  34016. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  34017. AssertFalse(bio2 != BIO_next(bio));
  34018. /* make buffer filled with no terminating characters */
  34019. XMEMSET(bio_buffer, 1, bufferSz);
  34020. /* BIO_gets reads a line of data */
  34021. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  34022. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  34023. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  34024. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  34025. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  34026. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  34027. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  34028. /* check not null terminated string */
  34029. BIO_free(bio);
  34030. msg[0] = 0x33;
  34031. msg[1] = 0x33;
  34032. msg[2] = 0x33;
  34033. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  34034. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  34035. AssertIntEQ(bio_buffer[0], msg[0]);
  34036. AssertIntEQ(bio_buffer[1], msg[1]);
  34037. AssertIntNE(bio_buffer[2], msg[2]);
  34038. BIO_free(bio);
  34039. msg[3] = 0x33;
  34040. bio_buffer[3] = 0x33;
  34041. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  34042. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  34043. AssertIntEQ(bio_buffer[0], msg[0]);
  34044. AssertIntEQ(bio_buffer[1], msg[1]);
  34045. AssertIntEQ(bio_buffer[2], msg[2]);
  34046. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  34047. /* check reading an empty string */
  34048. BIO_free(bio);
  34049. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  34050. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  34051. AssertStrEQ(emp, bio_buffer);
  34052. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  34053. /* check error cases */
  34054. BIO_free(bio);
  34055. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  34056. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34057. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  34058. #if !defined(NO_FILESYSTEM)
  34059. {
  34060. BIO* f_bio;
  34061. XFILE f;
  34062. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  34063. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  34064. f = XFOPEN(svrCertFile, "rb");
  34065. AssertTrue((f != XBADFILE));
  34066. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  34067. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  34068. BIO_free(f_bio);
  34069. }
  34070. #endif /* NO_FILESYSTEM */
  34071. BIO_free(bio);
  34072. BIO_free(bio2);
  34073. /* try with type BIO */
  34074. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  34075. sizeof(msg));
  34076. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  34077. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  34078. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  34079. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  34080. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  34081. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  34082. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  34083. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  34084. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  34085. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  34086. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  34087. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  34088. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  34089. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  34090. BIO_free(bio);
  34091. BIO_free(bio2);
  34092. /* check reading an empty string */
  34093. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  34094. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  34095. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  34096. AssertStrEQ(emp, bio_buffer);
  34097. BIO_free(bio);
  34098. printf(resultFmt, passed);
  34099. #endif
  34100. return 0;
  34101. }
  34102. static int test_wolfSSL_BIO_puts(void)
  34103. {
  34104. #if defined(OPENSSL_EXTRA)
  34105. BIO* bio;
  34106. char input[] = "hello\0world\n.....ok\n\0";
  34107. char output[128];
  34108. printf(testingFmt, "wolfSSL_BIO_puts()");
  34109. XMEMSET(output, 0, sizeof(output));
  34110. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34111. AssertIntEQ(BIO_puts(bio, input), 5);
  34112. AssertIntEQ(BIO_pending(bio), 5);
  34113. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  34114. AssertIntEQ(BIO_pending(bio), 19);
  34115. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  34116. AssertStrEQ(output, "helloworld\n");
  34117. AssertIntEQ(BIO_pending(bio), 8);
  34118. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  34119. AssertStrEQ(output, ".....ok\n");
  34120. AssertIntEQ(BIO_pending(bio), 0);
  34121. AssertIntEQ(BIO_puts(bio, ""), -1);
  34122. BIO_free(bio);
  34123. printf(resultFmt, passed);
  34124. #endif
  34125. return 0;
  34126. }
  34127. static int test_wolfSSL_BIO_dump(void)
  34128. {
  34129. #if defined(OPENSSL_EXTRA)
  34130. BIO* bio;
  34131. static const unsigned char data[] = {
  34132. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  34133. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  34134. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  34135. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  34136. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  34137. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  34138. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  34139. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  34140. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  34141. 0xB4
  34142. };
  34143. /* Generated with OpenSSL. */
  34144. static const char expected[] =
  34145. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  34146. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  34147. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  34148. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  34149. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  34150. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  34151. static const char expectedAll[] =
  34152. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  34153. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  34154. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  34155. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  34156. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  34157. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  34158. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  34159. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  34160. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  34161. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  34162. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  34163. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  34164. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  34165. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  34166. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  34167. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  34168. char output[16 * 80];
  34169. int i;
  34170. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34171. /* Example key dumped. */
  34172. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  34173. sizeof(expected) - 1);
  34174. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  34175. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  34176. /* Try every possible value for a character. */
  34177. for (i = 0; i < 256; i++)
  34178. output[i] = i;
  34179. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  34180. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  34181. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  34182. BIO_free(bio);
  34183. printf(resultFmt, passed);
  34184. #endif
  34185. return 0;
  34186. }
  34187. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34188. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  34189. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  34190. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  34191. {
  34192. (void)ssl;
  34193. (void)buf;
  34194. (void)sz;
  34195. (void)ctx;
  34196. return WOLFSSL_CBIO_ERR_WANT_READ;
  34197. }
  34198. #endif
  34199. static int test_wolfSSL_BIO_should_retry(void)
  34200. {
  34201. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34202. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  34203. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  34204. tcp_ready ready;
  34205. func_args server_args;
  34206. THREAD_TYPE serverThread;
  34207. SOCKET_T sockfd = 0;
  34208. WOLFSSL_CTX* ctx;
  34209. WOLFSSL* ssl;
  34210. char msg[64] = "hello wolfssl!";
  34211. char reply[1024];
  34212. int msgSz = (int)XSTRLEN(msg);
  34213. int ret;
  34214. BIO* bio;
  34215. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  34216. XMEMSET(&server_args, 0, sizeof(func_args));
  34217. #ifdef WOLFSSL_TIRTOS
  34218. fdOpenSession(Task_self());
  34219. #endif
  34220. StartTCP();
  34221. InitTcpReady(&ready);
  34222. #if defined(USE_WINDOWS_API)
  34223. /* use RNG to get random port if using windows */
  34224. ready.port = GetRandomPort();
  34225. #endif
  34226. server_args.signal = &ready;
  34227. start_thread(test_server_nofail, &server_args, &serverThread);
  34228. wait_tcp_ready(&server_args);
  34229. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34230. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  34231. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  34232. #endif
  34233. AssertIntEQ(WOLFSSL_SUCCESS,
  34234. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34235. AssertIntEQ(WOLFSSL_SUCCESS,
  34236. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34237. AssertIntEQ(WOLFSSL_SUCCESS,
  34238. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34239. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  34240. /* force retry */
  34241. ssl = wolfSSL_new(ctx);
  34242. AssertNotNull(ssl);
  34243. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34244. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  34245. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  34246. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  34247. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  34248. AssertIntNE(BIO_should_retry(bio), 0);
  34249. /* now perform successful connection */
  34250. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  34251. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  34252. BIO_read(bio, reply, sizeof(reply));
  34253. ret = wolfSSL_get_error(ssl, -1);
  34254. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  34255. AssertIntNE(BIO_should_retry(bio), 0);
  34256. }
  34257. else {
  34258. AssertIntEQ(BIO_should_retry(bio), 0);
  34259. }
  34260. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  34261. XSTRLEN("I hear you fa shizzle!")), 0);
  34262. BIO_free(bio);
  34263. wolfSSL_CTX_free(ctx);
  34264. join_thread(serverThread);
  34265. FreeTcpReady(&ready);
  34266. #ifdef WOLFSSL_TIRTOS
  34267. fdOpenSession(Task_self());
  34268. #endif
  34269. printf(resultFmt, passed);
  34270. #endif
  34271. return 0;
  34272. }
  34273. static int test_wolfSSL_BIO_connect(void)
  34274. {
  34275. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34276. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  34277. tcp_ready ready;
  34278. func_args server_args;
  34279. THREAD_TYPE serverThread;
  34280. BIO *tcpBio;
  34281. BIO *sslBio;
  34282. SSL_CTX* ctx;
  34283. SSL *ssl;
  34284. SSL *sslPtr;
  34285. char msg[] = "hello wolfssl!";
  34286. char reply[30];
  34287. char buff[10] = {0};
  34288. printf(testingFmt, "wolfSSL_BIO_new_connect()");
  34289. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34290. AssertIntEQ(WOLFSSL_SUCCESS,
  34291. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  34292. AssertIntEQ(WOLFSSL_SUCCESS,
  34293. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  34294. AssertIntEQ(WOLFSSL_SUCCESS,
  34295. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  34296. /* Setup server */
  34297. XMEMSET(&server_args, 0, sizeof(func_args));
  34298. StartTCP();
  34299. InitTcpReady(&ready);
  34300. #if defined(USE_WINDOWS_API)
  34301. /* use RNG to get random port if using windows */
  34302. ready.port = GetRandomPort();
  34303. #endif
  34304. server_args.signal = &ready;
  34305. start_thread(test_server_nofail, &server_args, &serverThread);
  34306. wait_tcp_ready(&server_args);
  34307. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  34308. /* Start the test proper */
  34309. /* Setup the TCP BIO */
  34310. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  34311. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  34312. /* Setup the SSL object */
  34313. AssertNotNull(ssl = SSL_new(ctx));
  34314. SSL_set_connect_state(ssl);
  34315. /* Setup the SSL BIO */
  34316. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  34317. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  34318. /* Verify that BIO_get_ssl works. */
  34319. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  34320. AssertPtrEq(ssl, sslPtr);
  34321. /* Link BIO's so that sslBio uses tcpBio for IO */
  34322. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  34323. /* Do TCP connect */
  34324. AssertIntEQ(BIO_do_connect(sslBio), 1);
  34325. /* Do TLS handshake */
  34326. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  34327. /* Test writing */
  34328. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  34329. /* Expect length of default wolfSSL reply */
  34330. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  34331. /* Clean it all up */
  34332. BIO_free_all(sslBio);
  34333. /* Server clean up */
  34334. join_thread(serverThread);
  34335. FreeTcpReady(&ready);
  34336. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  34337. * after. */
  34338. XMEMSET(&server_args, 0, sizeof(func_args));
  34339. StartTCP();
  34340. InitTcpReady(&ready);
  34341. #if defined(USE_WINDOWS_API)
  34342. /* use RNG to get random port if using windows */
  34343. ready.port = GetRandomPort();
  34344. #endif
  34345. server_args.signal = &ready;
  34346. start_thread(test_server_nofail, &server_args, &serverThread);
  34347. wait_tcp_ready(&server_args);
  34348. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  34349. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  34350. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  34351. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  34352. AssertIntEQ(BIO_do_connect(sslBio), 1);
  34353. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  34354. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  34355. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  34356. /* Attempt to close the TLS connection gracefully. */
  34357. BIO_ssl_shutdown(sslBio);
  34358. BIO_free_all(sslBio);
  34359. join_thread(serverThread);
  34360. FreeTcpReady(&ready);
  34361. SSL_CTX_free(ctx);
  34362. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  34363. wc_ecc_fp_free(); /* free per thread cache */
  34364. #endif
  34365. printf(resultFmt, passed);
  34366. #endif
  34367. return 0;
  34368. }
  34369. static int test_wolfSSL_BIO_tls(void)
  34370. {
  34371. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  34372. SSL_CTX* ctx;
  34373. SSL *ssl;
  34374. BIO *readBio;
  34375. BIO *writeBio;
  34376. int ret, err = 0;
  34377. printf(testingFmt, "test_wolfSSL_BIO_tls()");
  34378. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  34379. AssertNotNull(ssl = SSL_new(ctx));
  34380. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  34381. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  34382. /* Qt reads data from write-bio,
  34383. * then writes the read data into plain packet.
  34384. * Qt reads data from plain packet,
  34385. * then writes the read data into read-bio.
  34386. */
  34387. SSL_set_bio(ssl, readBio, writeBio);
  34388. do {
  34389. #ifdef WOLFSSL_ASYNC_CRYPT
  34390. if (err == WC_PENDING_E) {
  34391. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34392. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34393. }
  34394. #endif
  34395. ret = SSL_connect(ssl);
  34396. err = SSL_get_error(ssl, 0);
  34397. } while (err == WC_PENDING_E);
  34398. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  34399. /* in this use case, should return WANT READ
  34400. * so that Qt will read the data from plain packet for next state.
  34401. */
  34402. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  34403. SSL_free(ssl);
  34404. SSL_CTX_free(ctx);
  34405. printf(resultFmt, passed);
  34406. #endif
  34407. return 0;
  34408. }
  34409. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  34410. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  34411. {
  34412. BIO* clientBio;
  34413. SSL* sslClient;
  34414. SSL_CTX* ctx;
  34415. char connectAddr[20]; /* IP + port */;
  34416. (void)args;
  34417. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  34418. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  34419. AssertIntEQ(BIO_do_connect(clientBio), 1);
  34420. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  34421. AssertNotNull(sslClient = SSL_new(ctx));
  34422. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  34423. SSL_set_bio(sslClient, clientBio, clientBio);
  34424. AssertIntEQ(SSL_connect(sslClient), 1);
  34425. SSL_free(sslClient);
  34426. SSL_CTX_free(ctx);
  34427. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  34428. wc_ecc_fp_free(); /* free per thread cache */
  34429. #endif
  34430. return 0;
  34431. }
  34432. #endif
  34433. static int test_wolfSSL_BIO_accept(void)
  34434. {
  34435. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  34436. BIO* serverBindBio;
  34437. BIO* serverAcceptBio;
  34438. SSL* sslServer;
  34439. SSL_CTX* ctx;
  34440. func_args args;
  34441. THREAD_TYPE thread;
  34442. char port[10]; /* 10 bytes should be enough to store the string
  34443. * representation of the port */
  34444. printf(testingFmt, "wolfSSL_BIO_new_accept()");
  34445. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  34446. AssertNotNull(serverBindBio = BIO_new_accept(port));
  34447. /* First BIO_do_accept binds the port */
  34448. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  34449. XMEMSET(&args, 0, sizeof(func_args));
  34450. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  34451. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  34452. /* Let's plug it into SSL to test */
  34453. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  34454. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  34455. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  34456. AssertNotNull(sslServer = SSL_new(ctx));
  34457. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  34458. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  34459. AssertIntEQ(SSL_accept(sslServer), 1);
  34460. join_thread(thread);
  34461. BIO_free(serverBindBio);
  34462. SSL_free(sslServer);
  34463. SSL_CTX_free(ctx);
  34464. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  34465. wc_ecc_fp_free(); /* free per thread cache */
  34466. #endif
  34467. printf(resultFmt, passed);
  34468. #endif
  34469. return 0;
  34470. }
  34471. static int test_wolfSSL_BIO_write(void)
  34472. {
  34473. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  34474. BIO* bio;
  34475. BIO* bio64;
  34476. BIO* ptr;
  34477. int sz;
  34478. char msg[] = "conversion test";
  34479. char out[40];
  34480. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  34481. void* bufPtr = NULL;
  34482. BUF_MEM* buf = NULL;
  34483. printf(testingFmt, "wolfSSL_BIO_write()");
  34484. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  34485. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  34486. /* now should convert to base64 then write to memory */
  34487. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34488. BIO_flush(bio);
  34489. /* test BIO chain */
  34490. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  34491. AssertNotNull(buf);
  34492. AssertIntEQ(buf->length, 25);
  34493. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  34494. AssertPtrEq(buf->data, bufPtr);
  34495. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  34496. sz = sizeof(out);
  34497. XMEMSET(out, 0, sz);
  34498. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  34499. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  34500. /* write then read should return the same message */
  34501. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34502. sz = sizeof(out);
  34503. XMEMSET(out, 0, sz);
  34504. AssertIntEQ(BIO_read(bio, out, sz), 16);
  34505. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  34506. /* now try encoding with no line ending */
  34507. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  34508. #ifdef HAVE_EX_DATA
  34509. BIO_set_ex_data(bio64, 0, (void*) "data");
  34510. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  34511. #endif
  34512. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34513. BIO_flush(bio);
  34514. sz = sizeof(out);
  34515. XMEMSET(out, 0, sz);
  34516. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  34517. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  34518. BIO_free_all(bio); /* frees bio64 also */
  34519. /* test with more than one bio64 in list */
  34520. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  34521. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  34522. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  34523. /* now should convert to base64 when stored and then decode with read */
  34524. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  34525. BIO_flush(bio);
  34526. sz = sizeof(out);
  34527. XMEMSET(out, 0, sz);
  34528. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  34529. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  34530. BIO_clear_flags(bio64, ~0);
  34531. BIO_set_retry_read(bio);
  34532. BIO_free_all(bio); /* frees bio64s also */
  34533. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  34534. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  34535. BIO_free(bio);
  34536. printf(resultFmt, passed);
  34537. #endif
  34538. return 0;
  34539. }
  34540. static int test_wolfSSL_BIO_printf(void)
  34541. {
  34542. #if defined(OPENSSL_ALL)
  34543. BIO* bio;
  34544. int sz = 7;
  34545. char msg[] = "TLS 1.3 for the world";
  34546. char out[60];
  34547. char expected[] = "TLS 1.3 for the world : sz = 7";
  34548. printf(testingFmt, "wolfSSL_BIO_printf()");
  34549. XMEMSET(out, 0, sizeof(out));
  34550. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34551. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  34552. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  34553. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  34554. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  34555. BIO_free(bio);
  34556. printf(resultFmt, passed);
  34557. #endif
  34558. return 0;
  34559. }
  34560. static int test_wolfSSL_BIO_f_md(void)
  34561. {
  34562. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  34563. BIO *bio, *mem;
  34564. char msg[] = "message to hash";
  34565. char out[60];
  34566. EVP_MD_CTX* ctx;
  34567. const unsigned char testKey[] =
  34568. {
  34569. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  34570. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  34571. 0x0b, 0x0b, 0x0b, 0x0b
  34572. };
  34573. const char testData[] = "Hi There";
  34574. const unsigned char testResult[] =
  34575. {
  34576. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  34577. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  34578. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  34579. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  34580. };
  34581. const unsigned char expectedHash[] =
  34582. {
  34583. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  34584. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  34585. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  34586. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  34587. };
  34588. const unsigned char emptyHash[] =
  34589. {
  34590. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  34591. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  34592. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  34593. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  34594. };
  34595. unsigned char check[sizeof(testResult) + 1];
  34596. size_t checkSz = -1;
  34597. EVP_PKEY* key;
  34598. printf(testingFmt, "wolfSSL_BIO_f_md()");
  34599. XMEMSET(out, 0, sizeof(out));
  34600. AssertNotNull(bio = BIO_new(BIO_f_md()));
  34601. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  34602. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  34603. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  34604. /* should not be able to write/read yet since just digest wrapper and no
  34605. * data is passing through the bio */
  34606. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  34607. AssertIntEQ(BIO_pending(bio), 0);
  34608. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  34609. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  34610. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  34611. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  34612. BIO_reset(bio);
  34613. /* append BIO mem to bio in order to read/write */
  34614. AssertNotNull(bio = BIO_push(bio, mem));
  34615. XMEMSET(out, 0, sizeof(out));
  34616. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  34617. AssertIntEQ(BIO_pending(bio), 16);
  34618. /* this just reads the message and does not hash it (gets calls final) */
  34619. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  34620. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  34621. /* create a message digest using BIO */
  34622. XMEMSET(out, 0, sizeof(out));
  34623. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  34624. AssertIntEQ(BIO_pending(mem), 16);
  34625. AssertIntEQ(BIO_pending(bio), 16);
  34626. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  34627. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  34628. BIO_free(bio);
  34629. BIO_free(mem);
  34630. /* test with HMAC */
  34631. XMEMSET(out, 0, sizeof(out));
  34632. AssertNotNull(bio = BIO_new(BIO_f_md()));
  34633. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  34634. BIO_get_md_ctx(bio, &ctx);
  34635. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  34636. testKey, (int)sizeof(testKey)));
  34637. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  34638. AssertNotNull(bio = BIO_push(bio, mem));
  34639. BIO_write(bio, testData, (int)strlen(testData));
  34640. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  34641. EVP_DigestSignFinal(ctx, check, &checkSz);
  34642. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  34643. EVP_PKEY_free(key);
  34644. BIO_free(bio);
  34645. BIO_free(mem);
  34646. printf(resultFmt, passed);
  34647. #endif
  34648. return 0;
  34649. }
  34650. static int test_wolfSSL_BIO_up_ref(void)
  34651. {
  34652. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  34653. BIO* bio;
  34654. printf(testingFmt, "wolfSSL_BIO_up_ref()");
  34655. AssertNotNull(bio = BIO_new(BIO_f_md()));
  34656. AssertIntEQ(BIO_up_ref(NULL), 0);
  34657. AssertIntEQ(BIO_up_ref(bio), 1);
  34658. BIO_free(bio);
  34659. AssertIntEQ(BIO_up_ref(bio), 1);
  34660. BIO_free(bio);
  34661. BIO_free(bio);
  34662. printf(resultFmt, "passed");
  34663. #endif
  34664. return 0;
  34665. }
  34666. #endif /* !NO_BIO */
  34667. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  34668. /* test that the callback arg is correct */
  34669. static int certCbArg = 0;
  34670. static int clientCertCb(WOLFSSL* ssl, void* arg)
  34671. {
  34672. if (ssl == NULL || arg != &certCbArg)
  34673. return 0;
  34674. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  34675. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34676. return 0;
  34677. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  34678. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34679. return 0;
  34680. return 1;
  34681. }
  34682. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  34683. {
  34684. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  34685. }
  34686. /**
  34687. * This is only done because test_client_nofail has no way to stop
  34688. * certificate and key loading
  34689. */
  34690. static void clientCertClearCb(WOLFSSL* ssl)
  34691. {
  34692. /* Clear the loaded certs to force the callbacks to set them up */
  34693. SSL_certs_clear(ssl);
  34694. }
  34695. static int serverCertCb(WOLFSSL* ssl, void* arg)
  34696. {
  34697. if (ssl == NULL || arg != &certCbArg)
  34698. return 0;
  34699. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  34700. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34701. return 0;
  34702. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  34703. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  34704. return 0;
  34705. return 1;
  34706. }
  34707. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  34708. {
  34709. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  34710. }
  34711. /**
  34712. * This is only done because test_server_nofail has no way to stop
  34713. * certificate and key loading
  34714. */
  34715. static void serverCertClearCb(WOLFSSL* ssl)
  34716. {
  34717. /* Clear the loaded certs to force the callbacks to set them up */
  34718. SSL_certs_clear(ssl);
  34719. }
  34720. #endif
  34721. static int test_wolfSSL_cert_cb(void)
  34722. {
  34723. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  34724. callback_functions func_cb_client;
  34725. callback_functions func_cb_server;
  34726. tcp_ready ready;
  34727. func_args client_args;
  34728. func_args server_args;
  34729. THREAD_TYPE serverThread;
  34730. XMEMSET(&client_args, 0, sizeof(func_args));
  34731. XMEMSET(&server_args, 0, sizeof(func_args));
  34732. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  34733. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  34734. #ifdef WOLFSSL_TIRTOS
  34735. fdOpenSession(Task_self());
  34736. #endif
  34737. StartTCP();
  34738. InitTcpReady(&ready);
  34739. #if defined(USE_WINDOWS_API)
  34740. /* use RNG to get random port if using windows */
  34741. ready.port = GetRandomPort();
  34742. #endif
  34743. server_args.signal = &ready;
  34744. client_args.signal = &ready;
  34745. client_args.callbacks = &func_cb_client;
  34746. server_args.callbacks = &func_cb_server;
  34747. func_cb_client.ctx_ready = clientCertSetupCb;
  34748. func_cb_client.ssl_ready = clientCertClearCb;
  34749. func_cb_server.ctx_ready = serverCertSetupCb;
  34750. func_cb_server.ssl_ready = serverCertClearCb;
  34751. start_thread(test_server_nofail, &server_args, &serverThread);
  34752. wait_tcp_ready(&server_args);
  34753. test_client_nofail(&client_args, NULL);
  34754. join_thread(serverThread);
  34755. AssertTrue(client_args.return_code);
  34756. AssertTrue(server_args.return_code);
  34757. FreeTcpReady(&ready);
  34758. #ifdef WOLFSSL_TIRTOS
  34759. fdOpenSession(Task_self());
  34760. #endif
  34761. #endif
  34762. return 0;
  34763. }
  34764. static int test_wolfSSL_SESSION(void)
  34765. {
  34766. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  34767. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34768. !defined(NO_SESSION_CACHE)
  34769. WOLFSSL* ssl;
  34770. WOLFSSL_CTX* ctx;
  34771. WOLFSSL_SESSION* sess;
  34772. WOLFSSL_SESSION* sess_copy;
  34773. #ifdef OPENSSL_EXTRA
  34774. unsigned char* sessDer = NULL;
  34775. unsigned char* ptr = NULL;
  34776. const unsigned char context[] = "user app context";
  34777. unsigned int contextSz = (unsigned int)sizeof(context);
  34778. int sz;
  34779. #endif
  34780. int ret, err;
  34781. SOCKET_T sockfd;
  34782. tcp_ready ready;
  34783. func_args server_args;
  34784. THREAD_TYPE serverThread;
  34785. char msg[80];
  34786. const char* sendGET = "GET";
  34787. printf(testingFmt, "wolfSSL_SESSION()");
  34788. /* TLS v1.3 requires session tickets */
  34789. /* CHACHA and POLY1305 required for myTicketEncCb */
  34790. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  34791. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  34792. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  34793. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  34794. #else
  34795. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34796. #endif
  34797. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  34798. WOLFSSL_FILETYPE_PEM));
  34799. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  34800. WOLFSSL_FILETYPE_PEM));
  34801. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  34802. WOLFSSL_SUCCESS);
  34803. #ifdef WOLFSSL_ENCRYPTED_KEYS
  34804. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  34805. #endif
  34806. #ifdef HAVE_SESSION_TICKET
  34807. /* Use session tickets, for ticket tests below */
  34808. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  34809. #endif
  34810. XMEMSET(&server_args, 0, sizeof(func_args));
  34811. #ifdef WOLFSSL_TIRTOS
  34812. fdOpenSession(Task_self());
  34813. #endif
  34814. StartTCP();
  34815. InitTcpReady(&ready);
  34816. #if defined(USE_WINDOWS_API)
  34817. /* use RNG to get random port if using windows */
  34818. ready.port = GetRandomPort();
  34819. #endif
  34820. server_args.signal = &ready;
  34821. start_thread(test_server_nofail, &server_args, &serverThread);
  34822. wait_tcp_ready(&server_args);
  34823. /* client connection */
  34824. ssl = wolfSSL_new(ctx);
  34825. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  34826. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  34827. #ifdef WOLFSSL_ASYNC_CRYPT
  34828. err = 0; /* Reset error */
  34829. #endif
  34830. do {
  34831. #ifdef WOLFSSL_ASYNC_CRYPT
  34832. if (err == WC_PENDING_E) {
  34833. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34834. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34835. }
  34836. #endif
  34837. ret = wolfSSL_connect(ssl);
  34838. err = wolfSSL_get_error(ssl, 0);
  34839. } while (err == WC_PENDING_E);
  34840. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  34841. #ifdef WOLFSSL_ASYNC_CRYPT
  34842. err = 0; /* Reset error */
  34843. #endif
  34844. do {
  34845. #ifdef WOLFSSL_ASYNC_CRYPT
  34846. if (err == WC_PENDING_E) {
  34847. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34848. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34849. }
  34850. #endif
  34851. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  34852. err = wolfSSL_get_error(ssl, 0);
  34853. } while (err == WC_PENDING_E);
  34854. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  34855. #ifdef WOLFSSL_ASYNC_CRYPT
  34856. err = 0; /* Reset error */
  34857. #endif
  34858. do {
  34859. #ifdef WOLFSSL_ASYNC_CRYPT
  34860. if (err == WC_PENDING_E) {
  34861. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  34862. if (ret < 0) { break; } else if (ret == 0) { continue; }
  34863. }
  34864. #endif
  34865. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  34866. err = wolfSSL_get_error(ssl, 0);
  34867. } while (err == WC_PENDING_E);
  34868. AssertIntEQ(ret, 23);
  34869. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  34870. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  34871. #ifdef HAVE_EXT_CACHE
  34872. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  34873. * HAVE_EXT_CACHE we get new objects each time */
  34874. #endif
  34875. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  34876. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  34877. sess = wolfSSL_get_session(ssl);
  34878. #ifdef OPENSSL_EXTRA
  34879. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  34880. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  34881. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  34882. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  34883. #ifdef HAVE_SESSION_TICKET
  34884. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  34885. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  34886. SESSION_TICKET_HINT_DEFAULT);
  34887. #else
  34888. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  34889. #endif
  34890. #else
  34891. (void)sess;
  34892. #endif /* OPENSSL_EXTRA */
  34893. /* Retain copy of the session for later testing */
  34894. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  34895. wolfSSL_shutdown(ssl);
  34896. wolfSSL_free(ssl);
  34897. join_thread(serverThread);
  34898. FreeTcpReady(&ready);
  34899. #ifdef WOLFSSL_TIRTOS
  34900. fdOpenSession(Task_self());
  34901. #endif
  34902. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  34903. {
  34904. X509 *x509;
  34905. char buf[30];
  34906. int bufSz;
  34907. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  34908. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  34909. X509_get_subject_name(x509), NID_organizationalUnitName,
  34910. buf, sizeof(buf))), 0);
  34911. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  34912. if (bufSz == 7) {
  34913. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  34914. }
  34915. if (bufSz == 16) {
  34916. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  34917. }
  34918. }
  34919. #endif
  34920. #ifdef HAVE_EXT_CACHE
  34921. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  34922. wolfSSL_SESSION_free(sess_copy);
  34923. sess_copy = NULL;
  34924. #endif
  34925. #ifdef OPENSSL_EXTRA
  34926. /* get session from DER and update the timeout */
  34927. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  34928. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  34929. wolfSSL_SESSION_free(sess);
  34930. sess = NULL;
  34931. ptr = sessDer;
  34932. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  34933. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  34934. (const unsigned char**)&ptr, sz));
  34935. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  34936. sessDer = NULL;
  34937. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  34938. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  34939. #endif
  34940. /* successful set session test */
  34941. AssertNotNull(ssl = wolfSSL_new(ctx));
  34942. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  34943. #ifdef HAVE_SESSION_TICKET
  34944. /* Test set/get session ticket */
  34945. {
  34946. const char* ticket = "This is a session ticket";
  34947. char buf[64] = {0};
  34948. word32 bufSz = (word32)sizeof(buf);
  34949. AssertIntEQ(SSL_SUCCESS,
  34950. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  34951. (word32)XSTRLEN(ticket)));
  34952. AssertIntEQ(SSL_SUCCESS,
  34953. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  34954. AssertStrEQ(ticket, buf);
  34955. }
  34956. #endif
  34957. #ifdef OPENSSL_EXTRA
  34958. /* session timeout case */
  34959. /* make the session to be expired */
  34960. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  34961. XSLEEP_MS(1200);
  34962. /* SSL_set_session should reject specified session but return success
  34963. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  34964. */
  34965. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  34966. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  34967. #else
  34968. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  34969. #endif
  34970. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  34971. /* fail case with miss match session context IDs (use compatibility API) */
  34972. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  34973. SSL_SUCCESS);
  34974. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  34975. wolfSSL_free(ssl);
  34976. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  34977. SSL_FAILURE);
  34978. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  34979. SSL_SUCCESS);
  34980. AssertNotNull(ssl = wolfSSL_new(ctx));
  34981. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  34982. #endif /* OPENSSL_EXTRA */
  34983. wolfSSL_free(ssl);
  34984. wolfSSL_SESSION_free(sess);
  34985. wolfSSL_CTX_free(ctx);
  34986. printf(resultFmt, passed);
  34987. #endif
  34988. return 0;
  34989. }
  34990. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  34991. defined(HAVE_EX_DATA)
  34992. static int clientSessRemCountMalloc = 0;
  34993. static int serverSessRemCountMalloc = 0;
  34994. static int clientSessRemCountFree = 0;
  34995. static int serverSessRemCountFree = 0;
  34996. static WOLFSSL_CTX* serverSessCtx = NULL;
  34997. static WOLFSSL_SESSION* serverSess = NULL;
  34998. #ifndef NO_SESSION_CACHE_REF
  34999. static WOLFSSL_CTX* clientSessCtx = NULL;
  35000. static WOLFSSL_SESSION* clientSess = NULL;
  35001. #endif
  35002. static int serverSessRemIdx = 3;
  35003. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  35004. {
  35005. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  35006. (void)ctx;
  35007. AssertNotNull(mallocedData);
  35008. if (!*mallocedData)
  35009. clientSessRemCountFree++;
  35010. else
  35011. serverSessRemCountFree++;
  35012. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  35013. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  35014. }
  35015. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  35016. {
  35017. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  35018. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  35019. !defined(NO_SESSION_CACHE_REF)
  35020. /* Allow downgrade, set min version, and disable TLS 1.3.
  35021. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  35022. * reference to the session cache. But with WOLFSSL_TLS13 and without
  35023. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  35024. * session in the cache. In this case we need to downgrade to previous
  35025. * versions to just use the legacy session ID field. */
  35026. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  35027. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  35028. #endif
  35029. }
  35030. static void SessRemSslSetupCb(WOLFSSL* ssl)
  35031. {
  35032. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  35033. AssertNotNull(mallocedData);
  35034. *mallocedData = SSL_is_server(ssl);
  35035. if (!*mallocedData) {
  35036. clientSessRemCountMalloc++;
  35037. #ifndef NO_SESSION_CACHE_REF
  35038. AssertNotNull(clientSess = SSL_get1_session(ssl));
  35039. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  35040. SSL_SUCCESS);
  35041. #endif
  35042. }
  35043. else {
  35044. serverSessRemCountMalloc++;
  35045. AssertNotNull(serverSess = SSL_get1_session(ssl));
  35046. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  35047. SSL_SUCCESS);
  35048. }
  35049. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  35050. mallocedData), SSL_SUCCESS);
  35051. }
  35052. #endif
  35053. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  35054. {
  35055. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35056. defined(HAVE_EX_DATA)
  35057. /* Check that the remove callback gets called for external data in a
  35058. * session object */
  35059. callback_functions func_cb;
  35060. tcp_ready ready;
  35061. func_args client_args;
  35062. func_args server_args;
  35063. THREAD_TYPE serverThread;
  35064. printf(testingFmt, "wolfSSL_CTX_sess_set_remove_cb()");
  35065. XMEMSET(&client_args, 0, sizeof(func_args));
  35066. XMEMSET(&server_args, 0, sizeof(func_args));
  35067. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  35068. #ifdef WOLFSSL_TIRTOS
  35069. fdOpenSession(Task_self());
  35070. #endif
  35071. StartTCP();
  35072. InitTcpReady(&ready);
  35073. #if defined(USE_WINDOWS_API)
  35074. /* use RNG to get random port if using windows */
  35075. ready.port = GetRandomPort();
  35076. #endif
  35077. server_args.signal = &ready;
  35078. client_args.signal = &ready;
  35079. client_args.callbacks = &func_cb;
  35080. server_args.callbacks = &func_cb;
  35081. func_cb.ctx_ready = SessRemCtxSetupCb;
  35082. func_cb.on_result = SessRemSslSetupCb;
  35083. start_thread(test_server_nofail, &server_args, &serverThread);
  35084. wait_tcp_ready(&server_args);
  35085. test_client_nofail(&client_args, NULL);
  35086. join_thread(serverThread);
  35087. AssertTrue(client_args.return_code);
  35088. AssertTrue(server_args.return_code);
  35089. FreeTcpReady(&ready);
  35090. #ifdef WOLFSSL_TIRTOS
  35091. fdOpenSession(Task_self());
  35092. #endif
  35093. /* Both should have been allocated */
  35094. AssertIntEQ(clientSessRemCountMalloc, 1);
  35095. AssertIntEQ(serverSessRemCountMalloc, 1);
  35096. #ifdef NO_SESSION_CACHE_REF
  35097. /* Client session should not be added to cache so this should be free'd when
  35098. * the SSL object was being free'd */
  35099. AssertIntEQ(clientSessRemCountFree, 1);
  35100. #else
  35101. /* Client session is in cache due to requiring a persistent reference */
  35102. AssertIntEQ(clientSessRemCountFree, 0);
  35103. /* Force a cache lookup */
  35104. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  35105. /* Force a cache update */
  35106. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  35107. /* This should set the timeout to 0 and call the remove callback from within
  35108. * the session cache. */
  35109. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  35110. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  35111. AssertIntEQ(clientSessRemCountFree, 1);
  35112. #endif
  35113. /* Server session is in the cache so ex_data isn't free'd with the SSL
  35114. * object */
  35115. AssertIntEQ(serverSessRemCountFree, 0);
  35116. /* Force a cache lookup */
  35117. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  35118. /* Force a cache update */
  35119. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  35120. /* This should set the timeout to 0 and call the remove callback from within
  35121. * the session cache. */
  35122. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  35123. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  35124. AssertIntEQ(serverSessRemCountFree, 1);
  35125. /* Need to free the references that we kept */
  35126. SSL_CTX_free(serverSessCtx);
  35127. SSL_SESSION_free(serverSess);
  35128. #ifndef NO_SESSION_CACHE_REF
  35129. SSL_CTX_free(clientSessCtx);
  35130. SSL_SESSION_free(clientSess);
  35131. #endif
  35132. printf(resultFmt, passed);
  35133. #endif
  35134. return 0;
  35135. }
  35136. static int test_wolfSSL_ticket_keys(void)
  35137. {
  35138. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  35139. !defined(NO_WOLFSSL_SERVER)
  35140. WOLFSSL_CTX* ctx;
  35141. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  35142. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35143. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  35144. WOLFSSL_FAILURE);
  35145. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  35146. WOLFSSL_FAILURE);
  35147. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  35148. WOLFSSL_FAILURE);
  35149. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  35150. WOLFSSL_FAILURE);
  35151. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  35152. WOLFSSL_FAILURE);
  35153. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  35154. WOLFSSL_FAILURE);
  35155. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  35156. WOLFSSL_FAILURE);
  35157. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  35158. WOLFSSL_FAILURE);
  35159. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  35160. WOLFSSL_FAILURE);
  35161. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  35162. WOLFSSL_FAILURE);
  35163. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  35164. WOLFSSL_FAILURE);
  35165. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  35166. WOLFSSL_FAILURE);
  35167. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  35168. WOLFSSL_FAILURE);
  35169. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  35170. WOLFSSL_FAILURE);
  35171. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  35172. WOLFSSL_SUCCESS);
  35173. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  35174. WOLFSSL_SUCCESS);
  35175. wolfSSL_CTX_free(ctx);
  35176. #endif
  35177. return 0;
  35178. }
  35179. #ifndef NO_BIO
  35180. static int test_wolfSSL_d2i_PUBKEY(void)
  35181. {
  35182. #if defined(OPENSSL_EXTRA)
  35183. BIO* bio;
  35184. EVP_PKEY* pkey;
  35185. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  35186. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35187. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  35188. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  35189. /* RSA PUBKEY test */
  35190. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  35191. sizeof_client_keypub_der_2048), 0);
  35192. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35193. EVP_PKEY_free(pkey);
  35194. #endif
  35195. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  35196. /* ECC PUBKEY test */
  35197. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  35198. sizeof_ecc_clikeypub_der_256), 0);
  35199. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35200. EVP_PKEY_free(pkey);
  35201. #endif
  35202. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  35203. /* DSA PUBKEY test */
  35204. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  35205. sizeof_dsa_pub_key_der_2048), 0);
  35206. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35207. EVP_PKEY_free(pkey);
  35208. #endif
  35209. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  35210. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  35211. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  35212. (HAVE_FIPS_VERSION > 2))
  35213. /* DH PUBKEY test */
  35214. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  35215. sizeof_dh_pub_key_der_2048), 0);
  35216. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  35217. EVP_PKEY_free(pkey);
  35218. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  35219. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  35220. BIO_free(bio);
  35221. (void)pkey;
  35222. printf(resultFmt, passed);
  35223. #endif
  35224. return 0;
  35225. }
  35226. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  35227. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  35228. {
  35229. BIO* bio = NULL;
  35230. EVP_PKEY* pkey = NULL;
  35231. #ifndef NO_RSA
  35232. #endif
  35233. WOLFSSL_CTX* ctx;
  35234. #if defined(WOLFSSL_KEY_GEN)
  35235. unsigned char buff[4096];
  35236. unsigned char* bufPtr = buff;
  35237. #endif
  35238. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  35239. /* test creating new EVP_PKEY with bad arg */
  35240. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  35241. /* test loading RSA key using BIO */
  35242. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  35243. {
  35244. XFILE file;
  35245. const char* fname = "./certs/server-key.der";
  35246. size_t sz;
  35247. byte* buf;
  35248. file = XFOPEN(fname, "rb");
  35249. AssertTrue((file != XBADFILE));
  35250. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  35251. sz = XFTELL(file);
  35252. XREWIND(file);
  35253. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  35254. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  35255. XFCLOSE(file);
  35256. /* Test using BIO new mem and loading DER private key */
  35257. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  35258. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  35259. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  35260. BIO_free(bio);
  35261. bio = NULL;
  35262. EVP_PKEY_free(pkey);
  35263. pkey = NULL;
  35264. }
  35265. #endif
  35266. /* test loading ECC key using BIO */
  35267. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  35268. {
  35269. XFILE file;
  35270. const char* fname = "./certs/ecc-key.der";
  35271. size_t sz;
  35272. byte* buf;
  35273. file = XFOPEN(fname, "rb");
  35274. AssertTrue((file != XBADFILE));
  35275. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  35276. sz = XFTELL(file);
  35277. XREWIND(file);
  35278. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  35279. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  35280. XFCLOSE(file);
  35281. /* Test using BIO new mem and loading DER private key */
  35282. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  35283. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  35284. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  35285. BIO_free(bio);
  35286. bio = NULL;
  35287. EVP_PKEY_free(pkey);
  35288. pkey = NULL;
  35289. }
  35290. #endif
  35291. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35292. #ifndef NO_WOLFSSL_SERVER
  35293. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35294. #else
  35295. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35296. #endif
  35297. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  35298. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  35299. {
  35300. RSA* rsa = NULL;
  35301. /* Tests bad parameters */
  35302. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  35303. /* RSA not set yet, expecting to fail*/
  35304. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  35305. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  35306. /* set RSA using bio*/
  35307. AssertIntGT(BIO_write(bio, client_key_der_2048,
  35308. sizeof_client_key_der_2048), 0);
  35309. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  35310. AssertNotNull(rsa);
  35311. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  35312. /*i2d RSAprivate key tests */
  35313. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  35314. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  35315. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  35316. sizeof_client_key_der_2048);
  35317. bufPtr -= sizeof_client_key_der_2048;
  35318. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  35319. sizeof_client_key_der_2048), 0);
  35320. bufPtr = NULL;
  35321. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  35322. sizeof_client_key_der_2048);
  35323. AssertNotNull(bufPtr);
  35324. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  35325. sizeof_client_key_der_2048), 0);
  35326. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  35327. RSA_free(rsa);
  35328. rsa = RSA_new();
  35329. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  35330. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  35331. RSA_free(rsa);
  35332. }
  35333. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  35334. SSL_CTX_free(ctx);
  35335. ctx = NULL;
  35336. BIO_free(bio);
  35337. bio = NULL;
  35338. printf(resultFmt, passed);
  35339. return 0;
  35340. }
  35341. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35342. #endif /* !NO_BIO */
  35343. static int test_wolfSSL_sk_GENERAL_NAME(void)
  35344. {
  35345. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35346. !defined(NO_RSA)
  35347. X509* x509;
  35348. GENERAL_NAME* gn;
  35349. unsigned char buf[4096];
  35350. const unsigned char* bufPt;
  35351. int bytes, i;
  35352. int j;
  35353. XFILE f;
  35354. STACK_OF(GENERAL_NAME)* sk;
  35355. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  35356. f = XFOPEN(cliCertDerFileExt, "rb");
  35357. AssertTrue((f != XBADFILE));
  35358. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  35359. XFCLOSE(f);
  35360. for (j = 0; j < 2; ++j) {
  35361. bufPt = buf;
  35362. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  35363. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  35364. NID_subject_alt_name, NULL, NULL));
  35365. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  35366. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  35367. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  35368. switch (gn->type) {
  35369. case GEN_DNS:
  35370. printf("found type GEN_DNS\n");
  35371. break;
  35372. case GEN_EMAIL:
  35373. printf("found type GEN_EMAIL\n");
  35374. break;
  35375. case GEN_URI:
  35376. printf("found type GEN_URI\n");
  35377. break;
  35378. }
  35379. }
  35380. X509_free(x509);
  35381. if (j == 0) {
  35382. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  35383. }
  35384. else {
  35385. /*
  35386. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  35387. * it was supposed to. This is a regression test for that bug.
  35388. */
  35389. GENERAL_NAMES_free(sk);
  35390. }
  35391. }
  35392. printf(resultFmt, passed);
  35393. #endif
  35394. return 0;
  35395. }
  35396. static int test_wolfSSL_GENERAL_NAME_print(void)
  35397. {
  35398. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  35399. X509* x509;
  35400. GENERAL_NAME* gn;
  35401. unsigned char buf[4096];
  35402. const unsigned char* bufPt;
  35403. int bytes;
  35404. XFILE f;
  35405. STACK_OF(GENERAL_NAME)* sk;
  35406. BIO* out;
  35407. unsigned char outbuf[128];
  35408. X509_EXTENSION* ext;
  35409. AUTHORITY_INFO_ACCESS* aia;
  35410. ACCESS_DESCRIPTION* ad;
  35411. const unsigned char v4Addr[] = {192,168,53,1};
  35412. const unsigned char v6Addr[] =
  35413. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35414. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  35415. const unsigned char email[] =
  35416. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  35417. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  35418. const char* dnsStr = "DNS:example.com";
  35419. const char* uriStr = "URI:http://127.0.0.1:22220";
  35420. const char* v4addStr = "IP Address:192.168.53.1";
  35421. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  35422. const char* emailStr = "email:info@wolfssl.com";
  35423. const char* othrStr = "othername:<unsupported>";
  35424. const char* x400Str = "X400Name:<unsupported>";
  35425. const char* ediStr = "EdiPartyName:<unsupported>";
  35426. printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
  35427. /* BIO to output */
  35428. AssertNotNull(out = BIO_new(BIO_s_mem()));
  35429. /* test for NULL param */
  35430. gn = NULL;
  35431. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  35432. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  35433. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  35434. /* test for GEN_DNS */
  35435. f = XFOPEN(cliCertDerFileExt, "rb");
  35436. AssertTrue((f != XBADFILE));
  35437. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  35438. XFCLOSE(f);
  35439. bufPt = buf;
  35440. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  35441. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  35442. NID_subject_alt_name, NULL, NULL));
  35443. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  35444. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35445. XMEMSET(outbuf,0,sizeof(outbuf));
  35446. BIO_read(out, outbuf, sizeof(outbuf));
  35447. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  35448. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  35449. X509_free(x509);
  35450. /* test for GEN_URI */
  35451. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  35452. AssertTrue((f != XBADFILE));
  35453. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  35454. XFCLOSE(f);
  35455. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  35456. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  35457. AssertNotNull(aia);
  35458. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  35459. gn = ad->location;
  35460. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35461. XMEMSET(outbuf,0,sizeof(outbuf));
  35462. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35463. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  35464. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  35465. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  35466. AssertNotNull(aia);
  35467. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  35468. X509_free(x509);
  35469. /* test for GEN_IPADD */
  35470. /* ip v4 address */
  35471. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35472. gn->type = GEN_IPADD;
  35473. gn->d.iPAddress->length = sizeof(v4Addr);
  35474. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  35475. sizeof(v4Addr)), 1);
  35476. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35477. XMEMSET(outbuf,0,sizeof(outbuf));
  35478. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35479. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  35480. GENERAL_NAME_free(gn);
  35481. /* ip v6 address */
  35482. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35483. gn->type = GEN_IPADD;
  35484. gn->d.iPAddress->length = sizeof(v6Addr);
  35485. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  35486. sizeof(v6Addr)), 1);
  35487. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35488. XMEMSET(outbuf,0,sizeof(outbuf));
  35489. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35490. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  35491. GENERAL_NAME_free(gn);
  35492. /* test for GEN_EMAIL */
  35493. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35494. gn->type = GEN_EMAIL;
  35495. gn->d.rfc822Name->length = sizeof(email);
  35496. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  35497. sizeof(email)), 1);
  35498. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35499. XMEMSET(outbuf,0,sizeof(outbuf));
  35500. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35501. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  35502. GENERAL_NAME_free(gn);
  35503. /* test for GEN_OTHERNAME */
  35504. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35505. gn->type = GEN_OTHERNAME;
  35506. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35507. XMEMSET(outbuf,0,sizeof(outbuf));
  35508. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35509. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  35510. GENERAL_NAME_free(gn);
  35511. /* test for GEN_X400 */
  35512. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35513. gn->type = GEN_X400;
  35514. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35515. XMEMSET(outbuf,0,sizeof(outbuf));
  35516. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35517. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  35518. GENERAL_NAME_free(gn);
  35519. /* test for GEN_EDIPARTY */
  35520. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  35521. gn->type = GEN_EDIPARTY;
  35522. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  35523. XMEMSET(outbuf,0,sizeof(outbuf));
  35524. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  35525. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  35526. GENERAL_NAME_free(gn);
  35527. BIO_free(out);
  35528. printf(resultFmt, passed);
  35529. #endif /* OPENSSL_ALL */
  35530. return 0;
  35531. }
  35532. static int test_wolfSSL_sk_DIST_POINT(void)
  35533. {
  35534. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35535. !defined(NO_RSA)
  35536. X509* x509;
  35537. unsigned char buf[4096];
  35538. const unsigned char* bufPt;
  35539. int bytes, i, j;
  35540. XFILE f;
  35541. DIST_POINT* dp;
  35542. GENERAL_NAME* gn;
  35543. ASN1_IA5STRING* uri;
  35544. STACK_OF(DIST_POINT)* dps;
  35545. STACK_OF(GENERAL_NAME)* gns;
  35546. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  35547. printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
  35548. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  35549. AssertTrue((f != XBADFILE));
  35550. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  35551. XFCLOSE(f);
  35552. bufPt = buf;
  35553. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  35554. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  35555. NID_crl_distribution_points, NULL, NULL));
  35556. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  35557. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  35558. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  35559. gns = dp->distpoint->name.fullname;
  35560. AssertNotNull(gns);
  35561. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  35562. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  35563. gn = sk_GENERAL_NAME_value(gns, j);
  35564. AssertIntEQ(gn->type, GEN_URI);
  35565. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  35566. AssertNotNull(uri->data);
  35567. AssertIntGT(uri->length, 0);
  35568. }
  35569. }
  35570. X509_free(x509);
  35571. CRL_DIST_POINTS_free(dps);
  35572. printf(resultFmt, passed);
  35573. #endif
  35574. return 0;
  35575. }
  35576. static int test_wolfSSL_MD4(void)
  35577. {
  35578. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  35579. MD4_CTX md4;
  35580. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  35581. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  35582. "789012345678901234567890";
  35583. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  35584. "\xcc\x05\x36";
  35585. int msgSz = (int)XSTRLEN(msg);
  35586. printf(testingFmt, "wolfSSL_MD4()");
  35587. XMEMSET(out, 0, sizeof(out));
  35588. MD4_Init(&md4);
  35589. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  35590. MD4_Final(out, &md4);
  35591. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  35592. printf(resultFmt, passed);
  35593. #endif
  35594. return 0;
  35595. }
  35596. static int test_wolfSSL_verify_mode(void)
  35597. {
  35598. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  35599. WOLFSSL* ssl;
  35600. WOLFSSL_CTX* ctx;
  35601. printf(testingFmt, "test_wolfSSL_verify()");
  35602. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35603. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35604. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35605. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  35606. AssertNotNull(ssl = SSL_new(ctx));
  35607. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  35608. SSL_free(ssl);
  35609. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  35610. AssertNotNull(ssl = SSL_new(ctx));
  35611. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  35612. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  35613. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  35614. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  35615. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  35616. SSL_free(ssl);
  35617. wolfSSL_CTX_set_verify(ctx,
  35618. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  35619. AssertNotNull(ssl = SSL_new(ctx));
  35620. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  35621. AssertIntEQ(SSL_get_verify_mode(ssl),
  35622. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35623. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  35624. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  35625. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35626. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  35627. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  35628. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  35629. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  35630. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35631. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  35632. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  35633. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  35634. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  35635. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  35636. #endif
  35637. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  35638. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  35639. SSL_free(ssl);
  35640. SSL_CTX_free(ctx);
  35641. printf(resultFmt, passed);
  35642. #endif
  35643. return 0;
  35644. }
  35645. static int test_wolfSSL_verify_depth(void)
  35646. {
  35647. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35648. WOLFSSL* ssl;
  35649. WOLFSSL_CTX* ctx;
  35650. long depth;
  35651. printf(testingFmt, "test_wolfSSL_verify_depth()");
  35652. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35653. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35654. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35655. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  35656. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  35657. AssertNotNull(ssl = SSL_new(ctx));
  35658. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  35659. SSL_free(ssl);
  35660. SSL_CTX_set_verify_depth(ctx, -1);
  35661. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  35662. SSL_CTX_set_verify_depth(ctx, 2);
  35663. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  35664. AssertNotNull(ssl = SSL_new(ctx));
  35665. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  35666. SSL_free(ssl);
  35667. SSL_CTX_free(ctx);
  35668. printf(resultFmt, passed);
  35669. #endif
  35670. return 0;
  35671. }
  35672. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  35673. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  35674. * buffer of 64 bytes.
  35675. *
  35676. * returns the size of the digest buffer on success and a negative value on
  35677. * failure.
  35678. */
  35679. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  35680. {
  35681. HMAC_CTX ctx1;
  35682. HMAC_CTX ctx2;
  35683. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  35684. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  35685. unsigned char long_key[] =
  35686. "0123456789012345678901234567890123456789"
  35687. "0123456789012345678901234567890123456789"
  35688. "0123456789012345678901234567890123456789"
  35689. "0123456789012345678901234567890123456789";
  35690. unsigned char msg[] = "message to hash";
  35691. unsigned int digestSz = 64;
  35692. int keySz = sizeof(key);
  35693. int long_keySz = sizeof(long_key);
  35694. int msgSz = sizeof(msg);
  35695. unsigned char digest2[64];
  35696. unsigned int digestSz2 = 64;
  35697. HMAC_CTX_init(&ctx1);
  35698. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  35699. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35700. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35701. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35702. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35703. HMAC_CTX_cleanup(&ctx1);
  35704. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35705. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  35706. HMAC_CTX_cleanup(&ctx2);
  35707. AssertIntEQ(digestSz, digestSz2);
  35708. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35709. /* test HMAC_Init with NULL key */
  35710. /* init after copy */
  35711. printf("test HMAC_Init with NULL key (0)\n");
  35712. HMAC_CTX_init(&ctx1);
  35713. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  35714. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35715. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35716. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  35717. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35718. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35719. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35720. HMAC_CTX_cleanup(&ctx1);
  35721. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  35722. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35723. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35724. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  35725. HMAC_CTX_cleanup(&ctx2);
  35726. AssertIntEQ(digestSz, digestSz2);
  35727. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35728. /* long key */
  35729. printf("test HMAC_Init with NULL key (1)\n");
  35730. HMAC_CTX_init(&ctx1);
  35731. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  35732. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35733. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35734. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  35735. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35736. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35737. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35738. HMAC_CTX_cleanup(&ctx1);
  35739. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  35740. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35741. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35742. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  35743. HMAC_CTX_cleanup(&ctx2);
  35744. AssertIntEQ(digestSz, digestSz2);
  35745. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35746. /* init before copy */
  35747. printf("test HMAC_Init with NULL key (2)\n");
  35748. HMAC_CTX_init(&ctx1);
  35749. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  35750. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35751. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  35752. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  35753. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35754. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  35755. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  35756. HMAC_CTX_cleanup(&ctx1);
  35757. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35758. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  35759. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  35760. HMAC_CTX_cleanup(&ctx2);
  35761. AssertIntEQ(digestSz, digestSz2);
  35762. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  35763. return digestSz;
  35764. }
  35765. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  35766. static int test_wolfSSL_HMAC_CTX(void)
  35767. {
  35768. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  35769. unsigned char digest[64];
  35770. int digestSz;
  35771. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  35772. #ifndef NO_SHA
  35773. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  35774. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  35775. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  35776. #endif /* !NO_SHA */
  35777. #ifdef WOLFSSL_SHA224
  35778. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  35779. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  35780. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  35781. "\x02\x0E", digest, digestSz), 0);
  35782. #endif /* WOLFSSL_SHA224 */
  35783. #ifndef NO_SHA256
  35784. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  35785. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  35786. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  35787. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  35788. #endif /* !NO_SHA256 */
  35789. #ifdef WOLFSSL_SHA384
  35790. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  35791. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  35792. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  35793. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  35794. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  35795. digest, digestSz), 0);
  35796. #endif /* WOLFSSL_SHA384 */
  35797. #ifdef WOLFSSL_SHA512
  35798. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  35799. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  35800. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  35801. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  35802. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  35803. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  35804. digest, digestSz), 0);
  35805. #endif /* WOLFSSL_SHA512 */
  35806. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  35807. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  35808. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  35809. "\xE4\x98\xDD", digest, digestSz), 0);
  35810. #endif /* !NO_MD5 */
  35811. printf(resultFmt, passed);
  35812. #endif
  35813. return 0;
  35814. }
  35815. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35816. static void sslMsgCb(int w, int version, int type, const void* buf,
  35817. size_t sz, SSL* ssl, void* arg)
  35818. {
  35819. int i;
  35820. unsigned char* pt = (unsigned char*)buf;
  35821. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  35822. (int)sz, version, type);
  35823. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  35824. printf("\n");
  35825. (void)ssl;
  35826. (void)arg;
  35827. }
  35828. #endif /* OPENSSL_EXTRA */
  35829. static int test_wolfSSL_msg_callback(void)
  35830. {
  35831. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  35832. WOLFSSL* ssl;
  35833. WOLFSSL_CTX* ctx;
  35834. printf(testingFmt, "wolfSSL_msg_callback()");
  35835. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35836. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35837. SSL_FILETYPE_PEM));
  35838. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35839. SSL_FILETYPE_PEM));
  35840. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35841. SSL_SUCCESS);
  35842. AssertNotNull(ssl = SSL_new(ctx));
  35843. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  35844. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  35845. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  35846. SSL_free(ssl);
  35847. SSL_CTX_free(ctx);
  35848. printf(resultFmt, passed);
  35849. #endif
  35850. return 0;
  35851. }
  35852. static int test_wolfSSL_SHA(void)
  35853. {
  35854. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  35855. printf(testingFmt, "wolfSSL_SHA()");
  35856. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  35857. (!defined(HAVE_FIPS) || \
  35858. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  35859. {
  35860. const unsigned char in[] = "abc";
  35861. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  35862. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  35863. unsigned char out[WC_SHA_DIGEST_SIZE];
  35864. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35865. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  35866. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35867. /* SHA interface test */
  35868. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  35869. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  35870. AssertNotNull(SHA(in, 0, out));
  35871. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  35872. AssertNotNull(SHA(NULL, 0, out));
  35873. AssertNotNull(SHA(NULL, 0, NULL));
  35874. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  35875. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  35876. }
  35877. #endif
  35878. #if !defined(NO_SHA256)
  35879. {
  35880. const unsigned char in[] = "abc";
  35881. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  35882. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  35883. "\x15\xAD";
  35884. unsigned char out[WC_SHA256_DIGEST_SIZE];
  35885. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  35886. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35887. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  35888. #else
  35889. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  35890. #endif
  35891. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  35892. }
  35893. #endif
  35894. #if defined(WOLFSSL_SHA384)
  35895. {
  35896. const unsigned char in[] = "abc";
  35897. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  35898. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  35899. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  35900. "\xc8\x25\xa7";
  35901. unsigned char out[WC_SHA384_DIGEST_SIZE];
  35902. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  35903. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35904. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  35905. #else
  35906. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  35907. #endif
  35908. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  35909. }
  35910. #endif
  35911. #if defined(WOLFSSL_SHA512)
  35912. {
  35913. const unsigned char in[] = "abc";
  35914. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  35915. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  35916. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  35917. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  35918. "\xa5\x4c\xa4\x9f";
  35919. unsigned char out[WC_SHA512_DIGEST_SIZE];
  35920. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  35921. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  35922. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  35923. #else
  35924. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  35925. #endif
  35926. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  35927. }
  35928. #endif
  35929. printf(resultFmt, passed);
  35930. #endif
  35931. return 0;
  35932. }
  35933. static int test_wolfSSL_DH_1536_prime(void)
  35934. {
  35935. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  35936. BIGNUM* bn;
  35937. unsigned char bits[200];
  35938. int sz = 192; /* known binary size */
  35939. const byte expected[] = {
  35940. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35941. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  35942. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  35943. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  35944. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  35945. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  35946. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  35947. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  35948. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  35949. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  35950. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  35951. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  35952. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  35953. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  35954. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  35955. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  35956. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  35957. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  35958. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  35959. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  35960. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  35961. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  35962. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  35963. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  35964. };
  35965. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  35966. bn = get_rfc3526_prime_1536(NULL);
  35967. AssertNotNull(bn);
  35968. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  35969. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  35970. BN_free(bn);
  35971. printf(resultFmt, passed);
  35972. #endif
  35973. return 0;
  35974. }
  35975. static int test_wolfSSL_DH_get_2048_256(void)
  35976. {
  35977. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  35978. WOLFSSL_DH* dh;
  35979. const WOLFSSL_BIGNUM* pBn;
  35980. const WOLFSSL_BIGNUM* gBn;
  35981. const WOLFSSL_BIGNUM* qBn;
  35982. const byte pExpected[] = {
  35983. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  35984. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  35985. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  35986. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  35987. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  35988. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  35989. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  35990. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  35991. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  35992. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  35993. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  35994. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  35995. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  35996. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  35997. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  35998. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  35999. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  36000. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  36001. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  36002. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  36003. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  36004. 0x1E, 0x1A, 0x15, 0x97
  36005. };
  36006. const byte gExpected[] = {
  36007. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  36008. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  36009. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  36010. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  36011. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  36012. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  36013. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  36014. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  36015. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  36016. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  36017. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  36018. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  36019. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  36020. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  36021. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  36022. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  36023. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  36024. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  36025. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  36026. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  36027. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  36028. 0x6C, 0xC4, 0x16, 0x59
  36029. };
  36030. const byte qExpected[] = {
  36031. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  36032. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  36033. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  36034. };
  36035. int pSz;
  36036. int qSz;
  36037. int gSz;
  36038. byte* pReturned;
  36039. byte* qReturned;
  36040. byte* gReturned;
  36041. printf(testingFmt, "wolfSSL_DH_get_2048_256()");
  36042. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  36043. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  36044. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  36045. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  36046. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  36047. AssertIntEQ(pSz, sizeof(pExpected));
  36048. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  36049. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  36050. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  36051. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  36052. AssertIntEQ(qSz, sizeof(qExpected));
  36053. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  36054. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  36055. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  36056. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  36057. AssertIntEQ(gSz, sizeof(gExpected));
  36058. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  36059. wolfSSL_DH_free(dh);
  36060. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36061. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36062. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36063. printf(resultFmt, passed);
  36064. #endif
  36065. return 0;
  36066. }
  36067. static int test_wolfSSL_PEM_write_DHparams(void)
  36068. {
  36069. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  36070. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  36071. DH* dh;
  36072. BIO* bio;
  36073. XFILE fp;
  36074. byte pem[2048];
  36075. int pemSz;
  36076. const char expected[] =
  36077. "-----BEGIN DH PARAMETERS-----\n\
  36078. MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n\
  36079. v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n\
  36080. nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n\
  36081. joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n\
  36082. wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n\
  36083. tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n\
  36084. -----END DH PARAMETERS-----\n";
  36085. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  36086. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  36087. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  36088. XFCLOSE(fp);
  36089. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36090. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  36091. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  36092. BIO_free(bio);
  36093. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  36094. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  36095. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  36096. XFCLOSE(fp);
  36097. DH_free(dh);
  36098. /* check results */
  36099. XMEMSET(pem, 0, sizeof(pem));
  36100. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  36101. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  36102. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  36103. XFCLOSE(fp);
  36104. printf(resultFmt, passed);
  36105. #endif
  36106. return 0;
  36107. }
  36108. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  36109. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36110. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36111. static void binary_dump(void *ptr, int size)
  36112. {
  36113. #ifdef WOLFSSL_EVP_PRINT
  36114. int i = 0;
  36115. unsigned char *p = (unsigned char *) ptr;
  36116. printf("{");
  36117. while((p != NULL) && (i < size)) {
  36118. if((i % 8) == 0) {
  36119. printf("\n");
  36120. printf(" ");
  36121. }
  36122. printf("0x%02x, ", p[i]);
  36123. i++;
  36124. }
  36125. printf("\n};\n");
  36126. #else
  36127. (void) ptr;
  36128. (void) size;
  36129. #endif
  36130. }
  36131. static int last_val = 0x0f;
  36132. static int check_result(unsigned char *data, int len)
  36133. {
  36134. int i;
  36135. for( ; len; ) {
  36136. last_val = (last_val + 1) % 16;
  36137. for(i = 0; i < 16; len--, i++, data++)
  36138. if(*data != last_val) {
  36139. return -1;
  36140. }
  36141. }
  36142. return 0;
  36143. }
  36144. static int r_offset;
  36145. static int w_offset;
  36146. static void init_offset(void)
  36147. {
  36148. r_offset = 0;
  36149. w_offset = 0;
  36150. }
  36151. static void get_record(unsigned char *data, unsigned char *buf, int len)
  36152. {
  36153. XMEMCPY(buf, data+r_offset, len);
  36154. r_offset += len;
  36155. }
  36156. static void set_record(unsigned char *data, unsigned char *buf, int len)
  36157. {
  36158. XMEMCPY(data+w_offset, buf, len);
  36159. w_offset += len;
  36160. }
  36161. static void set_plain(unsigned char *plain, int rec)
  36162. {
  36163. int i, j;
  36164. unsigned char *p = plain;
  36165. #define BLOCKSZ 16
  36166. for(i=0; i<(rec/BLOCKSZ); i++){
  36167. for(j=0; j<BLOCKSZ; j++)
  36168. *p++ = (i % 16);
  36169. }
  36170. }
  36171. #endif
  36172. static int test_wolfSSL_EVP_Cipher_extra(void)
  36173. {
  36174. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36175. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36176. /* aes128-cbc, keylen=16, ivlen=16 */
  36177. byte aes128_cbc_key[] = {
  36178. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36179. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36180. };
  36181. byte aes128_cbc_iv[] = {
  36182. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  36183. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  36184. };
  36185. /* teset data size table */
  36186. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  36187. int test_drive2[] = {8, 3, 8, 512, 0};
  36188. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  36189. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  36190. int test_drive_len[100];
  36191. int ret = 0;
  36192. EVP_CIPHER_CTX *evp = NULL;
  36193. int ilen = 0;
  36194. int klen = 0;
  36195. int i, j;
  36196. const EVP_CIPHER *type;
  36197. byte *iv;
  36198. byte *key;
  36199. int ivlen;
  36200. int keylen;
  36201. #define RECORDS 16
  36202. #define BUFFSZ 512
  36203. byte plain [BUFFSZ * RECORDS];
  36204. byte cipher[BUFFSZ * RECORDS];
  36205. byte inb[BUFFSZ];
  36206. byte outb[BUFFSZ+16];
  36207. int outl, inl;
  36208. iv = aes128_cbc_iv;
  36209. ivlen = sizeof(aes128_cbc_iv);
  36210. key = aes128_cbc_key;
  36211. keylen = sizeof(aes128_cbc_key);
  36212. type = EVP_aes_128_cbc();
  36213. set_plain(plain, BUFFSZ * RECORDS);
  36214. SSL_library_init();
  36215. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  36216. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  36217. klen = EVP_CIPHER_CTX_key_length(evp);
  36218. if (klen > 0 && keylen != klen) {
  36219. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  36220. }
  36221. ilen = EVP_CIPHER_CTX_iv_length(evp);
  36222. if (ilen > 0 && ivlen != ilen) {
  36223. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  36224. }
  36225. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  36226. for (j = 0; j<RECORDS; j++)
  36227. {
  36228. inl = BUFFSZ;
  36229. get_record(plain, inb, inl);
  36230. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  36231. set_record(cipher, outb, outl);
  36232. }
  36233. for (i = 0; test_drive[i]; i++) {
  36234. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  36235. init_offset();
  36236. test_drive_len[i] = 0;
  36237. for (j = 0; test_drive[i][j]; j++)
  36238. {
  36239. inl = test_drive[i][j];
  36240. test_drive_len[i] += inl;
  36241. get_record(plain, inb, inl);
  36242. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  36243. /* output to cipher buffer, so that following Dec test can detect
  36244. if any error */
  36245. set_record(cipher, outb, outl);
  36246. }
  36247. EVP_CipherFinal(evp, outb, &outl);
  36248. if(outl > 0)
  36249. set_record(cipher, outb, outl);
  36250. }
  36251. for (i = 0; test_drive[i]; i++) {
  36252. last_val = 0x0f;
  36253. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  36254. init_offset();
  36255. for (j = 0; test_drive[i][j]; j++){
  36256. inl = test_drive[i][j];
  36257. get_record(cipher, inb, inl);
  36258. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  36259. binary_dump(outb, outl);
  36260. AssertIntEQ((ret = check_result(outb, outl)), 0);
  36261. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  36262. }
  36263. ret = EVP_CipherFinal(evp, outb, &outl);
  36264. binary_dump(outb, outl);
  36265. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  36266. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  36267. AssertTrue(ret);
  36268. }
  36269. EVP_CIPHER_CTX_free(evp);
  36270. #endif /* test_EVP_Cipher */
  36271. return 0;
  36272. }
  36273. static int test_wolfSSL_PEM_read_DHparams(void)
  36274. {
  36275. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  36276. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  36277. DH* dh;
  36278. XFILE fp;
  36279. unsigned char derOut[300];
  36280. unsigned char* derOutBuf = derOut;
  36281. int derOutSz = 0;
  36282. unsigned char derExpected[300];
  36283. int derExpectedSz = 0;
  36284. printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
  36285. XMEMSET(derOut, 0, sizeof(derOut));
  36286. XMEMSET(derExpected, 0, sizeof(derExpected));
  36287. /* open DH param file, read into DH struct */
  36288. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  36289. /* bad args */
  36290. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  36291. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  36292. /* good args */
  36293. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  36294. XFCLOSE(fp);
  36295. /* read in certs/dh2048.der for comparison against exported params */
  36296. fp = XFOPEN("./certs/dh2048.der", "rb");
  36297. AssertTrue(fp != XBADFILE);
  36298. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  36299. XFCLOSE(fp);
  36300. /* export DH back to DER and compare */
  36301. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  36302. AssertIntEQ(derOutSz, derExpectedSz);
  36303. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  36304. /* Test parsing with X9.42 header */
  36305. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  36306. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  36307. XFCLOSE(fp);
  36308. DH_free(dh);
  36309. printf(resultFmt, passed);
  36310. #endif
  36311. return 0;
  36312. }
  36313. static int test_wolfSSL_AES_ecb_encrypt(void)
  36314. {
  36315. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  36316. AES_KEY aes;
  36317. const byte msg[] =
  36318. {
  36319. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  36320. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  36321. };
  36322. const byte verify[] =
  36323. {
  36324. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  36325. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  36326. };
  36327. const byte key[] =
  36328. {
  36329. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  36330. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  36331. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  36332. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  36333. };
  36334. byte out[AES_BLOCK_SIZE];
  36335. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  36336. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  36337. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36338. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  36339. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  36340. #ifdef HAVE_AES_DECRYPT
  36341. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  36342. XMEMSET(out, 0, AES_BLOCK_SIZE);
  36343. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  36344. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  36345. #endif
  36346. /* test bad arguments */
  36347. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  36348. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  36349. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  36350. printf(resultFmt, passed);
  36351. #endif
  36352. return 0;
  36353. }
  36354. static int test_wolfSSL_MD5(void)
  36355. {
  36356. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36357. byte input1[] = "";
  36358. byte input2[] = "message digest";
  36359. byte hash[WC_MD5_DIGEST_SIZE];
  36360. unsigned char output1[] =
  36361. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  36362. unsigned char output2[] =
  36363. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  36364. WOLFSSL_MD5_CTX md5;
  36365. printf(testingFmt, "wolfSSL_MD5()");
  36366. XMEMSET(&md5, 0, sizeof(md5));
  36367. /* Test cases for illegal parameters */
  36368. AssertIntEQ(MD5_Init(NULL), 0);
  36369. AssertIntEQ(MD5_Init(&md5), 1);
  36370. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  36371. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  36372. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  36373. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  36374. AssertIntEQ(MD5_Final(hash, NULL), 0);
  36375. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  36376. /* Init MD5 CTX */
  36377. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36378. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  36379. XSTRLEN((const char*)&input1)), 1);
  36380. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36381. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36382. /* Init MD5 CTX */
  36383. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  36384. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  36385. (int)XSTRLEN((const char*)input2)), 1);
  36386. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  36387. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36388. #if !defined(NO_OLD_NAMES) && \
  36389. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  36390. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  36391. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  36392. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  36393. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  36394. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  36395. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  36396. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  36397. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  36398. {
  36399. byte data[] = "Data to be hashed.";
  36400. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  36401. AssertNotNull(MD5(data, sizeof(data), NULL));
  36402. AssertNotNull(MD5(data, sizeof(data), hash));
  36403. AssertNotNull(MD5(NULL, 0, hash));
  36404. AssertNull(MD5(NULL, sizeof(data), hash));
  36405. }
  36406. #endif
  36407. printf(resultFmt, passed);
  36408. #endif
  36409. return 0;
  36410. }
  36411. static int test_wolfSSL_MD5_Transform(void)
  36412. {
  36413. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  36414. byte input1[] = "";
  36415. byte input2[] = "abc";
  36416. byte local[WC_MD5_BLOCK_SIZE];
  36417. word32 sLen = 0;
  36418. #ifdef BIG_ENDIAN_ORDER
  36419. unsigned char output1[] =
  36420. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  36421. unsigned char output2[] =
  36422. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  36423. #else
  36424. unsigned char output1[] =
  36425. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  36426. unsigned char output2[] =
  36427. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  36428. #endif
  36429. union {
  36430. wc_Md5 native;
  36431. MD5_CTX compat;
  36432. } md5;
  36433. printf(testingFmt, "wolfSSL_MD5_Transform()");
  36434. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  36435. XMEMSET(&local, 0, sizeof(local));
  36436. /* sanity check */
  36437. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  36438. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  36439. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  36440. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  36441. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36442. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  36443. /* Init MD5 CTX */
  36444. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  36445. /* Do Transform*/
  36446. sLen = (word32)XSTRLEN((char*)input1);
  36447. XMEMCPY(local, input1, sLen);
  36448. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36449. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  36450. WC_MD5_DIGEST_SIZE), 0);
  36451. /* Init MD5 CTX */
  36452. AssertIntEQ(MD5_Init(&md5.compat), 1);
  36453. sLen = (word32)XSTRLEN((char*)input2);
  36454. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  36455. XMEMCPY(local, input2, sLen);
  36456. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  36457. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  36458. WC_MD5_DIGEST_SIZE), 0);
  36459. printf(resultFmt, passed);
  36460. #endif
  36461. return 0;
  36462. }
  36463. static int test_wolfSSL_SHA224(void)
  36464. {
  36465. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  36466. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36467. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36468. unsigned char input[] =
  36469. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36470. unsigned char output[] =
  36471. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  36472. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  36473. size_t inLen;
  36474. byte hash[WC_SHA224_DIGEST_SIZE];
  36475. printf(testingFmt, "wolfSSL_SHA224()");
  36476. inLen = XSTRLEN((char*)input);
  36477. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  36478. AssertNull(SHA224(NULL, inLen, hash));
  36479. AssertNotNull(SHA224(input, 0, hash));
  36480. AssertNotNull(SHA224(input, inLen, NULL));
  36481. AssertNotNull(SHA224(NULL, 0, hash));
  36482. AssertNotNull(SHA224(NULL, 0, NULL));
  36483. AssertNotNull(SHA224(input, inLen, hash));
  36484. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  36485. printf(resultFmt, passed);
  36486. #endif
  36487. return 0;
  36488. }
  36489. static int test_wolfSSL_SHA_Transform(void)
  36490. {
  36491. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  36492. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36493. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36494. byte input1[] = "";
  36495. byte input2[] = "abc";
  36496. byte local[WC_SHA_BLOCK_SIZE];
  36497. word32 sLen = 0;
  36498. #ifdef BIG_ENDIAN_ORDER
  36499. unsigned char output1[] =
  36500. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  36501. "\x09\xf7\x3a\x93";
  36502. unsigned char output2[] =
  36503. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  36504. "\x7c\x6e\x08\xb8";
  36505. #else
  36506. unsigned char output1[] =
  36507. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  36508. "\x93\x3a\xf7\x09";
  36509. unsigned char output2[] =
  36510. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  36511. "\xb8\x08\x6e\x7c";
  36512. #endif
  36513. union {
  36514. wc_Sha native;
  36515. SHA_CTX compat;
  36516. } sha;
  36517. union {
  36518. wc_Sha native;
  36519. SHA_CTX compat;
  36520. } sha1;
  36521. printf(testingFmt, "wolfSSL_SHA_Transform()");
  36522. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  36523. XMEMSET(&local, 0, sizeof(local));
  36524. /* sanity check */
  36525. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  36526. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  36527. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  36528. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  36529. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  36530. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  36531. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  36532. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36533. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  36534. /* Init SHA CTX */
  36535. AssertIntEQ(SHA_Init(&sha.compat), 1);
  36536. /* Do Transform*/
  36537. sLen = (word32)XSTRLEN((char*)input1);
  36538. XMEMCPY(local, input1, sLen);
  36539. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36540. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  36541. WC_SHA_DIGEST_SIZE), 0);
  36542. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36543. /* Init SHA CTX */
  36544. AssertIntEQ(SHA_Init(&sha.compat), 1);
  36545. sLen = (word32)XSTRLEN((char*)input2);
  36546. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36547. XMEMCPY(local, input2, sLen);
  36548. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  36549. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  36550. WC_SHA_DIGEST_SIZE), 0);
  36551. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  36552. /* SHA1 */
  36553. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36554. /* Init SHA CTX */
  36555. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  36556. /* Do Transform*/
  36557. sLen = (word32)XSTRLEN((char*)input1);
  36558. XMEMCPY(local, input1, sLen);
  36559. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36560. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  36561. WC_SHA_DIGEST_SIZE), 0);
  36562. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36563. /* Init SHA CTX */
  36564. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  36565. sLen = (word32)XSTRLEN((char*)input2);
  36566. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  36567. XMEMCPY(local, input2, sLen);
  36568. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  36569. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  36570. WC_SHA_DIGEST_SIZE), 0);
  36571. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  36572. printf(resultFmt, passed);
  36573. #endif
  36574. #endif
  36575. return 0;
  36576. }
  36577. static int test_wolfSSL_SHA256_Transform(void)
  36578. {
  36579. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  36580. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36581. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  36582. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  36583. byte input1[] = "";
  36584. byte input2[] = "abc";
  36585. byte local[WC_SHA256_BLOCK_SIZE];
  36586. word32 sLen = 0;
  36587. #ifdef BIG_ENDIAN_ORDER
  36588. unsigned char output1[] =
  36589. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  36590. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  36591. unsigned char output2[] =
  36592. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  36593. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  36594. #else
  36595. unsigned char output1[] =
  36596. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  36597. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  36598. unsigned char output2[] =
  36599. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  36600. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  36601. #endif
  36602. union {
  36603. wc_Sha256 native;
  36604. SHA256_CTX compat;
  36605. } sha256;
  36606. printf(testingFmt, "wolfSSL_SHA256_Transform()");
  36607. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  36608. XMEMSET(&local, 0, sizeof(local));
  36609. /* sanity check */
  36610. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  36611. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  36612. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  36613. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  36614. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36615. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  36616. /* Init SHA256 CTX */
  36617. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  36618. /* Do Transform*/
  36619. sLen = (word32)XSTRLEN((char*)input1);
  36620. XMEMCPY(local, input1, sLen);
  36621. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36622. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  36623. WC_SHA256_DIGEST_SIZE), 0);
  36624. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36625. /* Init SHA256 CTX */
  36626. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  36627. sLen = (word32)XSTRLEN((char*)input2);
  36628. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  36629. XMEMCPY(local, input2, sLen);
  36630. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  36631. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  36632. WC_SHA256_DIGEST_SIZE), 0);
  36633. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  36634. printf(resultFmt, passed);
  36635. #endif
  36636. #endif
  36637. return 0;
  36638. }
  36639. static int test_wolfSSL_SHA256(void)
  36640. {
  36641. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  36642. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  36643. unsigned char input[] =
  36644. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  36645. unsigned char output[] =
  36646. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  36647. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  36648. "\x06\xC1";
  36649. size_t inLen;
  36650. byte hash[WC_SHA256_DIGEST_SIZE];
  36651. printf(testingFmt, "wolfSSL_SHA256()");
  36652. inLen = XSTRLEN((char*)input);
  36653. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  36654. AssertNotNull(SHA256(input, inLen, hash));
  36655. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  36656. printf(resultFmt, passed);
  36657. #endif
  36658. return 0;
  36659. }
  36660. static int test_wolfSSL_SHA512_Transform(void)
  36661. {
  36662. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  36663. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  36664. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  36665. byte input1[] = "";
  36666. byte input2[] = "abc";
  36667. byte local[WC_SHA512_BLOCK_SIZE];
  36668. word32 sLen = 0;
  36669. #ifdef BIG_ENDIAN_ORDER
  36670. unsigned char output1[] =
  36671. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  36672. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  36673. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  36674. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  36675. unsigned char output2[] =
  36676. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  36677. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  36678. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  36679. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  36680. #else
  36681. unsigned char output1[] =
  36682. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  36683. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  36684. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  36685. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  36686. unsigned char output2[] =
  36687. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  36688. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  36689. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  36690. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  36691. #endif
  36692. union {
  36693. wc_Sha512 native;
  36694. SHA512_CTX compat;
  36695. } sha512;
  36696. printf(testingFmt, "wolfSSL_SHA512_Transform()");
  36697. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  36698. XMEMSET(&local, 0, sizeof(local));
  36699. /* sanity check */
  36700. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  36701. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  36702. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  36703. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  36704. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  36705. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  36706. /* Init SHA512 CTX */
  36707. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  36708. /* Do Transform*/
  36709. sLen = (word32)XSTRLEN((char*)input1);
  36710. XMEMCPY(local, input1, sLen);
  36711. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36712. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  36713. WC_SHA512_DIGEST_SIZE), 0);
  36714. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36715. /* Init SHA512 CTX */
  36716. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  36717. sLen = (word32)XSTRLEN((char*)input2);
  36718. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  36719. XMEMCPY(local, input2, sLen);
  36720. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  36721. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  36722. WC_SHA512_DIGEST_SIZE), 0);
  36723. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  36724. (void)input1;
  36725. printf(resultFmt, passed);
  36726. #endif
  36727. #endif
  36728. return 0;
  36729. }
  36730. static int test_wolfSSL_X509_get_serialNumber(void)
  36731. {
  36732. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36733. ASN1_INTEGER* a;
  36734. BIGNUM* bn;
  36735. X509* x509;
  36736. char *serialHex;
  36737. byte serial[3];
  36738. int serialSz;
  36739. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  36740. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36741. SSL_FILETYPE_PEM));
  36742. AssertNotNull(a = X509_get_serialNumber(x509));
  36743. /* check on value of ASN1 Integer */
  36744. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  36745. /* test setting serial number and then retrieving it */
  36746. AssertNotNull(a = ASN1_INTEGER_new());
  36747. ASN1_INTEGER_set(a, 3);
  36748. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  36749. serialSz = sizeof(serial);
  36750. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  36751. WOLFSSL_SUCCESS);
  36752. AssertIntEQ(serialSz, 1);
  36753. AssertIntEQ(serial[0], 3);
  36754. ASN1_INTEGER_free(a);
  36755. /* test setting serial number with 0's in it */
  36756. serial[0] = 0x01;
  36757. serial[1] = 0x00;
  36758. serial[2] = 0x02;
  36759. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  36760. a->data[0] = ASN_INTEGER;
  36761. a->data[1] = sizeof(serial);
  36762. XMEMCPY(&a->data[2], serial, sizeof(serial));
  36763. a->length = sizeof(serial) + 2;
  36764. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  36765. XMEMSET(serial, 0, sizeof(serial));
  36766. serialSz = sizeof(serial);
  36767. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  36768. WOLFSSL_SUCCESS);
  36769. AssertIntEQ(serialSz, 3);
  36770. AssertIntEQ(serial[0], 0x01);
  36771. AssertIntEQ(serial[1], 0x00);
  36772. AssertIntEQ(serial[2], 0x02);
  36773. ASN1_INTEGER_free(a);
  36774. X509_free(x509); /* free's a */
  36775. AssertNotNull(serialHex = BN_bn2hex(bn));
  36776. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  36777. AssertStrEQ(serialHex, "01");
  36778. #else
  36779. AssertStrEQ(serialHex, "1");
  36780. #endif
  36781. OPENSSL_free(serialHex);
  36782. AssertIntEQ(BN_get_word(bn), 1);
  36783. BN_free(bn);
  36784. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  36785. a = ASN1_INTEGER_new();
  36786. if (a) {
  36787. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  36788. DYNAMIC_TYPE_OPENSSL));
  36789. a->isDynamic = 1;
  36790. ASN1_INTEGER_free(a);
  36791. }
  36792. printf(resultFmt, passed);
  36793. #endif
  36794. return 0;
  36795. }
  36796. static int test_wolfSSL_OpenSSL_add_all_algorithms(void){
  36797. #if defined(OPENSSL_EXTRA)
  36798. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  36799. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  36800. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  36801. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  36802. printf(resultFmt, passed);
  36803. #endif
  36804. return 0;
  36805. }
  36806. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  36807. {
  36808. #if defined(OPENSSL_EXTRA)
  36809. #define MAX_HEXSTR_BUFSZ 9
  36810. #define NUM_CASES 5
  36811. struct Output {
  36812. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  36813. long ret;
  36814. };
  36815. int i;
  36816. int j;
  36817. const char* inputs[NUM_CASES] = {
  36818. "aabcd1357e",
  36819. "01:12:23:34:a5:b6:c7:d8:e9",
  36820. ":01:02",
  36821. "012",
  36822. ":ab:ac:d"
  36823. };
  36824. struct Output expectedOutputs[NUM_CASES] = {
  36825. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  36826. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  36827. {{0x01, 0x02}, 2},
  36828. {{0x00}, 0},
  36829. {{0x00}, 0}
  36830. };
  36831. long len = 0;
  36832. unsigned char* returnedBuf = NULL;
  36833. printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
  36834. for (i = 0; i < NUM_CASES; ++i) {
  36835. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  36836. if (returnedBuf == NULL) {
  36837. AssertIntEQ(expectedOutputs[i].ret, 0);
  36838. continue;
  36839. }
  36840. AssertIntEQ(expectedOutputs[i].ret, len);
  36841. for (j = 0; j < len; ++j) {
  36842. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  36843. }
  36844. OPENSSL_free(returnedBuf);
  36845. }
  36846. printf(resultFmt, passed);
  36847. #endif
  36848. return 0;
  36849. }
  36850. static int test_wolfSSL_ASN1_STRING_print_ex(void){
  36851. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  36852. #ifndef NO_BIO
  36853. ASN1_STRING* asn_str;
  36854. const char data[] = "Hello wolfSSL!";
  36855. ASN1_STRING* esc_str;
  36856. const char esc_data[] = "a+;<>";
  36857. BIO *bio;
  36858. unsigned long flags;
  36859. int p_len;
  36860. unsigned char rbuf[255];
  36861. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  36862. /* setup */
  36863. XMEMSET(rbuf, 0, 255);
  36864. bio = BIO_new(BIO_s_mem());
  36865. BIO_set_write_buf_size(bio,255);
  36866. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  36867. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  36868. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  36869. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  36870. /* no flags */
  36871. XMEMSET(rbuf, 0, 255);
  36872. flags = 0;
  36873. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36874. AssertIntEQ(p_len, 15);
  36875. BIO_read(bio, (void*)rbuf, 15);
  36876. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  36877. /* RFC2253 Escape */
  36878. XMEMSET(rbuf, 0, 255);
  36879. flags = ASN1_STRFLGS_ESC_2253;
  36880. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  36881. AssertIntEQ(p_len, 9);
  36882. BIO_read(bio, (void*)rbuf, 9);
  36883. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  36884. /* Show type */
  36885. XMEMSET(rbuf, 0, 255);
  36886. flags = ASN1_STRFLGS_SHOW_TYPE;
  36887. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36888. AssertIntEQ(p_len, 28);
  36889. BIO_read(bio, (void*)rbuf, 28);
  36890. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  36891. /* Dump All */
  36892. XMEMSET(rbuf, 0, 255);
  36893. flags = ASN1_STRFLGS_DUMP_ALL;
  36894. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36895. AssertIntEQ(p_len, 31);
  36896. BIO_read(bio, (void*)rbuf, 31);
  36897. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  36898. /* Dump Der */
  36899. XMEMSET(rbuf, 0, 255);
  36900. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  36901. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36902. AssertIntEQ(p_len, 35);
  36903. BIO_read(bio, (void*)rbuf, 35);
  36904. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  36905. /* Dump All + Show type */
  36906. XMEMSET(rbuf, 0, 255);
  36907. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  36908. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  36909. AssertIntEQ(p_len, 44);
  36910. BIO_read(bio, (void*)rbuf, 44);
  36911. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  36912. BIO_free(bio);
  36913. ASN1_STRING_free(asn_str);
  36914. ASN1_STRING_free(esc_str);
  36915. printf(resultFmt, passed);
  36916. #endif /* !NO_BIO */
  36917. #endif
  36918. return 0;
  36919. }
  36920. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  36921. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  36922. WOLFSSL_ASN1_TIME *t;
  36923. WOLFSSL_ASN1_TIME *out;
  36924. WOLFSSL_ASN1_TIME *gtime;
  36925. int tlen = 0;
  36926. unsigned char *data;
  36927. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  36928. /* UTC Time test */
  36929. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  36930. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  36931. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  36932. t->type = ASN_UTC_TIME;
  36933. t->length = ASN_UTC_TIME_SIZE;
  36934. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  36935. tlen = wolfSSL_ASN1_TIME_get_length(t);
  36936. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  36937. data = wolfSSL_ASN1_TIME_get_data(t);
  36938. AssertStrEQ((char*)data, "050727123456Z");
  36939. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  36940. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36941. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36942. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36943. /* Generalized Time test */
  36944. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  36945. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  36946. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  36947. t->type = ASN_GENERALIZED_TIME;
  36948. t->length = ASN_GENERALIZED_TIME_SIZE;
  36949. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  36950. tlen = wolfSSL_ASN1_TIME_get_length(t);
  36951. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  36952. data = wolfSSL_ASN1_TIME_get_data(t);
  36953. AssertStrEQ((char*)data, "20050727123456Z");
  36954. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  36955. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36956. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36957. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36958. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36959. /* Null parameter test */
  36960. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  36961. gtime = NULL;
  36962. out = NULL;
  36963. t->type = ASN_UTC_TIME;
  36964. t->length = ASN_UTC_TIME_SIZE;
  36965. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  36966. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  36967. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  36968. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  36969. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  36970. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36971. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36972. printf(resultFmt, passed);
  36973. #endif
  36974. return 0;
  36975. }
  36976. static int test_wolfSSL_X509_CA_num(void){
  36977. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36978. defined(HAVE_ECC) && !defined(NO_RSA)
  36979. WOLFSSL_X509_STORE *store;
  36980. WOLFSSL_X509 *x509_1, *x509_2;
  36981. int ca_num = 0;
  36982. printf(testingFmt, "wolfSSL_X509_CA_num()");
  36983. store = wolfSSL_X509_STORE_new();
  36984. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  36985. wolfSSL_X509_STORE_add_cert(store, x509_1);
  36986. ca_num = wolfSSL_X509_CA_num(store);
  36987. AssertIntEQ(ca_num, 1);
  36988. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  36989. wolfSSL_X509_STORE_add_cert(store, x509_2);
  36990. ca_num = wolfSSL_X509_CA_num(store);
  36991. AssertIntEQ(ca_num, 2);
  36992. wolfSSL_X509_free(x509_1);
  36993. wolfSSL_X509_free(x509_2);
  36994. wolfSSL_X509_STORE_free(store);
  36995. printf(resultFmt, passed);
  36996. #endif
  36997. return 0;
  36998. }
  36999. static int test_wolfSSL_X509_check_ca(void){
  37000. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37001. WOLFSSL_X509 *x509;
  37002. printf(testingFmt, "wolfSSL_X509_check_ca()");
  37003. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37004. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  37005. wolfSSL_X509_free(x509);
  37006. printf(resultFmt, passed);
  37007. #endif
  37008. return 0;
  37009. }
  37010. static int test_wolfSSL_X509_check_ip_asc(void){
  37011. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37012. WOLFSSL_X509 *x509;
  37013. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  37014. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  37015. #if 0
  37016. /* TODO: add cert gen for testing positive case */
  37017. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  37018. #endif
  37019. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  37020. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  37021. wolfSSL_X509_free(x509);
  37022. printf(resultFmt, passed);
  37023. #endif
  37024. return 0;
  37025. }
  37026. static int test_wolfSSL_make_cert(void)
  37027. {
  37028. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  37029. int ret;
  37030. Cert cert;
  37031. CertName name;
  37032. RsaKey key;
  37033. WC_RNG rng;
  37034. byte der[FOURK_BUF];
  37035. word32 idx;
  37036. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  37037. #ifdef OPENSSL_EXTRA
  37038. const unsigned char* pt;
  37039. int certSz;
  37040. X509* x509;
  37041. X509_NAME* x509name;
  37042. X509_NAME_ENTRY* entry;
  37043. ASN1_STRING* entryValue;
  37044. #endif
  37045. printf(testingFmt, "wolfSSL Make Certs");
  37046. XMEMSET(&name, 0, sizeof(CertName));
  37047. /* set up cert name */
  37048. XMEMCPY(name.country, "US", sizeof("US"));
  37049. name.countryEnc = CTC_PRINTABLE;
  37050. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  37051. name.stateEnc = CTC_UTF8;
  37052. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  37053. name.localityEnc = CTC_UTF8;
  37054. XMEMCPY(name.sur, "Test", sizeof("Test"));
  37055. name.surEnc = CTC_UTF8;
  37056. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  37057. name.orgEnc = CTC_UTF8;
  37058. XMEMCPY(name.unit, "Development", sizeof("Development"));
  37059. name.unitEnc = CTC_UTF8;
  37060. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  37061. name.commonNameEnc = CTC_UTF8;
  37062. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  37063. name.serialDevEnc = CTC_PRINTABLE;
  37064. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  37065. name.userIdEnc = CTC_PRINTABLE;
  37066. #ifdef WOLFSSL_MULTI_ATTRIB
  37067. #if CTC_MAX_ATTRIB > 2
  37068. {
  37069. NameAttrib* n;
  37070. n = &name.name[0];
  37071. n->id = ASN_DOMAIN_COMPONENT;
  37072. n->type = CTC_UTF8;
  37073. n->sz = sizeof("com");
  37074. XMEMCPY(n->value, "com", sizeof("com"));
  37075. n = &name.name[1];
  37076. n->id = ASN_DOMAIN_COMPONENT;
  37077. n->type = CTC_UTF8;
  37078. n->sz = sizeof("wolfssl");
  37079. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  37080. }
  37081. #endif
  37082. #endif /* WOLFSSL_MULTI_ATTRIB */
  37083. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  37084. #ifndef HAVE_FIPS
  37085. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  37086. #else
  37087. AssertIntEQ(wc_InitRng(&rng), 0);
  37088. #endif
  37089. /* load test RSA key */
  37090. idx = 0;
  37091. #if defined(USE_CERT_BUFFERS_1024)
  37092. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  37093. sizeof_server_key_der_1024), 0);
  37094. #elif defined(USE_CERT_BUFFERS_2048)
  37095. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  37096. sizeof_server_key_der_2048), 0);
  37097. #else
  37098. /* error case, no RSA key loaded, happens later */
  37099. (void)idx;
  37100. #endif
  37101. XMEMSET(&cert, 0 , sizeof(Cert));
  37102. AssertIntEQ(wc_InitCert(&cert), 0);
  37103. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  37104. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  37105. cert.serialSz = (int)sizeof(mySerial);
  37106. cert.isCA = 1;
  37107. #ifndef NO_SHA256
  37108. cert.sigType = CTC_SHA256wRSA;
  37109. #else
  37110. cert.sigType = CTC_SHAwRSA;
  37111. #endif
  37112. /* add SKID from the Public Key */
  37113. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37114. /* add AKID from the Public Key */
  37115. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37116. ret = 0;
  37117. do {
  37118. #if defined(WOLFSSL_ASYNC_CRYPT)
  37119. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  37120. #endif
  37121. if (ret >= 0) {
  37122. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  37123. }
  37124. } while (ret == WC_PENDING_E);
  37125. AssertIntGT(ret, 0);
  37126. #ifdef OPENSSL_EXTRA
  37127. /* der holds a certificate with DC's now check X509 parsing of it */
  37128. certSz = ret;
  37129. pt = der;
  37130. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  37131. AssertNotNull(x509name = X509_get_subject_name(x509));
  37132. #ifdef WOLFSSL_MULTI_ATTRIB
  37133. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37134. -1)), 5);
  37135. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37136. idx)), 6);
  37137. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37138. idx)), -1);
  37139. #endif /* WOLFSSL_MULTI_ATTRIB */
  37140. /* compare DN at index 0 */
  37141. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  37142. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37143. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  37144. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  37145. #ifdef WOLFSSL_MULTI_ATTRIB
  37146. /* get first and second DC and compare result */
  37147. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37148. -1)), 5);
  37149. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37150. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37151. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  37152. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37153. idx)), 6);
  37154. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37155. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37156. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  37157. #endif /* WOLFSSL_MULTI_ATTRIB */
  37158. /* try invalid index locations for regression test and sanity check */
  37159. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  37160. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  37161. X509_free(x509);
  37162. #endif /* OPENSSL_EXTRA */
  37163. wc_FreeRsaKey(&key);
  37164. wc_FreeRng(&rng);
  37165. printf(resultFmt, passed);
  37166. #endif
  37167. return 0;
  37168. }
  37169. static int test_wolfSSL_X509_get_version(void){
  37170. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37171. WOLFSSL_X509 *x509;
  37172. printf(testingFmt, "wolfSSL_X509_get_version()");
  37173. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37174. AssertNotNull(x509);
  37175. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  37176. wolfSSL_X509_free(x509);
  37177. printf(resultFmt, passed);
  37178. #endif
  37179. return 0;
  37180. }
  37181. static int test_wolfSSL_DES_ncbc(void){
  37182. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37183. const_DES_cblock myDes;
  37184. DES_cblock iv = {1};
  37185. DES_key_schedule key = {0};
  37186. unsigned char msg[] = "hello wolfssl";
  37187. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37188. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37189. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37190. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37191. printf(testingFmt, "wolfSSL_DES_ncbc()");
  37192. /* partial block test */
  37193. DES_set_key(&key, &myDes);
  37194. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37195. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37196. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37197. DES_set_key(&key, &myDes);
  37198. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37199. *((byte*)&iv) = 1;
  37200. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  37201. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  37202. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37203. /* full block test */
  37204. DES_set_key(&key, &myDes);
  37205. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  37206. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37207. *((byte*)&iv) = 1;
  37208. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  37209. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  37210. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37211. DES_set_key(&key, &myDes);
  37212. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37213. *((byte*)&iv) = 1;
  37214. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  37215. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  37216. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37217. printf(resultFmt, passed);
  37218. #endif
  37219. return 0;
  37220. }
  37221. static int test_wolfSSL_AES_cbc_encrypt(void)
  37222. {
  37223. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  37224. AES_KEY aes;
  37225. AES_KEY* aesN = NULL;
  37226. size_t len = 0;
  37227. size_t lenB = 0;
  37228. int keySz0 = 0;
  37229. int keySzN = -1;
  37230. byte out[AES_BLOCK_SIZE] = {0};
  37231. byte* outN = NULL;
  37232. /* Test vectors retrieved from:
  37233. * <begin URL>
  37234. * https://csrc.nist.gov/
  37235. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37236. * documents/aes/KAT_AES.zip
  37237. * </end URL>
  37238. */
  37239. const byte* pt128N = NULL;
  37240. byte* key128N = NULL;
  37241. byte* iv128N = NULL;
  37242. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37243. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37244. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37245. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37246. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37247. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37248. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37249. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37250. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37251. len = sizeof(pt128);
  37252. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37253. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37254. AssertIntNE(XMEMCMP(b, g, h), i)
  37255. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37256. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  37257. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37258. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37259. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37260. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37261. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37262. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37263. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37264. printf(resultFmt, "Stress Tests: passed");
  37265. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  37266. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37267. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37268. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37269. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37270. printf(resultFmt, "Stress Tests: passed");
  37271. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  37272. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37273. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37274. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37275. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37276. printf(resultFmt, "Stress Tests: passed");
  37277. #ifdef WOLFSSL_AES_128
  37278. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  37279. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37280. RESET_IV(iv128tmp, iv128);
  37281. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37282. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37283. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37284. printf(resultFmt, "passed");
  37285. #ifdef HAVE_AES_DECRYPT
  37286. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  37287. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37288. RESET_IV(iv128tmp, iv128);
  37289. len = sizeof(ct128);
  37290. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37291. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37292. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37293. printf(resultFmt, "passed");
  37294. #endif
  37295. #endif /* WOLFSSL_AES_128 */
  37296. #ifdef WOLFSSL_AES_192
  37297. {
  37298. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37299. * Appendix F.2.3 */
  37300. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37301. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37302. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37303. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37304. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37305. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37306. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37307. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37308. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37309. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37310. len = sizeof(pt192);
  37311. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  37312. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37313. RESET_IV(iv192tmp, iv192);
  37314. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37315. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37316. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37317. printf(resultFmt, "passed");
  37318. #ifdef HAVE_AES_DECRYPT
  37319. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  37320. len = sizeof(ct192);
  37321. RESET_IV(iv192tmp, iv192);
  37322. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37323. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37324. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  37325. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  37326. printf(resultFmt, "passed");
  37327. #endif
  37328. }
  37329. #endif /* WOLFSSL_AES_192 */
  37330. #ifdef WOLFSSL_AES_256
  37331. {
  37332. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  37333. * Appendix F.2.5 */
  37334. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  37335. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37336. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37337. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  37338. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  37339. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37340. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37341. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37342. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37343. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37344. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  37345. len = sizeof(pt256);
  37346. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  37347. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37348. RESET_IV(iv256tmp, iv256);
  37349. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37350. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  37351. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  37352. printf(resultFmt, "passed");
  37353. #ifdef HAVE_AES_DECRYPT
  37354. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  37355. len = sizeof(ct256);
  37356. RESET_IV(iv256tmp, iv256);
  37357. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37358. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37359. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  37360. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  37361. printf(resultFmt, "passed");
  37362. #endif
  37363. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37364. {
  37365. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  37366. byte wrapPlain[sizeof(key256)] = { 0 };
  37367. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  37368. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
  37369. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37370. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37371. 15), WOLFSSL_FAILURE);
  37372. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  37373. sizeof(key256)), sizeof(wrapCipher));
  37374. printf(resultFmt, "passed");
  37375. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
  37376. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37377. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37378. 23), WOLFSSL_FAILURE);
  37379. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  37380. sizeof(wrapCipher)), sizeof(wrapPlain));
  37381. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37382. printf(resultFmt, "passed");
  37383. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  37384. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  37385. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
  37386. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37387. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  37388. sizeof(key256)), sizeof(wrapCipher));
  37389. printf(resultFmt, "passed");
  37390. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
  37391. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  37392. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  37393. sizeof(wrapCipher)), sizeof(wrapPlain));
  37394. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  37395. printf(resultFmt, "passed");
  37396. }
  37397. #endif /* HAVE_AES_KEYWRAP */
  37398. }
  37399. #endif /* WOLFSSL_AES_256 */
  37400. #endif
  37401. return 0;
  37402. }
  37403. static int test_wolfSSL_CRYPTO_cts128(void)
  37404. {
  37405. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  37406. && defined(HAVE_CTS)
  37407. byte tmp[64]; /* Largest vector size */
  37408. /* Test vectors taken form RFC3962 Appendix B */
  37409. const testVector vects[] = {
  37410. {
  37411. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37412. "\x20",
  37413. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  37414. "\x97",
  37415. 17, 17
  37416. },
  37417. {
  37418. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37419. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  37420. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  37421. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  37422. 31, 31
  37423. },
  37424. {
  37425. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37426. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  37427. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37428. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  37429. 32, 32
  37430. },
  37431. {
  37432. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37433. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37434. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  37435. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37436. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  37437. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  37438. 47, 47
  37439. },
  37440. {
  37441. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37442. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37443. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  37444. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37445. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  37446. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  37447. 48, 48
  37448. },
  37449. {
  37450. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  37451. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  37452. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  37453. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  37454. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  37455. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  37456. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  37457. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  37458. 64, 64
  37459. }
  37460. };
  37461. byte keyBytes[AES_128_KEY_SIZE] = {
  37462. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  37463. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  37464. };
  37465. size_t i;
  37466. XMEMSET(tmp, 0, sizeof(tmp));
  37467. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  37468. AES_KEY encKey;
  37469. AES_KEY decKey;
  37470. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  37471. XMEMSET(iv, 0, sizeof(iv));
  37472. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  37473. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  37474. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  37475. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  37476. vects[i].outLen);
  37477. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  37478. XMEMSET(iv, 0, sizeof(iv));
  37479. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  37480. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  37481. vects[i].inLen);
  37482. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  37483. }
  37484. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  37485. return 0;
  37486. }
  37487. #if defined(OPENSSL_ALL)
  37488. #if !defined(NO_ASN)
  37489. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  37490. {
  37491. #if !defined(NO_RSA)
  37492. WOLFSSL_X509* x509;
  37493. WOLFSSL_X509_NAME* subject;
  37494. WOLFSSL_X509_NAME_ENTRY* e;
  37495. WOLFSSL_ASN1_STRING* a;
  37496. FILE* file;
  37497. int idx = 0;
  37498. char targetOutput[16] = "www.wolfssl.com";
  37499. unsigned char* actual_output;
  37500. int len = 0;
  37501. int result = 0;
  37502. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  37503. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  37504. fclose(file);
  37505. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  37506. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  37507. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  37508. NID_commonName, -1)), 5);
  37509. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  37510. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  37511. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  37512. result = strncmp((const char*)actual_output, targetOutput, len);
  37513. AssertIntEQ(result, 0);
  37514. printf(resultFmt, result == 0 ? passed : failed);
  37515. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  37516. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  37517. WOLFSSL_FATAL_ERROR);
  37518. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  37519. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  37520. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  37521. WOLFSSL_FATAL_ERROR);
  37522. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  37523. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  37524. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  37525. WOLFSSL_FATAL_ERROR);
  37526. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  37527. wolfSSL_X509_free(x509);
  37528. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37529. #endif
  37530. return 0;
  37531. }
  37532. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  37533. {
  37534. ASN1_STRING* asn1str_test;
  37535. ASN1_STRING* asn1str_answer;
  37536. /* Each character is encoded using 4 bytes */
  37537. char input[] = {
  37538. 0, 0, 0, 'T',
  37539. 0, 0, 0, 'e',
  37540. 0, 0, 0, 's',
  37541. 0, 0, 0, 't',
  37542. };
  37543. char output[] = "Test";
  37544. printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
  37545. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  37546. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  37547. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  37548. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  37549. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  37550. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  37551. ASN1_STRING_free(asn1str_test);
  37552. ASN1_STRING_free(asn1str_answer);
  37553. printf(resultFmt, "passed");
  37554. return 0;
  37555. }
  37556. #endif /* !defined(NO_ASN) */
  37557. static int test_wolfSSL_sk_CIPHER_description(void)
  37558. {
  37559. #if !defined(NO_RSA)
  37560. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  37561. int i,j,k;
  37562. int numCiphers = 0;
  37563. const SSL_METHOD *method = NULL;
  37564. const SSL_CIPHER *cipher = NULL;
  37565. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  37566. SSL_CTX *ctx = NULL;
  37567. SSL *ssl = NULL;
  37568. char buf[256];
  37569. char test_str[9] = "0000000";
  37570. const char badStr[] = "unknown";
  37571. const char certPath[] = "./certs/client-cert.pem";
  37572. XMEMSET(buf, 0, sizeof(buf));
  37573. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  37574. AssertNotNull(method = TLSv1_2_client_method());
  37575. AssertNotNull(ctx = SSL_CTX_new(method));
  37576. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  37577. SSL_CTX_set_verify_depth(ctx, 4);
  37578. SSL_CTX_set_options(ctx, flags);
  37579. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  37580. WOLFSSL_SUCCESS);
  37581. AssertNotNull(ssl = SSL_new(ctx));
  37582. /* SSL_get_ciphers returns a stack of all configured ciphers
  37583. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  37584. */
  37585. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  37586. /* loop through the amount of supportedCiphers */
  37587. numCiphers = sk_num(supportedCiphers);
  37588. for (i = 0; i < numCiphers; ++i) {
  37589. /* sk_value increments "sk->data.cipher->cipherOffset".
  37590. * wolfSSL_sk_CIPHER_description sets the description for
  37591. * the cipher based on the provided offset.
  37592. */
  37593. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  37594. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  37595. }
  37596. /* Search cipher description string for "unknown" descriptor */
  37597. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  37598. k = 0;
  37599. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  37600. test_str[k] = badStr[k];
  37601. j++;
  37602. k++;
  37603. }
  37604. }
  37605. /* Fail if test_str == badStr == "unknown" */
  37606. AssertStrNE(test_str,badStr);
  37607. }
  37608. SSL_free(ssl);
  37609. SSL_CTX_free(ctx);
  37610. printf(resultFmt, passed);
  37611. #endif
  37612. return 0;
  37613. }
  37614. static int test_wolfSSL_get_ciphers_compat(void)
  37615. {
  37616. #if !defined(NO_RSA)
  37617. const SSL_METHOD *method = NULL;
  37618. const char certPath[] = "./certs/client-cert.pem";
  37619. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  37620. SSL_CTX *ctx = NULL;
  37621. WOLFSSL *ssl = NULL;
  37622. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  37623. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  37624. method = SSLv23_client_method();
  37625. AssertNotNull(method);
  37626. ctx = SSL_CTX_new(method);
  37627. AssertNotNull(ctx);
  37628. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  37629. SSL_CTX_set_verify_depth(ctx, 4);
  37630. SSL_CTX_set_options(ctx, flags);
  37631. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  37632. WOLFSSL_SUCCESS);
  37633. AssertNotNull(ssl = SSL_new(ctx));
  37634. /* Test Bad NULL input */
  37635. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  37636. /* Test for Good input */
  37637. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  37638. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  37639. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  37640. SSL_free(ssl);
  37641. SSL_CTX_free(ctx);
  37642. printf(resultFmt, passed);
  37643. #endif
  37644. return 0;
  37645. }
  37646. static int test_wolfSSL_X509_PUBKEY_get(void)
  37647. {
  37648. WOLFSSL_X509_PUBKEY pubkey;
  37649. WOLFSSL_X509_PUBKEY* key;
  37650. WOLFSSL_EVP_PKEY evpkey ;
  37651. WOLFSSL_EVP_PKEY* evpPkey;
  37652. WOLFSSL_EVP_PKEY* retEvpPkey;
  37653. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  37654. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  37655. key = &pubkey;
  37656. evpPkey = &evpkey;
  37657. evpPkey->type = WOLFSSL_SUCCESS;
  37658. key->pkey = evpPkey;
  37659. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  37660. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  37661. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  37662. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  37663. key->pkey = NULL;
  37664. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  37665. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  37666. return 0;
  37667. }
  37668. static int test_wolfSSL_d2i_DHparams(void)
  37669. {
  37670. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  37671. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37672. FILE* f = NULL;
  37673. unsigned char buf[4096];
  37674. const unsigned char* pt = buf;
  37675. #ifdef HAVE_FFDHE_2048
  37676. const char* params1 = "./certs/dh2048.der";
  37677. #endif
  37678. #ifdef HAVE_FFDHE_3072
  37679. const char* params2 = "./certs/dh3072.der";
  37680. #endif
  37681. long len = 0;
  37682. WOLFSSL_DH* dh = NULL;
  37683. XMEMSET(buf, 0, sizeof(buf));
  37684. /* Test 2048 bit parameters */
  37685. #ifdef HAVE_FFDHE_2048
  37686. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  37687. f = XFOPEN(params1, "rb");
  37688. AssertTrue(f != XBADFILE);
  37689. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37690. XFCLOSE(f);
  37691. /* Valid case */
  37692. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37693. AssertNotNull(dh->p);
  37694. AssertNotNull(dh->g);
  37695. AssertTrue(pt != buf);
  37696. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  37697. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
  37698. #endif
  37699. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  37700. /* Invalid cases */
  37701. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  37702. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  37703. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  37704. DH_free(dh);
  37705. printf(resultFmt, passed);
  37706. *buf = 0;
  37707. pt = buf;
  37708. #endif /* HAVE_FFDHE_2048 */
  37709. /* Test 3072 bit parameters */
  37710. #ifdef HAVE_FFDHE_3072
  37711. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  37712. f = XFOPEN(params2, "rb");
  37713. AssertTrue(f != XBADFILE);
  37714. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37715. XFCLOSE(f);
  37716. /* Valid case */
  37717. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37718. AssertNotNull(dh->p);
  37719. AssertNotNull(dh->g);
  37720. AssertTrue(pt != buf);
  37721. AssertIntEQ(DH_generate_key(dh), 1);
  37722. /* Invalid cases */
  37723. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  37724. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  37725. DH_free(dh);
  37726. printf(resultFmt, passed);
  37727. #endif /* HAVE_FFDHE_3072 */
  37728. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  37729. #endif /* !NO_DH */
  37730. return 0;
  37731. }
  37732. static int test_wolfSSL_i2d_DHparams(void)
  37733. {
  37734. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  37735. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  37736. FILE* f;
  37737. unsigned char buf[4096];
  37738. const unsigned char* pt = buf;
  37739. unsigned char* pt2 = buf;
  37740. #ifdef HAVE_FFDHE_2048
  37741. const char* params1 = "./certs/dh2048.der";
  37742. #endif
  37743. #ifdef HAVE_FFDHE_3072
  37744. const char* params2 = "./certs/dh3072.der";
  37745. #endif
  37746. long len;
  37747. WOLFSSL_DH* dh;
  37748. /* Test 2048 bit parameters */
  37749. #ifdef HAVE_FFDHE_2048
  37750. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  37751. f = XFOPEN(params1, "rb");
  37752. AssertTrue(f != XBADFILE);
  37753. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37754. XFCLOSE(f);
  37755. /* Valid case */
  37756. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37757. AssertTrue(pt != buf);
  37758. AssertIntEQ(DH_generate_key(dh), 1);
  37759. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  37760. /* Invalid case */
  37761. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  37762. /* Return length only */
  37763. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  37764. DH_free(dh);
  37765. printf(resultFmt, passed);
  37766. *buf = 0;
  37767. pt = buf;
  37768. pt2 = buf;
  37769. #endif
  37770. /* Test 3072 bit parameters */
  37771. #ifdef HAVE_FFDHE_3072
  37772. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  37773. f = XFOPEN(params2, "rb");
  37774. AssertTrue(f != XBADFILE);
  37775. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  37776. XFCLOSE(f);
  37777. /* Valid case */
  37778. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  37779. AssertTrue(pt != buf);
  37780. AssertIntEQ(DH_generate_key(dh), 1);
  37781. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  37782. /* Invalid case */
  37783. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  37784. /* Return length only */
  37785. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  37786. DH_free(dh);
  37787. printf(resultFmt, passed);
  37788. #endif
  37789. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  37790. #endif
  37791. return 0;
  37792. }
  37793. static int test_wolfSSL_EC_KEY_dup(void)
  37794. {
  37795. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  37796. defined(OPENSSL_EXTRA_X509_SMALL))
  37797. WOLFSSL_EC_KEY* ecKey;
  37798. WOLFSSL_EC_KEY* dupKey;
  37799. ecc_key* srcKey;
  37800. ecc_key* destKey;
  37801. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  37802. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37803. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37804. /* Valid cases */
  37805. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37806. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  37807. /* Compare pubkey */
  37808. srcKey = (ecc_key*)ecKey->internal;
  37809. destKey = (ecc_key*)dupKey->internal;
  37810. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  37811. /* compare EC_GROUP */
  37812. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  37813. /* compare EC_POINT */
  37814. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  37815. dupKey->pub_key, NULL), MP_EQ);
  37816. /* compare BIGNUM */
  37817. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  37818. wolfSSL_EC_KEY_free(dupKey);
  37819. /* Invalid cases */
  37820. /* NULL key */
  37821. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  37822. /* NULL ecc_key */
  37823. wc_ecc_free((ecc_key*)ecKey->internal);
  37824. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  37825. ecKey->internal = NULL; /* Set ecc_key to NULL */
  37826. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37827. wolfSSL_EC_KEY_free(ecKey);
  37828. wolfSSL_EC_KEY_free(dupKey);
  37829. /* NULL Group */
  37830. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37831. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37832. wolfSSL_EC_GROUP_free(ecKey->group);
  37833. ecKey->group = NULL; /* Set group to NULL */
  37834. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37835. wolfSSL_EC_KEY_free(ecKey);
  37836. wolfSSL_EC_KEY_free(dupKey);
  37837. /* NULL public key */
  37838. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37839. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37840. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  37841. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  37842. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37843. wolfSSL_EC_POINT_free(ecKey->pub_key);
  37844. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  37845. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37846. wolfSSL_EC_KEY_free(ecKey);
  37847. wolfSSL_EC_KEY_free(dupKey);
  37848. /* NULL private key */
  37849. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37850. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  37851. wolfSSL_BN_free(ecKey->priv_key);
  37852. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  37853. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37854. wolfSSL_EC_KEY_free(ecKey);
  37855. wolfSSL_EC_KEY_free(dupKey);
  37856. /* Test EC_KEY_up_ref */
  37857. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  37858. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  37859. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  37860. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  37861. /* reference count doesn't follow duplicate */
  37862. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  37863. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  37864. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  37865. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  37866. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  37867. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  37868. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  37869. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  37870. printf(resultFmt, passed);
  37871. #endif
  37872. return 0;
  37873. }
  37874. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  37875. {
  37876. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  37877. DSA *dsa = NULL;
  37878. DSA *setDsa = NULL;
  37879. EVP_PKEY *pkey = NULL;
  37880. EVP_PKEY *set1Pkey = NULL;
  37881. SHA_CTX sha;
  37882. byte signature[DSA_SIG_SIZE];
  37883. byte hash[WC_SHA_DIGEST_SIZE];
  37884. word32 bytes;
  37885. int answer;
  37886. #ifdef USE_CERT_BUFFERS_1024
  37887. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  37888. int dsaKeySz = sizeof_dsa_key_der_1024;
  37889. byte tmp[ONEK_BUF];
  37890. XMEMSET(tmp, 0, sizeof(tmp));
  37891. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  37892. bytes = dsaKeySz;
  37893. #elif defined(USE_CERT_BUFFERS_2048)
  37894. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  37895. int dsaKeySz = sizeof_dsa_key_der_2048;
  37896. byte tmp[TWOK_BUF];
  37897. XMEMSET(tmp, 0, sizeof(tmp));
  37898. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  37899. bytes = dsaKeySz;
  37900. #else
  37901. byte tmp[TWOK_BUF];
  37902. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  37903. int dsaKeySz;
  37904. XMEMSET(tmp, 0, sizeof(tmp));
  37905. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  37906. if (fp == XBADFILE) {
  37907. return WOLFSSL_BAD_FILE;
  37908. }
  37909. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  37910. XFCLOSE(fp);
  37911. #endif /* END USE_CERT_BUFFERS_1024 */
  37912. printf(testingFmt,
  37913. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  37914. /* Create hash to later Sign and Verify */
  37915. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  37916. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  37917. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  37918. /* Initialize pkey with der format dsa key */
  37919. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  37920. &dsaKeyDer ,(long)dsaKeySz));
  37921. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  37922. /* Should Fail: NULL argument */
  37923. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  37924. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  37925. /* Should Pass: Initialized pkey argument */
  37926. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  37927. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  37928. #ifdef USE_CERT_BUFFERS_1024
  37929. AssertIntEQ(DSA_bits(dsa), 1024);
  37930. #else
  37931. AssertIntEQ(DSA_bits(dsa), 2048);
  37932. #endif
  37933. /* Sign */
  37934. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  37935. /* Verify. */
  37936. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  37937. WOLFSSL_SUCCESS);
  37938. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  37939. /* Should Fail: set1Pkey not initialized */
  37940. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  37941. /* Initialize set1Pkey */
  37942. set1Pkey = EVP_PKEY_new();
  37943. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  37944. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  37945. WOLFSSL_SUCCESS);
  37946. /* Should Pass: set dsa into set1Pkey */
  37947. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  37948. printf(resultFmt, passed);
  37949. DSA_free(dsa);
  37950. DSA_free(setDsa);
  37951. EVP_PKEY_free(pkey);
  37952. EVP_PKEY_free(set1Pkey);
  37953. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  37954. return 0;
  37955. } /* END test_EVP_PKEY_set1_get1_DSA */
  37956. static int test_wolfSSL_DSA_SIG(void)
  37957. {
  37958. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  37959. !defined(HAVE_FIPS)
  37960. DSA *dsa = NULL;
  37961. DSA *dsa2 = NULL;
  37962. DSA_SIG *sig = NULL;
  37963. const BIGNUM *p = NULL;
  37964. const BIGNUM *q = NULL;
  37965. const BIGNUM *g = NULL;
  37966. const BIGNUM *pub = NULL;
  37967. const BIGNUM *priv = NULL;
  37968. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  37969. printf(testingFmt, "wolfSSL_DSA_SIG");
  37970. AssertNotNull(dsa = DSA_generate_parameters(2048,
  37971. NULL, 0, NULL, NULL, NULL, NULL));
  37972. DSA_free(dsa);
  37973. AssertNotNull(dsa = DSA_new());
  37974. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  37975. NULL, 0, NULL, NULL, NULL), 1);
  37976. AssertIntEQ(DSA_generate_key(dsa), 1);
  37977. DSA_get0_pqg(dsa, &p, &q, &g);
  37978. DSA_get0_key(dsa, &pub, &priv);
  37979. AssertNotNull(p = BN_dup(p));
  37980. AssertNotNull(q = BN_dup(q));
  37981. AssertNotNull(g = BN_dup(g));
  37982. AssertNotNull(pub = BN_dup(pub));
  37983. AssertNotNull(priv = BN_dup(priv));
  37984. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  37985. AssertNotNull(dsa2 = DSA_new());
  37986. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  37987. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  37988. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  37989. printf(resultFmt, passed);
  37990. DSA_free(dsa);
  37991. DSA_free(dsa2);
  37992. DSA_SIG_free(sig);
  37993. #endif
  37994. return 0;
  37995. }
  37996. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  37997. {
  37998. #ifdef HAVE_ECC
  37999. WOLFSSL_EC_KEY *ecKey = NULL;
  38000. WOLFSSL_EC_KEY *ecGet1 = NULL;
  38001. EVP_PKEY *pkey = NULL;
  38002. printf(testingFmt,
  38003. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  38004. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38005. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38006. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38007. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38008. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38009. /* Should fail since ecKey is empty */
  38010. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38011. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38012. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38013. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38014. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38015. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38016. wolfSSL_EC_KEY_free(ecKey);
  38017. wolfSSL_EC_KEY_free(ecGet1);
  38018. EVP_PKEY_free(pkey);
  38019. /* PASSED */
  38020. printf(resultFmt, passed);
  38021. #endif /* HAVE_ECC */
  38022. return 0;
  38023. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38024. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38025. {
  38026. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38027. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38028. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38029. DH *dh = NULL;
  38030. DH *setDh = NULL;
  38031. EVP_PKEY *pkey = NULL;
  38032. FILE* f = NULL;
  38033. unsigned char buf[4096];
  38034. const unsigned char* pt = buf;
  38035. const char* dh2048 = "./certs/dh2048.der";
  38036. long len = 0;
  38037. int code = -1;
  38038. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  38039. XMEMSET(buf, 0, sizeof(buf));
  38040. f = XFOPEN(dh2048, "rb");
  38041. AssertTrue(f != XBADFILE);
  38042. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38043. XFCLOSE(f);
  38044. /* Load dh2048.der into DH with internal format */
  38045. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38046. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38047. AssertIntEQ(code, 0);
  38048. code = -1;
  38049. pkey = wolfSSL_EVP_PKEY_new();
  38050. /* Set DH into PKEY */
  38051. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38052. /* Get DH from PKEY */
  38053. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  38054. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  38055. AssertIntEQ(code, 0);
  38056. EVP_PKEY_free(pkey);
  38057. DH_free(setDh);
  38058. DH_free(dh);
  38059. printf(resultFmt, passed);
  38060. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  38061. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38062. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38063. return 0;
  38064. } /* END test_EVP_PKEY_set1_get1_DH */
  38065. static int test_wolfSSL_CTX_ctrl(void)
  38066. {
  38067. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38068. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38069. char caFile[] = "./certs/client-ca.pem";
  38070. char clientFile[] = "./certs/client-cert.pem";
  38071. SSL_CTX* ctx;
  38072. X509* x509 = NULL;
  38073. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38074. byte buf[6000];
  38075. char file[] = "./certs/dsaparams.pem";
  38076. XFILE f;
  38077. int bytes;
  38078. BIO* bio;
  38079. DSA* dsa;
  38080. DH* dh;
  38081. #endif
  38082. #ifdef HAVE_ECC
  38083. WOLFSSL_EC_KEY* ecKey;
  38084. #endif
  38085. printf(testingFmt, "wolfSSL_CTX_ctrl");
  38086. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38087. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  38088. AssertNotNull(x509);
  38089. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  38090. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  38091. AssertNotNull(x509);
  38092. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38093. /* Initialize DH */
  38094. f = XFOPEN(file, "rb");
  38095. AssertTrue((f != XBADFILE));
  38096. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  38097. XFCLOSE(f);
  38098. bio = BIO_new_mem_buf((void*)buf, bytes);
  38099. AssertNotNull(bio);
  38100. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  38101. AssertNotNull(dsa);
  38102. dh = wolfSSL_DSA_dup_DH(dsa);
  38103. AssertNotNull(dh);
  38104. #endif
  38105. #ifdef HAVE_ECC
  38106. /* Initialize WOLFSSL_EC_KEY */
  38107. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38108. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  38109. #endif
  38110. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  38111. /* additional test of getting EVP_PKEY key size from X509
  38112. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38113. * allowed with user RSA */
  38114. {
  38115. EVP_PKEY* pkey;
  38116. #if defined(HAVE_ECC)
  38117. X509* ecX509;
  38118. #endif /* HAVE_ECC */
  38119. AssertNotNull(pkey = X509_get_pubkey(x509));
  38120. /* current RSA key is 2048 bit (256 bytes) */
  38121. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  38122. EVP_PKEY_free(pkey);
  38123. #if defined(HAVE_ECC)
  38124. #if defined(USE_CERT_BUFFERS_256)
  38125. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38126. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38127. SSL_FILETYPE_ASN1));
  38128. #else
  38129. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38130. cliEccCertFile, SSL_FILETYPE_PEM));
  38131. #endif
  38132. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  38133. /* current ECC key is 256 bit (32 bytes) */
  38134. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  38135. X509_free(ecX509);
  38136. EVP_PKEY_free(pkey);
  38137. #endif /* HAVE_ECC */
  38138. }
  38139. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38140. /* Tests should fail with passed in NULL pointer */
  38141. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  38142. SSL_FAILURE);
  38143. #if !defined(NO_DH) && !defined(NO_DSA)
  38144. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  38145. SSL_FAILURE);
  38146. #endif
  38147. #ifdef HAVE_ECC
  38148. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  38149. SSL_FAILURE);
  38150. #endif
  38151. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38152. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38153. */
  38154. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  38155. SSL_SUCCESS);
  38156. /* Test with SSL_CTRL_OPTIONS
  38157. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  38158. */
  38159. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  38160. == SSL_OP_NO_TLSv1);
  38161. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  38162. /* Test with SSL_CTRL_SET_TMP_DH
  38163. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  38164. */
  38165. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38166. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  38167. SSL_SUCCESS);
  38168. #endif
  38169. /* Test with SSL_CTRL_SET_TMP_ECDH
  38170. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  38171. */
  38172. #ifdef HAVE_ECC
  38173. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  38174. SSL_SUCCESS);
  38175. #endif
  38176. #ifdef WOLFSSL_ENCRYPTED_KEYS
  38177. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  38178. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  38179. #endif
  38180. /* Test for min/max proto */
  38181. #ifndef WOLFSSL_NO_TLS12
  38182. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38183. 0, NULL), SSL_SUCCESS);
  38184. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38185. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38186. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  38187. #endif
  38188. #ifdef WOLFSSL_TLS13
  38189. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38190. 0, NULL), SSL_SUCCESS);
  38191. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38192. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  38193. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  38194. #ifndef WOLFSSL_NO_TLS12
  38195. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38196. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38197. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  38198. #endif
  38199. #endif
  38200. /* Cleanup and Pass */
  38201. #if !defined(NO_DH) && !defined(NO_DSA)
  38202. #ifndef NO_BIO
  38203. BIO_free(bio);
  38204. DSA_free(dsa);
  38205. DH_free(dh);
  38206. #endif
  38207. #endif
  38208. #ifdef HAVE_ECC
  38209. wolfSSL_EC_KEY_free(ecKey);
  38210. #endif
  38211. SSL_CTX_free(ctx);
  38212. printf(resultFmt, passed);
  38213. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38214. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  38215. return 0;
  38216. }
  38217. static int test_wolfSSL_DH_check(void)
  38218. {
  38219. #if !defined(NO_DH) && !defined(NO_DSA)
  38220. #ifndef NO_BIO
  38221. byte buf[6000];
  38222. char file[] = "./certs/dsaparams.pem";
  38223. XFILE f;
  38224. int bytes;
  38225. BIO* bio;
  38226. DSA* dsa;
  38227. DH* dh = NULL;
  38228. WOLFSSL_BIGNUM* pTmp = NULL;
  38229. WOLFSSL_BIGNUM* gTmp = NULL;
  38230. int codes = -1;
  38231. printf(testingFmt, "wolfSSL_DH_check");
  38232. /* Initialize DH */
  38233. f = XFOPEN(file, "rb");
  38234. AssertTrue((f != XBADFILE));
  38235. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  38236. XFCLOSE(f);
  38237. bio = BIO_new_mem_buf((void*)buf, bytes);
  38238. AssertNotNull(bio);
  38239. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  38240. AssertNotNull(dsa);
  38241. dh = wolfSSL_DSA_dup_DH(dsa);
  38242. AssertNotNull(dh);
  38243. /* Test assumed to be valid dh.
  38244. * Should return WOLFSSL_SUCCESS
  38245. * codes should be 0
  38246. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  38247. */
  38248. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_SUCCESS);
  38249. AssertIntEQ(codes, 0);
  38250. /* Test NULL dh: expected BAD_FUNC_ARG */
  38251. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
  38252. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  38253. pTmp = dh->p;
  38254. dh->p = NULL;
  38255. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  38256. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  38257. /* set dh->p back to normal so it wont fail on next tests */
  38258. dh->p = pTmp;
  38259. pTmp = NULL;
  38260. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  38261. gTmp = dh->g;
  38262. dh->g = NULL;
  38263. AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
  38264. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  38265. dh->g = gTmp;
  38266. gTmp = NULL;
  38267. /* Cleanup and Pass Test */
  38268. BIO_free(bio);
  38269. DSA_free(dsa);
  38270. DH_free(dh);
  38271. printf(resultFmt, passed);
  38272. #endif
  38273. #endif /* !NO_DH && !NO_DSA */
  38274. return 0;
  38275. }
  38276. static int test_wolfSSL_EVP_PKEY_assign(void)
  38277. {
  38278. int type;
  38279. WOLFSSL_EVP_PKEY* pkey;
  38280. #ifndef NO_RSA
  38281. WOLFSSL_RSA* rsa;
  38282. #endif
  38283. #ifndef NO_DSA
  38284. WOLFSSL_DSA* dsa;
  38285. #endif
  38286. #ifdef HAVE_ECC
  38287. WOLFSSL_EC_KEY* ecKey;
  38288. #endif
  38289. (void)pkey;
  38290. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  38291. #ifndef NO_RSA
  38292. type = EVP_PKEY_RSA;
  38293. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38294. AssertNotNull(rsa = wolfSSL_RSA_new());
  38295. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  38296. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38297. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  38298. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  38299. wolfSSL_EVP_PKEY_free(pkey);
  38300. #endif /* NO_RSA */
  38301. #ifndef NO_DSA
  38302. type = EVP_PKEY_DSA;
  38303. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38304. AssertNotNull(dsa = wolfSSL_DSA_new());
  38305. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  38306. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38307. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  38308. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  38309. wolfSSL_EVP_PKEY_free(pkey);
  38310. #endif /* NO_DSA */
  38311. #ifdef HAVE_ECC
  38312. type = EVP_PKEY_EC;
  38313. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38314. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38315. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  38316. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38317. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  38318. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  38319. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38320. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  38321. wolfSSL_EVP_PKEY_free(pkey);
  38322. #endif /* HAVE_ECC */
  38323. (void)type;
  38324. printf(resultFmt, passed);
  38325. return 0;
  38326. }
  38327. static int test_wolfSSL_EVP_PKEY_base_id(void)
  38328. {
  38329. WOLFSSL_EVP_PKEY* pkey;
  38330. printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
  38331. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38332. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  38333. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  38334. EVP_PKEY_free(pkey);
  38335. printf(resultFmt, passed);
  38336. return 0;
  38337. }
  38338. static int test_wolfSSL_EVP_PKEY_id(void)
  38339. {
  38340. WOLFSSL_EVP_PKEY* pkey;
  38341. printf(testingFmt, "wolfSSL_EVP_PKEY_id");
  38342. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38343. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  38344. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  38345. EVP_PKEY_free(pkey);
  38346. printf(resultFmt, passed);
  38347. return 0;
  38348. }
  38349. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  38350. {
  38351. #if defined(OPENSSL_ALL) && \
  38352. !defined(NO_ECC_SECP) && \
  38353. /* This last bit is taken from ecc.c. It is the condition that
  38354. * defines ECC256 */ \
  38355. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  38356. ECC_MIN_KEY_SZ <= 256)
  38357. EVP_PKEY_CTX* ctx;
  38358. EVP_PKEY* pkey = NULL;
  38359. printf(testingFmt, "wolfSSL_EVP_PKEY_paramgen");
  38360. /* Test error conditions. */
  38361. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  38362. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38363. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  38364. #ifndef NO_RSA
  38365. EVP_PKEY_CTX_free(ctx);
  38366. /* Parameter generation for RSA not supported yet. */
  38367. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  38368. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  38369. #endif
  38370. #ifdef HAVE_ECC
  38371. EVP_PKEY_CTX_free(ctx);
  38372. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38373. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  38374. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  38375. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  38376. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  38377. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  38378. WOLFSSL_SUCCESS);
  38379. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38380. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38381. #endif
  38382. EVP_PKEY_CTX_free(ctx);
  38383. EVP_PKEY_free(pkey);
  38384. printf(resultFmt, passed);
  38385. #endif
  38386. return 0;
  38387. }
  38388. static int test_wolfSSL_EVP_PKEY_keygen(void)
  38389. {
  38390. WOLFSSL_EVP_PKEY* pkey = NULL;
  38391. EVP_PKEY_CTX* ctx = NULL;
  38392. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38393. WOLFSSL_EVP_PKEY* params = NULL;
  38394. DH* dh = NULL;
  38395. const BIGNUM* pubkey = NULL;
  38396. const BIGNUM* privkey = NULL;
  38397. ASN1_INTEGER* asn1int = NULL;
  38398. unsigned int length = 0;
  38399. byte* derBuffer = NULL;
  38400. #endif
  38401. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
  38402. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38403. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38404. /* Bad cases */
  38405. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  38406. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  38407. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  38408. /* Good case */
  38409. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  38410. EVP_PKEY_CTX_free(ctx);
  38411. EVP_PKEY_free(pkey);
  38412. pkey = NULL;
  38413. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38414. /* Test DH keygen */
  38415. {
  38416. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  38417. AssertNotNull(dh = DH_get_2048_256());
  38418. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  38419. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  38420. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38421. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38422. DH_free(dh);
  38423. EVP_PKEY_CTX_free(ctx);
  38424. EVP_PKEY_free(params);
  38425. /* try exporting generated key to DER, to verify */
  38426. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  38427. DH_get0_key(dh, &pubkey, &privkey);
  38428. AssertNotNull(pubkey);
  38429. AssertNotNull(privkey);
  38430. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  38431. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  38432. ASN1_INTEGER_free(asn1int);
  38433. DH_free(dh);
  38434. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38435. EVP_PKEY_free(pkey);
  38436. }
  38437. #endif
  38438. printf(resultFmt, passed);
  38439. return 0;
  38440. }
  38441. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  38442. {
  38443. WOLFSSL_EVP_PKEY* pkey;
  38444. EVP_PKEY_CTX *ctx;
  38445. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
  38446. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38447. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38448. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38449. EVP_PKEY_CTX_free(ctx);
  38450. EVP_PKEY_free(pkey);
  38451. printf(resultFmt, passed);
  38452. return 0;
  38453. }
  38454. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  38455. {
  38456. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  38457. WOLFSSL_EVP_PKEY* pkey;
  38458. printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
  38459. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38460. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  38461. EVP_PKEY_free(pkey);
  38462. printf(resultFmt, passed);
  38463. #endif
  38464. return 0;
  38465. }
  38466. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  38467. {
  38468. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  38469. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  38470. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  38471. !defined(NO_FILESYSTEM)
  38472. WOLFSSL_EVP_PKEY* params = NULL;
  38473. WOLFSSL_EVP_PKEY* copy = NULL;
  38474. DH* dh = NULL;
  38475. BIGNUM* p1;
  38476. BIGNUM* g1;
  38477. BIGNUM* q1;
  38478. BIGNUM* p2;
  38479. BIGNUM* g2;
  38480. BIGNUM* q2;
  38481. printf(testingFmt, "wolfSSL_EVP_PKEY_copy_parameters");
  38482. /* create DH with DH_get_2048_256 params */
  38483. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  38484. AssertNotNull(dh = DH_get_2048_256());
  38485. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  38486. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  38487. (const BIGNUM**)&q1,
  38488. (const BIGNUM**)&g1);
  38489. DH_free(dh);
  38490. /* create DH with random generated DH params */
  38491. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  38492. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  38493. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  38494. DH_free(dh);
  38495. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  38496. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  38497. AssertNotNull(dh->p);
  38498. AssertNotNull(dh->g);
  38499. AssertNotNull(dh->q);
  38500. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  38501. (const BIGNUM**)&q2,
  38502. (const BIGNUM**)&g2);
  38503. AssertIntEQ(BN_cmp(p1, p2), 0);
  38504. AssertIntEQ(BN_cmp(q1, q2), 0);
  38505. AssertIntEQ(BN_cmp(g1, g2), 0);
  38506. DH_free(dh);
  38507. EVP_PKEY_free(copy);
  38508. EVP_PKEY_free(params);
  38509. printf(resultFmt, passed);
  38510. #endif
  38511. return 0;
  38512. }
  38513. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  38514. {
  38515. WOLFSSL_EVP_PKEY* pkey;
  38516. EVP_PKEY_CTX *ctx;
  38517. int bits = 2048;
  38518. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
  38519. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38520. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38521. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  38522. WOLFSSL_SUCCESS);
  38523. EVP_PKEY_CTX_free(ctx);
  38524. EVP_PKEY_free(pkey);
  38525. printf(resultFmt, passed);
  38526. return 0;
  38527. }
  38528. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  38529. {
  38530. /* This is large enough to be used for all key sizes */
  38531. byte key[AES_256_KEY_SIZE] = {0};
  38532. byte iv[AES_BLOCK_SIZE] = {0};
  38533. int i, enumlen;
  38534. EVP_CIPHER_CTX *ctx;
  38535. const EVP_CIPHER *init;
  38536. int enumArray[] = {
  38537. #ifdef HAVE_AES_CBC
  38538. NID_aes_128_cbc,
  38539. #endif
  38540. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38541. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38542. #ifdef HAVE_AESGCM
  38543. NID_aes_128_gcm,
  38544. #endif
  38545. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38546. #ifdef WOLFSSL_AES_COUNTER
  38547. NID_aes_128_ctr,
  38548. #endif
  38549. #ifndef NO_DES3
  38550. NID_des_cbc,
  38551. NID_des_ede3_cbc,
  38552. #endif
  38553. };
  38554. int iv_lengths[] = {
  38555. #ifdef HAVE_AES_CBC
  38556. AES_BLOCK_SIZE,
  38557. #endif
  38558. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38559. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38560. #ifdef HAVE_AESGCM
  38561. GCM_NONCE_MID_SZ,
  38562. #endif
  38563. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38564. #ifdef WOLFSSL_AES_COUNTER
  38565. AES_BLOCK_SIZE,
  38566. #endif
  38567. #ifndef NO_DES3
  38568. DES_BLOCK_SIZE,
  38569. DES_BLOCK_SIZE,
  38570. #endif
  38571. };
  38572. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
  38573. enumlen = (sizeof(enumArray)/sizeof(int));
  38574. for(i = 0; i < enumlen; i++)
  38575. {
  38576. ctx = EVP_CIPHER_CTX_new();
  38577. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  38578. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38579. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38580. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  38581. EVP_CIPHER_CTX_free(ctx);
  38582. }
  38583. printf(resultFmt, passed);
  38584. return 0;
  38585. }
  38586. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  38587. {
  38588. #if !defined(NO_DES3)
  38589. byte key[AES_256_KEY_SIZE] = {0};
  38590. byte iv[AES_BLOCK_SIZE] = {0};
  38591. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  38592. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  38593. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
  38594. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38595. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38596. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  38597. EVP_CIPHER_CTX_free(ctx);
  38598. printf(resultFmt, passed);
  38599. #endif
  38600. return 0;
  38601. }
  38602. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  38603. {
  38604. #if !defined(NO_DES3)
  38605. byte key[AES_256_KEY_SIZE] = {0};
  38606. byte iv[AES_BLOCK_SIZE] = {0};
  38607. int keylen;
  38608. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  38609. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  38610. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
  38611. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38612. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38613. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  38614. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  38615. WOLFSSL_SUCCESS);
  38616. EVP_CIPHER_CTX_free(ctx);
  38617. printf(resultFmt, passed);
  38618. #endif
  38619. return 0;
  38620. }
  38621. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  38622. {
  38623. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  38624. byte key[DES3_KEY_SIZE] = {0};
  38625. byte iv[DES_BLOCK_SIZE] = {0};
  38626. int ivLen, keyLen;
  38627. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  38628. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  38629. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
  38630. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38631. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38632. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  38633. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  38634. /* Bad cases */
  38635. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  38636. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  38637. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  38638. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  38639. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  38640. /* Good case */
  38641. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  38642. EVP_CIPHER_CTX_free(ctx);
  38643. printf(resultFmt, passed);
  38644. #endif
  38645. return 0;
  38646. }
  38647. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  38648. {
  38649. WOLFSSL_ENGINE* e = NULL;
  38650. int id = 0;
  38651. EVP_PKEY_CTX *ctx;
  38652. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
  38653. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  38654. EVP_PKEY_CTX_free(ctx);
  38655. printf(resultFmt, passed);
  38656. return 0;
  38657. }
  38658. static int test_wolfSSL_EVP_rc4(void)
  38659. {
  38660. #if !defined(NO_RC4)
  38661. printf(testingFmt, "wolfSSL_EVP_rc4");
  38662. AssertNotNull(wolfSSL_EVP_rc4());
  38663. printf(resultFmt, passed);
  38664. #endif
  38665. return 0;
  38666. }
  38667. static int test_wolfSSL_EVP_enc_null(void)
  38668. {
  38669. printf(testingFmt, "wolfSSL_EVP_enc_null");
  38670. AssertNotNull(wolfSSL_EVP_enc_null());
  38671. printf(resultFmt, passed);
  38672. return 0;
  38673. }
  38674. static int test_wolfSSL_EVP_rc2_cbc(void)
  38675. {
  38676. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  38677. printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
  38678. AssertNull(wolfSSL_EVP_rc2_cbc());
  38679. printf(resultFmt, passed);
  38680. #endif
  38681. return 0;
  38682. }
  38683. static int test_wolfSSL_EVP_mdc2(void)
  38684. {
  38685. #if !defined(NO_WOLFSSL_STUB)
  38686. printf(testingFmt, "wolfSSL_EVP_mdc2");
  38687. AssertNull(wolfSSL_EVP_mdc2());
  38688. printf(resultFmt, passed);
  38689. #endif
  38690. return 0;
  38691. }
  38692. static int test_wolfSSL_EVP_md4(void)
  38693. {
  38694. #if !defined(NO_MD4)
  38695. printf(testingFmt, "wolfSSL_EVP_md4");
  38696. AssertNotNull(wolfSSL_EVP_md4());
  38697. printf(resultFmt, passed);
  38698. #endif
  38699. return 0;
  38700. }
  38701. static int test_wolfSSL_EVP_aes_256_gcm(void)
  38702. {
  38703. printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
  38704. AssertNotNull(wolfSSL_EVP_aes_256_gcm());
  38705. printf(resultFmt, passed);
  38706. return 0;
  38707. }
  38708. static int test_wolfSSL_EVP_aes_192_gcm(void)
  38709. {
  38710. printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
  38711. AssertNotNull(wolfSSL_EVP_aes_192_gcm());
  38712. printf(resultFmt, passed);
  38713. return 0;
  38714. }
  38715. static int test_wolfSSL_EVP_ripemd160(void)
  38716. {
  38717. #if !defined(NO_WOLFSSL_STUB)
  38718. printf(testingFmt, "wolfSSL_EVP_ripemd160");
  38719. AssertNull(wolfSSL_EVP_ripemd160());
  38720. printf(resultFmt, passed);
  38721. #endif
  38722. return 0;
  38723. }
  38724. static int test_wolfSSL_EVP_get_digestbynid(void)
  38725. {
  38726. printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
  38727. #ifndef NO_MD5
  38728. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  38729. #endif
  38730. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  38731. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  38732. printf(resultFmt, passed);
  38733. return 0;
  38734. }
  38735. static int test_wolfSSL_EVP_MD_nid(void)
  38736. {
  38737. printf(testingFmt, "wolfSSL_EVP_MD_nid");
  38738. #ifndef NO_MD5
  38739. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  38740. #endif
  38741. #ifndef NO_SHA
  38742. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  38743. #endif
  38744. #ifndef NO_SHA256
  38745. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  38746. #endif
  38747. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  38748. printf(resultFmt, passed);
  38749. return 0;
  38750. }
  38751. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  38752. {
  38753. #if defined(HAVE_ECC)
  38754. WOLFSSL_EVP_PKEY* pkey;
  38755. printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
  38756. AssertNotNull(pkey = EVP_PKEY_new());
  38757. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  38758. EVP_PKEY_free(pkey);
  38759. printf(resultFmt, passed);
  38760. #endif
  38761. return 0;
  38762. }
  38763. static int test_wolfSSL_EVP_X_STATE(void)
  38764. {
  38765. #if !defined(NO_DES3) && !defined(NO_RC4)
  38766. byte key[DES3_KEY_SIZE] = {0};
  38767. byte iv[DES_IV_SIZE] = {0};
  38768. EVP_CIPHER_CTX *ctx;
  38769. const EVP_CIPHER *init;
  38770. printf(testingFmt, "wolfSSL_EVP_X_STATE");
  38771. /* Bad test cases */
  38772. ctx = EVP_CIPHER_CTX_new();
  38773. init = EVP_des_ede3_cbc();
  38774. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38775. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38776. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  38777. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  38778. EVP_CIPHER_CTX_free(ctx);
  38779. /* Good test case */
  38780. ctx = EVP_CIPHER_CTX_new();
  38781. init = wolfSSL_EVP_rc4();
  38782. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38783. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38784. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  38785. EVP_CIPHER_CTX_free(ctx);
  38786. printf(resultFmt, passed);
  38787. #endif
  38788. return 0;
  38789. }
  38790. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  38791. {
  38792. #if !defined(NO_DES3) && !defined(NO_RC4)
  38793. byte key[DES3_KEY_SIZE] = {0};
  38794. byte iv[DES_IV_SIZE] = {0};
  38795. EVP_CIPHER_CTX *ctx;
  38796. const EVP_CIPHER *init;
  38797. printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
  38798. /* Bad test cases */
  38799. ctx = EVP_CIPHER_CTX_new();
  38800. init = EVP_des_ede3_cbc();
  38801. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38802. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38803. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  38804. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  38805. EVP_CIPHER_CTX_free(ctx);
  38806. /* Good test case */
  38807. ctx = EVP_CIPHER_CTX_new();
  38808. init = wolfSSL_EVP_rc4();
  38809. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  38810. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  38811. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  38812. EVP_CIPHER_CTX_free(ctx);
  38813. printf(resultFmt, passed);
  38814. #endif
  38815. return 0;
  38816. }
  38817. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  38818. {
  38819. #ifdef HAVE_AES_CBC
  38820. #ifdef WOLFSSL_AES_128
  38821. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  38822. #endif
  38823. #ifdef WOLFSSL_AES_192
  38824. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  38825. #endif
  38826. #ifdef WOLFSSL_AES_256
  38827. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  38828. #endif
  38829. #endif
  38830. #ifdef HAVE_AESGCM
  38831. #ifdef WOLFSSL_AES_128
  38832. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  38833. #endif
  38834. #ifdef WOLFSSL_AES_192
  38835. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  38836. #endif
  38837. #ifdef WOLFSSL_AES_256
  38838. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  38839. #endif
  38840. #endif
  38841. #ifdef WOLFSSL_AES_COUNTER
  38842. #ifdef WOLFSSL_AES_128
  38843. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  38844. #endif
  38845. #ifdef WOLFSSL_AES_192
  38846. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  38847. #endif
  38848. #ifdef WOLFSSL_AES_256
  38849. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  38850. #endif
  38851. #endif
  38852. #ifdef HAVE_AES_ECB
  38853. #ifdef WOLFSSL_AES_128
  38854. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  38855. #endif
  38856. #ifdef WOLFSSL_AES_192
  38857. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  38858. #endif
  38859. #ifdef WOLFSSL_AES_256
  38860. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  38861. #endif
  38862. #endif
  38863. #ifdef WOLFSSL_AES_OFB
  38864. #ifdef WOLFSSL_AES_128
  38865. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  38866. #endif
  38867. #ifdef WOLFSSL_AES_192
  38868. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  38869. #endif
  38870. #ifdef WOLFSSL_AES_256
  38871. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  38872. #endif
  38873. #endif
  38874. #ifndef NO_RC4
  38875. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  38876. #endif
  38877. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  38878. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  38879. #endif
  38880. return 0;
  38881. }
  38882. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  38883. {
  38884. int i, enumlen;
  38885. int enumArray[] = {
  38886. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  38887. #ifdef WOLFSSL_AES_128
  38888. NID_aes_128_cbc,
  38889. #endif
  38890. #ifdef WOLFSSL_AES_192
  38891. NID_aes_192_cbc,
  38892. #endif
  38893. #ifdef WOLFSSL_AES_256
  38894. NID_aes_256_cbc,
  38895. #endif
  38896. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  38897. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38898. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38899. #ifdef HAVE_AESGCM
  38900. #ifdef WOLFSSL_AES_128
  38901. NID_aes_128_gcm,
  38902. #endif
  38903. #ifdef WOLFSSL_AES_192
  38904. NID_aes_192_gcm,
  38905. #endif
  38906. #ifdef WOLFSSL_AES_256
  38907. NID_aes_256_gcm,
  38908. #endif
  38909. #endif /* HAVE_AESGCM */
  38910. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38911. #ifdef WOLFSSL_AES_COUNTER
  38912. #ifdef WOLFSSL_AES_128
  38913. NID_aes_128_ctr,
  38914. #endif
  38915. #ifdef WOLFSSL_AES_192
  38916. NID_aes_192_ctr,
  38917. #endif
  38918. #ifdef WOLFSSL_AES_256
  38919. NID_aes_256_ctr,
  38920. #endif
  38921. #endif
  38922. #ifndef NO_DES3
  38923. NID_des_cbc,
  38924. NID_des_ede3_cbc,
  38925. #endif
  38926. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  38927. NID_chacha20_poly1305,
  38928. #endif
  38929. };
  38930. int iv_lengths[] = {
  38931. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  38932. #ifdef WOLFSSL_AES_128
  38933. AES_BLOCK_SIZE,
  38934. #endif
  38935. #ifdef WOLFSSL_AES_192
  38936. AES_BLOCK_SIZE,
  38937. #endif
  38938. #ifdef WOLFSSL_AES_256
  38939. AES_BLOCK_SIZE,
  38940. #endif
  38941. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  38942. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  38943. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  38944. #ifdef HAVE_AESGCM
  38945. #ifdef WOLFSSL_AES_128
  38946. GCM_NONCE_MID_SZ,
  38947. #endif
  38948. #ifdef WOLFSSL_AES_192
  38949. GCM_NONCE_MID_SZ,
  38950. #endif
  38951. #ifdef WOLFSSL_AES_256
  38952. GCM_NONCE_MID_SZ,
  38953. #endif
  38954. #endif /* HAVE_AESGCM */
  38955. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  38956. #ifdef WOLFSSL_AES_COUNTER
  38957. #ifdef WOLFSSL_AES_128
  38958. AES_BLOCK_SIZE,
  38959. #endif
  38960. #ifdef WOLFSSL_AES_192
  38961. AES_BLOCK_SIZE,
  38962. #endif
  38963. #ifdef WOLFSSL_AES_256
  38964. AES_BLOCK_SIZE,
  38965. #endif
  38966. #endif
  38967. #ifndef NO_DES3
  38968. DES_BLOCK_SIZE,
  38969. DES_BLOCK_SIZE,
  38970. #endif
  38971. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  38972. CHACHA20_POLY1305_AEAD_IV_SIZE,
  38973. #endif
  38974. };
  38975. printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
  38976. enumlen = (sizeof(enumArray)/sizeof(int));
  38977. for(i = 0; i < enumlen; i++)
  38978. {
  38979. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  38980. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  38981. }
  38982. printf(resultFmt, passed);
  38983. return 0;
  38984. }
  38985. static int test_wolfSSL_EVP_SignInit_ex(void)
  38986. {
  38987. WOLFSSL_EVP_MD_CTX mdCtx;
  38988. WOLFSSL_ENGINE* e = 0;
  38989. const EVP_MD* md;
  38990. md = "SHA256";
  38991. printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
  38992. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  38993. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  38994. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  38995. printf(resultFmt, passed);
  38996. return 0;
  38997. }
  38998. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  38999. {
  39000. #if !defined(NO_SHA256)
  39001. WOLFSSL_EVP_MD_CTX mdCtx;
  39002. unsigned int s = 0;
  39003. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39004. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39005. printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
  39006. /* Bad Case */
  39007. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39008. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39009. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39010. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39011. #else
  39012. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39013. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39014. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39015. #endif
  39016. /* Good Case */
  39017. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39018. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  39019. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39020. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39021. printf(resultFmt, passed);
  39022. #endif
  39023. return 0;
  39024. }
  39025. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  39026. {
  39027. #if !defined(NO_DH) && \
  39028. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39029. FILE* f = NULL;
  39030. unsigned char buf[4096];
  39031. const unsigned char* pt = buf;
  39032. const char* params1 = "./certs/dh2048.der";
  39033. long len = 0;
  39034. WOLFSSL_DH* dh = NULL;
  39035. WOLFSSL_EVP_PKEY* pkey;
  39036. XMEMSET(buf, 0, sizeof(buf));
  39037. f = XFOPEN(params1, "rb");
  39038. AssertTrue(f != XBADFILE);
  39039. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39040. XFCLOSE(f);
  39041. printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
  39042. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  39043. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  39044. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39045. /* Bad cases */
  39046. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  39047. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39048. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39049. /* Good case */
  39050. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39051. EVP_PKEY_free(pkey);
  39052. printf(resultFmt, passed);
  39053. #endif
  39054. return 0;
  39055. }
  39056. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39057. {
  39058. #if defined(OPENSSL_EXTRA)
  39059. EVP_PKEY* pkey;
  39060. EVP_PKEY_CTX* ctx;
  39061. printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
  39062. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39063. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39064. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  39065. /* void */
  39066. EVP_PKEY_CTX_free(ctx);
  39067. AssertTrue(1);
  39068. #else
  39069. /* int */
  39070. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  39071. #endif
  39072. EVP_PKEY_free(pkey);
  39073. printf(resultFmt, passed);
  39074. #endif
  39075. return 0;
  39076. }
  39077. static int test_wolfSSL_EVP_PKEY_param_check(void)
  39078. {
  39079. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  39080. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  39081. DH *dh = NULL;
  39082. DH *setDh = NULL;
  39083. EVP_PKEY *pkey = NULL;
  39084. EVP_PKEY_CTX* ctx = NULL;
  39085. FILE* f = NULL;
  39086. unsigned char buf[512];
  39087. const unsigned char* pt = buf;
  39088. const char* dh2048 = "./certs/dh2048.der";
  39089. long len = 0;
  39090. int code = -1;
  39091. printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
  39092. XMEMSET(buf, 0, sizeof(buf));
  39093. f = XFOPEN(dh2048, "rb");
  39094. AssertTrue(f != XBADFILE);
  39095. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39096. XFCLOSE(f);
  39097. /* Load dh2048.der into DH with internal format */
  39098. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  39099. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39100. AssertIntEQ(code, 0);
  39101. code = -1;
  39102. pkey = wolfSSL_EVP_PKEY_new();
  39103. /* Set DH into PKEY */
  39104. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39105. /* create ctx from pkey */
  39106. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39107. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  39108. /* */
  39109. /* TO DO invlaid case */
  39110. /* */
  39111. EVP_PKEY_CTX_free(ctx);
  39112. EVP_PKEY_free(pkey);
  39113. DH_free(setDh);
  39114. DH_free(dh);
  39115. printf(resultFmt, passed);
  39116. #endif
  39117. #endif
  39118. return 0;
  39119. }
  39120. static int test_wolfSSL_EVP_BytesToKey(void)
  39121. {
  39122. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39123. byte key[AES_BLOCK_SIZE] = {0};
  39124. byte iv[AES_BLOCK_SIZE] = {0};
  39125. int sz = 5;
  39126. int count = 0;
  39127. const EVP_MD* md = "SHA256";
  39128. const EVP_CIPHER *type;
  39129. const unsigned char *salt = (unsigned char *)"salt1234";
  39130. const byte data[] = {
  39131. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39132. 0x72,0x6c,0x64
  39133. };
  39134. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  39135. printf(testingFmt, "EVP_BytesToKey");
  39136. /* Bad cases */
  39137. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  39138. 0);
  39139. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  39140. 16);
  39141. md = "2";
  39142. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39143. WOLFSSL_FAILURE);
  39144. /* Good case */
  39145. md = "SHA256";
  39146. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39147. 16);
  39148. printf(resultFmt, passed);
  39149. #endif
  39150. return 0;
  39151. }
  39152. static int test_evp_cipher_aes_gcm(void)
  39153. {
  39154. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  39155. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  39156. (HAVE_FIPS_VERSION >= 2)))
  39157. /*
  39158. * This test checks data at various points in the encrypt/decrypt process
  39159. * against known values produced using the same test with OpenSSL. This
  39160. * interop testing is critical for verifying the correctness of our
  39161. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  39162. * a flow supported by OpenSSL that uses the control command
  39163. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  39164. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  39165. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  39166. * wolfSSL OpenSSH clients because there was a bug in this flow that
  39167. * happened to "cancel out" if both sides of the connection had the bug.
  39168. */
  39169. enum {
  39170. NUM_ENCRYPTIONS = 3,
  39171. AAD_SIZE = 4
  39172. };
  39173. byte plainText1[] = {
  39174. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  39175. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  39176. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  39177. };
  39178. byte plainText2[] = {
  39179. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  39180. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  39181. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  39182. };
  39183. byte plainText3[] = {
  39184. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  39185. 0x5b, 0x57, 0x10
  39186. };
  39187. byte* plainTexts[NUM_ENCRYPTIONS] = {
  39188. plainText1,
  39189. plainText2,
  39190. plainText3
  39191. };
  39192. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  39193. sizeof(plainText1),
  39194. sizeof(plainText2),
  39195. sizeof(plainText3)
  39196. };
  39197. byte aad1[AAD_SIZE] = {
  39198. 0x00, 0x00, 0x00, 0x01
  39199. };
  39200. byte aad2[AAD_SIZE] = {
  39201. 0x00, 0x00, 0x00, 0x10
  39202. };
  39203. byte aad3[AAD_SIZE] = {
  39204. 0x00, 0x00, 0x01, 0x00
  39205. };
  39206. byte* aads[NUM_ENCRYPTIONS] = {
  39207. aad1,
  39208. aad2,
  39209. aad3
  39210. };
  39211. const byte iv[GCM_NONCE_MID_SZ] = {
  39212. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  39213. };
  39214. byte currentIv[GCM_NONCE_MID_SZ];
  39215. const byte key[] = {
  39216. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  39217. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  39218. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  39219. };
  39220. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  39221. {
  39222. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39223. 0xEF
  39224. },
  39225. {
  39226. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39227. 0xF0
  39228. },
  39229. {
  39230. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39231. 0xF1
  39232. }
  39233. };
  39234. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  39235. {
  39236. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  39237. 0x3D, 0x32, 0x18, 0x34, 0xA9
  39238. },
  39239. {
  39240. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  39241. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  39242. },
  39243. {
  39244. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  39245. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  39246. }
  39247. };
  39248. const byte expCipherText1[] = {
  39249. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  39250. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  39251. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  39252. };
  39253. const byte expCipherText2[] = {
  39254. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  39255. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  39256. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  39257. };
  39258. const byte expCipherText3[] = {
  39259. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  39260. 0x80, 0x5E, 0x6B,
  39261. };
  39262. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  39263. expCipherText1,
  39264. expCipherText2,
  39265. expCipherText3
  39266. };
  39267. byte* cipherText;
  39268. byte* calcPlainText;
  39269. byte tag[AES_BLOCK_SIZE];
  39270. EVP_CIPHER_CTX* encCtx = NULL;
  39271. EVP_CIPHER_CTX* decCtx = NULL;
  39272. int i, j, outl;
  39273. printf(testingFmt, "test_evp_cipher_aes_gcm");
  39274. /****************************************************/
  39275. for (i = 0; i < 3; ++i) {
  39276. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  39277. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  39278. /* First iteration, set key before IV. */
  39279. if (i == 0) {
  39280. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  39281. SSL_SUCCESS);
  39282. /*
  39283. * The call to EVP_CipherInit below (with NULL key) should clear the
  39284. * gcmIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  39285. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  39286. * behavior.
  39287. */
  39288. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39289. (void*)iv), SSL_SUCCESS);
  39290. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  39291. SSL_SUCCESS);
  39292. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39293. currentIv), SSL_FAILURE);
  39294. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  39295. SSL_SUCCESS);
  39296. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  39297. SSL_SUCCESS);
  39298. }
  39299. /* Second iteration, IV before key. */
  39300. else {
  39301. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  39302. SSL_SUCCESS);
  39303. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  39304. SSL_SUCCESS);
  39305. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  39306. SSL_SUCCESS);
  39307. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  39308. SSL_SUCCESS);
  39309. }
  39310. /*
  39311. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  39312. * been issued first.
  39313. */
  39314. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39315. currentIv), SSL_FAILURE);
  39316. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39317. (void*)iv), SSL_SUCCESS);
  39318. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39319. (void*)iv), SSL_SUCCESS);
  39320. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  39321. /*************** Encrypt ***************/
  39322. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39323. currentIv), SSL_SUCCESS);
  39324. /* Check current IV against expected. */
  39325. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39326. /* Add AAD. */
  39327. if (i == 2) {
  39328. /* Test streaming API. */
  39329. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  39330. AAD_SIZE), SSL_SUCCESS);
  39331. }
  39332. else {
  39333. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  39334. AAD_SIZE);
  39335. }
  39336. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39337. DYNAMIC_TYPE_TMP_BUFFER));
  39338. /* Encrypt plaintext. */
  39339. if (i == 2){
  39340. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  39341. plainTexts[j], plainTextSzs[j]),
  39342. SSL_SUCCESS);
  39343. }
  39344. else {
  39345. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  39346. plainTextSzs[j]), plainTextSzs[j]);
  39347. }
  39348. if (i == 2) {
  39349. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  39350. SSL_SUCCESS);
  39351. }
  39352. else {
  39353. /*
  39354. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  39355. * akin to calling EVP_CipherFinal.
  39356. */
  39357. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  39358. }
  39359. /* Check ciphertext against expected. */
  39360. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  39361. 0);
  39362. /* Get and check tag against expected. */
  39363. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  39364. sizeof(tag), tag), SSL_SUCCESS);
  39365. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  39366. /*************** Decrypt ***************/
  39367. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39368. currentIv), SSL_SUCCESS);
  39369. /* Check current IV against expected. */
  39370. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39371. /* Add AAD. */
  39372. if (i == 2) {
  39373. /* Test streaming API. */
  39374. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  39375. AAD_SIZE), SSL_SUCCESS);
  39376. }
  39377. else {
  39378. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  39379. AAD_SIZE);
  39380. }
  39381. /* Set expected tag. */
  39382. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  39383. sizeof(tag), tag), SSL_SUCCESS);
  39384. /* Decrypt ciphertext. */
  39385. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39386. DYNAMIC_TYPE_TMP_BUFFER));
  39387. if (i == 2){
  39388. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  39389. cipherText, plainTextSzs[j]),
  39390. SSL_SUCCESS);
  39391. }
  39392. else {
  39393. /* This first EVP_Cipher call will check the tag, too. */
  39394. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  39395. plainTextSzs[j]), plainTextSzs[j]);
  39396. }
  39397. if (i == 2) {
  39398. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  39399. SSL_SUCCESS);
  39400. }
  39401. else {
  39402. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  39403. }
  39404. /* Check plaintext against expected. */
  39405. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  39406. 0);
  39407. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39408. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39409. }
  39410. EVP_CIPHER_CTX_free(encCtx);
  39411. EVP_CIPHER_CTX_free(decCtx);
  39412. }
  39413. printf(resultFmt, passed);
  39414. #endif
  39415. return 0;
  39416. }
  39417. static int test_wolfSSL_OBJ_ln(void)
  39418. {
  39419. const int nid_set[] = {
  39420. NID_commonName,
  39421. NID_serialNumber,
  39422. NID_countryName,
  39423. NID_localityName,
  39424. NID_stateOrProvinceName,
  39425. NID_organizationName,
  39426. NID_organizationalUnitName,
  39427. NID_domainComponent,
  39428. NID_businessCategory,
  39429. NID_jurisdictionCountryName,
  39430. NID_jurisdictionStateOrProvinceName,
  39431. NID_emailAddress
  39432. };
  39433. const char* ln_set[] = {
  39434. "commonName",
  39435. "serialNumber",
  39436. "countryName",
  39437. "localityName",
  39438. "stateOrProvinceName",
  39439. "organizationName",
  39440. "organizationalUnitName",
  39441. "domainComponent",
  39442. "businessCategory",
  39443. "jurisdictionCountryName",
  39444. "jurisdictionStateOrProvinceName",
  39445. "emailAddress",
  39446. };
  39447. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  39448. printf(testingFmt, "wolfSSL_OBJ_ln");
  39449. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  39450. #ifdef HAVE_ECC
  39451. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39452. {
  39453. EC_builtin_curve r[27];
  39454. size_t nCurves = sizeof(r) / sizeof(r[0]);
  39455. nCurves = EC_get_builtin_curves(r,nCurves);
  39456. for (i = 0; i < nCurves; i++) {
  39457. /* skip ECC_CURVE_INVALID */
  39458. if (r[i].nid != ECC_CURVE_INVALID) {
  39459. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  39460. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  39461. }
  39462. }
  39463. }
  39464. #endif
  39465. #endif
  39466. for (i = 0; i < maxIdx; i++) {
  39467. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  39468. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  39469. }
  39470. printf(resultFmt, passed);
  39471. return 0;
  39472. }
  39473. static int test_wolfSSL_OBJ_sn(void)
  39474. {
  39475. int i = 0, maxIdx = 7;
  39476. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  39477. NID_stateOrProvinceName,NID_organizationName,
  39478. NID_organizationalUnitName,NID_emailAddress};
  39479. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  39480. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  39481. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  39482. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  39483. WOLFSSL_EMAIL_ADDR};
  39484. printf(testingFmt, "wolfSSL_OBJ_sn");
  39485. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  39486. for (i = 0; i < maxIdx; i++) {
  39487. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  39488. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  39489. }
  39490. printf(resultFmt, passed);
  39491. return 0;
  39492. }
  39493. #if !defined(NO_BIO)
  39494. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  39495. {
  39496. return lh_strhash(s[3]);
  39497. }
  39498. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  39499. {
  39500. return XSTRCMP(a[3], b[3]);
  39501. }
  39502. #endif
  39503. static int test_wolfSSL_TXT_DB(void)
  39504. {
  39505. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39506. BIO *bio;
  39507. TXT_DB *db = NULL;
  39508. const int columns = 6;
  39509. const char *fields[6] = {
  39510. "V",
  39511. "320926161116Z",
  39512. "",
  39513. "12BD",
  39514. "unknown",
  39515. "/CN=rsa doe",
  39516. };
  39517. char** fields_copy;
  39518. printf(testingFmt, "wolfSSL_TXT_DB");
  39519. /* Test read */
  39520. AssertNotNull(bio = BIO_new(BIO_s_file()));
  39521. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  39522. AssertNotNull(db = TXT_DB_read(bio, columns));
  39523. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  39524. DYNAMIC_TYPE_OPENSSL));
  39525. XMEMCPY(fields_copy, fields, sizeof(fields));
  39526. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  39527. BIO_free(bio);
  39528. /* Test write */
  39529. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  39530. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  39531. BIO_free(bio);
  39532. /* Test index */
  39533. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  39534. (wolf_sk_compare_cb)TXT_DB_cmp), 1);
  39535. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39536. fields[3] = "12DA";
  39537. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39538. fields[3] = "FFFF";
  39539. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39540. fields[3] = "";
  39541. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  39542. TXT_DB_free(db);
  39543. printf(resultFmt, passed);
  39544. #endif
  39545. return 0;
  39546. }
  39547. static int test_wolfSSL_NCONF(void)
  39548. {
  39549. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39550. const char* confFile = "./tests/NCONF_test.cnf";
  39551. CONF* conf = NULL;
  39552. long eline = 0;
  39553. long num = 0;
  39554. printf(testingFmt, "wolfSSL_NCONF");
  39555. AssertNotNull(conf = NCONF_new(NULL));
  39556. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  39557. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  39558. AssertIntEQ(num, 1234);
  39559. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  39560. AssertIntEQ(num, 4321);
  39561. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  39562. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  39563. "./test-dir/file1");
  39564. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  39565. "./test-dir/file1:./test-dir/file2:./section1:file2");
  39566. NCONF_free(conf);
  39567. printf(resultFmt, passed);
  39568. #endif
  39569. return 0;
  39570. }
  39571. #endif /* OPENSSL_ALL */
  39572. static int test_wolfSSL_EC_KEY_set_group(void)
  39573. {
  39574. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  39575. defined(OPENSSL_EXTRA)
  39576. EC_KEY *key = NULL;
  39577. EC_GROUP *group = NULL;
  39578. const EC_GROUP *group2 = NULL;
  39579. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  39580. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  39581. AssertNotNull(key = EC_KEY_new());
  39582. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  39583. AssertNotNull(group2 = EC_KEY_get0_group(key));
  39584. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  39585. EC_GROUP_free(group);
  39586. EC_KEY_free(key);
  39587. printf(resultFmt, passed);
  39588. #endif
  39589. return 0;
  39590. }
  39591. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  39592. {
  39593. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
  39594. BIO* bio;
  39595. EC_KEY* key;
  39596. printf(testingFmt, "test_wolfSSL_EC_KEY_set_conv_form");
  39597. /* Error condition: NULL key. */
  39598. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  39599. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  39600. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  39601. /* Conversion form defaults to uncompressed. */
  39602. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  39603. #ifdef HAVE_COMP_KEY
  39604. /* Explicitly set to compressed. */
  39605. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  39606. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  39607. #endif
  39608. BIO_free(bio);
  39609. EC_KEY_free(key);
  39610. printf(resultFmt, passed);
  39611. #endif
  39612. return 0;
  39613. }
  39614. static int test_wolfSSL_EC_KEY_print_fp(void)
  39615. {
  39616. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  39617. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256 && \
  39618. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  39619. !defined(NO_STDIO_FILESYSTEM)
  39620. EC_KEY* key = NULL;
  39621. printf(testingFmt, "test_wolfSSL_EC_KEY_print_fp");
  39622. /* Bad file pointer. */
  39623. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  39624. /* NULL key. */
  39625. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, NULL, 0), WOLFSSL_FAILURE);
  39626. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  39627. /* Negative indent. */
  39628. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, -1), WOLFSSL_FAILURE);
  39629. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  39630. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  39631. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  39632. wolfSSL_EC_KEY_free(key);
  39633. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  39634. NID_X9_62_prime256v1)));
  39635. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  39636. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  39637. wolfSSL_EC_KEY_free(key);
  39638. printf(resultFmt, passed);
  39639. #endif
  39640. return 0;
  39641. }
  39642. static int test_wolfSSL_X509V3_EXT_get(void) {
  39643. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39644. FILE* f;
  39645. int numOfExt =0;
  39646. int extNid = 0;
  39647. int i = 0;
  39648. WOLFSSL_X509* x509;
  39649. WOLFSSL_X509_EXTENSION* ext;
  39650. const WOLFSSL_v3_ext_method* method;
  39651. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39652. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39653. fclose(f);
  39654. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  39655. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  39656. for (i = 0; i < numOfExt; i++) {
  39657. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39658. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  39659. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  39660. AssertIntEQ(method->ext_nid, extNid);
  39661. }
  39662. printf(resultFmt, "passed");
  39663. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  39664. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  39665. printf(resultFmt, "passed");
  39666. wolfSSL_X509_free(x509);
  39667. #endif
  39668. return 0;
  39669. }
  39670. static int test_wolfSSL_X509V3_EXT_nconf(void)
  39671. {
  39672. #ifdef OPENSSL_ALL
  39673. const char *ext_names[] = {
  39674. "subjectKeyIdentifier",
  39675. "authorityKeyIdentifier",
  39676. "subjectAltName",
  39677. "keyUsage",
  39678. };
  39679. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  39680. int ext_nids[] = {
  39681. NID_subject_key_identifier,
  39682. NID_authority_key_identifier,
  39683. NID_subject_alt_name,
  39684. NID_key_usage,
  39685. };
  39686. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  39687. const char *ext_values[] = {
  39688. "hash",
  39689. "hash",
  39690. "DNS:example.com, IP:127.0.0.1",
  39691. "digitalSignature,keyEncipherment,dataEncipherment",
  39692. };
  39693. size_t i;
  39694. X509_EXTENSION* ext;
  39695. X509* x509 = X509_new();
  39696. printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
  39697. for (i = 0; i < ext_names_count; i++) {
  39698. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  39699. AssertNotNull(ext);
  39700. X509_EXTENSION_free(ext);
  39701. }
  39702. for (i = 0; i < ext_nids_count; i++) {
  39703. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  39704. AssertNotNull(ext);
  39705. X509_EXTENSION_free(ext);
  39706. }
  39707. /* Test adding extension to X509 */
  39708. for (i = 0; i < ext_nids_count; i++) {
  39709. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  39710. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  39711. X509_EXTENSION_free(ext);
  39712. }
  39713. X509_free(x509);
  39714. printf(resultFmt, "passed");
  39715. #endif
  39716. return 0;
  39717. }
  39718. static int test_wolfSSL_X509V3_EXT(void) {
  39719. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39720. FILE* f;
  39721. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  39722. char* str;
  39723. unsigned char* data;
  39724. const WOLFSSL_v3_ext_method* method;
  39725. WOLFSSL_X509* x509;
  39726. WOLFSSL_X509_EXTENSION* ext;
  39727. WOLFSSL_X509_EXTENSION* ext2;
  39728. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  39729. WOLFSSL_ASN1_STRING* asn1str;
  39730. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  39731. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  39732. WOLFSSL_BASIC_CONSTRAINTS* bc;
  39733. WOLFSSL_ACCESS_DESCRIPTION* ad;
  39734. WOLFSSL_GENERAL_NAME* gn;
  39735. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  39736. /* Check NULL argument */
  39737. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  39738. /* Using OCSP cert with X509V3 extensions */
  39739. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  39740. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39741. fclose(f);
  39742. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  39743. /* Basic Constraints */
  39744. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39745. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39746. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  39747. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  39748. AssertIntEQ(bc->ca, 1);
  39749. AssertNull(bc->pathlen);
  39750. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  39751. i++;
  39752. /* Subject Key Identifier */
  39753. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39754. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39755. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  39756. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  39757. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  39758. asn1str));
  39759. X509_EXTENSION_free(ext2);
  39760. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  39761. AssertNotNull(method->i2s);
  39762. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  39763. wolfSSL_ASN1_STRING_free(asn1str);
  39764. actual = strcmp(str,
  39765. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  39766. AssertIntEQ(actual, 0);
  39767. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39768. i++;
  39769. /* Authority Key Identifier */
  39770. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39771. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39772. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  39773. AssertNotNull(aKeyId =
  39774. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  39775. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  39776. AssertNotNull(asn1str = aKeyId->keyid);
  39777. AssertNotNull(str =
  39778. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  39779. actual = strcmp(str,
  39780. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  39781. AssertIntEQ(actual, 0);
  39782. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39783. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  39784. i++;
  39785. /* Key Usage */
  39786. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39787. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39788. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  39789. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  39790. #if defined(WOLFSSL_QT)
  39791. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  39792. #else
  39793. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  39794. #endif
  39795. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  39796. #ifdef BIG_ENDIAN_ORDER
  39797. actual = data[1];
  39798. #else
  39799. actual = data[0];
  39800. #endif
  39801. AssertIntEQ(actual, expected);
  39802. wolfSSL_ASN1_STRING_free(asn1str);
  39803. #if 1
  39804. i++;
  39805. /* Authority Info Access */
  39806. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  39807. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  39808. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  39809. AssertNotNull(aia =
  39810. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  39811. #if defined(WOLFSSL_QT)
  39812. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  39813. #else
  39814. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  39815. #endif
  39816. /* URI entry is an ACCESS_DESCRIPTION type */
  39817. #if defined(WOLFSSL_QT)
  39818. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  39819. #else
  39820. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  39821. #endif
  39822. AssertNotNull(adObj = ad->method);
  39823. /* Make sure nid is OCSP */
  39824. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  39825. /* GENERAL_NAME stores URI as an ASN1_STRING */
  39826. AssertNotNull(gn = ad->location);
  39827. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  39828. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  39829. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  39830. #if defined(WOLFSSL_QT)
  39831. str = (char*)ASN1_STRING_get0_data(asn1str);
  39832. #else
  39833. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  39834. #endif
  39835. actual = strcmp(str, "http://127.0.0.1:22220");
  39836. AssertIntEQ(actual, 0);
  39837. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  39838. #else
  39839. (void) aia; (void) ad; (void) adObj; (void) gn;
  39840. #endif
  39841. wolfSSL_X509_free(x509);
  39842. printf(resultFmt, "passed");
  39843. #endif
  39844. return 0;
  39845. }
  39846. static int test_wolfSSL_X509_get_extension_flags(void)
  39847. {
  39848. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39849. XFILE f;
  39850. X509* x509;
  39851. unsigned int extFlags;
  39852. unsigned int keyUsageFlags;
  39853. unsigned int extKeyUsageFlags;
  39854. printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
  39855. /* client-int-cert.pem has the following extension flags. */
  39856. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  39857. /* and the following key usage flags. */
  39858. keyUsageFlags = KU_DIGITAL_SIGNATURE
  39859. | KU_NON_REPUDIATION
  39860. | KU_KEY_ENCIPHERMENT;
  39861. /* and the following extended key usage flags. */
  39862. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  39863. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  39864. AssertTrue(f != XBADFILE);
  39865. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39866. XFCLOSE(f);
  39867. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  39868. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  39869. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  39870. X509_free(x509);
  39871. /* client-cert-ext.pem has the following extension flags. */
  39872. extFlags = EXFLAG_KUSAGE;
  39873. /* and the following key usage flags. */
  39874. keyUsageFlags = KU_DIGITAL_SIGNATURE
  39875. | KU_KEY_CERT_SIGN
  39876. | KU_CRL_SIGN;
  39877. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  39878. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39879. XFCLOSE(f);
  39880. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  39881. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  39882. X509_free(x509);
  39883. printf(resultFmt, passed);
  39884. #endif /* OPENSSL_ALL */
  39885. return 0;
  39886. }
  39887. static int test_wolfSSL_X509_get_ext(void){
  39888. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39889. int ret = 0;
  39890. FILE* f;
  39891. WOLFSSL_X509* x509;
  39892. WOLFSSL_X509_EXTENSION* foundExtension;
  39893. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39894. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39895. fclose(f);
  39896. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  39897. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  39898. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  39899. printf(resultFmt, "passed");
  39900. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  39901. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  39902. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  39903. printf(resultFmt, "passed");
  39904. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  39905. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  39906. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  39907. printf(resultFmt, "passed");
  39908. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  39909. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  39910. printf(resultFmt, "passed");
  39911. wolfSSL_X509_free(x509);
  39912. #endif
  39913. return 0;
  39914. }
  39915. static int test_wolfSSL_X509_get_ext_by_NID(void)
  39916. {
  39917. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39918. int rc;
  39919. FILE* f;
  39920. WOLFSSL_X509* x509;
  39921. ASN1_OBJECT* obj = NULL;
  39922. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  39923. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  39924. fclose(f);
  39925. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  39926. AssertIntGE(rc, 0);
  39927. /* Start search from last location (should fail) */
  39928. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  39929. AssertIntGE(rc, -1);
  39930. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  39931. AssertIntGE(rc, -1);
  39932. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  39933. AssertIntEQ(rc, -1);
  39934. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  39935. AssertIntEQ(rc, -1);
  39936. /* NID_ext_key_usage, check also its nid and oid */
  39937. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  39938. AssertIntGT(rc, -1);
  39939. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  39940. AssertIntEQ(obj->nid, NID_ext_key_usage);
  39941. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  39942. wolfSSL_X509_free(x509);
  39943. #endif
  39944. return 0;
  39945. }
  39946. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  39947. {
  39948. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  39949. int rc;
  39950. XFILE f;
  39951. WOLFSSL_X509* x509;
  39952. WOLFSSL_X509_EXTENSION* ext;
  39953. WOLFSSL_ASN1_STRING* sanString;
  39954. byte* sanDer;
  39955. const byte expectedDer[] = {
  39956. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  39957. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  39958. printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
  39959. f = XFOPEN("./certs/server-cert.pem", "rb");
  39960. AssertTrue(f != XBADFILE);
  39961. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  39962. fclose(f);
  39963. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  39964. AssertIntNE(rc, -1);
  39965. AssertNotNull(ext = X509_get_ext(x509, rc));
  39966. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  39967. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  39968. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  39969. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  39970. X509_free(x509);
  39971. printf(resultFmt, passed);
  39972. #endif
  39973. return 0;
  39974. }
  39975. static int test_wolfSSL_X509_EXTENSION_new(void)
  39976. {
  39977. #if defined (OPENSSL_ALL)
  39978. WOLFSSL_X509_EXTENSION* ext;
  39979. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  39980. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  39981. ext->obj->nid = WOLFSSL_SUCCESS;
  39982. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  39983. wolfSSL_X509_EXTENSION_free(ext);
  39984. #endif
  39985. return 0;
  39986. }
  39987. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  39988. {
  39989. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  39990. WOLFSSL_X509* x509;
  39991. WOLFSSL_X509_EXTENSION* ext;
  39992. WOLFSSL_ASN1_OBJECT* o;
  39993. FILE* file;
  39994. int nid = 0;
  39995. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  39996. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  39997. fclose(file);
  39998. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  39999. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40000. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40001. AssertIntEQ(o->nid, 128);
  40002. nid = o->nid;
  40003. printf(resultFmt, nid == 128 ? passed : failed);
  40004. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  40005. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40006. printf(resultFmt, passed);
  40007. wolfSSL_X509_free(x509);
  40008. #endif
  40009. return 0;
  40010. }
  40011. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40012. {
  40013. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40014. WOLFSSL_X509* x509;
  40015. WOLFSSL_X509_EXTENSION* ext;
  40016. WOLFSSL_ASN1_STRING* str;
  40017. FILE* file;
  40018. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  40019. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40020. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40021. fclose(file);
  40022. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40023. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40024. printf(resultFmt, passed);
  40025. wolfSSL_X509_free(x509);
  40026. #endif
  40027. return 0;
  40028. }
  40029. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40030. {
  40031. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40032. WOLFSSL_X509* x509;
  40033. WOLFSSL_X509_EXTENSION* ext;
  40034. FILE* file;
  40035. int crit;
  40036. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  40037. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40038. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40039. fclose(file);
  40040. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40041. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  40042. AssertIntEQ(crit, 0);
  40043. printf(resultFmt, passed);
  40044. wolfSSL_X509_free(x509);
  40045. #endif
  40046. return 0;
  40047. }
  40048. static int test_wolfSSL_X509V3_EXT_print(void)
  40049. {
  40050. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40051. !defined(NO_RSA)
  40052. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  40053. {
  40054. FILE* f;
  40055. WOLFSSL_X509* x509;
  40056. X509_EXTENSION * ext = NULL;
  40057. int loc;
  40058. BIO *bio = NULL;
  40059. AssertNotNull(f = fopen(svrCertFile, "rb"));
  40060. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40061. fclose(f);
  40062. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40063. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40064. AssertIntGT(loc, -1);
  40065. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40066. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40067. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  40068. AssertIntGT(loc, -1);
  40069. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40070. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40071. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  40072. AssertIntGT(loc, -1);
  40073. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40074. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40075. wolfSSL_BIO_free(bio);
  40076. wolfSSL_X509_free(x509);
  40077. }
  40078. {
  40079. X509 *x509;
  40080. BIO *bio;
  40081. X509_EXTENSION *ext;
  40082. unsigned int i;
  40083. unsigned int idx;
  40084. /* Some NIDs to test with */
  40085. int nids[] = {
  40086. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40087. * string, which messes up X509V3_EXT_print */
  40088. /* NID_ext_key_usage, */
  40089. NID_subject_alt_name,
  40090. };
  40091. int* n;
  40092. AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
  40093. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40094. WOLFSSL_FILETYPE_PEM));
  40095. printf("\nPrinting extension values:\n");
  40096. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40097. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40098. * update the index */
  40099. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40100. AssertNotNull(ext = X509_get_ext(x509, idx));
  40101. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40102. printf("\n");
  40103. }
  40104. BIO_free(bio);
  40105. X509_free(x509);
  40106. }
  40107. printf(resultFmt, passed);
  40108. #endif
  40109. return 0;
  40110. }
  40111. static int test_wolfSSL_X509_cmp(void)
  40112. {
  40113. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40114. FILE* file1;
  40115. FILE* file2;
  40116. WOLFSSL_X509* cert1;
  40117. WOLFSSL_X509* cert2;
  40118. int ret = 0;
  40119. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  40120. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  40121. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40122. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40123. fclose(file1);
  40124. fclose(file2);
  40125. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  40126. ret = wolfSSL_X509_cmp(cert1, cert1);
  40127. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40128. printf(resultFmt, ret == 0 ? passed : failed);
  40129. fflush(stdout);
  40130. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  40131. ret = wolfSSL_X509_cmp(cert1, cert2);
  40132. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40133. printf(resultFmt, ret == -1 ? passed : failed);
  40134. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  40135. ret = wolfSSL_X509_cmp(NULL, cert2);
  40136. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40137. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40138. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  40139. ret = wolfSSL_X509_cmp(cert1, NULL);
  40140. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40141. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40142. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  40143. ret = wolfSSL_X509_cmp(NULL, NULL);
  40144. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40145. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40146. wolfSSL_X509_free(cert1);
  40147. wolfSSL_X509_free(cert2);
  40148. #endif
  40149. return 0;
  40150. }
  40151. static int test_wolfSSL_PKEY_up_ref(void)
  40152. {
  40153. #if defined(OPENSSL_ALL)
  40154. EVP_PKEY* pkey;
  40155. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  40156. pkey = EVP_PKEY_new();
  40157. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  40158. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40159. EVP_PKEY_free(pkey);
  40160. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40161. EVP_PKEY_free(pkey);
  40162. EVP_PKEY_free(pkey);
  40163. printf(resultFmt, "passed");
  40164. #endif
  40165. return 0;
  40166. }
  40167. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  40168. {
  40169. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40170. EVP_PKEY* pkey;
  40171. const unsigned char* p;
  40172. unsigned char* der = NULL;
  40173. int derLen;
  40174. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
  40175. p = client_keypub_der_2048;
  40176. /* Check that key can be successfully decoded. */
  40177. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  40178. sizeof_client_keypub_der_2048));
  40179. /* Check that key can be successfully encoded. */
  40180. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40181. /* Ensure that the encoded version matches the original. */
  40182. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  40183. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40184. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40185. EVP_PKEY_free(pkey);
  40186. printf(resultFmt, passed);
  40187. #endif
  40188. return 0;
  40189. }
  40190. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  40191. {
  40192. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  40193. DSA* dsa;
  40194. char file[] = "./certs/dsaparams.der";
  40195. XFILE f;
  40196. int derInLen;
  40197. byte* derIn;
  40198. int derOutLen;
  40199. byte* derOut = NULL;
  40200. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
  40201. f = XFOPEN(file, "rb");
  40202. AssertTrue(f != XBADFILE);
  40203. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  40204. derInLen = (int)XFTELL(f);
  40205. XREWIND(f);
  40206. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  40207. DYNAMIC_TYPE_TMP_BUFFER));
  40208. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  40209. XFCLOSE(f);
  40210. /* Check that params can be successfully decoded. */
  40211. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  40212. /* Check that params can be successfully encoded. */
  40213. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  40214. /* Ensure that the encoded version matches the original. */
  40215. AssertIntEQ(derInLen, derOutLen);
  40216. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  40217. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40218. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40219. DSA_free(dsa);
  40220. printf(resultFmt, passed);
  40221. #endif
  40222. return 0;
  40223. }
  40224. static int test_wolfSSL_i2d_PrivateKey(void)
  40225. {
  40226. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  40227. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  40228. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  40229. {
  40230. EVP_PKEY* pkey;
  40231. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  40232. unsigned char buf[FOURK_BUF];
  40233. unsigned char* pt = NULL;
  40234. int bufSz;
  40235. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  40236. (long)sizeof_server_key_der_2048));
  40237. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  40238. pt = buf;
  40239. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  40240. AssertIntNE((pt - buf), 0);
  40241. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  40242. EVP_PKEY_free(pkey);
  40243. }
  40244. #endif
  40245. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40246. {
  40247. EVP_PKEY* pkey;
  40248. const unsigned char* client_key =
  40249. (const unsigned char*)ecc_clikey_der_256;
  40250. unsigned char buf[FOURK_BUF];
  40251. unsigned char* pt = NULL;
  40252. int bufSz;
  40253. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  40254. sizeof_ecc_clikey_der_256)));
  40255. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  40256. pt = buf;
  40257. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  40258. AssertIntNE((pt - buf), 0);
  40259. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  40260. EVP_PKEY_free(pkey);
  40261. }
  40262. #endif
  40263. printf(resultFmt, "passed");
  40264. #endif
  40265. return 0;
  40266. }
  40267. static int test_wolfSSL_OCSP_id_get0_info(void)
  40268. {
  40269. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  40270. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40271. X509* cert;
  40272. X509* issuer;
  40273. OCSP_CERTID* id;
  40274. OCSP_CERTID* id2;
  40275. ASN1_STRING* name = NULL;
  40276. ASN1_OBJECT* pmd = NULL;
  40277. ASN1_STRING* keyHash = NULL;
  40278. ASN1_INTEGER* serial = NULL;
  40279. ASN1_INTEGER* x509Int;
  40280. printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
  40281. AssertNotNull(cert =
  40282. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  40283. AssertNotNull(issuer =
  40284. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  40285. id = OCSP_cert_to_id(NULL, cert, issuer);
  40286. AssertNotNull(id);
  40287. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  40288. AssertNotNull(id2);
  40289. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  40290. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  40291. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  40292. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  40293. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  40294. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  40295. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  40296. AssertNotNull(serial);
  40297. /* compare serial number to one in cert, should be equal */
  40298. x509Int = X509_get_serialNumber(cert);
  40299. AssertNotNull(x509Int);
  40300. AssertIntEQ(x509Int->length, serial->length);
  40301. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  40302. /* test OCSP_id_cmp */
  40303. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  40304. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  40305. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  40306. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  40307. id->issuerHash[0] = ~id->issuerHash[0];
  40308. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  40309. OCSP_CERTID_free(id);
  40310. OCSP_CERTID_free(id2);
  40311. X509_free(cert); /* free's x509Int */
  40312. X509_free(issuer);
  40313. printf(resultFmt, "passed");
  40314. #endif
  40315. return 0;
  40316. }
  40317. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  40318. {
  40319. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40320. WOLFSSL_OCSP_CERTID certId;
  40321. byte* targetBuffer;
  40322. byte* beginTargetBuffer;
  40323. /* OCSP CertID bytes taken from PCAP */
  40324. byte rawCertId[] = {
  40325. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40326. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40327. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40328. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40329. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40330. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40331. 0xcf, 0xbc, 0x91
  40332. };
  40333. int ret, i;
  40334. printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
  40335. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  40336. certId.rawCertId = rawCertId;
  40337. certId.rawCertIdSize = sizeof(rawCertId);
  40338. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40339. beginTargetBuffer = targetBuffer;
  40340. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40341. /* If target buffer is not null, function increments targetBuffer to point
  40342. just past the end of the encoded data. */
  40343. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  40344. /* Function returns the size of the encoded data. */
  40345. AssertIntEQ(ret, sizeof(rawCertId));
  40346. for (i = 0; i < ret; ++i)
  40347. {
  40348. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  40349. }
  40350. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40351. targetBuffer = NULL;
  40352. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40353. /* If target buffer is null, function allocates memory for a buffer and
  40354. copies the encoded data into it. targetBuffer then points to the start of
  40355. this newly allocate buffer. */
  40356. AssertIntEQ(ret, sizeof(rawCertId));
  40357. for (i = 0; i < ret; ++i)
  40358. {
  40359. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  40360. }
  40361. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  40362. printf(resultFmt, passed);
  40363. #endif
  40364. return 0;
  40365. }
  40366. static int test_wolfSSL_OCSP_id_cmp(void)
  40367. {
  40368. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40369. OCSP_CERTID id1;
  40370. OCSP_CERTID id2;
  40371. printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
  40372. XMEMSET(&id1, 0, sizeof(id1));
  40373. XMEMSET(&id2, 0, sizeof(id2));
  40374. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  40375. printf(resultFmt, passed);
  40376. #endif
  40377. return 0;
  40378. }
  40379. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  40380. {
  40381. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40382. WOLFSSL_OCSP_SINGLERESP single;
  40383. const WOLFSSL_OCSP_CERTID* certId;
  40384. XMEMSET(&single, 0, sizeof(single));
  40385. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  40386. printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
  40387. AssertPtrEq(&single, certId);
  40388. printf(resultFmt, passed);
  40389. #endif
  40390. return 0;
  40391. }
  40392. static int test_wolfSSL_OCSP_single_get0_status(void)
  40393. {
  40394. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40395. WOLFSSL_OCSP_SINGLERESP single;
  40396. CertStatus certStatus;
  40397. WOLFSSL_ASN1_TIME* thisDate;
  40398. WOLFSSL_ASN1_TIME* nextDate;
  40399. int ret, i;
  40400. printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
  40401. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40402. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  40403. /* Fill the date fields with some dummy data. */
  40404. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  40405. certStatus.thisDateParsed.data[i] = i;
  40406. certStatus.nextDateParsed.data[i] = i;
  40407. }
  40408. certStatus.status = CERT_GOOD;
  40409. single.status = &certStatus;
  40410. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  40411. &nextDate);
  40412. AssertIntEQ(ret, CERT_GOOD);
  40413. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  40414. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  40415. printf(resultFmt, passed);
  40416. #endif
  40417. return 0;
  40418. }
  40419. static int test_wolfSSL_OCSP_resp_count(void)
  40420. {
  40421. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40422. WOLFSSL_OCSP_BASICRESP basicResp;
  40423. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  40424. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  40425. int count;
  40426. printf(testingFmt, "wolfSSL_OCSP_resp_count()");
  40427. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  40428. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40429. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40430. count = wolfSSL_OCSP_resp_count(&basicResp);
  40431. AssertIntEQ(count, 0);
  40432. basicResp.single = &singleRespOne;
  40433. count = wolfSSL_OCSP_resp_count(&basicResp);
  40434. AssertIntEQ(count, 1);
  40435. singleRespOne.next = &singleRespTwo;
  40436. count = wolfSSL_OCSP_resp_count(&basicResp);
  40437. AssertIntEQ(count, 2);
  40438. printf(resultFmt, passed);
  40439. #endif
  40440. return 0;
  40441. }
  40442. static int test_wolfSSL_OCSP_resp_get0(void)
  40443. {
  40444. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40445. WOLFSSL_OCSP_BASICRESP basicResp;
  40446. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  40447. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  40448. WOLFSSL_OCSP_SINGLERESP* ret;
  40449. printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
  40450. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  40451. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40452. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40453. basicResp.single = &singleRespOne;
  40454. singleRespOne.next = &singleRespTwo;
  40455. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  40456. AssertPtrEq(ret, &singleRespOne);
  40457. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  40458. AssertPtrEq(ret, &singleRespTwo);
  40459. printf(resultFmt, passed);
  40460. #endif
  40461. return 0;
  40462. }
  40463. static int test_wolfSSL_EVP_PKEY_derive(void)
  40464. {
  40465. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  40466. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  40467. EVP_PKEY_CTX *ctx;
  40468. unsigned char *skey;
  40469. size_t skeylen;
  40470. EVP_PKEY *pkey, *peerkey;
  40471. const unsigned char* key;
  40472. printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
  40473. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  40474. /* DH */
  40475. key = dh_key_der_2048;
  40476. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  40477. sizeof_dh_key_der_2048)));
  40478. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  40479. key = dh_key_der_2048;
  40480. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  40481. sizeof_dh_key_der_2048)));
  40482. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  40483. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40484. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  40485. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  40486. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  40487. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  40488. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  40489. EVP_PKEY_CTX_free(ctx);
  40490. EVP_PKEY_free(peerkey);
  40491. EVP_PKEY_free(pkey);
  40492. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  40493. #endif
  40494. #ifdef HAVE_ECC
  40495. /* ECDH */
  40496. key = ecc_clikey_der_256;
  40497. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  40498. sizeof_ecc_clikey_der_256)));
  40499. key = ecc_clikeypub_der_256;
  40500. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  40501. sizeof_ecc_clikeypub_der_256)));
  40502. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  40503. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  40504. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  40505. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  40506. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  40507. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  40508. EVP_PKEY_CTX_free(ctx);
  40509. EVP_PKEY_free(peerkey);
  40510. EVP_PKEY_free(pkey);
  40511. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  40512. #endif /* HAVE_ECC */
  40513. printf(resultFmt, "passed");
  40514. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  40515. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  40516. return 0;
  40517. }
  40518. static int test_wolfSSL_EVP_PBE_scrypt(void)
  40519. {
  40520. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  40521. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  40522. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  40523. int ret;
  40524. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  40525. int pwdlen = sizeof(pwd);
  40526. const byte salt[] = {'N','a','C','l'};
  40527. int saltlen = sizeof(salt);
  40528. byte key[80];
  40529. word64 numOvr32 = (word64)INT32_MAX + 1;
  40530. /* expected derived key for N:16, r:1, p:1 */
  40531. const byte expectedKey[] = {
  40532. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  40533. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  40534. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  40535. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  40536. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  40537. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  40538. 0xE7, 0xE9, 0xC0, 0x9A};
  40539. printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
  40540. /* N r p mx key keylen */
  40541. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  40542. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  40543. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  40544. AssertIntEQ(ret, 0); /* N must be power of 2 */
  40545. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  40546. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  40547. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  40548. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  40549. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  40550. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  40551. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  40552. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  40553. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  40554. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  40555. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  40556. AssertIntEQ(ret, 1); /* should succeed */
  40557. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  40558. key, 64);
  40559. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  40560. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  40561. key, 64);
  40562. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  40563. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  40564. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  40565. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  40566. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  40567. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  40568. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  40569. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  40570. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  40571. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  40572. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  40573. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  40574. AssertIntEQ(ret, 1);
  40575. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  40576. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  40577. printf(resultFmt, "passed");
  40578. #endif /* !NO_PWDBASED && !NO_SHA256 */
  40579. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  40580. return 0;
  40581. }
  40582. static int test_wolfSSL_EC_get_builtin_curves(void)
  40583. {
  40584. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  40585. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  40586. EC_builtin_curve* curves = NULL;
  40587. size_t crv_len = 0;
  40588. size_t i = 0;
  40589. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  40590. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  40591. AssertNotNull(curves = (EC_builtin_curve*)
  40592. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  40593. DYNAMIC_TYPE_TMP_BUFFER));
  40594. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  40595. for (i = 0; i < crv_len; i++)
  40596. {
  40597. if (curves[i].comment != NULL)
  40598. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  40599. }
  40600. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40601. printf(resultFmt, passed);
  40602. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  40603. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  40604. return 0;
  40605. }
  40606. static int test_no_op_functions(void)
  40607. {
  40608. #if defined(OPENSSL_EXTRA)
  40609. printf(testingFmt, "no_op_functions()");
  40610. /* this makes sure wolfSSL can compile and run these no-op functions */
  40611. SSL_load_error_strings();
  40612. ENGINE_load_builtin_engines();
  40613. OpenSSL_add_all_ciphers();
  40614. AssertIntEQ(CRYPTO_malloc_init(), 0);
  40615. printf(resultFmt, passed);
  40616. #endif
  40617. return 0;
  40618. }
  40619. static int test_wolfSSL_CRYPTO_memcmp(void)
  40620. {
  40621. #ifdef OPENSSL_EXTRA
  40622. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  40623. "implementation of TLS/SSL for embedded devices to the cloud.";
  40624. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  40625. "implementation of TLS/SSL for embedded devices to the cloud.";
  40626. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  40627. "implementation of TLS/SSL for embedded devices to the cloud!";
  40628. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  40629. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  40630. #endif
  40631. return 0;
  40632. }
  40633. /*----------------------------------------------------------------------------*
  40634. | wolfCrypt ASN
  40635. *----------------------------------------------------------------------------*/
  40636. static int test_wc_CreateEncryptedPKCS8Key(void)
  40637. {
  40638. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  40639. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  40640. WC_RNG rng;
  40641. byte* encKey = NULL;
  40642. word32 encKeySz = 0;
  40643. word32 decKeySz = 0;
  40644. const char password[] = "Lorem ipsum dolor sit amet";
  40645. word32 passwordSz = (word32)XSTRLEN(password);
  40646. word32 tradIdx = 0;
  40647. printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
  40648. AssertIntEQ(wc_InitRng(&rng), 0);
  40649. /* Call with NULL for out buffer to get necessary length. */
  40650. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  40651. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  40652. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  40653. LENGTH_ONLY_E);
  40654. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  40655. DYNAMIC_TYPE_TMP_BUFFER));
  40656. /* Call with the allocated out buffer. */
  40657. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  40658. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  40659. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  40660. 0);
  40661. /* Decrypt the encrypted PKCS8 key we just made. */
  40662. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  40663. passwordSz)), 0);
  40664. /* encKey now holds the decrypted key (decrypted in place). */
  40665. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  40666. /* Check that the decrypted key matches the key prior to encryption. */
  40667. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  40668. sizeof_server_key_der_2048), 0);
  40669. if (encKey != NULL)
  40670. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40671. wc_FreeRng(&rng);
  40672. printf(resultFmt, passed);
  40673. #endif
  40674. return 0;
  40675. }
  40676. static int test_wc_GetPkcs8TraditionalOffset(void)
  40677. {
  40678. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  40679. int length, derSz;
  40680. word32 inOutIdx;
  40681. const char* path = "./certs/server-keyPkcs8.der";
  40682. XFILE file;
  40683. byte der[2048];
  40684. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  40685. file = XFOPEN(path, "rb");
  40686. AssertTrue(file != XBADFILE);
  40687. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  40688. XFCLOSE(file);
  40689. /* valid case */
  40690. inOutIdx = 0;
  40691. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  40692. AssertIntGT(length, 0);
  40693. /* inOutIdx > sz */
  40694. inOutIdx = 4000;
  40695. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  40696. AssertIntEQ(length, BAD_FUNC_ARG);
  40697. /* null input */
  40698. inOutIdx = 0;
  40699. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  40700. AssertIntEQ(length, BAD_FUNC_ARG);
  40701. /* invalid input, fill buffer with 1's */
  40702. XMEMSET(der, 1, sizeof(der));
  40703. inOutIdx = 0;
  40704. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  40705. AssertIntEQ(length, ASN_PARSE_E);
  40706. printf(resultFmt, passed);
  40707. #endif /* NO_ASN */
  40708. return 0;
  40709. }
  40710. static int test_wc_SetSubjectRaw(void)
  40711. {
  40712. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40713. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  40714. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  40715. WOLFSSL_X509* x509;
  40716. int peerCertSz;
  40717. const byte* peerCertBuf;
  40718. Cert forgedCert;
  40719. printf(testingFmt, "test_wc_SetSubjectRaw()");
  40720. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  40721. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  40722. AssertIntEQ(0, wc_InitCert(&forgedCert));
  40723. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  40724. wolfSSL_FreeX509(x509);
  40725. printf(resultFmt, passed);
  40726. #endif
  40727. return 0;
  40728. }
  40729. static int test_wc_GetSubjectRaw(void)
  40730. {
  40731. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40732. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  40733. Cert cert;
  40734. byte *subjectRaw;
  40735. printf(testingFmt, "test_wc_GetSubjectRaw()");
  40736. AssertIntEQ(0, wc_InitCert(&cert));
  40737. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  40738. printf(resultFmt, passed);
  40739. #endif
  40740. return 0;
  40741. }
  40742. static int test_wc_SetIssuerRaw(void)
  40743. {
  40744. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40745. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  40746. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  40747. WOLFSSL_X509* x509;
  40748. int peerCertSz;
  40749. const byte* peerCertBuf;
  40750. Cert forgedCert;
  40751. printf(testingFmt, "test_wc_SetIssuerRaw()");
  40752. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  40753. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  40754. AssertIntEQ(0, wc_InitCert(&forgedCert));
  40755. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  40756. wolfSSL_FreeX509(x509);
  40757. printf(resultFmt, passed);
  40758. #endif
  40759. return 0;
  40760. }
  40761. static int test_wc_SetIssueBuffer(void)
  40762. {
  40763. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40764. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  40765. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  40766. WOLFSSL_X509* x509;
  40767. int peerCertSz;
  40768. const byte* peerCertBuf;
  40769. Cert forgedCert;
  40770. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  40771. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  40772. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  40773. AssertIntEQ(0, wc_InitCert(&forgedCert));
  40774. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  40775. wolfSSL_FreeX509(x509);
  40776. printf(resultFmt, passed);
  40777. #endif
  40778. return 0;
  40779. }
  40780. /*
  40781. * Testing wc_SetSubjectKeyId
  40782. */
  40783. static int test_wc_SetSubjectKeyId(void)
  40784. {
  40785. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40786. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  40787. Cert cert;
  40788. const char* file = "certs/ecc-client-keyPub.pem";
  40789. printf(testingFmt, "wc_SetSubjectKeyId()");
  40790. AssertIntEQ(0, wc_InitCert(&cert));
  40791. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  40792. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  40793. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  40794. printf(resultFmt, passed);
  40795. #endif
  40796. return 0;
  40797. } /* END test_wc_SetSubjectKeyId */
  40798. /*
  40799. * Testing wc_SetSubject
  40800. */
  40801. static int test_wc_SetSubject(void)
  40802. {
  40803. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  40804. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  40805. Cert cert;
  40806. const char* file = "./certs/ca-ecc-cert.pem";
  40807. printf(testingFmt, "wc_SetSubject()");
  40808. AssertIntEQ(0, wc_InitCert(&cert));
  40809. AssertIntEQ(0, wc_SetSubject(&cert, file));
  40810. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  40811. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  40812. printf(resultFmt, passed);
  40813. #endif
  40814. return 0;
  40815. } /* END test_wc_SetSubject */
  40816. static int test_CheckCertSignature(void)
  40817. {
  40818. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  40819. WOLFSSL_CERT_MANAGER* cm = NULL;
  40820. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  40821. FILE* fp;
  40822. byte cert[4096];
  40823. int certSz;
  40824. #endif
  40825. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  40826. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  40827. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  40828. #ifndef NO_RSA
  40829. #ifdef USE_CERT_BUFFERS_1024
  40830. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  40831. sizeof_server_cert_der_1024, NULL, cm));
  40832. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  40833. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  40834. WOLFSSL_FILETYPE_ASN1));
  40835. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  40836. sizeof_server_cert_der_1024, NULL, cm));
  40837. #elif defined(USE_CERT_BUFFERS_2048)
  40838. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  40839. sizeof_server_cert_der_2048, NULL, cm));
  40840. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  40841. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  40842. WOLFSSL_FILETYPE_ASN1));
  40843. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  40844. sizeof_server_cert_der_2048, NULL, cm));
  40845. #endif
  40846. #endif
  40847. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40848. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  40849. sizeof_serv_ecc_der_256, NULL, cm));
  40850. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  40851. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  40852. WOLFSSL_FILETYPE_ASN1));
  40853. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  40854. NULL, cm));
  40855. #endif
  40856. #if !defined(NO_FILESYSTEM)
  40857. wolfSSL_CertManagerFree(cm);
  40858. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  40859. #ifndef NO_RSA
  40860. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  40861. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  40862. XFCLOSE(fp);
  40863. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  40864. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  40865. "./certs/ca-cert.pem", NULL));
  40866. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  40867. #endif
  40868. #ifdef HAVE_ECC
  40869. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  40870. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  40871. XFCLOSE(fp);
  40872. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  40873. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  40874. "./certs/ca-ecc-cert.pem", NULL));
  40875. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  40876. #endif
  40877. #endif
  40878. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  40879. (void)fp;
  40880. (void)cert;
  40881. (void)certSz;
  40882. #endif
  40883. wolfSSL_CertManagerFree(cm);
  40884. #endif
  40885. return 0;
  40886. }
  40887. static int test_wc_ParseCert(void)
  40888. {
  40889. #if !defined(NO_CERTS) && !defined(NO_RSA)
  40890. DecodedCert decodedCert;
  40891. const byte* rawCert = client_cert_der_2048;
  40892. const int rawCertSize = sizeof_client_cert_der_2048;
  40893. printf(testingFmt, "wc_ParseCert");
  40894. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  40895. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  40896. #ifndef IGNORE_NAME_CONSTRAINTS
  40897. /* check that the subjects emailAddress was not put in the alt name list */
  40898. AssertNotNull(decodedCert.subjectEmail);
  40899. AssertNull(decodedCert.altEmailNames);
  40900. #endif
  40901. wc_FreeDecodedCert(&decodedCert);
  40902. printf(resultFmt, passed);
  40903. #endif
  40904. return 0;
  40905. }
  40906. static int test_MakeCertWithPathLen(void)
  40907. {
  40908. #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  40909. const byte expectedPathLen = 7;
  40910. Cert cert;
  40911. DecodedCert decodedCert;
  40912. byte der[FOURK_BUF];
  40913. int derSize = 0;
  40914. WC_RNG rng;
  40915. ecc_key key;
  40916. printf(testingFmt, "test_MakeCertWithPathLen");
  40917. AssertIntEQ(wc_InitRng(&rng), 0);
  40918. AssertIntEQ(wc_ecc_init(&key), 0);
  40919. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  40920. AssertIntEQ(wc_InitCert(&cert), 0);
  40921. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  40922. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  40923. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  40924. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  40925. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  40926. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  40927. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  40928. cert.selfSigned = 1;
  40929. cert.isCA = 1;
  40930. cert.pathLen = expectedPathLen;
  40931. cert.pathLenSet = 1;
  40932. cert.sigType = CTC_SHA256wECDSA;
  40933. #ifdef WOLFSSL_CERT_EXT
  40934. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  40935. #endif
  40936. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  40937. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  40938. &key, &rng);
  40939. AssertIntGE(derSize, 0);
  40940. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  40941. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  40942. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  40943. wc_FreeDecodedCert(&decodedCert);
  40944. AssertIntEQ(wc_ecc_free(&key), 0);
  40945. AssertIntEQ(wc_FreeRng(&rng), 0);
  40946. printf(resultFmt, passed);
  40947. #endif
  40948. return 0;
  40949. }
  40950. /*----------------------------------------------------------------------------*
  40951. | wolfCrypt ECC
  40952. *----------------------------------------------------------------------------*/
  40953. static int test_wc_ecc_get_curve_size_from_name(void)
  40954. {
  40955. #ifdef HAVE_ECC
  40956. int ret;
  40957. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  40958. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40959. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  40960. AssertIntEQ(ret, 32);
  40961. #endif
  40962. /* invalid case */
  40963. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  40964. AssertIntEQ(ret, -1);
  40965. /* NULL input */
  40966. ret = wc_ecc_get_curve_size_from_name(NULL);
  40967. AssertIntEQ(ret, BAD_FUNC_ARG);
  40968. printf(resultFmt, passed);
  40969. #endif /* HAVE_ECC */
  40970. return 0;
  40971. }
  40972. static int test_wc_ecc_get_curve_id_from_name(void)
  40973. {
  40974. #ifdef HAVE_ECC
  40975. int id;
  40976. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  40977. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40978. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  40979. AssertIntEQ(id, ECC_SECP256R1);
  40980. #endif
  40981. /* invalid case */
  40982. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  40983. AssertIntEQ(id, -1);
  40984. /* NULL input */
  40985. id = wc_ecc_get_curve_id_from_name(NULL);
  40986. AssertIntEQ(id, BAD_FUNC_ARG);
  40987. printf(resultFmt, passed);
  40988. #endif /* HAVE_ECC */
  40989. return 0;
  40990. }
  40991. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  40992. !defined(HAVE_SELFTEST) && \
  40993. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  40994. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  40995. {
  40996. int id;
  40997. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  40998. int curve_id;
  40999. ecc_key* key;
  41000. const ecc_set_type* params;
  41001. int ret;
  41002. #endif
  41003. WOLFSSL_EC_KEY *ecKey = NULL;
  41004. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  41005. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41006. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41007. AssertIntEQ(id, ECC_SECP256R1);
  41008. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  41009. AssertNotNull(ecKey);
  41010. ret = EC_KEY_generate_key(ecKey);
  41011. if (ret == 0) {
  41012. /* normal test */
  41013. key = (ecc_key*)ecKey->internal;
  41014. params = key->dp;
  41015. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  41016. AssertIntEQ(curve_id, id);
  41017. }
  41018. #endif
  41019. /* invalid case, NULL input*/
  41020. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  41021. AssertIntEQ(id, BAD_FUNC_ARG);
  41022. wolfSSL_EC_KEY_free(ecKey);
  41023. printf(resultFmt, passed);
  41024. return 0;
  41025. }
  41026. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  41027. static int test_wc_ecc_get_curve_id_from_params(void)
  41028. {
  41029. #ifdef HAVE_ECC
  41030. int id;
  41031. const byte prime[] =
  41032. {
  41033. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41034. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41035. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41036. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  41037. };
  41038. const byte primeInvalid[] =
  41039. {
  41040. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41041. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41042. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41043. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  41044. };
  41045. const byte Af[] =
  41046. {
  41047. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41048. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41049. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41050. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  41051. };
  41052. const byte Bf[] =
  41053. {
  41054. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  41055. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  41056. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  41057. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  41058. };
  41059. const byte order[] =
  41060. {
  41061. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  41062. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41063. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  41064. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  41065. };
  41066. const byte Gx[] =
  41067. {
  41068. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  41069. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  41070. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  41071. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  41072. };
  41073. const byte Gy[] =
  41074. {
  41075. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  41076. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  41077. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  41078. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  41079. };
  41080. int cofactor = 1;
  41081. int fieldSize = 256;
  41082. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  41083. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41084. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  41085. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41086. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41087. AssertIntEQ(id, ECC_SECP256R1);
  41088. #endif
  41089. /* invalid case, fieldSize = 0 */
  41090. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  41091. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41092. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41093. AssertIntEQ(id, ECC_CURVE_INVALID);
  41094. /* invalid case, NULL prime */
  41095. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  41096. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41097. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41098. AssertIntEQ(id, BAD_FUNC_ARG);
  41099. /* invalid case, invalid prime */
  41100. id = wc_ecc_get_curve_id_from_params(fieldSize,
  41101. primeInvalid, sizeof(primeInvalid),
  41102. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41103. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41104. AssertIntEQ(id, ECC_CURVE_INVALID);
  41105. printf(resultFmt, passed);
  41106. #endif
  41107. return 0;
  41108. }
  41109. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  41110. {
  41111. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41112. !defined(HAVE_FAST_RSA)
  41113. WOLFSSL_RSA* rsa = NULL;
  41114. WOLFSSL_EVP_PKEY* pkey = NULL;
  41115. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41116. const char* in = "What is easy to do is easy not to do.";
  41117. size_t inlen = XSTRLEN(in);
  41118. size_t outEncLen = 0;
  41119. byte* outEnc = NULL;
  41120. byte* outDec = NULL;
  41121. size_t outDecLen = 0;
  41122. size_t rsaKeySz = 2048/8; /* Bytes */
  41123. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41124. byte* inTmp = NULL;
  41125. byte* outEncTmp = NULL;
  41126. byte* outDecTmp = NULL;
  41127. #endif
  41128. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  41129. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41130. XMEMSET(outEnc, 0, rsaKeySz);
  41131. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41132. XMEMSET(outDec, 0, rsaKeySz);
  41133. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41134. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41135. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41136. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41137. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41138. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41139. WOLFSSL_SUCCESS);
  41140. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  41141. since ctx uses it.*/
  41142. AssertIntEQ(pkey->references, 2);
  41143. EVP_PKEY_free(pkey);
  41144. AssertIntEQ(pkey->references, 1);
  41145. /* Encrypt data */
  41146. /* Check that we can get the required output buffer length by passing in a
  41147. * NULL output buffer. */
  41148. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  41149. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41150. AssertIntEQ(rsaKeySz, outEncLen);
  41151. /* Now do the actual encryption. */
  41152. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  41153. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41154. /* Decrypt data */
  41155. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41156. /* Check that we can get the required output buffer length by passing in a
  41157. * NULL output buffer. */
  41158. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  41159. WOLFSSL_SUCCESS);
  41160. AssertIntEQ(rsaKeySz, outDecLen);
  41161. /* Now do the actual decryption. */
  41162. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  41163. WOLFSSL_SUCCESS);
  41164. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  41165. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41166. /* The input length must be the same size as the RSA key.*/
  41167. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41168. XMEMSET(inTmp, 9, rsaKeySz);
  41169. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41170. XMEMSET(outEncTmp, 0, rsaKeySz);
  41171. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41172. XMEMSET(outDecTmp, 0, rsaKeySz);
  41173. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41174. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41175. WOLFSSL_SUCCESS);
  41176. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  41177. WOLFSSL_SUCCESS);
  41178. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41179. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  41180. WOLFSSL_SUCCESS);
  41181. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  41182. #endif
  41183. EVP_PKEY_CTX_free(ctx);
  41184. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41185. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41186. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41187. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41188. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41189. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41190. #endif
  41191. printf(resultFmt, passed);
  41192. #endif
  41193. return 0;
  41194. }
  41195. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  41196. {
  41197. #if defined(OPENSSL_EXTRA)
  41198. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41199. WOLFSSL_DSA* dsa = NULL;
  41200. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41201. WOLFSSL_EVP_PKEY* pkey = NULL;
  41202. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41203. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  41204. const char* in = "What is easy to do is easy not to do.";
  41205. size_t inlen = XSTRLEN(in);
  41206. byte hash[SHA256_DIGEST_LENGTH] = {0};
  41207. byte zero[SHA256_DIGEST_LENGTH] = {0};
  41208. SHA256_CTX c;
  41209. byte* sig = NULL;
  41210. byte* sigVerify = NULL;
  41211. size_t siglen;
  41212. size_t siglenOnlyLen;
  41213. size_t keySz = 2048/8; /* Bytes */
  41214. int i;
  41215. int encs[3] = {0};
  41216. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41217. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41218. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41219. encs[0] = EVP_PKEY_RSA;
  41220. #endif
  41221. #endif
  41222. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41223. encs[1] = EVP_PKEY_DSA;
  41224. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41225. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41226. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41227. encs[2] = EVP_PKEY_EC;
  41228. #endif
  41229. #endif
  41230. printf(testingFmt, "wolfSSL_EVP_PKEY_sign_verify()");
  41231. AssertNotNull(sig =
  41232. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41233. AssertNotNull(sigVerify =
  41234. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41235. for (i = 0; i < 3; i++) {
  41236. if (encs[i] == 0)
  41237. continue;
  41238. siglen = keySz;
  41239. XMEMSET(sig, 0, keySz);
  41240. XMEMSET(sigVerify, 0, keySz);
  41241. /* Generate hash */
  41242. SHA256_Init(&c);
  41243. SHA256_Update(&c, in, inlen);
  41244. SHA256_Final(hash, &c);
  41245. #ifdef WOLFSSL_SMALL_STACK_CACHE
  41246. /* workaround for small stack cache case */
  41247. wc_Sha256Free((wc_Sha256*)&c);
  41248. #endif
  41249. /* Generate key */
  41250. AssertNotNull(pkey = EVP_PKEY_new());
  41251. switch (encs[i]) {
  41252. case EVP_PKEY_RSA:
  41253. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41254. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41255. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41256. {
  41257. WOLFSSL_RSA* rsa = NULL;
  41258. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41259. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41260. }
  41261. #endif
  41262. #endif
  41263. break;
  41264. case EVP_PKEY_DSA:
  41265. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41266. AssertNotNull(dsa = DSA_new());
  41267. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41268. NULL, 0, NULL, NULL, NULL), 1);
  41269. AssertIntEQ(DSA_generate_key(dsa), 1);
  41270. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  41271. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41272. break;
  41273. case EVP_PKEY_EC:
  41274. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41275. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41276. {
  41277. WOLFSSL_EC_KEY* ecKey = NULL;
  41278. AssertNotNull(ecKey = EC_KEY_new());
  41279. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  41280. AssertIntEQ(
  41281. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41282. }
  41283. #endif
  41284. #endif
  41285. break;
  41286. }
  41287. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41288. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41289. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41290. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41291. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41292. if (encs[i] == EVP_PKEY_RSA)
  41293. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41294. WOLFSSL_SUCCESS);
  41295. #endif
  41296. #endif
  41297. /* Check returning only length */
  41298. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  41299. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41300. AssertIntGT(siglenOnlyLen, 0);
  41301. /* Sign data */
  41302. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  41303. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41304. AssertIntGE(siglenOnlyLen, siglen);
  41305. /* Verify signature */
  41306. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  41307. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41308. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41309. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41310. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41311. if (encs[i] == EVP_PKEY_RSA)
  41312. AssertIntEQ(
  41313. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  41314. WOLFSSL_SUCCESS);
  41315. #endif
  41316. #endif
  41317. AssertIntEQ(EVP_PKEY_verify(
  41318. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  41319. WOLFSSL_SUCCESS);
  41320. AssertIntEQ(EVP_PKEY_verify(
  41321. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  41322. WOLFSSL_FAILURE);
  41323. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41324. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41325. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41326. if (encs[i] == EVP_PKEY_RSA) {
  41327. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  41328. /* Try RSA sign/verify with no padding. */
  41329. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41330. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41331. WOLFSSL_SUCCESS);
  41332. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41333. siglen), WOLFSSL_SUCCESS);
  41334. AssertIntGE(siglenOnlyLen, siglen);
  41335. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41336. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41337. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  41338. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41339. siglen), WOLFSSL_SUCCESS);
  41340. #endif
  41341. /* Wrong padding schemes. */
  41342. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41343. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  41344. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41345. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41346. siglen), WOLFSSL_SUCCESS);
  41347. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41348. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41349. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41350. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41351. siglen), WOLFSSL_SUCCESS);
  41352. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41353. WOLFSSL_SUCCESS);
  41354. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41355. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  41356. }
  41357. #endif
  41358. #endif
  41359. /* error cases */
  41360. siglen = keySz; /* Reset because sig size may vary slightly */
  41361. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  41362. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41363. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  41364. WOLFSSL_SUCCESS);
  41365. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  41366. WOLFSSL_SUCCESS);
  41367. EVP_PKEY_free(pkey);
  41368. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41369. DSA_free(dsa);
  41370. dsa = NULL;
  41371. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41372. EVP_PKEY_CTX_free(ctx_verify);
  41373. EVP_PKEY_CTX_free(ctx);
  41374. }
  41375. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41376. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41377. printf(resultFmt, passed);
  41378. #endif /* OPENSSL_EXTRA */
  41379. return 0;
  41380. }
  41381. static int test_EVP_PKEY_rsa(void)
  41382. {
  41383. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  41384. WOLFSSL_RSA* rsa;
  41385. WOLFSSL_EVP_PKEY* pkey;
  41386. AssertNotNull(rsa = wolfSSL_RSA_new());
  41387. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41388. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  41389. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  41390. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41391. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  41392. wolfSSL_EVP_PKEY_free(pkey);
  41393. printf(resultFmt, passed);
  41394. #endif
  41395. return 0;
  41396. }
  41397. static int test_EVP_PKEY_ec(void)
  41398. {
  41399. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41400. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41401. WOLFSSL_EC_KEY* ecKey;
  41402. WOLFSSL_EVP_PKEY* pkey;
  41403. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  41404. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41405. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41406. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41407. /* Should fail since ecKey is empty */
  41408. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41409. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41410. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41411. wolfSSL_EVP_PKEY_free(pkey);
  41412. printf(resultFmt, passed);
  41413. #endif
  41414. #endif
  41415. return 0;
  41416. }
  41417. static int test_EVP_PKEY_cmp(void)
  41418. {
  41419. #if defined(OPENSSL_EXTRA)
  41420. EVP_PKEY *a, *b;
  41421. const unsigned char *in;
  41422. printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
  41423. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  41424. in = client_key_der_2048;
  41425. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41426. &in, (long)sizeof_client_key_der_2048));
  41427. in = client_key_der_2048;
  41428. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41429. &in, (long)sizeof_client_key_der_2048));
  41430. /* Test success case RSA */
  41431. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41432. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  41433. #else
  41434. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41435. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41436. EVP_PKEY_free(b);
  41437. EVP_PKEY_free(a);
  41438. #endif
  41439. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41440. in = ecc_clikey_der_256;
  41441. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41442. &in, (long)sizeof_ecc_clikey_der_256));
  41443. in = ecc_clikey_der_256;
  41444. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41445. &in, (long)sizeof_ecc_clikey_der_256));
  41446. /* Test success case ECC */
  41447. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41448. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  41449. #else
  41450. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41451. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41452. EVP_PKEY_free(b);
  41453. EVP_PKEY_free(a);
  41454. #endif
  41455. /* Test failure cases */
  41456. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  41457. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41458. in = client_key_der_2048;
  41459. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41460. &in, (long)sizeof_client_key_der_2048));
  41461. in = ecc_clikey_der_256;
  41462. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41463. &in, (long)sizeof_ecc_clikey_der_256));
  41464. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41465. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  41466. #else
  41467. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  41468. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41469. EVP_PKEY_free(b);
  41470. EVP_PKEY_free(a);
  41471. #endif
  41472. /* invalid or empty failure cases */
  41473. a = EVP_PKEY_new();
  41474. b = EVP_PKEY_new();
  41475. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41476. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  41477. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  41478. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  41479. #ifdef NO_RSA
  41480. /* Type check will fail since RSA is the default EVP key type */
  41481. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  41482. #else
  41483. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41484. #endif
  41485. #else
  41486. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  41487. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  41488. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  41489. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  41490. #endif
  41491. EVP_PKEY_free(b);
  41492. EVP_PKEY_free(a);
  41493. (void)in;
  41494. printf(resultFmt, passed);
  41495. #endif
  41496. return 0;
  41497. }
  41498. static int test_ERR_load_crypto_strings(void)
  41499. {
  41500. #if defined(OPENSSL_ALL)
  41501. ERR_load_crypto_strings();
  41502. printf(resultFmt, passed);
  41503. #endif
  41504. return 0;
  41505. }
  41506. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  41507. static void free_x509(X509* x)
  41508. {
  41509. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  41510. }
  41511. #endif
  41512. static int test_sk_X509(void)
  41513. {
  41514. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  41515. STACK_OF(X509)* s;
  41516. AssertNotNull(s = sk_X509_new());
  41517. AssertIntEQ(sk_X509_num(s), 0);
  41518. sk_X509_pop_free(s, NULL);
  41519. AssertNotNull(s = sk_X509_new_null());
  41520. AssertIntEQ(sk_X509_num(s), 0);
  41521. sk_X509_pop_free(s, NULL);
  41522. AssertNotNull(s = sk_X509_new());
  41523. sk_X509_push(s, (X509*)1);
  41524. AssertIntEQ(sk_X509_num(s), 1);
  41525. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  41526. sk_X509_push(s, (X509*)2);
  41527. AssertIntEQ(sk_X509_num(s), 2);
  41528. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  41529. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  41530. sk_X509_push(s, (X509*)2);
  41531. sk_X509_pop_free(s, free_x509);
  41532. printf(resultFmt, passed);
  41533. #endif
  41534. return 0;
  41535. }
  41536. static int test_sk_X509_CRL(void)
  41537. {
  41538. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  41539. X509_CRL* crl;
  41540. XFILE fp;
  41541. STACK_OF(X509_CRL)* s;
  41542. printf(testingFmt, "test_sk_X509_CRL");
  41543. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  41544. AssertTrue((fp != XBADFILE));
  41545. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  41546. XFCLOSE(fp);
  41547. AssertNotNull(s = sk_X509_CRL_new());
  41548. AssertIntEQ(sk_X509_CRL_num(s), 0);
  41549. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  41550. AssertIntEQ(sk_X509_CRL_num(s), 1);
  41551. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  41552. sk_X509_CRL_free(s);
  41553. printf(resultFmt, passed);
  41554. #endif
  41555. return 0;
  41556. }
  41557. static int test_X509_get_signature_nid(void)
  41558. {
  41559. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41560. X509* x509;
  41561. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  41562. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  41563. SSL_FILETYPE_PEM));
  41564. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  41565. X509_free(x509);
  41566. printf(resultFmt, passed);
  41567. #endif
  41568. return 0;
  41569. }
  41570. static int test_X509_REQ(void)
  41571. {
  41572. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  41573. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  41574. X509_NAME* name;
  41575. #ifndef NO_RSA
  41576. X509_NAME* subject;
  41577. #endif
  41578. #if !defined(NO_RSA) || defined(HAVE_ECC)
  41579. X509_REQ* req;
  41580. EVP_PKEY* priv;
  41581. EVP_PKEY* pub;
  41582. unsigned char* der = NULL;
  41583. int len;
  41584. #endif
  41585. #ifndef NO_RSA
  41586. EVP_MD_CTX *mctx = NULL;
  41587. EVP_PKEY_CTX *pkctx = NULL;
  41588. #ifdef USE_CERT_BUFFERS_1024
  41589. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  41590. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  41591. #elif defined(USE_CERT_BUFFERS_2048)
  41592. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  41593. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  41594. #endif
  41595. #endif
  41596. #ifdef HAVE_ECC
  41597. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  41598. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  41599. #endif
  41600. AssertNotNull(name = X509_NAME_new());
  41601. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  41602. (byte*)"wolfssl.com", 11, 0, 1),
  41603. WOLFSSL_SUCCESS);
  41604. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  41605. (byte*)"support@wolfssl.com", 19, -1,
  41606. 1), WOLFSSL_SUCCESS);
  41607. #ifndef NO_RSA
  41608. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  41609. (long)sizeof_client_key_der_2048));
  41610. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  41611. (long)sizeof_client_keypub_der_2048));
  41612. AssertNotNull(req = X509_REQ_new());
  41613. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  41614. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  41615. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  41616. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  41617. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  41618. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  41619. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  41620. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  41621. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  41622. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  41623. len = i2d_X509_REQ(req, &der);
  41624. DEBUG_WRITE_DER(der, len, "req.der");
  41625. #ifdef USE_CERT_BUFFERS_1024
  41626. AssertIntEQ(len, 381);
  41627. #else
  41628. AssertIntEQ(len, 643);
  41629. #endif
  41630. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  41631. der = NULL;
  41632. mctx = EVP_MD_CTX_new();
  41633. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  41634. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  41635. EVP_MD_CTX_free(mctx);
  41636. X509_REQ_free(NULL);
  41637. X509_REQ_free(req);
  41638. /* Test getting the subject from a newly created X509_REQ */
  41639. AssertNotNull(req = X509_REQ_new());
  41640. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  41641. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  41642. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  41643. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  41644. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  41645. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  41646. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  41647. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  41648. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  41649. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  41650. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  41651. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  41652. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  41653. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  41654. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  41655. len = i2d_X509_REQ(req, &der);
  41656. DEBUG_WRITE_DER(der, len, "req2.der");
  41657. #ifdef USE_CERT_BUFFERS_1024
  41658. AssertIntEQ(len, 435);
  41659. #else
  41660. AssertIntEQ(len, 696);
  41661. #endif
  41662. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  41663. der = NULL;
  41664. EVP_PKEY_free(pub);
  41665. EVP_PKEY_free(priv);
  41666. X509_REQ_free(req);
  41667. #endif
  41668. #ifdef HAVE_ECC
  41669. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  41670. sizeof_ecc_clikey_der_256));
  41671. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  41672. sizeof_ecc_clikeypub_der_256));
  41673. AssertNotNull(req = X509_REQ_new());
  41674. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  41675. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  41676. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  41677. /* Signature is random and may be shorter or longer. */
  41678. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  41679. AssertIntLE(len, 253);
  41680. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  41681. X509_REQ_free(req);
  41682. EVP_PKEY_free(pub);
  41683. EVP_PKEY_free(priv);
  41684. #ifdef FP_ECC
  41685. wc_ecc_fp_free();
  41686. #endif
  41687. #endif /* HAVE_ECC */
  41688. X509_NAME_free(name);
  41689. printf(resultFmt, passed);
  41690. #endif
  41691. return 0;
  41692. }
  41693. static int test_wolfssl_PKCS7(void)
  41694. {
  41695. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
  41696. PKCS7* pkcs7;
  41697. byte data[FOURK_BUF];
  41698. word32 len = sizeof(data);
  41699. const byte* p = data;
  41700. byte content[] = "Test data to encode.";
  41701. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  41702. BIO* bio;
  41703. byte key[sizeof(client_key_der_2048)];
  41704. word32 keySz = (word32)sizeof(key);
  41705. byte* out = NULL;
  41706. #endif
  41707. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  41708. (word32)sizeof(content),
  41709. 0, 0)), 0);
  41710. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  41711. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  41712. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  41713. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  41714. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  41715. PKCS7_free(pkcs7);
  41716. /* fail case, without PKCS7_NOVERIFY */
  41717. p = data;
  41718. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  41719. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  41720. 0), WOLFSSL_FAILURE);
  41721. PKCS7_free(pkcs7);
  41722. /* success case, with PKCS7_NOVERIFY */
  41723. p = data;
  41724. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  41725. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  41726. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  41727. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  41728. /* test i2d */
  41729. XMEMCPY(key, client_key_der_2048, keySz);
  41730. pkcs7->privateKey = key;
  41731. pkcs7->privateKeySz = (word32)sizeof(key);
  41732. pkcs7->encryptOID = RSAk;
  41733. pkcs7->hashOID = SHAh;
  41734. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  41735. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  41736. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  41737. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41738. BIO_free(bio);
  41739. #endif
  41740. PKCS7_free(NULL);
  41741. PKCS7_free(pkcs7);
  41742. printf(resultFmt, passed);
  41743. #endif
  41744. return 0;
  41745. }
  41746. static int test_wolfSSL_PKCS7_sign(void)
  41747. {
  41748. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  41749. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  41750. PKCS7* p7 = NULL;
  41751. PKCS7* p7Ver = NULL;
  41752. byte* out = NULL;
  41753. byte* tmpPtr = NULL;
  41754. int outLen = 0;
  41755. int flags = 0;
  41756. byte data[] = "Test data to encode.";
  41757. const char* cert = "./certs/server-cert.pem";
  41758. const char* key = "./certs/server-key.pem";
  41759. const char* ca = "./certs/ca-cert.pem";
  41760. WOLFSSL_BIO* certBio = NULL;
  41761. WOLFSSL_BIO* keyBio = NULL;
  41762. WOLFSSL_BIO* caBio = NULL;
  41763. WOLFSSL_BIO* inBio = NULL;
  41764. X509* signCert = NULL;
  41765. EVP_PKEY* signKey = NULL;
  41766. X509* caCert = NULL;
  41767. X509_STORE* store = NULL;
  41768. printf(testingFmt, "wolfSSL_PKCS7_sign()");
  41769. /* read signer cert/key into BIO */
  41770. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  41771. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  41772. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  41773. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  41774. /* read CA cert into store (for verify) */
  41775. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  41776. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  41777. AssertNotNull(store = X509_STORE_new());
  41778. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  41779. /* data to be signed into BIO */
  41780. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41781. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41782. /* PKCS7_sign, bad args: signer NULL */
  41783. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  41784. /* PKCS7_sign, bad args: signer key NULL */
  41785. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  41786. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  41787. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  41788. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  41789. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  41790. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  41791. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  41792. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  41793. {
  41794. flags = PKCS7_BINARY;
  41795. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41796. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41797. /* verify with d2i_PKCS7 */
  41798. tmpPtr = out;
  41799. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  41800. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  41801. PKCS7_free(p7Ver);
  41802. /* verify with wc_PKCS7_VerifySignedData */
  41803. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  41804. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  41805. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  41806. /* compare the signer found to expected signer */
  41807. AssertIntNE(p7Ver->verifyCertSz, 0);
  41808. tmpPtr = NULL;
  41809. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  41810. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  41811. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  41812. tmpPtr = NULL;
  41813. wc_PKCS7_Free(p7Ver);
  41814. AssertNotNull(out);
  41815. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41816. out = NULL;
  41817. PKCS7_free(p7);
  41818. }
  41819. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  41820. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  41821. {
  41822. /* re-populate input BIO, may have been consumed */
  41823. BIO_free(inBio);
  41824. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41825. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41826. flags = PKCS7_BINARY | PKCS7_STREAM;
  41827. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41828. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  41829. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41830. /* PKCS7_final, bad args: PKCS7 null */
  41831. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  41832. /* PKCS7_final, bad args: PKCS7 null */
  41833. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  41834. tmpPtr = out;
  41835. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  41836. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  41837. PKCS7_free(p7Ver);
  41838. AssertNotNull(out);
  41839. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41840. out = NULL;
  41841. PKCS7_free(p7);
  41842. }
  41843. /* TEST SUCCESS: Detached, not streaming, not MIME */
  41844. {
  41845. /* re-populate input BIO, may have been consumed */
  41846. BIO_free(inBio);
  41847. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41848. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41849. flags = PKCS7_BINARY | PKCS7_DETACHED;
  41850. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41851. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41852. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  41853. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  41854. p7Ver->content = data;
  41855. p7Ver->contentSz = sizeof(data);
  41856. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  41857. wc_PKCS7_Free(p7Ver);
  41858. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  41859. * yet support detached content */
  41860. tmpPtr = out;
  41861. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  41862. PKCS7_free(p7Ver);
  41863. AssertNotNull(out);
  41864. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41865. out = NULL;
  41866. PKCS7_free(p7);
  41867. }
  41868. /* TEST SUCCESS: Detached, streaming, not MIME */
  41869. {
  41870. /* re-populate input BIO, may have been consumed */
  41871. BIO_free(inBio);
  41872. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  41873. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  41874. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  41875. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  41876. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  41877. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  41878. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  41879. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  41880. p7Ver->content = data;
  41881. p7Ver->contentSz = sizeof(data);
  41882. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  41883. wc_PKCS7_Free(p7Ver);
  41884. AssertNotNull(out);
  41885. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41886. PKCS7_free(p7);
  41887. }
  41888. X509_STORE_free(store);
  41889. X509_free(caCert);
  41890. X509_free(signCert);
  41891. EVP_PKEY_free(signKey);
  41892. BIO_free(inBio);
  41893. BIO_free(keyBio);
  41894. BIO_free(certBio);
  41895. BIO_free(caBio);
  41896. printf(resultFmt, passed);
  41897. #endif
  41898. return 0;
  41899. }
  41900. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  41901. {
  41902. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  41903. PKCS7_SIGNED* pkcs7;
  41904. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  41905. pkcs7 = PKCS7_SIGNED_new();
  41906. AssertNotNull(pkcs7);
  41907. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  41908. PKCS7_SIGNED_free(pkcs7);
  41909. printf(resultFmt, passed);
  41910. #endif
  41911. return 0;
  41912. }
  41913. #ifndef NO_BIO
  41914. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  41915. {
  41916. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  41917. PKCS7* pkcs7 = NULL;
  41918. BIO* bio = NULL;
  41919. const byte* cert_buf = NULL;
  41920. int ret = 0;
  41921. WC_RNG rng;
  41922. const byte data[] = { /* Hello World */
  41923. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  41924. 0x72,0x6c,0x64
  41925. };
  41926. #ifndef NO_RSA
  41927. #if defined(USE_CERT_BUFFERS_2048)
  41928. byte key[sizeof(client_key_der_2048)];
  41929. byte cert[sizeof(client_cert_der_2048)];
  41930. word32 keySz = (word32)sizeof(key);
  41931. word32 certSz = (word32)sizeof(cert);
  41932. XMEMSET(key, 0, keySz);
  41933. XMEMSET(cert, 0, certSz);
  41934. XMEMCPY(key, client_key_der_2048, keySz);
  41935. XMEMCPY(cert, client_cert_der_2048, certSz);
  41936. #elif defined(USE_CERT_BUFFERS_1024)
  41937. byte key[sizeof_client_key_der_1024];
  41938. byte cert[sizeof(sizeof_client_cert_der_1024)];
  41939. word32 keySz = (word32)sizeof(key);
  41940. word32 certSz = (word32)sizeof(cert);
  41941. XMEMSET(key, 0, keySz);
  41942. XMEMSET(cert, 0, certSz);
  41943. XMEMCPY(key, client_key_der_1024, keySz);
  41944. XMEMCPY(cert, client_cert_der_1024, certSz);
  41945. #else
  41946. unsigned char cert[ONEK_BUF];
  41947. unsigned char key[ONEK_BUF];
  41948. XFILE fp;
  41949. int certSz;
  41950. int keySz;
  41951. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  41952. AssertTrue((fp != XBADFILE));
  41953. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  41954. XFCLOSE(fp);
  41955. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  41956. AssertTrue(fp != XBADFILE);
  41957. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  41958. XFCLOSE(fp);
  41959. #endif
  41960. #elif defined(HAVE_ECC)
  41961. #if defined(USE_CERT_BUFFERS_256)
  41962. unsigned char cert[sizeof(cliecc_cert_der_256)];
  41963. unsigned char key[sizeof(ecc_clikey_der_256)];
  41964. int certSz = (int)sizeof(cert);
  41965. int keySz = (int)sizeof(key);
  41966. XMEMSET(cert, 0, certSz);
  41967. XMEMSET(key, 0, keySz);
  41968. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  41969. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  41970. #else
  41971. unsigned char cert[ONEK_BUF];
  41972. unsigned char key[ONEK_BUF];
  41973. XFILE fp;
  41974. int certSz, keySz;
  41975. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  41976. AssertTrue(fp != XBADFILE);
  41977. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  41978. XFCLOSE(fp);
  41979. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  41980. AssertTrue(fp != XBADFILE);
  41981. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  41982. XFCLOSE(fp);
  41983. #endif
  41984. #else
  41985. #error PKCS7 requires ECC or RSA
  41986. #endif
  41987. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  41988. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  41989. /* initialize with DER encoded cert */
  41990. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  41991. /* init rng */
  41992. AssertIntEQ(wc_InitRng(&rng), 0);
  41993. pkcs7->rng = &rng;
  41994. pkcs7->content = (byte*)data; /* not used for ex */
  41995. pkcs7->contentSz = (word32)sizeof(data);
  41996. pkcs7->contentOID = SIGNED_DATA;
  41997. pkcs7->privateKey = key;
  41998. pkcs7->privateKeySz = (word32)sizeof(key);
  41999. pkcs7->encryptOID = RSAk;
  42000. pkcs7->hashOID = SHAh;
  42001. pkcs7->signedAttribs = NULL;
  42002. pkcs7->signedAttribsSz = 0;
  42003. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42004. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  42005. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  42006. /* Read PKCS#7 PEM from BIO */
  42007. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  42008. AssertIntGE(ret, 0);
  42009. BIO_free(bio);
  42010. wc_PKCS7_Free(pkcs7);
  42011. wc_FreeRng(&rng);
  42012. printf(resultFmt, passed);
  42013. #endif
  42014. return 0;
  42015. }
  42016. #ifdef HAVE_SMIME
  42017. static int test_wolfSSL_SMIME_read_PKCS7(void)
  42018. {
  42019. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  42020. !defined(NO_RSA)
  42021. PKCS7* pkcs7 = NULL;
  42022. BIO* bio = NULL;
  42023. BIO* bcont = NULL;
  42024. BIO* out = NULL;
  42025. const byte* outBuf = NULL;
  42026. int outBufLen = 0;
  42027. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  42028. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  42029. printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
  42030. /* smime-test.p7s */
  42031. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  42032. AssertNotNull(bio);
  42033. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42034. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42035. AssertNotNull(pkcs7);
  42036. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42037. PKCS7_NOVERIFY), SSL_SUCCESS);
  42038. XFCLOSE(smimeTestFile);
  42039. if (bcont) BIO_free(bcont);
  42040. wolfSSL_PKCS7_free(pkcs7);
  42041. /* smime-test-multipart.p7s */
  42042. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  42043. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42044. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42045. AssertNotNull(pkcs7);
  42046. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42047. PKCS7_NOVERIFY), SSL_SUCCESS);
  42048. XFCLOSE(smimeTestFile);
  42049. if (bcont) BIO_free(bcont);
  42050. wolfSSL_PKCS7_free(pkcs7);
  42051. /* smime-test-multipart-badsig.p7s */
  42052. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  42053. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42054. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42055. AssertNull(pkcs7);
  42056. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42057. PKCS7_NOVERIFY), SSL_FAILURE);
  42058. XFCLOSE(smimeTestFile);
  42059. if (bcont) BIO_free(bcont);
  42060. wolfSSL_PKCS7_free(pkcs7);
  42061. /* smime-test-canon.p7s */
  42062. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42063. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42064. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42065. AssertNotNull(pkcs7);
  42066. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42067. PKCS7_NOVERIFY), SSL_SUCCESS);
  42068. XFCLOSE(smimeTestFile);
  42069. if (bcont) BIO_free(bcont);
  42070. wolfSSL_PKCS7_free(pkcs7);
  42071. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  42072. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42073. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42074. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42075. AssertNotNull(pkcs7);
  42076. out = wolfSSL_BIO_new(BIO_s_mem());
  42077. AssertNotNull(out);
  42078. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  42079. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  42080. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  42081. /* Content-Type should not show up at beginning of output buffer */
  42082. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  42083. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  42084. BIO_free(out);
  42085. BIO_free(bio);
  42086. if (bcont) BIO_free(bcont);
  42087. wolfSSL_PKCS7_free(pkcs7);
  42088. printf(resultFmt, passed);
  42089. #endif
  42090. return 0;
  42091. }
  42092. static int test_wolfSSL_SMIME_write_PKCS7(void)
  42093. {
  42094. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  42095. PKCS7* p7 = NULL;
  42096. PKCS7* p7Ver = NULL;
  42097. int flags = 0;
  42098. byte data[] = "Test data to encode.";
  42099. const char* cert = "./certs/server-cert.pem";
  42100. const char* key = "./certs/server-key.pem";
  42101. const char* ca = "./certs/ca-cert.pem";
  42102. WOLFSSL_BIO* certBio = NULL;
  42103. WOLFSSL_BIO* keyBio = NULL;
  42104. WOLFSSL_BIO* caBio = NULL;
  42105. WOLFSSL_BIO* inBio = NULL;
  42106. WOLFSSL_BIO* outBio = NULL;
  42107. WOLFSSL_BIO* content = NULL;
  42108. X509* signCert = NULL;
  42109. EVP_PKEY* signKey = NULL;
  42110. X509* caCert = NULL;
  42111. X509_STORE* store = NULL;
  42112. printf(testingFmt, "wolfSSL_SMIME_write_PKCS7()");
  42113. /* read signer cert/key into BIO */
  42114. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42115. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42116. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42117. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42118. /* read CA cert into store (for verify) */
  42119. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42120. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42121. AssertNotNull(store = X509_STORE_new());
  42122. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42123. /* generate and verify SMIME: not detached */
  42124. {
  42125. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42126. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42127. flags = PKCS7_STREAM;
  42128. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42129. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42130. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42131. /* bad arg: out NULL */
  42132. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  42133. /* bad arg: pkcs7 NULL */
  42134. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  42135. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42136. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42137. BIO_free(content);
  42138. BIO_free(inBio);
  42139. BIO_free(outBio);
  42140. PKCS7_free(p7Ver);
  42141. PKCS7_free(p7);
  42142. }
  42143. /* generate and verify SMIME: not detached, add Content-Type */
  42144. {
  42145. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42146. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42147. flags = PKCS7_STREAM | PKCS7_TEXT;
  42148. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42149. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42150. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42151. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42152. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42153. BIO_free(content);
  42154. BIO_free(inBio);
  42155. BIO_free(outBio);
  42156. PKCS7_free(p7Ver);
  42157. PKCS7_free(p7);
  42158. }
  42159. /* generate and verify SMIME: detached */
  42160. {
  42161. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42162. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42163. flags = PKCS7_DETACHED | PKCS7_STREAM;
  42164. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42165. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42166. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42167. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42168. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42169. BIO_free(content);
  42170. BIO_free(inBio);
  42171. BIO_free(outBio);
  42172. PKCS7_free(p7Ver);
  42173. PKCS7_free(p7);
  42174. }
  42175. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  42176. {
  42177. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42178. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42179. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  42180. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42181. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42182. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42183. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42184. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42185. BIO_free(content);
  42186. BIO_free(inBio);
  42187. BIO_free(outBio);
  42188. PKCS7_free(p7Ver);
  42189. PKCS7_free(p7);
  42190. }
  42191. X509_STORE_free(store);
  42192. X509_free(caCert);
  42193. X509_free(signCert);
  42194. EVP_PKEY_free(signKey);
  42195. BIO_free(keyBio);
  42196. BIO_free(certBio);
  42197. BIO_free(caBio);
  42198. printf(resultFmt, passed);
  42199. #endif
  42200. return 0;
  42201. }
  42202. #endif /* HAVE_SMIME */
  42203. #endif /* !NO_BIO */
  42204. /*----------------------------------------------------------------------------*
  42205. | Certificate Failure Checks
  42206. *----------------------------------------------------------------------------*/
  42207. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  42208. !defined(WOLFSSL_NO_CLIENT_AUTH))
  42209. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  42210. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  42211. int type)
  42212. {
  42213. int ret;
  42214. WOLFSSL_CERT_MANAGER* cm = NULL;
  42215. switch (type) {
  42216. case TESTING_RSA:
  42217. #ifdef NO_RSA
  42218. printf("RSA disabled, skipping test\n");
  42219. return ASN_SIG_CONFIRM_E;
  42220. #else
  42221. break;
  42222. #endif
  42223. case TESTING_ECC:
  42224. #ifndef HAVE_ECC
  42225. printf("ECC disabled, skipping test\n");
  42226. return ASN_SIG_CONFIRM_E;
  42227. #else
  42228. break;
  42229. #endif
  42230. default:
  42231. printf("Bad function argument\n");
  42232. return BAD_FUNC_ARG;
  42233. }
  42234. cm = wolfSSL_CertManagerNew();
  42235. if (cm == NULL) {
  42236. printf("wolfSSL_CertManagerNew failed\n");
  42237. return -1;
  42238. }
  42239. #ifndef NO_FILESYSTEM
  42240. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  42241. if (ret != WOLFSSL_SUCCESS) {
  42242. printf("wolfSSL_CertManagerLoadCA failed\n");
  42243. wolfSSL_CertManagerFree(cm);
  42244. return ret;
  42245. }
  42246. #else
  42247. (void)ca;
  42248. #endif
  42249. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  42250. /* Let AssertIntEQ handle return code */
  42251. wolfSSL_CertManagerFree(cm);
  42252. return ret;
  42253. }
  42254. static int test_RsaSigFailure_cm(void)
  42255. {
  42256. int ret = 0;
  42257. const char* ca_cert = "./certs/ca-cert.pem";
  42258. const char* server_cert = "./certs/server-cert.der";
  42259. byte* cert_buf = NULL;
  42260. size_t cert_sz = 0;
  42261. ret = load_file(server_cert, &cert_buf, &cert_sz);
  42262. if (ret == 0) {
  42263. /* corrupt DER - invert last byte, which is signature */
  42264. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  42265. /* test bad cert */
  42266. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  42267. }
  42268. printf("Signature failure test: RSA: Ret %d\n", ret);
  42269. if (cert_buf)
  42270. free(cert_buf);
  42271. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  42272. if (ret == WOLFSSL_FATAL_ERROR) {
  42273. ret = 0;
  42274. }
  42275. #else
  42276. if (ret == ASN_SIG_CONFIRM_E) {
  42277. ret = 0;
  42278. }
  42279. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  42280. return ret;
  42281. }
  42282. static int test_EccSigFailure_cm(void)
  42283. {
  42284. int ret = 0;
  42285. /* self-signed ECC cert, so use server cert as CA */
  42286. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  42287. const char* server_cert = "./certs/server-ecc.der";
  42288. byte* cert_buf = NULL;
  42289. size_t cert_sz = 0;
  42290. ret = load_file(server_cert, &cert_buf, &cert_sz);
  42291. if (ret == 0) {
  42292. /* corrupt DER - invert last byte, which is signature */
  42293. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  42294. /* test bad cert */
  42295. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  42296. }
  42297. printf("Signature failure test: ECC: Ret %d\n", ret);
  42298. if (cert_buf)
  42299. free(cert_buf);
  42300. #ifdef FP_ECC
  42301. wc_ecc_fp_free();
  42302. #endif
  42303. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  42304. if (ret == WOLFSSL_FATAL_ERROR) {
  42305. ret = 0;
  42306. }
  42307. #else
  42308. if (ret == ASN_SIG_CONFIRM_E) {
  42309. ret = 0;
  42310. }
  42311. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  42312. return ret;
  42313. }
  42314. #endif /* NO_CERTS */
  42315. #ifdef WOLFSSL_TLS13
  42316. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  42317. #ifdef WC_SHA384_DIGEST_SIZE
  42318. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  42319. #else
  42320. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  42321. #endif
  42322. #endif
  42323. #ifdef WOLFSSL_EARLY_DATA
  42324. static const char earlyData[] = "Early Data";
  42325. static char earlyDataBuffer[1];
  42326. #endif
  42327. static int test_tls13_apis(void)
  42328. {
  42329. int ret = 0;
  42330. #ifndef WOLFSSL_NO_TLS12
  42331. #ifndef NO_WOLFSSL_CLIENT
  42332. WOLFSSL_CTX* clientTls12Ctx;
  42333. WOLFSSL* clientTls12Ssl;
  42334. #endif
  42335. #ifndef NO_WOLFSSL_SERVER
  42336. WOLFSSL_CTX* serverTls12Ctx;
  42337. WOLFSSL* serverTls12Ssl;
  42338. #endif
  42339. #endif
  42340. #ifndef NO_WOLFSSL_CLIENT
  42341. WOLFSSL_CTX* clientCtx;
  42342. WOLFSSL* clientSsl;
  42343. #endif
  42344. #ifndef NO_WOLFSSL_SERVER
  42345. WOLFSSL_CTX* serverCtx;
  42346. WOLFSSL* serverSsl;
  42347. #ifndef NO_CERTS
  42348. const char* ourCert = svrCertFile;
  42349. const char* ourKey = svrKeyFile;
  42350. #endif
  42351. #endif
  42352. int required;
  42353. #ifdef WOLFSSL_EARLY_DATA
  42354. int outSz;
  42355. #endif
  42356. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  42357. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  42358. #ifdef HAVE_PQC
  42359. WOLFSSL_SABER_LEVEL3
  42360. #else
  42361. WOLFSSL_ECC_SECP256R1
  42362. #endif
  42363. };
  42364. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  42365. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  42366. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  42367. int numGroups = 2;
  42368. #endif
  42369. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  42370. char groupList[] =
  42371. #ifndef NO_ECC_SECP
  42372. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  42373. "P-521:"
  42374. #endif
  42375. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  42376. "P-384:"
  42377. #endif
  42378. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  42379. "P-256"
  42380. #ifdef HAVE_PQC
  42381. ":P256_SABER_LEVEL1"
  42382. #endif
  42383. #endif
  42384. #ifdef HAVE_PQC
  42385. ":KYBER_LEVEL1"
  42386. #endif
  42387. "";
  42388. #endif /* !defined(NO_ECC_SECP) */
  42389. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  42390. (void)ret;
  42391. #ifndef WOLFSSL_NO_TLS12
  42392. #ifndef NO_WOLFSSL_CLIENT
  42393. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  42394. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  42395. #endif
  42396. #ifndef NO_WOLFSSL_SERVER
  42397. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  42398. #ifndef NO_CERTS
  42399. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  42400. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  42401. #endif
  42402. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  42403. #endif
  42404. #endif
  42405. #ifndef NO_WOLFSSL_CLIENT
  42406. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  42407. clientSsl = wolfSSL_new(clientCtx);
  42408. #endif
  42409. #ifndef NO_WOLFSSL_SERVER
  42410. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  42411. #ifndef NO_CERTS
  42412. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  42413. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  42414. #endif
  42415. serverSsl = wolfSSL_new(serverCtx);
  42416. #endif
  42417. #ifdef WOLFSSL_SEND_HRR_COOKIE
  42418. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  42419. #ifndef NO_WOLFSSL_CLIENT
  42420. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  42421. #endif
  42422. #ifndef NO_WOLFSSL_SERVER
  42423. #ifndef WOLFSSL_NO_TLS12
  42424. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  42425. #endif
  42426. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  42427. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  42428. WOLFSSL_SUCCESS);
  42429. #endif
  42430. #endif
  42431. #ifdef HAVE_SUPPORTED_CURVES
  42432. #ifdef HAVE_ECC
  42433. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  42434. #ifndef NO_WOLFSSL_SERVER
  42435. do {
  42436. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  42437. #ifdef WOLFSSL_ASYNC_CRYPT
  42438. if (ret == WC_PENDING_E)
  42439. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  42440. #endif
  42441. } while (ret == WC_PENDING_E);
  42442. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42443. #endif
  42444. #ifndef NO_WOLFSSL_CLIENT
  42445. #ifndef WOLFSSL_NO_TLS12
  42446. do {
  42447. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  42448. #ifdef WOLFSSL_ASYNC_CRYPT
  42449. if (ret == WC_PENDING_E)
  42450. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  42451. #endif
  42452. } while (ret == WC_PENDING_E);
  42453. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42454. #endif
  42455. do {
  42456. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  42457. #ifdef WOLFSSL_ASYNC_CRYPT
  42458. if (ret == WC_PENDING_E)
  42459. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  42460. #endif
  42461. } while (ret == WC_PENDING_E);
  42462. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42463. #endif
  42464. #elif defined(HAVE_CURVE25519)
  42465. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  42466. #ifndef NO_WOLFSSL_SERVER
  42467. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  42468. WOLFSSL_SUCCESS);
  42469. #endif
  42470. #ifndef NO_WOLFSSL_CLIENT
  42471. #ifndef WOLFSSL_NO_TLS12
  42472. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  42473. WOLFSSL_SUCCESS);
  42474. #endif
  42475. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  42476. WOLFSSL_SUCCESS);
  42477. #endif
  42478. #elif defined(HAVE_CURVE448)
  42479. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  42480. #ifndef NO_WOLFSSL_SERVER
  42481. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  42482. WOLFSSL_SUCCESS);
  42483. #endif
  42484. #ifndef NO_WOLFSSL_CLIENT
  42485. #ifndef WOLFSSL_NO_TLS12
  42486. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  42487. WOLFSSL_SUCCESS);
  42488. #endif
  42489. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  42490. WOLFSSL_SUCCESS);
  42491. #endif
  42492. #else
  42493. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  42494. #ifndef NO_WOLFSSL_CLIENT
  42495. #ifndef WOLFSSL_NO_TLS12
  42496. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  42497. NOT_COMPILED_IN);
  42498. #endif
  42499. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  42500. NOT_COMPILED_IN);
  42501. #endif
  42502. #endif
  42503. #if defined(HAVE_PQC)
  42504. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  42505. #ifndef NO_WOLFSSL_SERVER
  42506. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  42507. WOLFSSL_SUCCESS);
  42508. #endif
  42509. #ifndef NO_WOLFSSL_CLIENT
  42510. #ifndef WOLFSSL_NO_TLS12
  42511. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  42512. BAD_FUNC_ARG);
  42513. #endif
  42514. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  42515. WOLFSSL_SUCCESS);
  42516. #endif
  42517. #endif
  42518. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  42519. #ifndef NO_WOLFSSL_SERVER
  42520. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  42521. #endif
  42522. #ifndef NO_WOLFSSL_CLIENT
  42523. #ifndef WOLFSSL_NO_TLS12
  42524. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  42525. #endif
  42526. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  42527. #endif
  42528. #endif /* HAVE_SUPPORTED_CURVES */
  42529. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  42530. #ifndef NO_WOLFSSL_CLIENT
  42531. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  42532. #endif
  42533. #ifndef NO_WOLFSSL_SERVER
  42534. #ifndef WOLFSSL_NO_TLS12
  42535. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  42536. #endif
  42537. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  42538. #endif
  42539. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  42540. #ifndef NO_WOLFSSL_CLIENT
  42541. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  42542. #endif
  42543. #ifndef NO_WOLFSSL_SERVER
  42544. #ifndef WOLFSSL_NO_TLS12
  42545. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  42546. #endif
  42547. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  42548. #endif
  42549. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  42550. #ifndef NO_WOLFSSL_CLIENT
  42551. #ifndef WOLFSSL_NO_TLS12
  42552. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  42553. #endif
  42554. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  42555. #endif
  42556. #ifndef NO_WOLFSSL_SERVER
  42557. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  42558. #endif
  42559. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  42560. #ifndef NO_WOLFSSL_CLIENT
  42561. #ifndef WOLFSSL_NO_TLS12
  42562. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  42563. #endif
  42564. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  42565. #endif
  42566. #ifndef NO_WOLFSSL_SERVER
  42567. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  42568. #endif
  42569. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  42570. #ifndef NO_WOLFSSL_CLIENT
  42571. #ifndef WOLFSSL_NO_TLS12
  42572. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  42573. #endif
  42574. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  42575. #endif
  42576. #ifndef NO_WOLFSSL_SERVER
  42577. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  42578. #endif
  42579. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  42580. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  42581. #ifndef NO_WOLFSSL_CLIENT
  42582. #ifndef WOLFSSL_NO_TLS12
  42583. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  42584. BAD_FUNC_ARG);
  42585. #endif
  42586. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  42587. #endif
  42588. #ifndef NO_WOLFSSL_SERVER
  42589. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  42590. #endif
  42591. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42592. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  42593. #ifndef NO_WOLFSSL_SERVER
  42594. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  42595. #endif
  42596. #ifndef NO_WOLFSSL_CLIENT
  42597. #ifndef WOLFSSL_NO_TLS12
  42598. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  42599. BAD_FUNC_ARG);
  42600. #endif
  42601. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  42602. #endif
  42603. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  42604. #ifndef NO_WOLFSSL_SERVER
  42605. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  42606. #endif
  42607. #ifndef NO_WOLFSSL_CLIENT
  42608. #ifndef WOLFSSL_NO_TLS12
  42609. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  42610. BAD_FUNC_ARG);
  42611. #endif
  42612. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  42613. #endif
  42614. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  42615. #ifndef NO_WOLFSSL_CLIENT
  42616. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  42617. #endif
  42618. #ifndef NO_WOLFSSL_SERVER
  42619. #ifndef WOLFSSL_NO_TLS12
  42620. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  42621. BAD_FUNC_ARG);
  42622. #endif
  42623. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  42624. #endif
  42625. #endif
  42626. #ifdef HAVE_ECC
  42627. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  42628. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  42629. #ifndef NO_WOLFSSL_SERVER
  42630. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  42631. #endif
  42632. #ifndef NO_WOLFSSL_CLIENT
  42633. #ifndef WOLFSSL_NO_TLS12
  42634. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  42635. #endif
  42636. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  42637. #endif
  42638. #endif
  42639. #ifdef HAVE_SUPPORTED_CURVES
  42640. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  42641. #ifndef NO_WOLFSSL_CLIENT
  42642. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  42643. #endif
  42644. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  42645. #ifndef NO_WOLFSSL_CLIENT
  42646. #ifndef WOLFSSL_NO_TLS12
  42647. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  42648. BAD_FUNC_ARG);
  42649. #endif
  42650. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  42651. WOLFSSL_MAX_GROUP_COUNT + 1),
  42652. BAD_FUNC_ARG);
  42653. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  42654. WOLFSSL_SUCCESS);
  42655. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  42656. BAD_FUNC_ARG);
  42657. #endif
  42658. #ifndef NO_WOLFSSL_SERVER
  42659. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  42660. WOLFSSL_SUCCESS);
  42661. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  42662. BAD_FUNC_ARG);
  42663. #endif
  42664. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  42665. #ifndef NO_WOLFSSL_CLIENT
  42666. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  42667. #endif
  42668. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  42669. #ifndef NO_WOLFSSL_CLIENT
  42670. #ifndef WOLFSSL_NO_TLS12
  42671. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  42672. BAD_FUNC_ARG);
  42673. #endif
  42674. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  42675. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  42676. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  42677. WOLFSSL_SUCCESS);
  42678. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  42679. BAD_FUNC_ARG);
  42680. #endif
  42681. #ifndef NO_WOLFSSL_SERVER
  42682. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  42683. WOLFSSL_SUCCESS);
  42684. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  42685. BAD_FUNC_ARG);
  42686. #endif
  42687. #ifdef OPENSSL_EXTRA
  42688. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  42689. #ifndef NO_WOLFSSL_CLIENT
  42690. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  42691. #endif
  42692. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  42693. #ifndef NO_WOLFSSL_CLIENT
  42694. #ifndef WOLFSSL_NO_TLS12
  42695. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  42696. WOLFSSL_FAILURE);
  42697. #endif
  42698. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  42699. WOLFSSL_SUCCESS);
  42700. #endif
  42701. #ifndef NO_WOLFSSL_SERVER
  42702. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  42703. WOLFSSL_SUCCESS);
  42704. #endif
  42705. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  42706. #ifndef NO_WOLFSSL_CLIENT
  42707. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  42708. #endif
  42709. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  42710. #ifndef NO_WOLFSSL_CLIENT
  42711. #ifndef WOLFSSL_NO_TLS12
  42712. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  42713. WOLFSSL_FAILURE);
  42714. #endif
  42715. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  42716. WOLFSSL_SUCCESS);
  42717. #endif
  42718. #ifndef NO_WOLFSSL_SERVER
  42719. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  42720. WOLFSSL_SUCCESS);
  42721. #endif
  42722. #endif /* OPENSSL_EXTRA */
  42723. #endif /* HAVE_SUPPORTED_CURVES */
  42724. #endif /* HAVE_ECC */
  42725. #ifdef WOLFSSL_EARLY_DATA
  42726. #ifndef OPENSSL_EXTRA
  42727. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42728. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  42729. #else
  42730. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42731. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  42732. #endif
  42733. #ifndef NO_WOLFSSL_CLIENT
  42734. #ifndef OPENSSL_EXTRA
  42735. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  42736. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  42737. #else
  42738. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  42739. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  42740. #endif
  42741. #endif
  42742. #ifndef NO_WOLFSSL_SERVER
  42743. #ifndef WOLFSSL_NO_TLS12
  42744. #ifndef OPENSSL_EXTRA
  42745. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  42746. BAD_FUNC_ARG);
  42747. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  42748. #else
  42749. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  42750. BAD_FUNC_ARG);
  42751. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  42752. #endif
  42753. #endif
  42754. #ifndef OPENSSL_EXTRA
  42755. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  42756. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  42757. #else
  42758. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  42759. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  42760. #endif
  42761. #endif
  42762. #ifndef OPENSSL_EXTRA
  42763. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42764. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  42765. #else
  42766. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  42767. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  42768. #endif
  42769. #ifndef NO_WOLFSSL_CLIENT
  42770. #ifndef OPENSSL_EXTRA
  42771. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  42772. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), SIDE_ERROR);
  42773. #else
  42774. AssertIntEQ(SSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
  42775. AssertIntEQ(SSL_get_max_early_data(clientSsl), SIDE_ERROR);
  42776. #endif
  42777. #endif
  42778. #ifndef NO_WOLFSSL_SERVER
  42779. #ifndef WOLFSSL_NO_TLS12
  42780. #ifndef OPENSSL_EXTRA
  42781. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  42782. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  42783. #else
  42784. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  42785. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  42786. #endif
  42787. #endif
  42788. #ifndef OPENSSL_EXTRA
  42789. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  42790. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  42791. #else
  42792. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  42793. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  42794. #endif
  42795. #endif
  42796. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  42797. &outSz), BAD_FUNC_ARG);
  42798. #ifndef NO_WOLFSSL_CLIENT
  42799. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  42800. &outSz), BAD_FUNC_ARG);
  42801. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  42802. BAD_FUNC_ARG);
  42803. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  42804. sizeof(earlyData), NULL),
  42805. BAD_FUNC_ARG);
  42806. #endif
  42807. #ifndef NO_WOLFSSL_SERVER
  42808. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  42809. sizeof(earlyData), &outSz),
  42810. SIDE_ERROR);
  42811. #endif
  42812. #ifndef NO_WOLFSSL_CLIENT
  42813. #ifndef WOLFSSL_NO_TLS12
  42814. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  42815. sizeof(earlyData), &outSz),
  42816. BAD_FUNC_ARG);
  42817. #endif
  42818. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  42819. sizeof(earlyData), &outSz),
  42820. WOLFSSL_FATAL_ERROR);
  42821. #endif
  42822. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  42823. sizeof(earlyDataBuffer), &outSz),
  42824. BAD_FUNC_ARG);
  42825. #ifndef NO_WOLFSSL_SERVER
  42826. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  42827. sizeof(earlyDataBuffer), &outSz),
  42828. BAD_FUNC_ARG);
  42829. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  42830. BAD_FUNC_ARG);
  42831. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  42832. sizeof(earlyDataBuffer), NULL),
  42833. BAD_FUNC_ARG);
  42834. #endif
  42835. #ifndef NO_WOLFSSL_CLIENT
  42836. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  42837. sizeof(earlyDataBuffer), &outSz),
  42838. SIDE_ERROR);
  42839. #endif
  42840. #ifndef NO_WOLFSSL_SERVER
  42841. #ifndef WOLFSSL_NO_TLS12
  42842. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  42843. sizeof(earlyDataBuffer), &outSz),
  42844. BAD_FUNC_ARG);
  42845. #endif
  42846. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  42847. sizeof(earlyDataBuffer), &outSz),
  42848. WOLFSSL_FATAL_ERROR);
  42849. #endif
  42850. #endif
  42851. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  42852. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  42853. #endif
  42854. #ifndef NO_WOLFSSL_SERVER
  42855. wolfSSL_free(serverSsl);
  42856. wolfSSL_CTX_free(serverCtx);
  42857. #endif
  42858. #ifndef NO_WOLFSSL_CLIENT
  42859. wolfSSL_free(clientSsl);
  42860. wolfSSL_CTX_free(clientCtx);
  42861. #endif
  42862. #ifndef WOLFSSL_NO_TLS12
  42863. #ifndef NO_WOLFSSL_SERVER
  42864. wolfSSL_free(serverTls12Ssl);
  42865. wolfSSL_CTX_free(serverTls12Ctx);
  42866. #endif
  42867. #ifndef NO_WOLFSSL_CLIENT
  42868. wolfSSL_free(clientTls12Ssl);
  42869. wolfSSL_CTX_free(clientTls12Ctx);
  42870. #endif
  42871. #endif
  42872. return 0;
  42873. }
  42874. #endif
  42875. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  42876. !defined(NO_OLD_TLS))
  42877. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  42878. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  42879. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  42880. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  42881. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  42882. const unsigned char* priv, unsigned int privSz,
  42883. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  42884. unsigned char* out, unsigned int* outlen,
  42885. void* ctx)
  42886. {
  42887. int result;
  42888. /* Test fail when context associated with WOLFSSL is NULL */
  42889. if (ctx == NULL) {
  42890. return -1;
  42891. }
  42892. (void)ssl;
  42893. /* return 0 on success */
  42894. PRIVATE_KEY_UNLOCK();
  42895. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  42896. PRIVATE_KEY_LOCK();
  42897. return result;
  42898. }
  42899. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  42900. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  42901. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  42902. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  42903. WOLFSSL_SUCCESS);
  42904. #endif
  42905. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  42906. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  42907. WOLFSSL_SUCCESS);
  42908. #endif
  42909. }
  42910. static void test_dh_ssl_setup(WOLFSSL* ssl)
  42911. {
  42912. static int dh_test_ctx = 1;
  42913. int ret;
  42914. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  42915. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  42916. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  42917. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  42918. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42919. }
  42920. }
  42921. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  42922. {
  42923. int ret;
  42924. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  42925. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  42926. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  42927. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  42928. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  42929. }
  42930. }
  42931. #endif
  42932. static int test_DhCallbacks(void)
  42933. {
  42934. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  42935. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  42936. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  42937. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  42938. WOLFSSL_CTX *ctx;
  42939. WOLFSSL *ssl;
  42940. tcp_ready ready;
  42941. func_args server_args;
  42942. func_args client_args;
  42943. THREAD_TYPE serverThread;
  42944. callback_functions func_cb_client;
  42945. callback_functions func_cb_server;
  42946. int test;
  42947. printf(testingFmt, "test_DhCallbacks");
  42948. #ifndef NO_WOLFSSL_CLIENT
  42949. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42950. #else
  42951. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42952. #endif
  42953. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  42954. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  42955. /* load client ca cert */
  42956. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  42957. WOLFSSL_SUCCESS);
  42958. /* test with NULL arguments */
  42959. wolfSSL_SetDhAgreeCtx(NULL, &test);
  42960. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  42961. /* test success case */
  42962. test = 1;
  42963. AssertNotNull(ssl = wolfSSL_new(ctx));
  42964. wolfSSL_SetDhAgreeCtx(ssl, &test);
  42965. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  42966. wolfSSL_free(ssl);
  42967. wolfSSL_CTX_free(ctx);
  42968. /* test a connection where callback is used */
  42969. #ifdef WOLFSSL_TIRTOS
  42970. fdOpenSession(Task_self());
  42971. #endif
  42972. XMEMSET(&server_args, 0, sizeof(func_args));
  42973. XMEMSET(&client_args, 0, sizeof(func_args));
  42974. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  42975. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  42976. StartTCP();
  42977. InitTcpReady(&ready);
  42978. #if defined(USE_WINDOWS_API)
  42979. /* use RNG to get random port if using windows */
  42980. ready.port = GetRandomPort();
  42981. #endif
  42982. server_args.signal = &ready;
  42983. client_args.signal = &ready;
  42984. server_args.return_code = TEST_FAIL;
  42985. client_args.return_code = TEST_FAIL;
  42986. /* set callbacks to use DH functions */
  42987. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  42988. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  42989. #ifndef WOLFSSL_NO_TLS12
  42990. func_cb_client.method = wolfTLSv1_2_client_method;
  42991. #else
  42992. func_cb_client.method = wolfTLSv1_3_client_method;
  42993. #endif
  42994. client_args.callbacks = &func_cb_client;
  42995. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  42996. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  42997. #ifndef WOLFSSL_NO_TLS12
  42998. func_cb_server.method = wolfTLSv1_2_server_method;
  42999. #else
  43000. func_cb_server.method = wolfTLSv1_3_server_method;
  43001. #endif
  43002. server_args.callbacks = &func_cb_server;
  43003. start_thread(test_server_nofail, &server_args, &serverThread);
  43004. wait_tcp_ready(&server_args);
  43005. test_client_nofail(&client_args, NULL);
  43006. join_thread(serverThread);
  43007. AssertTrue(client_args.return_code);
  43008. AssertTrue(server_args.return_code);
  43009. FreeTcpReady(&ready);
  43010. #ifdef WOLFSSL_TIRTOS
  43011. fdOpenSession(Task_self());
  43012. #endif
  43013. /* now set user ctx to not be 1 so that the callback returns fail case */
  43014. #ifdef WOLFSSL_TIRTOS
  43015. fdOpenSession(Task_self());
  43016. #endif
  43017. XMEMSET(&server_args, 0, sizeof(func_args));
  43018. XMEMSET(&client_args, 0, sizeof(func_args));
  43019. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  43020. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  43021. StartTCP();
  43022. InitTcpReady(&ready);
  43023. #if defined(USE_WINDOWS_API)
  43024. /* use RNG to get random port if using windows */
  43025. ready.port = GetRandomPort();
  43026. #endif
  43027. server_args.signal = &ready;
  43028. client_args.signal = &ready;
  43029. server_args.return_code = TEST_FAIL;
  43030. client_args.return_code = TEST_FAIL;
  43031. /* set callbacks to use DH functions */
  43032. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  43033. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  43034. #ifndef WOLFSSL_NO_TLS12
  43035. func_cb_client.method = wolfTLSv1_2_client_method;
  43036. #else
  43037. func_cb_client.method = wolfTLSv1_3_client_method;
  43038. #endif
  43039. client_args.callbacks = &func_cb_client;
  43040. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  43041. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  43042. #ifndef WOLFSSL_NO_TLS12
  43043. func_cb_server.method = wolfTLSv1_2_server_method;
  43044. #else
  43045. func_cb_server.method = wolfTLSv1_3_server_method;
  43046. #endif
  43047. server_args.callbacks = &func_cb_server;
  43048. start_thread(test_server_nofail, &server_args, &serverThread);
  43049. wait_tcp_ready(&server_args);
  43050. test_client_nofail(&client_args, NULL);
  43051. join_thread(serverThread);
  43052. AssertIntEQ(client_args.return_code, TEST_FAIL);
  43053. AssertIntEQ(server_args.return_code, TEST_FAIL);
  43054. FreeTcpReady(&ready);
  43055. #ifdef WOLFSSL_TIRTOS
  43056. fdOpenSession(Task_self());
  43057. #endif
  43058. printf(resultFmt, passed);
  43059. #endif
  43060. return 0;
  43061. }
  43062. #endif /* HAVE_PK_CALLBACKS */
  43063. #ifdef HAVE_HASHDRBG
  43064. #ifdef TEST_RESEED_INTERVAL
  43065. static int test_wc_RNG_GenerateBlock_Reseed(void)
  43066. {
  43067. int i, ret;
  43068. WC_RNG rng;
  43069. byte key[32];
  43070. ret = wc_InitRng(&rng);
  43071. if (ret == 0) {
  43072. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  43073. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  43074. if (ret != 0) {
  43075. break;
  43076. }
  43077. }
  43078. }
  43079. wc_FreeRng(&rng);
  43080. return ret;
  43081. }
  43082. #endif /* TEST_RESEED_INTERVAL */
  43083. static int test_wc_RNG_GenerateBlock(void)
  43084. {
  43085. int i, ret;
  43086. WC_RNG rng;
  43087. byte key[32];
  43088. ret = wc_InitRng(&rng);
  43089. if (ret == 0) {
  43090. for(i = 0; i < 10; i++) {
  43091. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  43092. if (ret != 0) {
  43093. break;
  43094. }
  43095. }
  43096. }
  43097. wc_FreeRng(&rng);
  43098. (void)rng; /* for WC_NO_RNG case */
  43099. (void)key;
  43100. return ret;
  43101. }
  43102. #endif
  43103. /*
  43104. * Testing get_rand_digit
  43105. */
  43106. static int test_get_rand_digit(void)
  43107. {
  43108. int ret = 0;
  43109. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  43110. WC_RNG rng;
  43111. mp_digit d;
  43112. printf(testingFmt, "get_rand_digit()");
  43113. ret = wc_InitRng(&rng);
  43114. if (ret == 0) {
  43115. ret = get_rand_digit(&rng, &d);
  43116. }
  43117. if (ret == 0) {
  43118. ret = get_rand_digit(NULL, NULL);
  43119. if (ret == BAD_FUNC_ARG) {
  43120. ret = 0;
  43121. }
  43122. }
  43123. if (ret == 0) {
  43124. ret = get_rand_digit(NULL, &d);
  43125. if (ret == BAD_FUNC_ARG) {
  43126. ret = 0;
  43127. }
  43128. }
  43129. if (ret == 0) {
  43130. ret = get_rand_digit(&rng, NULL);
  43131. if (ret == BAD_FUNC_ARG) {
  43132. ret = 0;
  43133. }
  43134. }
  43135. if (ret == 0) {
  43136. ret = wc_FreeRng(&rng);
  43137. }
  43138. printf(resultFmt, ret == 0 ? passed : failed);
  43139. fflush(stdout);
  43140. #endif
  43141. return ret;
  43142. }/* End test_get_rand_digit*/
  43143. /*
  43144. * Testing get_digit_count
  43145. */
  43146. static int test_get_digit_count(void)
  43147. {
  43148. int ret = 0;
  43149. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  43150. mp_int a;
  43151. printf(testingFmt, "get_digit_count()");
  43152. if (mp_init(&a) != MP_OKAY) {
  43153. ret = -1;
  43154. }
  43155. if (ret == 0) {
  43156. ret = get_digit_count(NULL);
  43157. }
  43158. if (ret == 0) {
  43159. ret = get_digit_count(&a);
  43160. }
  43161. printf(resultFmt, ret == 0 ? passed : failed);
  43162. fflush(stdout);
  43163. mp_clear(&a);
  43164. #endif
  43165. return ret;
  43166. }/* End test_get_digit_count*/
  43167. /*
  43168. * Testing mp_cond_copy
  43169. */
  43170. static int test_mp_cond_copy(void)
  43171. {
  43172. int ret = 0;
  43173. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  43174. defined(WOLFSSL_PUBLIC_MP)
  43175. mp_int a;
  43176. mp_int b;
  43177. int copy = 0;
  43178. printf(testingFmt, "mp_cond_copy()");
  43179. if (mp_init(&a) != MP_OKAY) {
  43180. ret = -1;
  43181. }
  43182. if (ret == 0) {
  43183. if (mp_init(&b) != MP_OKAY) {
  43184. ret = -1;
  43185. }
  43186. }
  43187. if (ret == 0) {
  43188. ret = mp_cond_copy(NULL, copy, NULL);
  43189. if (ret == BAD_FUNC_ARG) {
  43190. ret = 0;
  43191. }
  43192. }
  43193. if (ret == 0) {
  43194. ret = mp_cond_copy(NULL, copy, &b);
  43195. if (ret == BAD_FUNC_ARG) {
  43196. ret = 0;
  43197. }
  43198. }
  43199. if (ret == 0) {
  43200. ret = mp_cond_copy(&a, copy, NULL);
  43201. if (ret == BAD_FUNC_ARG) {
  43202. ret = 0;
  43203. }
  43204. }
  43205. if (ret == 0) {
  43206. ret = mp_cond_copy(&a, copy, &b);
  43207. }
  43208. printf(resultFmt, ret == 0 ? passed : failed);
  43209. fflush(stdout);
  43210. mp_clear(&a);
  43211. mp_clear(&b);
  43212. #endif
  43213. return ret;
  43214. }/* End test_mp_cond_copy*/
  43215. /*
  43216. * Testing mp_rand
  43217. */
  43218. static int test_mp_rand(void)
  43219. {
  43220. int ret = 0;
  43221. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  43222. mp_int a;
  43223. int digits = 1;
  43224. WC_RNG rng;
  43225. printf(testingFmt, "mp_rand()");
  43226. if (mp_init(&a) != MP_OKAY) {
  43227. ret = -1;
  43228. }
  43229. if (ret == 0) {
  43230. ret = wc_InitRng(&rng);
  43231. }
  43232. if (ret == 0) {
  43233. ret = mp_rand(&a, digits, NULL);
  43234. if (ret == MISSING_RNG_E) {
  43235. ret = 0;
  43236. }
  43237. }
  43238. if (ret == 0) {
  43239. ret = mp_rand(NULL, digits, &rng);
  43240. if (ret == BAD_FUNC_ARG) {
  43241. ret = 0;
  43242. }
  43243. }
  43244. if (ret == 0) {
  43245. ret = mp_rand(&a, 0, &rng);
  43246. if (ret == BAD_FUNC_ARG) {
  43247. ret = 0;
  43248. }
  43249. }
  43250. if (ret == 0) {
  43251. ret = mp_rand(&a, digits, &rng);
  43252. }
  43253. printf(resultFmt, ret == 0 ? passed : failed);
  43254. fflush(stdout);
  43255. mp_clear(&a);
  43256. wc_FreeRng(&rng);
  43257. #endif
  43258. return ret;
  43259. }/* End test_mp_rand*/
  43260. /*
  43261. * Testing get_digit
  43262. */
  43263. static int test_get_digit(void)
  43264. {
  43265. int ret = 0;
  43266. #if defined(WOLFSSL_PUBLIC_MP)
  43267. mp_int a;
  43268. int n = 0;
  43269. printf(testingFmt, "get_digit()");
  43270. if (mp_init(&a) != MP_OKAY) {
  43271. ret = -1;
  43272. }
  43273. if (ret == 0) {
  43274. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  43275. ret = -1;
  43276. }
  43277. }
  43278. if (ret == 0) {
  43279. if (get_digit(NULL, n) == 0) { /* Should hit this */
  43280. ret = 0;
  43281. }
  43282. }
  43283. if (ret == 0) {
  43284. if (get_digit(&a, n) != 0) { /* Should not hit this */
  43285. ret = -1;
  43286. }
  43287. }
  43288. if (ret == 0) {
  43289. if (get_digit(&a, n) == 0) { /* Should hit this */
  43290. ret = 0;
  43291. }
  43292. }
  43293. printf(resultFmt, ret == 0 ? passed : failed);
  43294. fflush(stdout);
  43295. mp_clear(&a);
  43296. #endif
  43297. return ret;
  43298. }/* End test_get_digit*/
  43299. /*
  43300. * Testing wc_export_int
  43301. */
  43302. static int test_wc_export_int(void)
  43303. {
  43304. int ret = 0;
  43305. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  43306. defined(WOLFSSL_PUBLIC_MP)
  43307. mp_int mp;
  43308. byte buf[32];
  43309. word32 keySz = (word32)sizeof(buf);
  43310. word32 len = (word32)sizeof(buf);
  43311. printf(testingFmt, "wc_export_int()");
  43312. if (mp_init(&mp) != MP_OKAY) {
  43313. ret = -1;
  43314. }
  43315. if (ret == 0) {
  43316. ret = mp_set(&mp, 1234);
  43317. }
  43318. if (ret == 0) {
  43319. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  43320. if (ret == BAD_FUNC_ARG) {
  43321. ret = 0;
  43322. }
  43323. }
  43324. if (ret == 0) {
  43325. len = sizeof(buf)-1;
  43326. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  43327. if (ret == BUFFER_E) {
  43328. ret = 0;
  43329. }
  43330. }
  43331. if (ret == 0) {
  43332. len = sizeof(buf);
  43333. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  43334. }
  43335. if (ret == 0) {
  43336. len = 4; /* test input too small */
  43337. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  43338. if (ret == BUFFER_E) {
  43339. ret = 0;
  43340. }
  43341. }
  43342. if (ret == 0) {
  43343. len = sizeof(buf);
  43344. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  43345. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  43346. if (ret == 0 && len != 5) {
  43347. ret = BAD_FUNC_ARG;
  43348. }
  43349. }
  43350. printf(resultFmt, ret == 0 ? passed : failed);
  43351. fflush(stdout);
  43352. mp_clear(&mp);
  43353. #endif
  43354. return ret;
  43355. }/* End test_wc_export_int*/
  43356. static int test_wc_InitRngNonce(void)
  43357. {
  43358. int ret=0;
  43359. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  43360. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  43361. WC_RNG rng;
  43362. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  43363. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  43364. word32 nonceSz = sizeof(nonce);
  43365. printf(testingFmt, "wc_InitRngNonce()");
  43366. if (ret == 0){
  43367. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  43368. }
  43369. wc_FreeRng(&rng);
  43370. printf(resultFmt, ret == 0 ? passed : failed);
  43371. fflush(stdout);
  43372. #endif
  43373. return ret;
  43374. }/* End test_wc_InitRngNonce*/
  43375. /*
  43376. * Testing wc_InitRngNonce_ex
  43377. */
  43378. static int test_wc_InitRngNonce_ex(void)
  43379. {
  43380. int ret=0;
  43381. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  43382. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  43383. WC_RNG rng;
  43384. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  43385. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  43386. word32 nonceSz = sizeof(nonce);
  43387. printf(testingFmt, "wc_InitRngNonce_ex()");
  43388. if (ret == 0){
  43389. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  43390. }
  43391. wc_FreeRng(&rng);
  43392. printf(resultFmt, ret == 0 ? passed : failed);
  43393. fflush(stdout);
  43394. #endif
  43395. return ret;
  43396. }/*End test_wc_InitRngNonce_ex*/
  43397. static int test_wolfSSL_X509_CRL(void)
  43398. {
  43399. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  43400. X509_CRL *crl;
  43401. char pem[][100] = {
  43402. "./certs/crl/crl.pem",
  43403. "./certs/crl/crl2.pem",
  43404. "./certs/crl/caEccCrl.pem",
  43405. "./certs/crl/eccCliCRL.pem",
  43406. "./certs/crl/eccSrvCRL.pem",
  43407. ""
  43408. };
  43409. #ifndef NO_BIO
  43410. BIO *bio;
  43411. #endif
  43412. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  43413. char der[][100] = {
  43414. "./certs/crl/crl.der",
  43415. "./certs/crl/crl2.der",
  43416. ""};
  43417. #endif
  43418. XFILE fp;
  43419. int i;
  43420. printf(testingFmt, "test_wolfSSL_X509_CRL");
  43421. for (i = 0; pem[i][0] != '\0'; i++)
  43422. {
  43423. fp = XFOPEN(pem[i], "rb");
  43424. AssertTrue((fp != XBADFILE));
  43425. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  43426. AssertNotNull(crl);
  43427. X509_CRL_free(crl);
  43428. XFCLOSE(fp);
  43429. fp = XFOPEN(pem[i], "rb");
  43430. AssertTrue((fp != XBADFILE));
  43431. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  43432. AssertNotNull(crl);
  43433. X509_CRL_free(crl);
  43434. XFCLOSE(fp);
  43435. }
  43436. #ifndef NO_BIO
  43437. for (i = 0; pem[i][0] != '\0'; i++)
  43438. {
  43439. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  43440. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  43441. X509_CRL_free(crl);
  43442. BIO_free(bio);
  43443. }
  43444. #endif
  43445. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  43446. for(i = 0; der[i][0] != '\0'; i++){
  43447. fp = XFOPEN(der[i], "rb");
  43448. AssertTrue((fp != XBADFILE));
  43449. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  43450. AssertNotNull(crl);
  43451. X509_CRL_free(crl);
  43452. XFCLOSE(fp);
  43453. fp = XFOPEN(der[i], "rb");
  43454. AssertTrue((fp != XBADFILE));
  43455. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  43456. AssertNotNull(crl);
  43457. X509_CRL_free(crl);
  43458. XFCLOSE(fp);
  43459. }
  43460. #endif
  43461. printf(resultFmt, passed);
  43462. fflush(stdout);
  43463. #endif
  43464. return 0;
  43465. }
  43466. static int test_wolfSSL_X509_load_crl_file(void)
  43467. {
  43468. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  43469. !defined(NO_RSA) && !defined(NO_BIO)
  43470. int i;
  43471. char pem[][100] = {
  43472. "./certs/crl/crl.pem",
  43473. "./certs/crl/crl2.pem",
  43474. "./certs/crl/caEccCrl.pem",
  43475. "./certs/crl/eccCliCRL.pem",
  43476. "./certs/crl/eccSrvCRL.pem",
  43477. ""
  43478. };
  43479. char der[][100] = {
  43480. "./certs/crl/crl.der",
  43481. "./certs/crl/crl2.der",
  43482. ""
  43483. };
  43484. WOLFSSL_X509_STORE* store;
  43485. WOLFSSL_X509_LOOKUP* lookup;
  43486. printf(testingFmt, "wolfSSL_X509_load_crl_file");
  43487. AssertNotNull(store = wolfSSL_X509_STORE_new());
  43488. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  43489. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  43490. X509_FILETYPE_PEM), 1);
  43491. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  43492. X509_FILETYPE_PEM), 1);
  43493. if (store) {
  43494. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  43495. WOLFSSL_FILETYPE_PEM), 1);
  43496. /* since store hasn't yet known the revoked cert*/
  43497. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43498. WOLFSSL_FILETYPE_PEM), 1);
  43499. }
  43500. for (i = 0; pem[i][0] != '\0'; i++)
  43501. {
  43502. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  43503. }
  43504. if (store) {
  43505. /* since store knows crl list */
  43506. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43507. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  43508. }
  43509. /* once feeing store */
  43510. X509_STORE_free(store);
  43511. store = NULL;
  43512. AssertNotNull(store = wolfSSL_X509_STORE_new());
  43513. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  43514. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  43515. X509_FILETYPE_PEM), 1);
  43516. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  43517. X509_FILETYPE_PEM), 1);
  43518. if (store) {
  43519. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  43520. WOLFSSL_FILETYPE_PEM), 1);
  43521. /* since store hasn't yet known the revoked cert*/
  43522. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43523. WOLFSSL_FILETYPE_PEM), 1);
  43524. }
  43525. for (i = 0; der[i][0] != '\0'; i++)
  43526. {
  43527. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  43528. }
  43529. if (store) {
  43530. /* since store knows crl list */
  43531. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  43532. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  43533. }
  43534. /* test for incorrect parameter */
  43535. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  43536. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  43537. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  43538. X509_STORE_free(store);
  43539. store = NULL;
  43540. printf(resultFmt, passed);
  43541. #endif
  43542. return 0;
  43543. }
  43544. static int test_wolfSSL_d2i_X509_REQ(void)
  43545. {
  43546. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  43547. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  43548. !defined(WOLFSSL_SP_MATH)
  43549. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  43550. * generated by libest
  43551. * ./certs/csr.attr.der contains sample attributes
  43552. * ./certs/csr.ext.der contains sample extensions */
  43553. const char* csrFile = "./certs/csr.signed.der";
  43554. const char* csrPopFile = "./certs/csr.attr.der";
  43555. const char* csrExtFile = "./certs/csr.ext.der";
  43556. /* ./certs/csr.dsa.pem is generated using
  43557. * openssl req -newkey dsa:certs/dsaparams.pem \
  43558. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  43559. * -outform PEM
  43560. * with the passphrase "wolfSSL"
  43561. */
  43562. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  43563. const char* csrDsaFile = "./certs/csr.dsa.pem";
  43564. XFILE f;
  43565. #endif
  43566. BIO* bio = NULL;
  43567. X509* req = NULL;
  43568. EVP_PKEY *pub_key = NULL;
  43569. {
  43570. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  43571. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  43572. /*
  43573. * Extract the public key from the CSR
  43574. */
  43575. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43576. /*
  43577. * Verify the signature in the CSR
  43578. */
  43579. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43580. X509_free(req);
  43581. BIO_free(bio);
  43582. EVP_PKEY_free(pub_key);
  43583. }
  43584. {
  43585. #ifdef OPENSSL_ALL
  43586. X509_ATTRIBUTE* attr;
  43587. ASN1_TYPE *at;
  43588. #endif
  43589. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  43590. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  43591. /*
  43592. * Extract the public key from the CSR
  43593. */
  43594. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43595. /*
  43596. * Verify the signature in the CSR
  43597. */
  43598. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43599. #ifdef OPENSSL_ALL
  43600. /*
  43601. * Obtain the challenge password from the CSR
  43602. */
  43603. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  43604. 1);
  43605. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  43606. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  43607. AssertNotNull(at->value.asn1_string);
  43608. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  43609. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  43610. #endif
  43611. X509_free(req);
  43612. BIO_free(bio);
  43613. EVP_PKEY_free(pub_key);
  43614. }
  43615. {
  43616. #ifdef OPENSSL_ALL
  43617. X509_ATTRIBUTE* attr;
  43618. ASN1_TYPE *at;
  43619. STACK_OF(X509_EXTENSION) *exts = NULL;
  43620. #endif
  43621. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  43622. /* This CSR contains an Extension Request attribute so
  43623. * we test extension parsing in a CSR attribute here. */
  43624. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  43625. /*
  43626. * Extract the public key from the CSR
  43627. */
  43628. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43629. /*
  43630. * Verify the signature in the CSR
  43631. */
  43632. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43633. #ifdef OPENSSL_ALL
  43634. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  43635. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  43636. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  43637. /*
  43638. * Obtain the challenge password from the CSR
  43639. */
  43640. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  43641. 0);
  43642. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  43643. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  43644. AssertNotNull(at->value.asn1_string);
  43645. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  43646. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  43647. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  43648. #endif
  43649. X509_free(req);
  43650. BIO_free(bio);
  43651. EVP_PKEY_free(pub_key);
  43652. }
  43653. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  43654. {
  43655. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  43656. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  43657. /*
  43658. * Extract the public key from the CSR
  43659. */
  43660. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  43661. /*
  43662. * Verify the signature in the CSR
  43663. */
  43664. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43665. X509_free(req);
  43666. BIO_free(bio);
  43667. /* Run the same test, but with a file pointer instead of a BIO.
  43668. * (PEM_read_X509_REQ)*/
  43669. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  43670. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  43671. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  43672. X509_free(req);
  43673. EVP_PKEY_free(pub_key);
  43674. }
  43675. #endif /* !NO_DSA && !HAVE_SELFTEST */
  43676. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  43677. return 0;
  43678. }
  43679. static int test_wolfSSL_PEM_read_X509(void)
  43680. {
  43681. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  43682. !defined(NO_RSA)
  43683. X509 *x509 = NULL;
  43684. XFILE fp;
  43685. printf(testingFmt, "wolfSSL_PEM_read_X509");
  43686. fp = XFOPEN(svrCertFile, "rb");
  43687. AssertTrue((fp != XBADFILE));
  43688. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  43689. X509_free(x509);
  43690. XFCLOSE(fp);
  43691. printf(resultFmt, passed);
  43692. #endif
  43693. return 0;
  43694. }
  43695. static int test_wolfSSL_PEM_read(void)
  43696. {
  43697. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  43698. const char* filename = "./certs/server-keyEnc.pem";
  43699. XFILE fp;
  43700. char* name = NULL;
  43701. char* header = NULL;
  43702. byte* data = NULL;
  43703. long len;
  43704. EVP_CIPHER_INFO cipher;
  43705. WOLFSSL_BIO* bio;
  43706. byte* fileData;
  43707. size_t fileDataSz;
  43708. byte* out;
  43709. printf(testingFmt, "wolfSSL_PEM_read");
  43710. fp = XFOPEN(filename, "rb");
  43711. AssertTrue((fp != XBADFILE));
  43712. /* Fail cases. */
  43713. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  43714. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  43715. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  43716. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  43717. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  43718. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  43719. AssertIntGT(XSTRLEN(header), 0);
  43720. AssertIntGT(len, 0);
  43721. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  43722. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  43723. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  43724. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  43725. DYNAMIC_TYPE_TMP_BUFFER));
  43726. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  43727. XFCLOSE(fp);
  43728. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  43729. /* Fail cases. */
  43730. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  43731. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  43732. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  43733. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  43734. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  43735. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  43736. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  43737. /* Fail cases. */
  43738. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  43739. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  43740. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  43741. #ifndef NO_DES3
  43742. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  43743. #endif
  43744. /* Fail cases. */
  43745. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  43746. (void*)"yassl123"), WOLFSSL_FAILURE);
  43747. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  43748. (void*)"yassl123"), WOLFSSL_FAILURE);
  43749. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  43750. (void*)"yassl123"), WOLFSSL_FAILURE);
  43751. #if !defined(NO_DES3) && !defined(NO_MD5)
  43752. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  43753. (void*)"yassl123"), WOLFSSL_SUCCESS);
  43754. #endif
  43755. BIO_free(bio);
  43756. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43757. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43758. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43759. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43760. name = NULL;
  43761. header = NULL;
  43762. data = NULL;
  43763. fp = XFOPEN(svrKeyFile, "rb");
  43764. AssertTrue((fp != XBADFILE));
  43765. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  43766. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  43767. AssertIntEQ(XSTRLEN(header), 0);
  43768. AssertIntGT(len, 0);
  43769. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  43770. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  43771. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  43772. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  43773. DYNAMIC_TYPE_TMP_BUFFER));
  43774. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  43775. XFCLOSE(fp);
  43776. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  43777. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  43778. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  43779. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  43780. BIO_free(bio);
  43781. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43782. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43783. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43784. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43785. printf(resultFmt, passed);
  43786. #endif
  43787. return 0;
  43788. }
  43789. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  43790. {
  43791. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43792. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  43793. const byte iv[12] = { 0 };
  43794. const byte key[16] = { 0 };
  43795. const byte cleartext[16] = { 0 };
  43796. const byte aad[] = {
  43797. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  43798. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  43799. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  43800. 0x4f
  43801. };
  43802. byte out1Part[16];
  43803. byte outTag1Part[16];
  43804. byte out2Part[16];
  43805. byte outTag2Part[16];
  43806. byte decryptBuf[16];
  43807. int len;
  43808. int tlen;
  43809. EVP_CIPHER_CTX* ctx = NULL;
  43810. printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
  43811. /* ENCRYPT */
  43812. /* Send AAD and data in 1 part */
  43813. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43814. tlen = 0;
  43815. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43816. 1);
  43817. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43818. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  43819. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  43820. sizeof(cleartext)), 1);
  43821. tlen += len;
  43822. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  43823. tlen += len;
  43824. AssertIntEQ(tlen, sizeof(cleartext));
  43825. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  43826. outTag1Part), 1);
  43827. EVP_CIPHER_CTX_free(ctx);
  43828. /* DECRYPT */
  43829. /* Send AAD and data in 1 part */
  43830. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43831. tlen = 0;
  43832. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43833. 1);
  43834. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43835. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  43836. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  43837. sizeof(cleartext)), 1);
  43838. tlen += len;
  43839. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  43840. outTag1Part), 1);
  43841. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  43842. tlen += len;
  43843. AssertIntEQ(tlen, sizeof(cleartext));
  43844. EVP_CIPHER_CTX_free(ctx);
  43845. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  43846. /* ENCRYPT */
  43847. /* Send AAD and data in 2 parts */
  43848. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43849. tlen = 0;
  43850. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43851. 1);
  43852. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43853. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  43854. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  43855. 1);
  43856. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  43857. tlen += len;
  43858. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  43859. sizeof(cleartext) - 1), 1);
  43860. tlen += len;
  43861. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  43862. tlen += len;
  43863. AssertIntEQ(tlen, sizeof(cleartext));
  43864. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  43865. outTag2Part), 1);
  43866. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  43867. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  43868. EVP_CIPHER_CTX_free(ctx);
  43869. /* DECRYPT */
  43870. /* Send AAD and data in 2 parts */
  43871. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  43872. tlen = 0;
  43873. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  43874. 1);
  43875. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  43876. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  43877. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  43878. 1);
  43879. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  43880. tlen += len;
  43881. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  43882. sizeof(cleartext) - 1), 1);
  43883. tlen += len;
  43884. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  43885. outTag1Part), 1);
  43886. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  43887. tlen += len;
  43888. AssertIntEQ(tlen, sizeof(cleartext));
  43889. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  43890. /* Test AAD re-use */
  43891. EVP_CIPHER_CTX_free(ctx);
  43892. printf(resultFmt, passed);
  43893. #endif
  43894. return 0;
  43895. }
  43896. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43897. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  43898. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  43899. {
  43900. /* Zero length plain text */
  43901. byte key[] = {
  43902. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43903. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43904. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  43905. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  43906. }; /* align */
  43907. byte iv[] = {
  43908. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  43909. }; /* align */
  43910. byte plaintxt[1];
  43911. int ivSz = 12;
  43912. int plaintxtSz = 0;
  43913. unsigned char tag[16];
  43914. unsigned char tag_kat[] =
  43915. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  43916. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  43917. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  43918. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  43919. int ciphertxtSz = 0;
  43920. int decryptedtxtSz = 0;
  43921. int len = 0;
  43922. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  43923. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  43924. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  43925. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43926. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  43927. plaintxtSz));
  43928. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  43929. ciphertxtSz += len;
  43930. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  43931. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  43932. AssertIntEQ(0, ciphertxtSz);
  43933. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  43934. EVP_CIPHER_CTX_init(de);
  43935. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  43936. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43937. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  43938. decryptedtxtSz = len;
  43939. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  43940. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  43941. decryptedtxtSz += len;
  43942. AssertIntEQ(0, decryptedtxtSz);
  43943. EVP_CIPHER_CTX_free(en);
  43944. EVP_CIPHER_CTX_free(de);
  43945. return 0;
  43946. }
  43947. #endif
  43948. static int test_wolfssl_EVP_aes_gcm(void)
  43949. {
  43950. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  43951. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  43952. /* A 256 bit key, AES_128 will use the first 128 bit*/
  43953. byte *key = (byte*)"01234567890123456789012345678901";
  43954. /* A 128 bit IV */
  43955. byte *iv = (byte*)"0123456789012345";
  43956. int ivSz = AES_BLOCK_SIZE;
  43957. /* Message to be encrypted */
  43958. byte *plaintxt = (byte*)"for things to change you have to change";
  43959. /* Additional non-confidential data */
  43960. byte *aad = (byte*)"Don't spend major time on minor things.";
  43961. unsigned char tag[AES_BLOCK_SIZE] = {0};
  43962. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  43963. int aadSz = (int)XSTRLEN((char*)aad);
  43964. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  43965. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  43966. int ciphertxtSz = 0;
  43967. int decryptedtxtSz = 0;
  43968. int len = 0;
  43969. int i = 0;
  43970. EVP_CIPHER_CTX en[2];
  43971. EVP_CIPHER_CTX de[2];
  43972. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  43973. for (i = 0; i < 2; i++) {
  43974. EVP_CIPHER_CTX_init(&en[i]);
  43975. if (i == 0) {
  43976. /* Default uses 96-bits IV length */
  43977. #ifdef WOLFSSL_AES_128
  43978. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  43979. #elif defined(WOLFSSL_AES_192)
  43980. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  43981. #elif defined(WOLFSSL_AES_256)
  43982. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  43983. #endif
  43984. }
  43985. else {
  43986. #ifdef WOLFSSL_AES_128
  43987. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  43988. #elif defined(WOLFSSL_AES_192)
  43989. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  43990. #elif defined(WOLFSSL_AES_256)
  43991. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  43992. #endif
  43993. /* non-default must to set the IV length first */
  43994. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  43995. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  43996. }
  43997. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  43998. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  43999. ciphertxtSz = len;
  44000. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  44001. ciphertxtSz += len;
  44002. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  44003. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  44004. EVP_CIPHER_CTX_init(&de[i]);
  44005. if (i == 0) {
  44006. /* Default uses 96-bits IV length */
  44007. #ifdef WOLFSSL_AES_128
  44008. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  44009. #elif defined(WOLFSSL_AES_192)
  44010. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  44011. #elif defined(WOLFSSL_AES_256)
  44012. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  44013. #endif
  44014. }
  44015. else {
  44016. #ifdef WOLFSSL_AES_128
  44017. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  44018. #elif defined(WOLFSSL_AES_192)
  44019. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  44020. #elif defined(WOLFSSL_AES_256)
  44021. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  44022. #endif
  44023. /* non-default must to set the IV length first */
  44024. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44025. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  44026. }
  44027. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  44028. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  44029. decryptedtxtSz = len;
  44030. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  44031. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  44032. decryptedtxtSz += len;
  44033. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  44034. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  44035. /* modify tag*/
  44036. tag[AES_BLOCK_SIZE-1]+=0xBB;
  44037. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  44038. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  44039. /* fail due to wrong tag */
  44040. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  44041. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  44042. AssertIntEQ(0, len);
  44043. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  44044. }
  44045. test_wolfssl_EVP_aes_gcm_zeroLen();
  44046. printf(resultFmt, passed);
  44047. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  44048. return 0;
  44049. }
  44050. static int test_wolfssl_EVP_chacha20_poly1305(void)
  44051. {
  44052. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44053. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  44054. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  44055. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  44056. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  44057. byte cipherText[sizeof(plainText)];
  44058. byte decryptedText[sizeof(plainText)];
  44059. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  44060. EVP_CIPHER_CTX* ctx;
  44061. int outSz;
  44062. printf(testingFmt, "test_wolfssl_EVP_chacha20_poly1305");
  44063. /* Encrypt. */
  44064. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  44065. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  44066. NULL), WOLFSSL_SUCCESS);
  44067. /* Invalid IV length. */
  44068. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  44069. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  44070. /* Valid IV length. */
  44071. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  44072. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  44073. /* Invalid tag length. */
  44074. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  44075. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  44076. /* Valid tag length. */
  44077. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  44078. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  44079. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  44080. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  44081. WOLFSSL_SUCCESS);
  44082. AssertIntEQ(outSz, sizeof(aad));
  44083. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  44084. sizeof(plainText)), WOLFSSL_SUCCESS);
  44085. AssertIntEQ(outSz, sizeof(plainText));
  44086. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  44087. AssertIntEQ(outSz, 0);
  44088. /* Invalid tag length. */
  44089. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  44090. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  44091. /* Valid tag length. */
  44092. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  44093. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  44094. EVP_CIPHER_CTX_free(ctx);
  44095. /* Decrypt. */
  44096. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  44097. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  44098. NULL), WOLFSSL_SUCCESS);
  44099. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  44100. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  44101. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  44102. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  44103. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  44104. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  44105. WOLFSSL_SUCCESS);
  44106. AssertIntEQ(outSz, sizeof(aad));
  44107. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  44108. sizeof(cipherText)), WOLFSSL_SUCCESS);
  44109. AssertIntEQ(outSz, sizeof(cipherText));
  44110. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  44111. WOLFSSL_SUCCESS);
  44112. AssertIntEQ(outSz, 0);
  44113. EVP_CIPHER_CTX_free(ctx);
  44114. /* Test partial Inits. CipherInit() allow setting of key and iv
  44115. * in separate calls. */
  44116. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  44117. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  44118. key, NULL, 1), WOLFSSL_SUCCESS);
  44119. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  44120. WOLFSSL_SUCCESS);
  44121. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  44122. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  44123. AssertIntEQ(outSz, sizeof(aad));
  44124. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  44125. sizeof(cipherText)), WOLFSSL_SUCCESS);
  44126. AssertIntEQ(outSz, sizeof(cipherText));
  44127. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  44128. WOLFSSL_SUCCESS);
  44129. AssertIntEQ(outSz, 0);
  44130. EVP_CIPHER_CTX_free(ctx);
  44131. printf(resultFmt, passed);
  44132. #endif
  44133. return 0;
  44134. }
  44135. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  44136. {
  44137. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  44138. EVP_PKEY_CTX* ctx;
  44139. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  44140. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  44141. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44142. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  44143. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  44144. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  44145. byte outKey[34];
  44146. size_t outKeySz = sizeof(outKey);
  44147. /* These expected outputs were gathered by running the same test below using
  44148. * OpenSSL. */
  44149. const byte extractAndExpand[] = {
  44150. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  44151. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  44152. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  44153. };
  44154. const byte extractOnly[] = {
  44155. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  44156. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  44157. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  44158. };
  44159. const byte expandOnly[] = {
  44160. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  44161. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  44162. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  44163. };
  44164. const byte extractAndExpandAddInfo[] = {
  44165. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  44166. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  44167. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  44168. };
  44169. printf(testingFmt, "test_wolfSSL_EVP_PKEY_hkdf");
  44170. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  44171. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  44172. /* NULL ctx. */
  44173. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  44174. /* NULL md. */
  44175. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  44176. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  44177. /* NULL ctx. */
  44178. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  44179. WOLFSSL_FAILURE);
  44180. /* NULL salt is ok. */
  44181. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  44182. WOLFSSL_SUCCESS);
  44183. /* Salt length <= 0. */
  44184. /* Length 0 salt is ok. */
  44185. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  44186. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  44187. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  44188. WOLFSSL_SUCCESS);
  44189. /* NULL ctx. */
  44190. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  44191. WOLFSSL_FAILURE);
  44192. /* NULL key. */
  44193. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  44194. WOLFSSL_FAILURE);
  44195. /* Key length <= 0 */
  44196. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  44197. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  44198. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  44199. WOLFSSL_SUCCESS);
  44200. /* NULL ctx. */
  44201. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  44202. WOLFSSL_FAILURE);
  44203. /* NULL info is ok. */
  44204. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  44205. WOLFSSL_SUCCESS);
  44206. /* Info length <= 0 */
  44207. /* Length 0 info is ok. */
  44208. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  44209. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  44210. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  44211. WOLFSSL_SUCCESS);
  44212. /* NULL ctx. */
  44213. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  44214. WOLFSSL_FAILURE);
  44215. /* Extract and expand (default). */
  44216. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44217. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  44218. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  44219. /* Extract only. */
  44220. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  44221. WOLFSSL_SUCCESS);
  44222. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44223. AssertIntEQ(outKeySz, sizeof(extractOnly));
  44224. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  44225. outKeySz = sizeof(outKey);
  44226. /* Expand only. */
  44227. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  44228. WOLFSSL_SUCCESS);
  44229. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44230. AssertIntEQ(outKeySz, sizeof(expandOnly));
  44231. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  44232. outKeySz = sizeof(outKey);
  44233. /* Extract and expand with appended additional info. */
  44234. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  44235. WOLFSSL_SUCCESS);
  44236. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  44237. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  44238. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  44239. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  44240. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  44241. EVP_PKEY_CTX_free(ctx);
  44242. printf(resultFmt, passed);
  44243. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  44244. return 0;
  44245. }
  44246. #ifndef NO_BIO
  44247. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  44248. {
  44249. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44250. BIO* bio;
  44251. X509_INFO* info;
  44252. STACK_OF(X509_INFO)* sk;
  44253. char* subject;
  44254. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  44255. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  44256. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  44257. AssertNotNull(bio = BIO_new(BIO_s_file()));
  44258. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  44259. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  44260. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  44261. /* using dereference to maintain testing for Apache port*/
  44262. AssertNotNull(info = sk_X509_INFO_pop(sk));
  44263. AssertNotNull(subject =
  44264. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  44265. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  44266. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  44267. X509_INFO_free(info);
  44268. AssertNotNull(info = sk_X509_INFO_pop(sk));
  44269. AssertNotNull(subject =
  44270. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  44271. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  44272. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  44273. X509_INFO_free(info);
  44274. AssertNull(info = sk_X509_INFO_pop(sk));
  44275. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  44276. BIO_free(bio);
  44277. printf(resultFmt, passed);
  44278. #endif
  44279. return 0;
  44280. }
  44281. #endif /* !NO_BIO */
  44282. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  44283. {
  44284. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44285. X509 *x509;
  44286. X509_NAME* name;
  44287. int idx = 0;
  44288. X509_NAME_ENTRY *ne;
  44289. ASN1_OBJECT *object = NULL;
  44290. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  44291. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  44292. AssertNotNull(x509);
  44293. name = X509_get_subject_name(x509);
  44294. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  44295. AssertIntGE(idx, 0);
  44296. ne = X509_NAME_get_entry(name, idx);
  44297. AssertNotNull(ne);
  44298. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  44299. X509_free(x509);
  44300. printf(resultFmt, passed);
  44301. #endif
  44302. return 0;
  44303. }
  44304. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  44305. {
  44306. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  44307. ASN1_INTEGER *a;
  44308. long val;
  44309. int ret;
  44310. printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
  44311. a = ASN1_INTEGER_new();
  44312. val = 0;
  44313. ret = ASN1_INTEGER_set(NULL, val);
  44314. AssertIntEQ(ret, 0);
  44315. ASN1_INTEGER_free(a);
  44316. /* 0 */
  44317. a = ASN1_INTEGER_new();
  44318. val = 0;
  44319. ret = ASN1_INTEGER_set(a, val);
  44320. AssertIntEQ(ret, 1);
  44321. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44322. ASN1_INTEGER_free(a);
  44323. /* 40 */
  44324. a = ASN1_INTEGER_new();
  44325. val = 40;
  44326. ret = ASN1_INTEGER_set(a, val);
  44327. AssertIntEQ(ret, 1);
  44328. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44329. ASN1_INTEGER_free(a);
  44330. /* -40 */
  44331. a = ASN1_INTEGER_new();
  44332. val = -40;
  44333. ret = ASN1_INTEGER_set(a, val);
  44334. AssertIntEQ(ret, 1);
  44335. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44336. ASN1_INTEGER_free(a);
  44337. /* 128 */
  44338. a = ASN1_INTEGER_new();
  44339. val = 128;
  44340. ret = ASN1_INTEGER_set(a, val);
  44341. AssertIntEQ(ret, 1);
  44342. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44343. ASN1_INTEGER_free(a);
  44344. /* -128 */
  44345. a = ASN1_INTEGER_new();
  44346. val = -128;
  44347. ret = ASN1_INTEGER_set(a, val);
  44348. AssertIntEQ(ret, 1);
  44349. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44350. ASN1_INTEGER_free(a);
  44351. /* 200 */
  44352. a = ASN1_INTEGER_new();
  44353. val = 200;
  44354. ret = ASN1_INTEGER_set(a, val);
  44355. AssertIntEQ(ret, 1);
  44356. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44357. ASN1_INTEGER_free(a);
  44358. /* int max (2147483647) */
  44359. a = ASN1_INTEGER_new();
  44360. val = 2147483647;
  44361. ret = ASN1_INTEGER_set(a, val);
  44362. AssertIntEQ(ret, 1);
  44363. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44364. ASN1_INTEGER_free(a);
  44365. /* int min (-2147483648) */
  44366. a = ASN1_INTEGER_new();
  44367. val = -2147483647 - 1;
  44368. ret = ASN1_INTEGER_set(a, val);
  44369. AssertIntEQ(ret, 1);
  44370. AssertIntEQ(ASN1_INTEGER_get(a), val);
  44371. ASN1_INTEGER_free(a);
  44372. printf(resultFmt, passed);
  44373. #endif
  44374. return 0;
  44375. }
  44376. #if defined(OPENSSL_EXTRA)
  44377. typedef struct ASN1IntTestVector {
  44378. const byte* der;
  44379. const size_t derSz;
  44380. const long value;
  44381. } ASN1IntTestVector;
  44382. #endif
  44383. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  44384. {
  44385. #if defined(OPENSSL_EXTRA)
  44386. size_t i;
  44387. WOLFSSL_ASN1_INTEGER* a = NULL;
  44388. WOLFSSL_ASN1_INTEGER* b = NULL;
  44389. WOLFSSL_ASN1_INTEGER* c = NULL;
  44390. const byte* p = NULL;
  44391. byte* reEncoded = NULL;
  44392. int reEncodedSz;
  44393. static const byte zeroDer[] = {
  44394. 0x02, 0x01, 0x00
  44395. };
  44396. static const byte oneDer[] = {
  44397. 0x02, 0x01, 0x01
  44398. };
  44399. static const byte negativeDer[] = {
  44400. 0x02, 0x03, 0xC1, 0x16, 0x0D
  44401. };
  44402. static const byte positiveDer[] = {
  44403. 0x02, 0x03, 0x01, 0x00, 0x01
  44404. };
  44405. static const byte primeDer[] = {
  44406. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  44407. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  44408. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  44409. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  44410. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  44411. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  44412. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  44413. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  44414. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  44415. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  44416. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  44417. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  44418. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  44419. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  44420. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  44421. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  44422. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  44423. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  44424. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  44425. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  44426. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  44427. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  44428. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  44429. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  44430. };
  44431. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  44432. static const ASN1IntTestVector testVectors[] = {
  44433. {zeroDer, sizeof(zeroDer), 0},
  44434. {oneDer, sizeof(oneDer), 1},
  44435. {negativeDer, sizeof(negativeDer), -4123123},
  44436. {positiveDer, sizeof(positiveDer), 65537},
  44437. {primeDer, sizeof(primeDer), 0}
  44438. };
  44439. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  44440. printf(testingFmt, "test_wolfSSL_d2i_ASN1_INTEGER");
  44441. /* Check d2i error conditions */
  44442. /* NULL pointer to input. */
  44443. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  44444. AssertNull(b);
  44445. /* NULL input. */
  44446. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  44447. AssertNull(b);
  44448. /* 0 length. */
  44449. p = testVectors[0].der;
  44450. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  44451. AssertNull(b);
  44452. /* Negative length. */
  44453. p = testVectors[0].der;
  44454. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  44455. AssertNull(b);
  44456. /* Garbage DER input. */
  44457. p = garbageDer;
  44458. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  44459. AssertNull(b);
  44460. {
  44461. /* Check i2d error conditions */
  44462. /* NULL input. */
  44463. byte* p2 = NULL;
  44464. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  44465. /* 0 length input data buffer (a->length == 0). */
  44466. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  44467. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  44468. a->data = NULL;
  44469. /* NULL input data buffer. */
  44470. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  44471. /* Reset a->data. */
  44472. a->data = a->intData;
  44473. /* Set a to valid value. */
  44474. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  44475. /* NULL output buffer. */
  44476. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  44477. wolfSSL_ASN1_INTEGER_free(a);
  44478. }
  44479. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  44480. p = testVectors[i].der;
  44481. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  44482. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  44483. if (testVectors[i].derSz <= sizeof(long)) {
  44484. c = wolfSSL_ASN1_INTEGER_new();
  44485. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  44486. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  44487. wolfSSL_ASN1_INTEGER_free(c);
  44488. }
  44489. /* Convert to DER without a pre-allocated output buffer. */
  44490. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  44491. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  44492. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  44493. /* Convert to DER with a pre-allocated output buffer. In this case, the
  44494. * output buffer pointer should be incremented just past the end of the
  44495. * encoded data. */
  44496. p = reEncoded;
  44497. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  44498. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  44499. AssertPtrEq(p, reEncoded - reEncodedSz);
  44500. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  44501. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  44502. reEncoded = NULL;
  44503. wolfSSL_ASN1_INTEGER_free(a);
  44504. }
  44505. printf(resultFmt, passed);
  44506. #endif /* OPENSSL_EXTRA */
  44507. return 0;
  44508. }
  44509. static int test_wolfSSL_X509_STORE_get1_certs(void)
  44510. {
  44511. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  44512. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  44513. X509_STORE_CTX *storeCtx;
  44514. X509_STORE *store;
  44515. X509 *caX509;
  44516. X509 *svrX509;
  44517. X509_NAME *subject;
  44518. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  44519. printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
  44520. AssertNotNull(caX509 =
  44521. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  44522. AssertNotNull((svrX509 =
  44523. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  44524. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  44525. AssertNotNull(store = X509_STORE_new());
  44526. AssertNotNull(subject = X509_get_subject_name(caX509));
  44527. /* Errors */
  44528. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  44529. AssertNull(X509_STORE_get1_certs(NULL, subject));
  44530. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  44531. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  44532. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  44533. /* Should find the cert */
  44534. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  44535. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  44536. sk_X509_pop_free(certs, NULL);
  44537. /* Should not find the cert */
  44538. AssertNotNull(subject = X509_get_subject_name(svrX509));
  44539. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  44540. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  44541. sk_X509_pop_free(certs, NULL);
  44542. X509_STORE_free(store);
  44543. X509_STORE_CTX_free(storeCtx);
  44544. X509_free(svrX509);
  44545. X509_free(caX509);
  44546. printf(resultFmt, passed);
  44547. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  44548. return 0;
  44549. }
  44550. /* Testing code used in dpp.c in hostap */
  44551. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44552. typedef struct {
  44553. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  44554. * as an OID identifying the curve */
  44555. X509_ALGOR *alg;
  44556. /* Compressed format public key per ANSI X9.63 */
  44557. ASN1_BIT_STRING *pub_key;
  44558. } DPP_BOOTSTRAPPING_KEY;
  44559. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  44560. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  44561. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  44562. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  44563. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  44564. #endif
  44565. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  44566. {
  44567. /* Testing code used in dpp.c in hostap */
  44568. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44569. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44570. EC_KEY *eckey;
  44571. EVP_PKEY *key;
  44572. size_t len;
  44573. unsigned char *der = NULL;
  44574. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  44575. const unsigned char *in = ecc_clikey_der_256;
  44576. const EC_GROUP *group;
  44577. const EC_POINT *point;
  44578. int nid;
  44579. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  44580. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  44581. (long)sizeof_ecc_clikey_der_256));
  44582. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  44583. AssertNotNull(group = EC_KEY_get0_group(eckey));
  44584. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  44585. nid = EC_GROUP_get_curve_name(group);
  44586. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  44587. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  44588. #ifdef HAVE_COMP_KEY
  44589. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  44590. NULL, 0, NULL)), 0);
  44591. #else
  44592. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  44593. NULL, 0, NULL)), 0);
  44594. #endif
  44595. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  44596. #ifdef HAVE_COMP_KEY
  44597. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  44598. der, len, NULL), len);
  44599. #else
  44600. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  44601. der, len, NULL), len);
  44602. #endif
  44603. bootstrap->pub_key->data = der;
  44604. bootstrap->pub_key->length = (int)len;
  44605. /* Not actually used */
  44606. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  44607. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  44608. der = NULL;
  44609. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  44610. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  44611. EVP_PKEY_free(key);
  44612. EC_KEY_free(eckey);
  44613. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  44614. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  44615. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  44616. return 0;
  44617. }
  44618. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  44619. {
  44620. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  44621. ASN1_INTEGER *a;
  44622. unsigned char *pp,*tpp;
  44623. int ret;
  44624. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  44625. a = wolfSSL_ASN1_INTEGER_new();
  44626. /* 40 */
  44627. a->intData[0] = ASN_INTEGER;
  44628. a->intData[1] = 1;
  44629. a->intData[2] = 40;
  44630. ret = i2c_ASN1_INTEGER(a, NULL);
  44631. AssertIntEQ(ret, 1);
  44632. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44633. DYNAMIC_TYPE_TMP_BUFFER));
  44634. tpp = pp;
  44635. XMEMSET(pp, 0, ret + 1);
  44636. i2c_ASN1_INTEGER(a, &pp);
  44637. pp--;
  44638. AssertIntEQ(*pp, 40);
  44639. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44640. /* 128 */
  44641. a->intData[0] = ASN_INTEGER;
  44642. a->intData[1] = 1;
  44643. a->intData[2] = 128;
  44644. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44645. AssertIntEQ(ret, 2);
  44646. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44647. DYNAMIC_TYPE_TMP_BUFFER));
  44648. tpp = pp;
  44649. XMEMSET(pp, 0, ret + 1);
  44650. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44651. pp--;
  44652. AssertIntEQ(*(pp--), 128);
  44653. AssertIntEQ(*pp, 0);
  44654. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44655. /* -40 */
  44656. a->intData[0] = ASN_INTEGER;
  44657. a->intData[1] = 1;
  44658. a->intData[2] = 40;
  44659. a->negative = 1;
  44660. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44661. AssertIntEQ(ret, 1);
  44662. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44663. DYNAMIC_TYPE_TMP_BUFFER));
  44664. tpp = pp;
  44665. XMEMSET(pp, 0, ret + 1);
  44666. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44667. pp--;
  44668. AssertIntEQ(*pp, 216);
  44669. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44670. /* -128 */
  44671. a->intData[0] = ASN_INTEGER;
  44672. a->intData[1] = 1;
  44673. a->intData[2] = 128;
  44674. a->negative = 1;
  44675. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44676. AssertIntEQ(ret, 1);
  44677. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44678. DYNAMIC_TYPE_TMP_BUFFER));
  44679. tpp = pp;
  44680. XMEMSET(pp, 0, ret + 1);
  44681. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44682. pp--;
  44683. AssertIntEQ(*pp, 128);
  44684. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44685. /* -200 */
  44686. a->intData[0] = ASN_INTEGER;
  44687. a->intData[1] = 1;
  44688. a->intData[2] = 200;
  44689. a->negative = 1;
  44690. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  44691. AssertIntEQ(ret, 2);
  44692. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  44693. DYNAMIC_TYPE_TMP_BUFFER));
  44694. tpp = pp;
  44695. XMEMSET(pp, 0, ret + 1);
  44696. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  44697. pp--;
  44698. AssertIntEQ(*(pp--), 56);
  44699. AssertIntEQ(*pp, 255);
  44700. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44701. wolfSSL_ASN1_INTEGER_free(a);
  44702. printf(resultFmt, passed);
  44703. #endif /* OPENSSL_EXTRA && !NO_ASN */
  44704. return 0;
  44705. }
  44706. #ifndef NO_INLINE
  44707. #define WOLFSSL_MISC_INCLUDED
  44708. #include <wolfcrypt/src/misc.c>
  44709. #else
  44710. #include <wolfssl/wolfcrypt/misc.h>
  44711. #endif
  44712. static int test_ForceZero(void)
  44713. {
  44714. unsigned char data[32];
  44715. unsigned int i, j, len;
  44716. /* Test case with 0 length */
  44717. ForceZero(data, 0);
  44718. /* Test ForceZero */
  44719. for (i = 0; i < sizeof(data); i++) {
  44720. for (len = 1; len < sizeof(data) - i; len++) {
  44721. for (j = 0; j < sizeof(data); j++)
  44722. data[j] = j + 1;
  44723. ForceZero(data + i, len);
  44724. for (j = 0; j < sizeof(data); j++) {
  44725. if (j < i || j >= i + len) {
  44726. if (data[j] == 0x00)
  44727. return -10200;
  44728. }
  44729. else if (data[j] != 0x00)
  44730. return -10201;
  44731. }
  44732. }
  44733. }
  44734. return 0;
  44735. }
  44736. #ifndef NO_BIO
  44737. static int test_wolfSSL_X509_print(void)
  44738. {
  44739. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  44740. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  44741. X509 *x509;
  44742. BIO *bio;
  44743. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  44744. const X509_ALGOR *cert_sig_alg;
  44745. #endif
  44746. printf(testingFmt, "wolfSSL_X509_print");
  44747. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  44748. AssertNotNull(x509);
  44749. /* print to memory */
  44750. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  44751. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  44752. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  44753. /* Will print IP address subject alt name. */
  44754. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3255);
  44755. #else
  44756. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3233);
  44757. #endif
  44758. BIO_free(bio);
  44759. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  44760. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  44761. /* Print signature */
  44762. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  44763. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  44764. #endif
  44765. /* print to stdout */
  44766. #if !defined(NO_WOLFSSL_DIR)
  44767. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  44768. #endif
  44769. /* print again */
  44770. AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
  44771. X509_free(x509);
  44772. BIO_free(bio);
  44773. printf(resultFmt, passed);
  44774. #endif
  44775. return 0;
  44776. }
  44777. static int test_wolfSSL_X509_CRL_print(void)
  44778. {
  44779. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  44780. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  44781. X509_CRL* crl;
  44782. BIO *bio;
  44783. XFILE fp;
  44784. printf(testingFmt, "test_X509_CRL_print");
  44785. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  44786. AssertTrue((fp != XBADFILE));
  44787. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  44788. NULL, NULL));
  44789. XFCLOSE(fp);
  44790. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  44791. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  44792. X509_CRL_free(crl);
  44793. BIO_free(bio);
  44794. printf(resultFmt, passed);
  44795. #endif
  44796. return 0;
  44797. }
  44798. static int test_wolfSSL_BIO_get_len(void)
  44799. {
  44800. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  44801. BIO *bio = NULL;
  44802. const char txt[] = "Some example text to push to the BIO.";
  44803. printf(testingFmt, "wolfSSL_BIO_get_len");
  44804. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  44805. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44806. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  44807. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  44808. BIO_free(bio);
  44809. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  44810. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  44811. BIO_free(bio);
  44812. printf(resultFmt, passed);
  44813. #endif
  44814. return 0;
  44815. }
  44816. static int test_wolfSSL_ASN1_STRING_print(void){
  44817. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  44818. ASN1_STRING* asnStr = NULL;
  44819. const char HELLO_DATA[]= \
  44820. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  44821. #define MAX_UNPRINTABLE_CHAR 32
  44822. #define MAX_BUF 255
  44823. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  44824. unsigned char expected[sizeof(unprintableData)+1];
  44825. unsigned char rbuf[MAX_BUF];
  44826. BIO *bio;
  44827. int p_len, i;
  44828. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  44829. /* setup */
  44830. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  44831. unprintableData[i] = HELLO_DATA[i];
  44832. expected[i] = HELLO_DATA[i];
  44833. }
  44834. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  44835. unprintableData[sizeof(HELLO_DATA)+i] = i;
  44836. if (i == (int)'\n' || i == (int)'\r')
  44837. expected[sizeof(HELLO_DATA)+i] = i;
  44838. else
  44839. expected[sizeof(HELLO_DATA)+i] = '.';
  44840. }
  44841. unprintableData[sizeof(unprintableData)-1] = '\0';
  44842. expected[sizeof(expected)-1] = '\0';
  44843. XMEMSET(rbuf, 0, MAX_BUF);
  44844. bio = BIO_new(BIO_s_mem());
  44845. BIO_set_write_buf_size(bio, MAX_BUF);
  44846. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  44847. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  44848. (int)sizeof(unprintableData));
  44849. /* test */
  44850. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  44851. AssertIntEQ(p_len, 46);
  44852. BIO_read(bio, (void*)rbuf, 46);
  44853. AssertStrEQ((char*)rbuf, (const char*)expected);
  44854. BIO_free(bio);
  44855. ASN1_STRING_free(asnStr);
  44856. printf(resultFmt, passed);
  44857. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  44858. return 0;
  44859. }
  44860. #endif /* !NO_BIO */
  44861. static int test_wolfSSL_ASN1_get_object(void)
  44862. {
  44863. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  44864. const unsigned char* derBuf = cliecc_cert_der_256;
  44865. int len = sizeof_cliecc_cert_der_256;
  44866. long asnLen = 0;
  44867. int tag = 0, cls = 0;
  44868. ASN1_OBJECT *a;
  44869. printf(testingFmt, "wolfSSL_ASN1_get_object()");
  44870. /* Read a couple TLV triplets and make sure they match the expected values */
  44871. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  44872. AssertIntEQ(asnLen, 862);
  44873. AssertIntEQ(tag, 0x10);
  44874. AssertIntEQ(cls, 0);
  44875. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44876. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44877. AssertIntEQ(asnLen, 772);
  44878. AssertIntEQ(tag, 0x10);
  44879. AssertIntEQ(cls, 0);
  44880. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44881. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44882. AssertIntEQ(asnLen, 3);
  44883. AssertIntEQ(tag, 0);
  44884. AssertIntEQ(cls, 0x80);
  44885. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44886. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44887. AssertIntEQ(asnLen, 1);
  44888. AssertIntEQ(tag, 0x2);
  44889. AssertIntEQ(cls, 0);
  44890. derBuf += asnLen;
  44891. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44892. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44893. AssertIntEQ(asnLen, 20);
  44894. AssertIntEQ(tag, 0x2);
  44895. AssertIntEQ(cls, 0);
  44896. derBuf += asnLen;
  44897. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  44898. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  44899. AssertIntEQ(asnLen, 10);
  44900. AssertIntEQ(tag, 0x10);
  44901. AssertIntEQ(cls, 0);
  44902. /* Read an ASN OBJECT */
  44903. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  44904. ASN1_OBJECT_free(a);
  44905. printf(resultFmt, passed);
  44906. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  44907. return 0;
  44908. }
  44909. static int test_wolfSSL_RSA(void)
  44910. {
  44911. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  44912. defined(WOLFSSL_KEY_GEN)
  44913. RSA* rsa;
  44914. const BIGNUM *n;
  44915. const BIGNUM *e;
  44916. const BIGNUM *d;
  44917. const BIGNUM *p;
  44918. const BIGNUM *q;
  44919. const BIGNUM *dmp1;
  44920. const BIGNUM *dmq1;
  44921. const BIGNUM *iqmp;
  44922. printf(testingFmt, "wolfSSL_RSA()");
  44923. AssertNotNull(rsa = RSA_new());
  44924. AssertIntEQ(RSA_size(NULL), 0);
  44925. AssertIntEQ(RSA_size(rsa), 0);
  44926. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  44927. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  44928. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  44929. #ifdef WOLFSSL_RSA_KEY_CHECK
  44930. AssertIntEQ(RSA_check_key(rsa), 0);
  44931. #endif
  44932. RSA_free(rsa);
  44933. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  44934. AssertIntEQ(RSA_size(rsa), 256);
  44935. #ifdef WOLFSSL_RSA_KEY_CHECK
  44936. AssertIntEQ(RSA_check_key(NULL), 0);
  44937. AssertIntEQ(RSA_check_key(rsa), 1);
  44938. #endif
  44939. /* sanity check */
  44940. AssertIntEQ(RSA_bits(NULL), 0);
  44941. /* key */
  44942. AssertIntEQ(RSA_bits(rsa), 2048);
  44943. RSA_get0_key(rsa, &n, &e, &d);
  44944. AssertPtrEq(rsa->n, n);
  44945. AssertPtrEq(rsa->e, e);
  44946. AssertPtrEq(rsa->d, d);
  44947. AssertNotNull(n = BN_new());
  44948. AssertNotNull(e = BN_new());
  44949. AssertNotNull(d = BN_new());
  44950. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  44951. AssertPtrEq(rsa->n, n);
  44952. AssertPtrEq(rsa->e, e);
  44953. AssertPtrEq(rsa->d, d);
  44954. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  44955. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  44956. /* crt_params */
  44957. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  44958. AssertPtrEq(rsa->dmp1, dmp1);
  44959. AssertPtrEq(rsa->dmq1, dmq1);
  44960. AssertPtrEq(rsa->iqmp, iqmp);
  44961. AssertNotNull(dmp1 = BN_new());
  44962. AssertNotNull(dmq1 = BN_new());
  44963. AssertNotNull(iqmp = BN_new());
  44964. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  44965. (BIGNUM*)iqmp), 1);
  44966. AssertPtrEq(rsa->dmp1, dmp1);
  44967. AssertPtrEq(rsa->dmq1, dmq1);
  44968. AssertPtrEq(rsa->iqmp, iqmp);
  44969. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  44970. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  44971. (BIGNUM*)iqmp), 0);
  44972. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  44973. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  44974. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  44975. AssertNull(dmp1);
  44976. AssertNull(dmq1);
  44977. AssertNull(iqmp);
  44978. /* factors */
  44979. RSA_get0_factors(rsa, NULL, NULL);
  44980. RSA_get0_factors(rsa, &p, &q);
  44981. AssertPtrEq(rsa->p, p);
  44982. AssertPtrEq(rsa->q, q);
  44983. AssertNotNull(p = BN_new());
  44984. AssertNotNull(q = BN_new());
  44985. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  44986. AssertPtrEq(rsa->p, p);
  44987. AssertPtrEq(rsa->q, q);
  44988. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  44989. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  44990. RSA_get0_factors(NULL, NULL, NULL);
  44991. RSA_get0_factors(NULL, &p, &q);
  44992. AssertNull(p);
  44993. AssertNull(q);
  44994. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  44995. AssertIntEQ(RSA_bits(rsa), 21);
  44996. RSA_free(rsa);
  44997. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  44998. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  44999. AssertIntEQ(RSA_size(rsa), 384);
  45000. AssertIntEQ(RSA_bits(rsa), 3072);
  45001. RSA_free(rsa);
  45002. #endif
  45003. /* remove for now with odd key size until adjusting rsa key size check with
  45004. wc_MakeRsaKey()
  45005. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  45006. RSA_free(rsa);
  45007. */
  45008. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  45009. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  45010. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  45011. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  45012. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  45013. {
  45014. byte buff[FOURK_BUF];
  45015. byte der[FOURK_BUF];
  45016. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  45017. XFILE f;
  45018. int bytes;
  45019. /* test loading encrypted RSA private pem w/o password */
  45020. f = XFOPEN(PrivKeyPemFile, "rb");
  45021. AssertTrue((f != XBADFILE));
  45022. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  45023. XFCLOSE(f);
  45024. XMEMSET(der, 0, sizeof(der));
  45025. /* test that error value is returned with no password */
  45026. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  45027. }
  45028. #endif
  45029. printf(resultFmt, passed);
  45030. #endif
  45031. return 0;
  45032. }
  45033. static int test_wolfSSL_RSA_DER(void)
  45034. {
  45035. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45036. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  45037. RSA *rsa;
  45038. int i;
  45039. const unsigned char *buff = NULL;
  45040. unsigned char *newBuff = NULL;
  45041. struct tbl_s
  45042. {
  45043. const unsigned char *der;
  45044. int sz;
  45045. } tbl[] = {
  45046. #ifdef USE_CERT_BUFFERS_1024
  45047. {client_key_der_1024, sizeof_client_key_der_1024},
  45048. {server_key_der_1024, sizeof_server_key_der_1024},
  45049. #endif
  45050. #ifdef USE_CERT_BUFFERS_2048
  45051. {client_key_der_2048, sizeof_client_key_der_2048},
  45052. {server_key_der_2048, sizeof_server_key_der_2048},
  45053. #endif
  45054. {NULL, 0}
  45055. };
  45056. /* Public Key DER */
  45057. struct tbl_s pub[] = {
  45058. #ifdef USE_CERT_BUFFERS_1024
  45059. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  45060. #endif
  45061. #ifdef USE_CERT_BUFFERS_2048
  45062. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  45063. #endif
  45064. {NULL, 0}
  45065. };
  45066. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  45067. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  45068. buff = pub[0].der;
  45069. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  45070. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  45071. buff = tbl[0].der;
  45072. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  45073. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  45074. rsa = RSA_new();
  45075. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  45076. RSA_free(rsa);
  45077. for (i = 0; tbl[i].der != NULL; i++)
  45078. {
  45079. /* Passing in pointer results in pointer moving. */
  45080. buff = tbl[i].der;
  45081. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  45082. AssertNotNull(rsa);
  45083. RSA_free(rsa);
  45084. }
  45085. for (i = 0; tbl[i].der != NULL; i++)
  45086. {
  45087. /* Passing in pointer results in pointer moving. */
  45088. buff = tbl[i].der;
  45089. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  45090. AssertNotNull(rsa);
  45091. RSA_free(rsa);
  45092. }
  45093. for (i = 0; pub[i].der != NULL; i++)
  45094. {
  45095. buff = pub[i].der;
  45096. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  45097. AssertNotNull(rsa);
  45098. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  45099. newBuff = NULL;
  45100. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  45101. AssertNotNull(newBuff);
  45102. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  45103. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45104. RSA_free(rsa);
  45105. }
  45106. printf(resultFmt, passed);
  45107. #endif
  45108. return 0;
  45109. }
  45110. static int test_wolfSSL_RSA_print(void)
  45111. {
  45112. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  45113. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  45114. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  45115. BIO *bio;
  45116. WOLFSSL_RSA* rsa = NULL;
  45117. printf(testingFmt, "wolfSSL_RSA_print");
  45118. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  45119. AssertNotNull(rsa = RSA_new());
  45120. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  45121. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  45122. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  45123. AssertIntEQ(RSA_print_fp(stdout, NULL, 0), 0);
  45124. /* Some very large number of indent spaces. */
  45125. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  45126. /* RSA is empty. */
  45127. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  45128. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 0);
  45129. RSA_free(rsa);
  45130. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45131. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  45132. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  45133. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  45134. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 1);
  45135. AssertIntEQ(RSA_print_fp(stdout, rsa, 4), 1);
  45136. AssertIntEQ(RSA_print_fp(stdout, rsa, -1), 1);
  45137. BIO_free(bio);
  45138. RSA_free(rsa);
  45139. printf(resultFmt, passed);
  45140. #endif
  45141. return 0;
  45142. }
  45143. #ifndef NO_RSA
  45144. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  45145. {
  45146. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  45147. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45148. RSA *rsa;
  45149. const unsigned char *derBuf = client_key_der_2048;
  45150. unsigned char em[256] = {0}; /* len = 2048/8 */
  45151. /* Random data simulating a hash */
  45152. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  45153. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  45154. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  45155. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  45156. };
  45157. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  45158. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  45159. RSA_PSS_SALTLEN_DIGEST), 0);
  45160. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  45161. RSA_PSS_SALTLEN_DIGEST), 0);
  45162. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  45163. RSA_PSS_SALTLEN_DIGEST), 0);
  45164. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  45165. RSA_PSS_SALTLEN_DIGEST), 0);
  45166. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  45167. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  45168. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45169. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  45170. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45171. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  45172. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45173. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  45174. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45175. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45176. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  45177. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  45178. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  45179. RSA_PSS_SALTLEN_DIGEST), 1);
  45180. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45181. RSA_PSS_SALTLEN_DIGEST), 1);
  45182. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  45183. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  45184. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45185. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  45186. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  45187. RSA_PSS_SALTLEN_MAX), 1);
  45188. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  45189. RSA_PSS_SALTLEN_MAX), 1);
  45190. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  45191. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  45192. RSA_free(rsa);
  45193. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45194. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  45195. return 0;
  45196. }
  45197. #endif
  45198. static int test_wolfSSL_RSA_sign_sha3(void)
  45199. {
  45200. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  45201. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  45202. RSA *rsa;
  45203. const unsigned char *derBuf = client_key_der_2048;
  45204. unsigned char sigRet[256] = {0};
  45205. unsigned int sigLen = sizeof(sigRet);
  45206. /* Random data simulating a hash */
  45207. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  45208. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  45209. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  45210. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  45211. };
  45212. printf(testingFmt, "wolfSSL_RSA_sign_sha3");
  45213. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  45214. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  45215. &sigLen, rsa), 1);
  45216. RSA_free(rsa);
  45217. printf(resultFmt, passed);
  45218. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  45219. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  45220. return 0;
  45221. }
  45222. static int test_wolfSSL_RSA_get0_key(void)
  45223. {
  45224. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  45225. RSA *rsa = NULL;
  45226. const BIGNUM* n = NULL;
  45227. const BIGNUM* e = NULL;
  45228. const BIGNUM* d = NULL;
  45229. const unsigned char* der;
  45230. int derSz;
  45231. #ifdef USE_CERT_BUFFERS_1024
  45232. der = client_key_der_1024;
  45233. derSz = sizeof_client_key_der_1024;
  45234. #elif defined(USE_CERT_BUFFERS_2048)
  45235. der = client_key_der_2048;
  45236. derSz = sizeof_client_key_der_2048;
  45237. #else
  45238. der = NULL;
  45239. derSz = 0;
  45240. #endif
  45241. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  45242. if (der != NULL) {
  45243. RSA_get0_key(NULL, NULL, NULL, NULL);
  45244. RSA_get0_key(rsa, NULL, NULL, NULL);
  45245. RSA_get0_key(NULL, &n, &e, &d);
  45246. AssertNull(n);
  45247. AssertNull(e);
  45248. AssertNull(d);
  45249. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  45250. AssertNotNull(rsa);
  45251. RSA_get0_key(rsa, NULL, NULL, NULL);
  45252. RSA_get0_key(rsa, &n, NULL, NULL);
  45253. AssertNotNull(n);
  45254. RSA_get0_key(rsa, NULL, &e, NULL);
  45255. AssertNotNull(e);
  45256. RSA_get0_key(rsa, NULL, NULL, &d);
  45257. AssertNotNull(d);
  45258. RSA_get0_key(rsa, &n, &e, &d);
  45259. AssertNotNull(n);
  45260. AssertNotNull(e);
  45261. AssertNotNull(d);
  45262. RSA_free(rsa);
  45263. }
  45264. printf(resultFmt, passed);
  45265. #endif
  45266. return 0;
  45267. }
  45268. static int test_wolfSSL_RSA_meth(void)
  45269. {
  45270. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45271. RSA *rsa;
  45272. RSA_METHOD *rsa_meth;
  45273. printf(testingFmt, "test_wolfSSL_RSA_meth");
  45274. #ifdef WOLFSSL_KEY_GEN
  45275. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45276. RSA_free(rsa);
  45277. #else
  45278. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45279. #endif
  45280. AssertNotNull(RSA_get_default_method());
  45281. wolfSSL_RSA_meth_free(NULL);
  45282. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  45283. AssertNotNull(rsa_meth =
  45284. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  45285. #ifndef NO_WOLFSSL_STUB
  45286. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  45287. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  45288. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  45289. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  45290. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  45291. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  45292. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  45293. #endif
  45294. AssertIntEQ(RSA_flags(NULL), 0);
  45295. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  45296. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  45297. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  45298. AssertNotNull(rsa = RSA_new());
  45299. /* No method set. */
  45300. AssertIntEQ(RSA_flags(rsa), 0);
  45301. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45302. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45303. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  45304. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  45305. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  45306. AssertNull(RSA_get_method(NULL));
  45307. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  45308. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  45309. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45310. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  45311. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  45312. RSA_METHOD_FLAG_NO_CHECK);
  45313. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  45314. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  45315. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  45316. /* rsa_meth is freed here */
  45317. RSA_free(rsa);
  45318. printf(resultFmt, passed);
  45319. #endif
  45320. return 0;
  45321. }
  45322. static int test_wolfSSL_RSA_verify(void)
  45323. {
  45324. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  45325. !defined(NO_FILESYSTEM)
  45326. #ifndef NO_BIO
  45327. XFILE fp;
  45328. RSA *pKey, *pubKey;
  45329. X509 *cert;
  45330. const char *text = "Hello wolfSSL !";
  45331. unsigned char hash[SHA256_DIGEST_LENGTH];
  45332. unsigned char signature[2048/8];
  45333. unsigned int signatureLength;
  45334. byte *buf;
  45335. BIO *bio;
  45336. SHA256_CTX c;
  45337. EVP_PKEY *evpPkey, *evpPubkey;
  45338. size_t sz;
  45339. printf(testingFmt, "wolfSSL_RSA_verify");
  45340. /* generate hash */
  45341. SHA256_Init(&c);
  45342. SHA256_Update(&c, text, strlen(text));
  45343. SHA256_Final(hash, &c);
  45344. #ifdef WOLFSSL_SMALL_STACK_CACHE
  45345. /* workaround for small stack cache case */
  45346. wc_Sha256Free((wc_Sha256*)&c);
  45347. #endif
  45348. /* read privete key file */
  45349. fp = XFOPEN(svrKeyFile, "rb");
  45350. AssertTrue((fp != XBADFILE));
  45351. AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
  45352. sz = XFTELL(fp);
  45353. XREWIND(fp);
  45354. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  45355. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  45356. XFCLOSE(fp);
  45357. /* read private key and sign hash data */
  45358. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  45359. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  45360. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  45361. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  45362. signature, &signatureLength, pKey), SSL_SUCCESS);
  45363. /* read public key and verify signed data */
  45364. fp = XFOPEN(svrCertFile,"rb");
  45365. AssertTrue((fp != XBADFILE));
  45366. cert = PEM_read_X509(fp, 0, 0, 0 );
  45367. XFCLOSE(fp);
  45368. evpPubkey = X509_get_pubkey(cert);
  45369. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  45370. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  45371. signatureLength, pubKey), SSL_SUCCESS);
  45372. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  45373. signatureLength, NULL), SSL_FAILURE);
  45374. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  45375. signatureLength, pubKey), SSL_FAILURE);
  45376. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  45377. signatureLength, pubKey), SSL_FAILURE);
  45378. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  45379. signatureLength, NULL), SSL_FAILURE);
  45380. RSA_free(pKey);
  45381. EVP_PKEY_free(evpPkey);
  45382. RSA_free(pubKey);
  45383. EVP_PKEY_free(evpPubkey);
  45384. X509_free(cert);
  45385. BIO_free(bio);
  45386. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  45387. printf(resultFmt, passed);
  45388. #endif
  45389. #endif
  45390. return 0;
  45391. }
  45392. static int test_wolfSSL_RSA_sign(void)
  45393. {
  45394. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45395. RSA *rsa;
  45396. unsigned char hash[SHA256_DIGEST_LENGTH];
  45397. #ifdef USE_CERT_BUFFERS_1024
  45398. const unsigned char* privDer = client_key_der_1024;
  45399. size_t privDerSz = sizeof_client_key_der_1024;
  45400. const unsigned char* pubDer = client_keypub_der_1024;
  45401. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45402. unsigned char signature[1024/8];
  45403. #else
  45404. const unsigned char* privDer = client_key_der_2048;
  45405. size_t privDerSz = sizeof_client_key_der_2048;
  45406. const unsigned char* pubDer = client_keypub_der_2048;
  45407. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45408. unsigned char signature[2048/8];
  45409. #endif
  45410. unsigned int signatureLen;
  45411. const unsigned char* der;
  45412. printf(testingFmt, "wolfSSL_RSA_sign");
  45413. XMEMSET(hash, 0, sizeof(hash));
  45414. der = privDer;
  45415. rsa = NULL;
  45416. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45417. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  45418. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  45419. &signatureLen, rsa), 0);
  45420. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  45421. &signatureLen, rsa), 0);
  45422. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  45423. &signatureLen, rsa), 0);
  45424. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  45425. NULL, rsa), 0);
  45426. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  45427. &signatureLen, NULL), 0);
  45428. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  45429. &signatureLen, rsa), 1);
  45430. RSA_free(rsa);
  45431. der = pubDer;
  45432. rsa = NULL;
  45433. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45434. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  45435. signatureLen, rsa), 1);
  45436. RSA_free(rsa);
  45437. printf(resultFmt, passed);
  45438. #endif
  45439. return 0;
  45440. }
  45441. static int test_wolfSSL_RSA_sign_ex(void)
  45442. {
  45443. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45444. RSA *rsa;
  45445. unsigned char hash[SHA256_DIGEST_LENGTH];
  45446. #ifdef USE_CERT_BUFFERS_1024
  45447. const unsigned char* privDer = client_key_der_1024;
  45448. size_t privDerSz = sizeof_client_key_der_1024;
  45449. const unsigned char* pubDer = client_keypub_der_1024;
  45450. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45451. unsigned char signature[1024/8];
  45452. #else
  45453. const unsigned char* privDer = client_key_der_2048;
  45454. size_t privDerSz = sizeof_client_key_der_2048;
  45455. const unsigned char* pubDer = client_keypub_der_2048;
  45456. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45457. unsigned char signature[2048/8];
  45458. #endif
  45459. unsigned int signatureLen;
  45460. const unsigned char* der;
  45461. unsigned char encodedHash[51];
  45462. unsigned int encodedHashLen;
  45463. const unsigned char expEncHash[] = {
  45464. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  45465. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  45466. 0x00, 0x04, 0x20,
  45467. /* Hash data */
  45468. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45469. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45470. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45471. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  45472. };
  45473. printf(testingFmt, "wolfSSL_RSA_sign_ex");
  45474. XMEMSET(hash, 0, sizeof(hash));
  45475. AssertNotNull(rsa = wolfSSL_RSA_new());
  45476. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45477. &signatureLen, rsa, 1), 0);
  45478. wolfSSL_RSA_free(rsa);
  45479. der = privDer;
  45480. rsa = NULL;
  45481. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45482. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  45483. -1), 0);
  45484. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  45485. signature, &signatureLen, rsa, 1), 0);
  45486. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  45487. &signatureLen, rsa, 1), 0);
  45488. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  45489. &signatureLen, rsa, 1), 0);
  45490. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45491. NULL, rsa, 1), 0);
  45492. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45493. &signatureLen, NULL, 1), 0);
  45494. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45495. &signatureLen, rsa, -1), 0);
  45496. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  45497. &signatureLen, rsa, 0), 0);
  45498. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  45499. &signatureLen, rsa, 0), 0);
  45500. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45501. NULL, rsa, 0), 0);
  45502. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  45503. &signatureLen, rsa, 1), 1);
  45504. /* Test returning encoded hash. */
  45505. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  45506. &encodedHashLen, rsa, 0), 1);
  45507. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  45508. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  45509. RSA_free(rsa);
  45510. der = pubDer;
  45511. rsa = NULL;
  45512. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45513. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  45514. signatureLen, rsa), 1);
  45515. RSA_free(rsa);
  45516. printf(resultFmt, passed);
  45517. #endif
  45518. return 0;
  45519. }
  45520. static int test_wolfSSL_RSA_public_decrypt(void)
  45521. {
  45522. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45523. RSA *rsa;
  45524. unsigned char msg[SHA256_DIGEST_LENGTH];
  45525. #ifdef USE_CERT_BUFFERS_1024
  45526. const unsigned char* pubDer = client_keypub_der_1024;
  45527. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45528. unsigned char decMsg[1024/8];
  45529. const unsigned char encMsg[] = {
  45530. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  45531. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  45532. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  45533. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  45534. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  45535. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  45536. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  45537. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  45538. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  45539. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  45540. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  45541. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  45542. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  45543. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  45544. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  45545. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  45546. };
  45547. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45548. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45549. defined(WC_RSA_NO_PADDING)
  45550. const unsigned char encMsgNoPad[] = {
  45551. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  45552. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  45553. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  45554. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  45555. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  45556. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  45557. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  45558. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  45559. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  45560. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  45561. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  45562. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  45563. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  45564. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  45565. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  45566. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  45567. };
  45568. #endif
  45569. #else
  45570. const unsigned char* pubDer = client_keypub_der_2048;
  45571. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45572. unsigned char decMsg[2048/8];
  45573. const unsigned char encMsg[] = {
  45574. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  45575. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  45576. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  45577. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  45578. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  45579. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  45580. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  45581. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  45582. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  45583. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  45584. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  45585. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  45586. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  45587. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  45588. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  45589. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  45590. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  45591. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  45592. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  45593. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  45594. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  45595. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  45596. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  45597. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  45598. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  45599. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  45600. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  45601. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  45602. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  45603. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  45604. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  45605. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  45606. };
  45607. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45608. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45609. defined(WC_RSA_NO_PADDING)
  45610. const unsigned char encMsgNoPad[] = {
  45611. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  45612. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  45613. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  45614. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  45615. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  45616. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  45617. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  45618. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  45619. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  45620. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  45621. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  45622. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  45623. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  45624. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  45625. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  45626. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  45627. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  45628. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  45629. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  45630. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  45631. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  45632. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  45633. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  45634. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  45635. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  45636. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  45637. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  45638. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  45639. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  45640. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  45641. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  45642. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  45643. };
  45644. #endif
  45645. #endif
  45646. const unsigned char* der;
  45647. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45648. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45649. defined(WC_RSA_NO_PADDING)
  45650. int i;
  45651. #endif
  45652. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  45653. XMEMSET(msg, 0, sizeof(msg));
  45654. der = pubDer;
  45655. rsa = NULL;
  45656. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45657. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  45658. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  45659. RSA_PKCS1_PADDING), -1);
  45660. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  45661. RSA_PKCS1_PADDING), -1);
  45662. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  45663. RSA_PKCS1_PADDING), -1);
  45664. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  45665. RSA_PKCS1_PADDING), -1);
  45666. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  45667. RSA_PKCS1_PSS_PADDING), -1);
  45668. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  45669. RSA_PKCS1_PADDING), 32);
  45670. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  45671. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  45672. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  45673. defined(WC_RSA_NO_PADDING)
  45674. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  45675. rsa, RSA_NO_PADDING), sizeof(decMsg));
  45676. /* Zeros before actual data. */
  45677. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  45678. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  45679. }
  45680. /* Check actual data. */
  45681. XMEMSET(msg, 0x01, sizeof(msg));
  45682. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  45683. #endif
  45684. RSA_free(rsa);
  45685. printf(resultFmt, passed);
  45686. #endif
  45687. return 0;
  45688. }
  45689. static int test_wolfSSL_RSA_private_encrypt(void)
  45690. {
  45691. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45692. RSA *rsa;
  45693. unsigned char msg[SHA256_DIGEST_LENGTH];
  45694. #ifdef USE_CERT_BUFFERS_1024
  45695. const unsigned char* privDer = client_key_der_1024;
  45696. size_t privDerSz = sizeof_client_key_der_1024;
  45697. unsigned char encMsg[1024/8];
  45698. const unsigned char expEncMsg[] = {
  45699. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  45700. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  45701. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  45702. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  45703. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  45704. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  45705. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  45706. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  45707. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  45708. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  45709. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  45710. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  45711. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  45712. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  45713. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  45714. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  45715. };
  45716. #ifdef WC_RSA_NO_PADDING
  45717. const unsigned char expEncMsgNoPad[] = {
  45718. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  45719. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  45720. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  45721. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  45722. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  45723. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  45724. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  45725. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  45726. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  45727. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  45728. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  45729. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  45730. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  45731. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  45732. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  45733. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  45734. };
  45735. #endif
  45736. #else
  45737. const unsigned char* privDer = client_key_der_2048;
  45738. size_t privDerSz = sizeof_client_key_der_2048;
  45739. unsigned char encMsg[2048/8];
  45740. const unsigned char expEncMsg[] = {
  45741. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  45742. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  45743. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  45744. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  45745. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  45746. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  45747. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  45748. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  45749. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  45750. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  45751. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  45752. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  45753. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  45754. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  45755. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  45756. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  45757. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  45758. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  45759. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  45760. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  45761. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  45762. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  45763. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  45764. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  45765. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  45766. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  45767. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  45768. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  45769. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  45770. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  45771. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  45772. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  45773. };
  45774. #ifdef WC_RSA_NO_PADDING
  45775. const unsigned char expEncMsgNoPad[] = {
  45776. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  45777. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  45778. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  45779. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  45780. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  45781. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  45782. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  45783. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  45784. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  45785. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  45786. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  45787. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  45788. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  45789. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  45790. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  45791. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  45792. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  45793. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  45794. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  45795. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  45796. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  45797. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  45798. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  45799. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  45800. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  45801. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  45802. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  45803. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  45804. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  45805. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  45806. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  45807. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  45808. };
  45809. #endif
  45810. #endif
  45811. const unsigned char* der;
  45812. printf(testingFmt, "wolfSSL_RSA_private_encrypt");
  45813. XMEMSET(msg, 0x00, sizeof(msg));
  45814. der = privDer;
  45815. rsa = NULL;
  45816. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45817. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  45818. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  45819. -1);
  45820. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  45821. RSA_PKCS1_PADDING), -1);
  45822. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  45823. RSA_PKCS1_PADDING), -1);
  45824. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  45825. RSA_PKCS1_PADDING), -1);
  45826. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  45827. RSA_PKCS1_PSS_PADDING), -1);
  45828. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  45829. RSA_PKCS1_PADDING), sizeof(encMsg));
  45830. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  45831. #ifdef WC_RSA_NO_PADDING
  45832. /* Non-zero message. */
  45833. XMEMSET(msg, 0x01, sizeof(msg));
  45834. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  45835. RSA_NO_PADDING), sizeof(encMsg));
  45836. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  45837. #endif
  45838. RSA_free(rsa);
  45839. printf(resultFmt, passed);
  45840. #endif
  45841. return 0;
  45842. }
  45843. static int test_wolfSSL_RSA_public_encrypt(void)
  45844. {
  45845. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45846. RSA* rsa;
  45847. const unsigned char msg[2048/8] = { 0 };
  45848. unsigned char encMsg[2048/8];
  45849. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  45850. AssertNotNull(rsa = RSA_new());
  45851. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  45852. RSA_PKCS1_PADDING), -1);
  45853. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  45854. RSA_PKCS1_PADDING), -1);
  45855. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  45856. RSA_PKCS1_PADDING), -1);
  45857. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  45858. RSA_PKCS1_PADDING), -1);
  45859. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  45860. RSA_PKCS1_PSS_PADDING), -1);
  45861. /* Empty RSA key. */
  45862. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  45863. RSA_PKCS1_PADDING), -1);
  45864. RSA_free(rsa);
  45865. printf(resultFmt, passed);
  45866. #endif
  45867. return 0;
  45868. }
  45869. static int test_wolfSSL_RSA_private_decrypt(void)
  45870. {
  45871. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  45872. RSA* rsa;
  45873. unsigned char msg[2048/8];
  45874. const unsigned char encMsg[2048/8] = { 0 };
  45875. printf(testingFmt, "wolfSSL_RSA_private_decrypt");
  45876. AssertNotNull(rsa = RSA_new());
  45877. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  45878. RSA_PKCS1_PADDING), -1);
  45879. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  45880. RSA_PKCS1_PADDING), -1);
  45881. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  45882. RSA_PKCS1_PADDING), -1);
  45883. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  45884. RSA_PKCS1_PADDING), -1);
  45885. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  45886. RSA_PKCS1_PSS_PADDING), -1);
  45887. /* Empty RSA key. */
  45888. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  45889. RSA_PKCS1_PADDING), -1);
  45890. RSA_free(rsa);
  45891. printf(resultFmt, passed);
  45892. #endif
  45893. return 0;
  45894. }
  45895. static int test_wolfSSL_RSA_GenAdd(void)
  45896. {
  45897. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45898. RSA *rsa;
  45899. #ifdef USE_CERT_BUFFERS_1024
  45900. const unsigned char* privDer = client_key_der_1024;
  45901. size_t privDerSz = sizeof_client_key_der_1024;
  45902. const unsigned char* pubDer = client_keypub_der_1024;
  45903. size_t pubDerSz = sizeof_client_keypub_der_1024;
  45904. #else
  45905. const unsigned char* privDer = client_key_der_2048;
  45906. size_t privDerSz = sizeof_client_key_der_2048;
  45907. const unsigned char* pubDer = client_keypub_der_2048;
  45908. size_t pubDerSz = sizeof_client_keypub_der_2048;
  45909. #endif
  45910. const unsigned char* der;
  45911. printf(testingFmt, "wolfSSL_RSA_GenAdd");
  45912. der = privDer;
  45913. rsa = NULL;
  45914. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45915. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  45916. #ifndef RSA_LOW_MEM
  45917. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  45918. #else
  45919. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  45920. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  45921. #endif
  45922. RSA_free(rsa);
  45923. der = pubDer;
  45924. rsa = NULL;
  45925. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  45926. /* Need private values. */
  45927. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  45928. RSA_free(rsa);
  45929. printf(resultFmt, passed);
  45930. #endif
  45931. return 0;
  45932. }
  45933. static int test_wolfSSL_RSA_blinding_on(void)
  45934. {
  45935. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  45936. RSA *rsa;
  45937. WOLFSSL_BN_CTX *bnCtx;
  45938. #ifdef USE_CERT_BUFFERS_1024
  45939. const unsigned char* privDer = client_key_der_1024;
  45940. size_t privDerSz = sizeof_client_key_der_1024;
  45941. #else
  45942. const unsigned char* privDer = client_key_der_2048;
  45943. size_t privDerSz = sizeof_client_key_der_2048;
  45944. #endif
  45945. const unsigned char* der;
  45946. printf(testingFmt, "wolfSSL_RSA_blinding_on");
  45947. der = privDer;
  45948. rsa = NULL;
  45949. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  45950. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  45951. /* Does nothing so all parameters are valid. */
  45952. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  45953. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  45954. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  45955. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  45956. wolfSSL_BN_CTX_free(bnCtx);
  45957. RSA_free(rsa);
  45958. printf(resultFmt, passed);
  45959. #endif
  45960. return 0;
  45961. }
  45962. static int test_wolfSSL_RSA_ex_data(void)
  45963. {
  45964. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  45965. RSA* rsa;
  45966. unsigned char data[1];
  45967. printf(testingFmt, "wolfSSL_RSA_ex_data");
  45968. rsa = RSA_new();
  45969. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  45970. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  45971. #ifdef MAX_EX_DATA
  45972. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  45973. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  45974. #endif
  45975. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  45976. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  45977. #ifdef HAVE_EX_DATA
  45978. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  45979. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  45980. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  45981. #else
  45982. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  45983. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  45984. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  45985. #endif
  45986. RSA_free(rsa);
  45987. printf(resultFmt, passed);
  45988. #endif /* !NO_RSA && OPENSSL_EXTRA */
  45989. return 0;
  45990. }
  45991. static int test_wolfSSL_RSA_LoadDer(void)
  45992. {
  45993. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  45994. defined(OPENSSL_EXTRA_X509_SMALL))
  45995. RSA *rsa;
  45996. #ifdef USE_CERT_BUFFERS_1024
  45997. const unsigned char* privDer = client_key_der_1024;
  45998. size_t privDerSz = sizeof_client_key_der_1024;
  45999. #else
  46000. const unsigned char* privDer = client_key_der_2048;
  46001. size_t privDerSz = sizeof_client_key_der_2048;
  46002. #endif
  46003. printf(testingFmt, "wolfSSL_RSA_LoadDer");
  46004. AssertNotNull(rsa = RSA_new());
  46005. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  46006. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  46007. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  46008. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  46009. RSA_free(rsa);
  46010. printf(resultFmt, passed);
  46011. #endif
  46012. return 0;
  46013. }
  46014. /* Local API. */
  46015. static int test_wolfSSL_RSA_To_Der(void)
  46016. {
  46017. #ifdef WOLFSSL_TEST_STATIC_BUILD
  46018. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  46019. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46020. RSA* rsa;
  46021. #ifdef USE_CERT_BUFFERS_1024
  46022. const unsigned char* privDer = client_key_der_1024;
  46023. size_t privDerSz = sizeof_client_key_der_1024;
  46024. const unsigned char* pubDer = client_keypub_der_1024;
  46025. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46026. unsigned char out[sizeof(client_key_der_1024)];
  46027. #else
  46028. const unsigned char* privDer = client_key_der_2048;
  46029. size_t privDerSz = sizeof_client_key_der_2048;
  46030. const unsigned char* pubDer = client_keypub_der_2048;
  46031. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46032. unsigned char out[sizeof(client_key_der_2048)];
  46033. #endif
  46034. const unsigned char* der;
  46035. unsigned char* outDer = NULL;
  46036. printf(testingFmt, "wolfSSL_RSA_To_Der");
  46037. der = privDer;
  46038. rsa = NULL;
  46039. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46040. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  46041. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  46042. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  46043. outDer = out;
  46044. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  46045. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  46046. outDer = NULL;
  46047. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  46048. AssertNotNull(outDer);
  46049. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  46050. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46051. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  46052. outDer = out;
  46053. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  46054. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  46055. RSA_free(rsa);
  46056. AssertNotNull(rsa = RSA_new());
  46057. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  46058. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  46059. RSA_free(rsa);
  46060. der = pubDer;
  46061. rsa = NULL;
  46062. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46063. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  46064. RSA_free(rsa);
  46065. printf(resultFmt, passed);
  46066. #endif
  46067. #endif
  46068. return 0;
  46069. }
  46070. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  46071. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  46072. {
  46073. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46074. !defined(NO_WOLFSSL_STUB)
  46075. RSA* rsa = NULL;
  46076. XFILE fp;
  46077. printf(testingFmt, "wolfSSL_PEM_read_RSAPublicKey");
  46078. fp = XFOPEN("./certs/client-keyPub.pem", "rb");
  46079. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  46080. /* Valid but stub so returns NULL. */
  46081. AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, NULL, NULL, NULL));
  46082. /* Valid but stub so returns NULL. */
  46083. AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL));
  46084. XFCLOSE(fp);
  46085. printf(resultFmt, passed);
  46086. #endif
  46087. return 0;
  46088. }
  46089. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  46090. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  46091. {
  46092. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46093. !defined(NO_WOLFSSL_STUB)
  46094. RSA* rsa = NULL;
  46095. printf(testingFmt, "wolfSSL_PEM_write_RSA_PUBKEY");
  46096. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  46097. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, NULL), 0);
  46098. /* Valid but stub so returns 0. */
  46099. AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, rsa), 0);
  46100. printf(resultFmt, passed);
  46101. #endif
  46102. return 0;
  46103. }
  46104. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  46105. {
  46106. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  46107. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  46108. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  46109. RSA* rsa;
  46110. #ifdef USE_CERT_BUFFERS_1024
  46111. const unsigned char* privDer = client_key_der_1024;
  46112. size_t privDerSz = sizeof_client_key_der_1024;
  46113. #else
  46114. const unsigned char* privDer = client_key_der_2048;
  46115. size_t privDerSz = sizeof_client_key_der_2048;
  46116. #endif
  46117. const unsigned char* der;
  46118. #ifndef NO_AES
  46119. unsigned char passwd[] = "password";
  46120. #endif
  46121. printf(testingFmt, "wolfSSL_PEM_write_RSAPrivateKey");
  46122. AssertNotNull(rsa = RSA_new());
  46123. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  46124. NULL, NULL), 0);
  46125. RSA_free(rsa);
  46126. der = privDer;
  46127. rsa = NULL;
  46128. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46129. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  46130. NULL, NULL), 0);
  46131. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, NULL, NULL, NULL, 0,
  46132. NULL, NULL), 0);
  46133. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  46134. NULL, NULL), 1);
  46135. #ifndef NO_AES
  46136. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  46137. NULL, 0, NULL, NULL), 1);
  46138. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  46139. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  46140. #endif
  46141. RSA_free(rsa);
  46142. printf(resultFmt, passed);
  46143. #endif
  46144. return 0;
  46145. }
  46146. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  46147. {
  46148. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  46149. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  46150. defined(WOLFSSL_DER_TO_PEM))
  46151. RSA* rsa;
  46152. #ifdef USE_CERT_BUFFERS_1024
  46153. const unsigned char* privDer = client_key_der_1024;
  46154. size_t privDerSz = sizeof_client_key_der_1024;
  46155. #else
  46156. const unsigned char* privDer = client_key_der_2048;
  46157. size_t privDerSz = sizeof_client_key_der_2048;
  46158. #endif
  46159. const unsigned char* der;
  46160. #ifndef NO_AES
  46161. unsigned char passwd[] = "password";
  46162. #endif
  46163. unsigned char* pem;
  46164. int plen;
  46165. printf(testingFmt, "wolfSSL_PEM_write_mem_RSAPrivateKey");
  46166. AssertNotNull(rsa = RSA_new());
  46167. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  46168. &plen), 0);
  46169. RSA_free(rsa);
  46170. der = privDer;
  46171. rsa = NULL;
  46172. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46173. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  46174. &plen), 0);
  46175. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  46176. &plen), 0);
  46177. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  46178. NULL), 0);
  46179. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  46180. &plen), 1);
  46181. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  46182. #ifndef NO_AES
  46183. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  46184. NULL, 0, &pem, &plen), 1);
  46185. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  46186. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  46187. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  46188. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  46189. #endif
  46190. RSA_free(rsa);
  46191. printf(resultFmt, passed);
  46192. #endif
  46193. return 0;
  46194. }
  46195. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  46196. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  46197. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  46198. {
  46199. X509* x509 = NULL;
  46200. BIGNUM* serial_number = NULL;
  46201. X509_NAME* name = NULL;
  46202. time_t epoch_off = 0;
  46203. ASN1_INTEGER* asn1_serial_number;
  46204. long not_before, not_after;
  46205. AssertNotNull(x509 = X509_new());
  46206. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  46207. AssertNotNull(serial_number = BN_new());
  46208. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  46209. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  46210. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  46211. /* version 3 */
  46212. AssertIntNE(X509_set_version(x509, 2L), 0);
  46213. AssertNotNull(name = X509_NAME_new());
  46214. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  46215. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  46216. AssertIntNE(X509_set_subject_name(x509, name), 0);
  46217. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  46218. not_before = (long)wc_Time(NULL);
  46219. not_after = not_before + (365 * 24 * 60 * 60);
  46220. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  46221. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  46222. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  46223. BN_free(serial_number);
  46224. X509_NAME_free(name);
  46225. X509_free(x509);
  46226. return 0;
  46227. }
  46228. #endif
  46229. static int test_openssl_generate_key_and_cert(void)
  46230. {
  46231. #if defined(OPENSSL_EXTRA)
  46232. #if !defined(NO_RSA)
  46233. {
  46234. EVP_PKEY* pkey = EVP_PKEY_new();
  46235. int key_length = 2048;
  46236. BIGNUM* exponent = BN_new();
  46237. RSA* rsa = RSA_new();
  46238. AssertNotNull(pkey);
  46239. AssertNotNull(exponent);
  46240. AssertNotNull(rsa);
  46241. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  46242. #ifndef WOLFSSL_KEY_GEN
  46243. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  46244. #if defined(USE_CERT_BUFFERS_1024)
  46245. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  46246. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  46247. key_length = 1024;
  46248. #elif defined(USE_CERT_BUFFERS_2048)
  46249. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  46250. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  46251. #else
  46252. RSA_free(rsa);
  46253. rsa = NULL;
  46254. #endif
  46255. #else
  46256. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  46257. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  46258. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  46259. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  46260. #endif
  46261. if (rsa) {
  46262. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  46263. BN_free(exponent);
  46264. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  46265. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  46266. test_openssl_make_self_signed_certificate(pkey);
  46267. #endif
  46268. }
  46269. EVP_PKEY_free(pkey);
  46270. }
  46271. #endif /* !NO_RSA */
  46272. #ifdef HAVE_ECC
  46273. {
  46274. EVP_PKEY* pkey = EVP_PKEY_new();
  46275. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  46276. AssertNotNull(pkey);
  46277. AssertNotNull(ec_key);
  46278. #ifndef NO_WOLFSSL_STUB
  46279. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  46280. #endif
  46281. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  46282. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  46283. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  46284. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  46285. test_openssl_make_self_signed_certificate(pkey);
  46286. #endif
  46287. EVP_PKEY_free(pkey);
  46288. }
  46289. #endif /* HAVE_ECC */
  46290. #endif /* OPENSSL_EXTRA */
  46291. return 0;
  46292. }
  46293. static int test_stubs_are_stubs(void)
  46294. {
  46295. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  46296. WOLFSSL_CTX* ctx = NULL;
  46297. WOLFSSL_CTX* ctxN = NULL;
  46298. #ifndef NO_WOLFSSL_CLIENT
  46299. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  46300. AssertNotNull(ctx);
  46301. #elif !defined(NO_WOLFSSL_SERVER)
  46302. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  46303. AssertNotNull(ctx);
  46304. #else
  46305. return;
  46306. #endif
  46307. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  46308. AssertIntEQ((int) x(z), 0)
  46309. /* test logic, all stubs return same result regardless of ctx being NULL
  46310. * as there are no sanity checks, it's just a stub! If at some
  46311. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  46312. * if invalid inputs are supplied. Test calling both
  46313. * with and without valid inputs, if a stub functionality remains unchanged.
  46314. */
  46315. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  46316. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  46317. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  46318. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  46319. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  46320. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  46321. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  46322. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  46323. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  46324. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  46325. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  46326. wolfSSL_CTX_free(ctx);
  46327. ctx = NULL;
  46328. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  46329. return 0;
  46330. }
  46331. static int test_CONF_modules_xxx(void)
  46332. {
  46333. #if defined(OPENSSL_EXTRA)
  46334. CONF_modules_free();
  46335. AssertTrue(1); /* to confirm previous call gives no harm */
  46336. CONF_modules_unload(0);
  46337. AssertTrue(1);
  46338. CONF_modules_unload(1);
  46339. AssertTrue(1);
  46340. CONF_modules_unload(-1);
  46341. AssertTrue(1);
  46342. #endif /* OPENSSL_EXTRA */
  46343. return 0;
  46344. }
  46345. static int test_CRYPTO_set_dynlock_xxx(void)
  46346. {
  46347. #if defined(OPENSSL_EXTRA)
  46348. printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
  46349. CRYPTO_set_dynlock_create_callback(
  46350. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  46351. CRYPTO_set_dynlock_create_callback(
  46352. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  46353. CRYPTO_set_dynlock_destroy_callback(
  46354. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  46355. CRYPTO_set_dynlock_destroy_callback(
  46356. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  46357. CRYPTO_set_dynlock_lock_callback(
  46358. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  46359. CRYPTO_set_dynlock_lock_callback(
  46360. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  46361. AssertTrue(1); /* to confirm previous call gives no harm */
  46362. printf(resultFmt, passed);
  46363. #endif /* OPENSSL_EXTRA */
  46364. return 0;
  46365. }
  46366. static int test_CRYPTO_THREADID_xxx(void)
  46367. {
  46368. #if defined(OPENSSL_EXTRA)
  46369. printf(testingFmt, "CRYPTO_THREADID_xxx()");
  46370. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  46371. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  46372. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  46373. printf(resultFmt, passed);
  46374. #endif /* OPENSSL_EXTRA */
  46375. return 0;
  46376. }
  46377. static int test_ENGINE_cleanup(void)
  46378. {
  46379. #if defined(OPENSSL_EXTRA)
  46380. printf(testingFmt, "ENGINE_cleanup()");
  46381. ENGINE_cleanup();
  46382. AssertTrue(1); /* to confirm previous call gives no harm */
  46383. printf(resultFmt, passed);
  46384. #endif /* OPENSSL_EXTRA */
  46385. return 0;
  46386. }
  46387. static int test_wolfSSL_CTX_LoadCRL(void)
  46388. {
  46389. #if defined(HAVE_CRL) && !defined(NO_RSA)
  46390. WOLFSSL_CTX* ctx = NULL;
  46391. WOLFSSL* ssl = NULL;
  46392. const char* badPath = "dummypath";
  46393. const char* validPath = "./certs/crl";
  46394. const char* validFilePath = "./certs/crl/cliCrl.pem";
  46395. const char* issuerCert = "./certs/client-cert.pem";
  46396. int derType = WOLFSSL_FILETYPE_ASN1;
  46397. int pemType = WOLFSSL_FILETYPE_PEM;
  46398. int monitor = WOLFSSL_CRL_MONITOR;
  46399. WOLFSSL_CERT_MANAGER* cm = NULL;
  46400. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  46401. BAD_FUNC_ARG)
  46402. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  46403. WOLFSSL_SUCCESS)
  46404. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  46405. #ifndef NO_WOLFSSL_CLIENT
  46406. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46407. #elif !defined(NO_WOLFSSL_SERVER)
  46408. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46409. #else
  46410. return;
  46411. #endif
  46412. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  46413. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  46414. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  46415. wolfSSL_CTX_free(ctx);
  46416. #ifndef NO_WOLFSSL_CLIENT
  46417. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46418. #elif !defined(NO_WOLFSSL_SERVER)
  46419. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46420. #else
  46421. return;
  46422. #endif
  46423. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  46424. WOLFSSL_SUCCESS);
  46425. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  46426. wolfSSL_CTX_free(ctx);
  46427. #ifndef NO_WOLFSSL_CLIENT
  46428. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  46429. #elif !defined(NO_WOLFSSL_SERVER)
  46430. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  46431. #else
  46432. return;
  46433. #endif
  46434. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  46435. WOLFSSL_SUCCESS);
  46436. AssertNotNull(ssl = wolfSSL_new(ctx));
  46437. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  46438. wolfSSL_free(ssl);
  46439. wolfSSL_CTX_free(ctx);
  46440. AssertNotNull(cm = wolfSSL_CertManagerNew());
  46441. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  46442. WOLFSSL_SUCCESS);
  46443. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  46444. wolfSSL_CertManagerFree(cm);
  46445. #endif
  46446. return 0;
  46447. }
  46448. static int test_SetTmpEC_DHE_Sz(void)
  46449. {
  46450. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  46451. WOLFSSL_CTX *ctx;
  46452. WOLFSSL *ssl;
  46453. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  46454. AssertNotNull(ctx);
  46455. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  46456. ssl = wolfSSL_new(ctx);
  46457. AssertNotNull(ssl);
  46458. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  46459. wolfSSL_free(ssl);
  46460. wolfSSL_CTX_free(ctx);
  46461. #endif
  46462. return 0;
  46463. }
  46464. static int test_wolfSSL_CTX_get0_privatekey(void)
  46465. {
  46466. #ifdef OPENSSL_ALL
  46467. WOLFSSL_CTX* ctx = NULL;
  46468. printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
  46469. #ifndef NO_RSA
  46470. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46471. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46472. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  46473. WOLFSSL_FILETYPE_PEM));
  46474. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46475. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  46476. WOLFSSL_FILETYPE_PEM));
  46477. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  46478. wolfSSL_CTX_free(ctx);
  46479. #endif
  46480. #ifdef HAVE_ECC
  46481. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  46482. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46483. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  46484. WOLFSSL_FILETYPE_PEM));
  46485. AssertNull(SSL_CTX_get0_privatekey(ctx));
  46486. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  46487. WOLFSSL_FILETYPE_PEM));
  46488. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  46489. wolfSSL_CTX_free(ctx);
  46490. #endif
  46491. printf(resultFmt, passed);
  46492. #endif
  46493. return 0;
  46494. }
  46495. static int test_wolfSSL_dtls_set_mtu(void)
  46496. {
  46497. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  46498. defined(WOLFSSL_DTLS)
  46499. WOLFSSL_CTX* ctx = NULL;
  46500. WOLFSSL* ssl = NULL;
  46501. const char* testCertFile;
  46502. const char* testKeyFile;
  46503. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  46504. #ifndef NO_RSA
  46505. testCertFile = svrCertFile;
  46506. testKeyFile = svrKeyFile;
  46507. #elif defined(HAVE_ECC)
  46508. testCertFile = eccCertFile;
  46509. testKeyFile = eccKeyFile;
  46510. #endif
  46511. if (testCertFile != NULL && testKeyFile != NULL) {
  46512. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  46513. WOLFSSL_FILETYPE_PEM));
  46514. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  46515. WOLFSSL_FILETYPE_PEM));
  46516. }
  46517. AssertNotNull(ssl = wolfSSL_new(ctx));
  46518. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  46519. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  46520. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  46521. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  46522. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  46523. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  46524. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  46525. wolfSSL_free(ssl);
  46526. wolfSSL_CTX_free(ctx);
  46527. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  46528. printf(resultFmt, passed);
  46529. #endif
  46530. return 0;
  46531. }
  46532. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  46533. defined(WOLFSSL_DTLS)
  46534. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  46535. enum HandShakeType hsType, word16 length)
  46536. {
  46537. size_t idx = 0;
  46538. byte* l;
  46539. /* record layer */
  46540. /* handshake type */
  46541. out[idx++] = handshake;
  46542. /* protocol version */
  46543. out[idx++] = 0xfe;
  46544. out[idx++] = 0xfd; /* DTLS 1.2 */
  46545. /* epoch 0 */
  46546. XMEMSET(out + idx, 0, 2);
  46547. idx += 2;
  46548. /* sequence number */
  46549. XMEMSET(out + idx, 0, 6);
  46550. c32toa(seq, out + idx + 2);
  46551. idx += 6;
  46552. /* length in BE */
  46553. if (length)
  46554. c16toa(length, out + idx);
  46555. else
  46556. c16toa(outSz - idx - 2, out + idx);
  46557. idx += 2;
  46558. /* handshake layer */
  46559. /* handshake type */
  46560. out[idx++] = (byte)hsType;
  46561. /* length */
  46562. l = out + idx;
  46563. idx += 3;
  46564. /* message seq */
  46565. c16toa(0, out + idx);
  46566. idx += 2;
  46567. /* frag offset */
  46568. c32to24(0, out + idx);
  46569. idx += 3;
  46570. /* frag length */
  46571. c32to24((word32)outSz - (word32)idx - 3, l);
  46572. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  46573. idx += 3;
  46574. XMEMSET(out + idx, 0, outSz - idx);
  46575. }
  46576. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  46577. {
  46578. byte msg[] = "This is a msg for the client";
  46579. byte reply[40];
  46580. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  46581. reply[sizeof(reply) - 1] = '\0';
  46582. printf("Client message: %s\n", reply);
  46583. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  46584. }
  46585. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  46586. {
  46587. byte ch[50];
  46588. int fd = wolfSSL_get_fd(ssl);
  46589. byte msg[] = "This is a msg for the server";
  46590. byte reply[40];
  46591. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  46592. /* Server should ignore this datagram */
  46593. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  46594. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  46595. /* Server should ignore this datagram */
  46596. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  46597. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  46598. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  46599. reply[sizeof(reply) - 1] = '\0';
  46600. printf("Server response: %s\n", reply);
  46601. }
  46602. static int test_wolfSSL_dtls_plaintext(void)
  46603. {
  46604. tcp_ready ready;
  46605. func_args client_args;
  46606. func_args server_args;
  46607. callback_functions func_cb_client;
  46608. callback_functions func_cb_server;
  46609. THREAD_TYPE serverThread;
  46610. size_t i;
  46611. struct test_params {
  46612. method_provider client_meth;
  46613. method_provider server_meth;
  46614. ssl_callback on_result_server;
  46615. ssl_callback on_result_client;
  46616. } params[] = {
  46617. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  46618. test_wolfSSL_dtls_plaintext_server,
  46619. test_wolfSSL_dtls_plaintext_client},
  46620. };
  46621. printf(testingFmt, "test_wolfSSL_dtls_plaintext");
  46622. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  46623. XMEMSET(&client_args, 0, sizeof(func_args));
  46624. XMEMSET(&server_args, 0, sizeof(func_args));
  46625. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  46626. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  46627. #ifdef WOLFSSL_TIRTOS
  46628. fdOpenSession(Task_self());
  46629. #endif
  46630. StartTCP();
  46631. InitTcpReady(&ready);
  46632. #if defined(USE_WINDOWS_API)
  46633. /* use RNG to get random port if using windows */
  46634. ready.port = GetRandomPort();
  46635. #endif
  46636. server_args.signal = &ready;
  46637. server_args.callbacks = &func_cb_server;
  46638. client_args.signal = &ready;
  46639. client_args.callbacks = &func_cb_client;
  46640. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  46641. func_cb_server.method = params[i].server_meth;
  46642. func_cb_client.method = params[i].client_meth;
  46643. func_cb_client.on_result = params[i].on_result_client;
  46644. func_cb_server.on_result = params[i].on_result_server;
  46645. start_thread(test_server_nofail, &server_args, &serverThread);
  46646. wait_tcp_ready(&server_args);
  46647. test_client_nofail(&client_args, NULL);
  46648. join_thread(serverThread);
  46649. AssertTrue(client_args.return_code);
  46650. AssertTrue(server_args.return_code);
  46651. FreeTcpReady(&ready);
  46652. #ifdef WOLFSSL_TIRTOS
  46653. fdOpenSession(Task_self());
  46654. #endif
  46655. }
  46656. printf(resultFmt, passed);
  46657. return 0;
  46658. }
  46659. #else
  46660. static int test_wolfSSL_dtls_plaintext(void) {
  46661. return 0;
  46662. }
  46663. #endif
  46664. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  46665. defined(WOLFSSL_DTLS)
  46666. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  46667. {
  46668. byte b[1100]; /* buffer for the messages to send */
  46669. size_t idx = 0;
  46670. size_t seq_offset = 0;
  46671. size_t msg_offset = 0;
  46672. int i;
  46673. int fd = wolfSSL_get_fd(ssl);
  46674. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  46675. word32 seq_number = 100; /* start high so server definitely reads this */
  46676. word16 msg_number = 50; /* start high so server has to buffer this */
  46677. AssertIntEQ(ret, 1);
  46678. /* Now let's start spamming the peer with fragments it needs to store */
  46679. XMEMSET(b, -1, sizeof(b));
  46680. /* record layer */
  46681. /* handshake type */
  46682. b[idx++] = 22;
  46683. /* protocol version */
  46684. b[idx++] = 0xfe;
  46685. b[idx++] = 0xfd; /* DTLS 1.2 */
  46686. /* epoch 0 */
  46687. XMEMSET(b + idx, 0, 2);
  46688. idx += 2;
  46689. /* sequence number */
  46690. XMEMSET(b + idx, 0, 6);
  46691. seq_offset = idx + 2; /* increment only the low 32 bits */
  46692. idx += 6;
  46693. /* static length in BE */
  46694. c16toa(42, b + idx);
  46695. idx += 2;
  46696. /* handshake layer */
  46697. /* cert type */
  46698. b[idx++] = 11;
  46699. /* length */
  46700. c32to24(1000, b + idx);
  46701. idx += 3;
  46702. /* message seq */
  46703. c16toa(0, b + idx);
  46704. msg_offset = idx;
  46705. idx += 2;
  46706. /* frag offset */
  46707. c32to24(500, b + idx);
  46708. idx += 3;
  46709. /* frag length */
  46710. c32to24(30, b + idx);
  46711. idx += 3;
  46712. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  46713. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  46714. seq_number++, msg_number++, i++) {
  46715. struct timespec delay;
  46716. XMEMSET(&delay, 0, sizeof(delay));
  46717. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  46718. c32toa(seq_number, b + seq_offset);
  46719. c16toa(msg_number, b + msg_offset);
  46720. ret = (int)send(fd, b, 55, 0);
  46721. nanosleep(&delay, NULL);
  46722. }
  46723. }
  46724. #ifdef WOLFSSL_DTLS13
  46725. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  46726. {
  46727. byte b[150]; /* buffer for the messages to send */
  46728. size_t idx = 0;
  46729. size_t msg_offset = 0;
  46730. int fd = wolfSSL_get_fd(ssl);
  46731. word16 msg_number = 10; /* start high so server has to buffer this */
  46732. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  46733. AssertIntEQ(ret, 1);
  46734. /* Now let's start spamming the peer with fragments it needs to store */
  46735. XMEMSET(b, -1, sizeof(b));
  46736. /* handshake type */
  46737. b[idx++] = 11;
  46738. /* length */
  46739. c32to24(10000, b + idx);
  46740. idx += 3;
  46741. /* message_seq */
  46742. msg_offset = idx;
  46743. idx += 2;
  46744. /* fragment_offset */
  46745. c32to24(5000, b + idx);
  46746. idx += 3;
  46747. /* fragment_length */
  46748. c32to24(100, b + idx);
  46749. idx += 3;
  46750. /* fragment contents */
  46751. idx += 100;
  46752. for (; ret > 0; msg_number++) {
  46753. byte sendBuf[150];
  46754. int sendSz = sizeof(sendBuf);
  46755. struct timespec delay;
  46756. XMEMSET(&delay, 0, sizeof(delay));
  46757. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  46758. c16toa(msg_number, b + msg_offset);
  46759. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  46760. (int)idx, handshake, 0, 0, 0);
  46761. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  46762. nanosleep(&delay, NULL);
  46763. }
  46764. }
  46765. #endif
  46766. static int test_wolfSSL_dtls_fragments(void)
  46767. {
  46768. tcp_ready ready;
  46769. func_args client_args;
  46770. func_args server_args;
  46771. callback_functions func_cb_client;
  46772. callback_functions func_cb_server;
  46773. THREAD_TYPE serverThread;
  46774. size_t i;
  46775. struct test_params {
  46776. method_provider client_meth;
  46777. method_provider server_meth;
  46778. ssl_callback spammer;
  46779. } params[] = {
  46780. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  46781. test_wolfSSL_dtls12_fragments_spammer},
  46782. #ifdef WOLFSSL_DTLS13
  46783. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  46784. test_wolfSSL_dtls13_fragments_spammer},
  46785. #endif
  46786. };
  46787. printf(testingFmt, "test_wolfSSL_dtls_fragments");
  46788. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  46789. XMEMSET(&client_args, 0, sizeof(func_args));
  46790. XMEMSET(&server_args, 0, sizeof(func_args));
  46791. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  46792. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  46793. #ifdef WOLFSSL_TIRTOS
  46794. fdOpenSession(Task_self());
  46795. #endif
  46796. StartTCP();
  46797. InitTcpReady(&ready);
  46798. #if defined(USE_WINDOWS_API)
  46799. /* use RNG to get random port if using windows */
  46800. ready.port = GetRandomPort();
  46801. #endif
  46802. server_args.signal = &ready;
  46803. server_args.callbacks = &func_cb_server;
  46804. client_args.signal = &ready;
  46805. client_args.callbacks = &func_cb_client;
  46806. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  46807. func_cb_server.method = params[i].server_meth;
  46808. func_cb_client.method = params[i].client_meth;
  46809. func_cb_client.ssl_ready = params[i].spammer;
  46810. start_thread(test_server_nofail, &server_args, &serverThread);
  46811. wait_tcp_ready(&server_args);
  46812. test_client_nofail(&client_args, NULL);
  46813. join_thread(serverThread);
  46814. AssertFalse(client_args.return_code);
  46815. AssertFalse(server_args.return_code);
  46816. /* The socket should be closed by the server resulting in a
  46817. * socket error */
  46818. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  46819. /* Check the server returned an error indicating the msg buffer
  46820. * was full */
  46821. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  46822. FreeTcpReady(&ready);
  46823. #ifdef WOLFSSL_TIRTOS
  46824. fdOpenSession(Task_self());
  46825. #endif
  46826. }
  46827. printf(resultFmt, passed);
  46828. return 0;
  46829. }
  46830. #else
  46831. static int test_wolfSSL_dtls_fragments(void) {
  46832. return 0;
  46833. }
  46834. #endif
  46835. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  46836. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  46837. !defined(WOLFSSL_NO_CLIENT_AUTH))
  46838. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  46839. {
  46840. int ret;
  46841. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  46842. printf("loading cert %s failed\n", certA);
  46843. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  46844. return -1;
  46845. }
  46846. return 0;
  46847. }
  46848. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  46849. {
  46850. int ret;
  46851. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  46852. != WOLFSSL_SUCCESS) {
  46853. printf("could not verify the cert: %s\n", certA);
  46854. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  46855. return -1;
  46856. } else {
  46857. printf("successfully verified: %s\n", certA);
  46858. }
  46859. return 0;
  46860. }
  46861. #define LOAD_ONE_CA(a, b, c, d) \
  46862. do { \
  46863. (a) = load_ca_into_cm(c, d); \
  46864. if ((a) != 0) \
  46865. return (b); \
  46866. else \
  46867. (b)--; \
  46868. } while(0)
  46869. #define VERIFY_ONE_CERT(a, b, c, d) \
  46870. do { \
  46871. (a) = verify_cert_with_cm(c, d); \
  46872. if ((a) != 0) \
  46873. return (b); \
  46874. else \
  46875. (b)--; \
  46876. } while(0)
  46877. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  46878. {
  46879. int ret;
  46880. int i = -1;
  46881. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  46882. char chainGArr[9][50] = {"certs/ca-cert.pem",
  46883. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  46884. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  46885. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  46886. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  46887. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  46888. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  46889. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  46890. "certs/test-pathlen/chainG-entity.pem"};
  46891. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  46892. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  46893. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  46894. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  46895. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  46896. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  46897. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  46898. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  46899. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  46900. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  46901. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  46902. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  46903. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  46904. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  46905. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  46906. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  46907. /* test validating the entity twice, should have no effect on pathLen since
  46908. * entity/leaf cert */
  46909. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  46910. return ret;
  46911. }
  46912. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  46913. {
  46914. int ret;
  46915. int i = -1;
  46916. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  46917. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  46918. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  46919. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  46920. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  46921. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  46922. */
  46923. char chainHArr[6][50] = {"certs/ca-cert.pem",
  46924. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  46925. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  46926. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  46927. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  46928. "certs/test-pathlen/chainH-entity.pem"};
  46929. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  46930. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  46931. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  46932. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  46933. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  46934. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  46935. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  46936. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  46937. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  46938. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  46939. return ret;
  46940. }
  46941. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  46942. {
  46943. int ret;
  46944. int i = -1;
  46945. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  46946. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  46947. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  46948. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  46949. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  46950. */
  46951. char chainIArr[5][50] = {"certs/ca-cert.pem",
  46952. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  46953. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  46954. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  46955. "certs/test-pathlen/chainI-entity.pem"};
  46956. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  46957. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  46958. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  46959. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  46960. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  46961. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  46962. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  46963. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  46964. return ret;
  46965. }
  46966. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  46967. {
  46968. int ret;
  46969. int i = -1;
  46970. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  46971. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  46972. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  46973. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  46974. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  46975. */
  46976. char chainJArr[6][50] = {"certs/ca-cert.pem",
  46977. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  46978. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  46979. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  46980. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  46981. "certs/test-pathlen/chainJ-entity.pem"};
  46982. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  46983. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  46984. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  46985. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  46986. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  46987. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  46988. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  46989. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  46990. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  46991. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  46992. return ret;
  46993. }
  46994. static int test_various_pathlen_chains(void)
  46995. {
  46996. int ret;
  46997. WOLFSSL_CERT_MANAGER* cm;
  46998. /* Test chain G (large chain with varying pathLens) */
  46999. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47000. printf("cert manager new failed\n");
  47001. return -1;
  47002. }
  47003. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47004. AssertIntEQ(test_chainG(cm), -1);
  47005. #else
  47006. AssertIntEQ(test_chainG(cm), 0);
  47007. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  47008. ret = wolfSSL_CertManagerUnloadCAs(cm);
  47009. if (ret != WOLFSSL_SUCCESS)
  47010. return -1;
  47011. wolfSSL_CertManagerFree(cm);
  47012. /* end test chain G */
  47013. /* Test chain H (5 chain with same pathLens) */
  47014. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47015. printf("cert manager new failed\n");
  47016. return -1;
  47017. }
  47018. AssertIntLT(test_chainH(cm), 0);
  47019. wolfSSL_CertManagerUnloadCAs(cm);
  47020. wolfSSL_CertManagerFree(cm);
  47021. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47022. printf("cert manager new failed\n");
  47023. return -1;
  47024. }
  47025. ret = wolfSSL_CertManagerUnloadCAs(cm);
  47026. if (ret != WOLFSSL_SUCCESS)
  47027. return -1;
  47028. wolfSSL_CertManagerFree(cm);
  47029. /* end test chain H */
  47030. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  47031. * followed by 2 ICA's, should pass) */
  47032. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47033. printf("cert manager new failed\n");
  47034. return -1;
  47035. }
  47036. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  47037. AssertIntEQ(test_chainI(cm), -1);
  47038. #else
  47039. AssertIntEQ(test_chainI(cm), 0);
  47040. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  47041. wolfSSL_CertManagerUnloadCAs(cm);
  47042. wolfSSL_CertManagerFree(cm);
  47043. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47044. printf("cert manager new failed\n");
  47045. return -1;
  47046. }
  47047. ret = wolfSSL_CertManagerUnloadCAs(cm);
  47048. if (ret != WOLFSSL_SUCCESS)
  47049. return -1;
  47050. wolfSSL_CertManagerFree(cm);
  47051. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  47052. * this time followed by 3 ICA's, should fail */
  47053. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47054. printf("cert manager new failed\n");
  47055. return -1;
  47056. }
  47057. AssertIntLT(test_chainJ(cm), 0);
  47058. wolfSSL_CertManagerUnloadCAs(cm);
  47059. wolfSSL_CertManagerFree(cm);
  47060. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  47061. printf("cert manager new failed\n");
  47062. return -1;
  47063. }
  47064. ret = wolfSSL_CertManagerUnloadCAs(cm);
  47065. wolfSSL_CertManagerFree(cm);
  47066. if (ret == WOLFSSL_SUCCESS) {
  47067. ret = 0;
  47068. }
  47069. return ret;
  47070. }
  47071. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  47072. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  47073. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  47074. {
  47075. byte ekm[100] = {0};
  47076. (void)ctx;
  47077. /* Succes Cases */
  47078. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47079. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  47080. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47081. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  47082. /* Use some random context */
  47083. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47084. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  47085. /* Failure cases */
  47086. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47087. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  47088. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47089. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  47090. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47091. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  47092. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47093. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  47094. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  47095. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  47096. return 0;
  47097. }
  47098. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  47099. {
  47100. wolfSSL_KeepArrays(ssl);
  47101. }
  47102. static int test_export_keying_material(void)
  47103. {
  47104. #ifndef SINGLE_THREADED
  47105. tcp_ready ready;
  47106. callback_functions clientCb;
  47107. func_args client_args;
  47108. func_args server_args;
  47109. THREAD_TYPE serverThread;
  47110. XMEMSET(&client_args, 0, sizeof(func_args));
  47111. XMEMSET(&server_args, 0, sizeof(func_args));
  47112. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  47113. #ifdef WOLFSSL_TIRTOS
  47114. fdOpenSession(Task_self());
  47115. #endif
  47116. StartTCP();
  47117. InitTcpReady(&ready);
  47118. #if defined(USE_WINDOWS_API)
  47119. /* use RNG to get random port if using windows */
  47120. ready.port = GetRandomPort();
  47121. #endif
  47122. server_args.signal = &ready;
  47123. client_args.signal = &ready;
  47124. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  47125. client_args.callbacks = &clientCb;
  47126. start_thread(test_server_nofail, &server_args, &serverThread);
  47127. wait_tcp_ready(&server_args);
  47128. test_client_nofail(&client_args, test_export_keying_material_cb);
  47129. join_thread(serverThread);
  47130. AssertTrue(client_args.return_code);
  47131. AssertTrue(server_args.return_code);
  47132. FreeTcpReady(&ready);
  47133. #ifdef WOLFSSL_TIRTOS
  47134. fdOpenSession(Task_self());
  47135. #endif
  47136. #endif /* !SINGLE_THREADED */
  47137. return 0;
  47138. }
  47139. #endif /* HAVE_KEYING_MATERIAL */
  47140. static int test_wolfSSL_THREADID_hash(void)
  47141. {
  47142. int ret = 0;
  47143. unsigned long res;
  47144. #if defined(OPENSSL_EXTRA)
  47145. CRYPTO_THREADID id;
  47146. printf(testingFmt, "wolfSSL_THREADID_hash");
  47147. CRYPTO_THREADID_current(NULL);
  47148. AssertTrue(1);
  47149. res = CRYPTO_THREADID_hash(NULL);
  47150. AssertTrue( res == 0UL);
  47151. XMEMSET(&id, 0, sizeof(id));
  47152. res = CRYPTO_THREADID_hash(&id);
  47153. AssertTrue( res == 0UL);
  47154. printf(resultFmt, passed);
  47155. #endif /* OPENSSL_EXTRA */
  47156. (void)res;
  47157. return ret;
  47158. }
  47159. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  47160. {
  47161. int ret = 0;
  47162. WOLFSSL_CTX* ctx = NULL;
  47163. #if defined(OPENSSL_EXTRA)
  47164. printf(testingFmt, "SSL_CTX_set_ecdh_auto");
  47165. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  47166. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  47167. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  47168. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  47169. printf(resultFmt, passed);
  47170. #endif /* OPENSSL_EXTRA */
  47171. (void)ctx;
  47172. return ret;
  47173. }
  47174. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  47175. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  47176. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  47177. {
  47178. callback_functions* callbacks = NULL;
  47179. WOLFSSL_CTX* ctx = NULL;
  47180. WOLFSSL* ssl = NULL;
  47181. SOCKET_T sfd = 0;
  47182. SOCKET_T cfd = 0;
  47183. word16 port;
  47184. char msg[] = "I hear you fa shizzle!";
  47185. int len = (int) XSTRLEN(msg);
  47186. char input[1024];
  47187. int ret, err;
  47188. if (!args)
  47189. return 0;
  47190. ((func_args*)args)->return_code = TEST_FAIL;
  47191. callbacks = ((func_args*)args)->callbacks;
  47192. ctx = wolfSSL_CTX_new(callbacks->method());
  47193. #if defined(USE_WINDOWS_API)
  47194. port = ((func_args*)args)->signal->port;
  47195. #else
  47196. /* Let tcp_listen assign port */
  47197. port = 0;
  47198. #endif
  47199. #ifdef WOLFSSL_TIRTOS
  47200. fdOpenSession(Task_self());
  47201. #endif
  47202. AssertIntEQ(WOLFSSL_SUCCESS,
  47203. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  47204. AssertIntEQ(WOLFSSL_SUCCESS,
  47205. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  47206. WOLFSSL_FILETYPE_PEM));
  47207. AssertIntEQ(WOLFSSL_SUCCESS,
  47208. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  47209. WOLFSSL_FILETYPE_PEM));
  47210. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  47211. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  47212. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  47213. #elif !defined(NO_DH)
  47214. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  47215. #endif
  47216. if (callbacks->ctx_ready)
  47217. callbacks->ctx_ready(ctx);
  47218. ssl = wolfSSL_new(ctx);
  47219. AssertNotNull(ssl);
  47220. /* listen and accept */
  47221. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  47222. CloseSocket(sfd);
  47223. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  47224. if (callbacks->ssl_ready)
  47225. callbacks->ssl_ready(ssl);
  47226. do {
  47227. err = 0; /* Reset error */
  47228. ret = wolfSSL_accept(ssl);
  47229. if (ret != WOLFSSL_SUCCESS) {
  47230. err = wolfSSL_get_error(ssl, 0);
  47231. }
  47232. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  47233. if (ret != WOLFSSL_SUCCESS) {
  47234. wolfSSL_free(ssl);
  47235. wolfSSL_CTX_free(ctx);
  47236. CloseSocket(cfd);
  47237. ((func_args*)args)->return_code = TEST_FAIL;
  47238. return 0;
  47239. }
  47240. /* read and write data */
  47241. XMEMSET( input, 0, sizeof(input));
  47242. while (1) {
  47243. ret = wolfSSL_read(ssl, input, sizeof(input));
  47244. if (ret > 0) {
  47245. break;
  47246. }
  47247. else {
  47248. err = wolfSSL_get_error(ssl,ret);
  47249. if (err == WOLFSSL_ERROR_WANT_READ) {
  47250. continue;
  47251. }
  47252. break;
  47253. }
  47254. }
  47255. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  47256. do {
  47257. ret = wolfSSL_write(ssl, msg, len);
  47258. if (ret > 0) {
  47259. break;
  47260. }
  47261. } while (ret < 0);
  47262. }
  47263. /* bidirectional shutdown */
  47264. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  47265. continue;
  47266. }
  47267. /* wait for the peer to disconnect the tcp connection */
  47268. do {
  47269. ret = wolfSSL_read(ssl, input, sizeof(input));
  47270. err = wolfSSL_get_error(ssl, ret);
  47271. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  47272. /* detect TCP disconnect */
  47273. AssertIntLE(ret,WOLFSSL_FAILURE);
  47274. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  47275. ((func_args*)args)->return_code = TEST_SUCCESS;
  47276. wolfSSL_free(ssl);
  47277. wolfSSL_CTX_free(ctx);
  47278. CloseSocket(cfd);
  47279. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  47280. wc_ecc_fp_free(); /* free per thread cache */
  47281. #endif
  47282. return 0;
  47283. }
  47284. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  47285. {
  47286. callback_functions* callbacks = NULL;
  47287. WOLFSSL_CTX* ctx = NULL;
  47288. WOLFSSL* ssl = NULL;
  47289. SOCKET_T sfd = 0;
  47290. char msg[] = "hello wolfssl server!";
  47291. int len = (int) XSTRLEN(msg);
  47292. char input[1024];
  47293. int idx;
  47294. int ret, err;
  47295. if (!args)
  47296. return 0;
  47297. ((func_args*)args)->return_code = TEST_FAIL;
  47298. callbacks = ((func_args*)args)->callbacks;
  47299. ctx = wolfSSL_CTX_new(callbacks->method());
  47300. #ifdef WOLFSSL_TIRTOS
  47301. fdOpenSession(Task_self());
  47302. #endif
  47303. AssertIntEQ(WOLFSSL_SUCCESS,
  47304. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  47305. AssertIntEQ(WOLFSSL_SUCCESS,
  47306. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  47307. WOLFSSL_FILETYPE_PEM));
  47308. AssertIntEQ(WOLFSSL_SUCCESS,
  47309. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  47310. WOLFSSL_FILETYPE_PEM));
  47311. AssertNotNull((ssl = wolfSSL_new(ctx)));
  47312. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  47313. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  47314. do {
  47315. err = 0; /* Reset error */
  47316. ret = wolfSSL_connect(ssl);
  47317. if (ret != WOLFSSL_SUCCESS) {
  47318. err = wolfSSL_get_error(ssl, 0);
  47319. }
  47320. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  47321. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  47322. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  47323. input[idx] = 0;
  47324. }
  47325. ret = wolfSSL_shutdown(ssl);
  47326. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  47327. ret = wolfSSL_shutdown(ssl);
  47328. }
  47329. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  47330. ((func_args*)args)->return_code = TEST_SUCCESS;
  47331. wolfSSL_free(ssl);
  47332. wolfSSL_CTX_free(ctx);
  47333. CloseSocket(sfd);
  47334. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  47335. wc_ecc_fp_free(); /* free per thread cache */
  47336. #endif
  47337. return 0;
  47338. }
  47339. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  47340. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  47341. /* This test is to check wolfSSL_read behaves as same as
  47342. * openSSL when it is called after SSL_shutdown completes.
  47343. */
  47344. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  47345. {
  47346. int ret = 0;
  47347. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  47348. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  47349. tcp_ready ready;
  47350. func_args client_args;
  47351. func_args server_args;
  47352. THREAD_TYPE serverThread;
  47353. THREAD_TYPE clientThread;
  47354. callback_functions server_cbf;
  47355. callback_functions client_cbf;
  47356. printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
  47357. #ifdef WOLFSSL_TIRTOS
  47358. fdOpenSession(Task_self());
  47359. #endif
  47360. StartTCP();
  47361. InitTcpReady(&ready);
  47362. #if defined(USE_WINDOWS_API)
  47363. /* use RNG to get random port if using windows */
  47364. ready.port = GetRandomPort();
  47365. #endif
  47366. XMEMSET(&client_args, 0, sizeof(func_args));
  47367. XMEMSET(&server_args, 0, sizeof(func_args));
  47368. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  47369. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  47370. server_cbf.method = wolfTLSv1_2_server_method;
  47371. client_cbf.method = wolfTLSv1_2_client_method;
  47372. server_args.callbacks = &server_cbf;
  47373. client_args.callbacks = &client_cbf;
  47374. server_args.signal = &ready;
  47375. client_args.signal = &ready;
  47376. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  47377. wait_tcp_ready(&server_args);
  47378. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  47379. join_thread(clientThread);
  47380. join_thread(serverThread);
  47381. AssertTrue(client_args.return_code);
  47382. AssertTrue(server_args.return_code);
  47383. FreeTcpReady(&ready);
  47384. printf(resultFmt, passed);
  47385. #endif
  47386. return ret;
  47387. }
  47388. static int test_wolfSSL_CTX_get_min_proto_version(void)
  47389. {
  47390. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  47391. WOLFSSL_CTX *ctx;
  47392. (void)ctx;
  47393. printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
  47394. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  47395. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  47396. #ifdef WOLFSSL_ALLOW_SSLV3
  47397. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  47398. #else
  47399. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  47400. #endif
  47401. wolfSSL_CTX_free(ctx);
  47402. #ifdef WOLFSSL_ALLOW_TLSV10
  47403. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  47404. #else
  47405. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  47406. #endif
  47407. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  47408. #ifdef WOLFSSL_ALLOW_TLSV10
  47409. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  47410. #else
  47411. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  47412. #endif
  47413. wolfSSL_CTX_free(ctx);
  47414. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  47415. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  47416. #ifndef NO_OLD_TLS
  47417. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  47418. #else
  47419. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  47420. #endif
  47421. wolfSSL_CTX_free(ctx);
  47422. #ifndef WOLFSSL_NO_TLS12
  47423. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  47424. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  47425. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  47426. wolfSSL_CTX_free(ctx);
  47427. #endif
  47428. #ifdef WOLFSSL_TLS13
  47429. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  47430. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  47431. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  47432. wolfSSL_CTX_free(ctx);
  47433. #endif
  47434. printf(resultFmt, passed);
  47435. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  47436. return 0;
  47437. }
  47438. static int test_wolfSSL_security_level(void)
  47439. {
  47440. #if defined(OPENSSL_EXTRA)
  47441. SSL_CTX *ctx;
  47442. printf(testingFmt, "test_wolfSSL_security_level()");
  47443. #ifdef WOLFSSL_TLS13
  47444. #ifdef NO_WOLFSSL_SERVER
  47445. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  47446. #else
  47447. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47448. #endif
  47449. SSL_CTX_set_security_level(ctx, 1);
  47450. AssertTrue(1);
  47451. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  47452. SSL_CTX_free(ctx);
  47453. #else
  47454. (void)ctx;
  47455. #endif
  47456. printf(resultFmt, passed);
  47457. #endif
  47458. return 0;
  47459. }
  47460. static int test_wolfSSL_SSL_in_init(void)
  47461. {
  47462. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  47463. SSL_CTX* ctx;
  47464. SSL* ssl;
  47465. const char* testCertFile;
  47466. const char* testKeyFile;
  47467. printf(testingFmt, "test_wolfSSL_SSL_in_init()");
  47468. #ifdef WOLFSSL_TLS13
  47469. #ifdef NO_WOLFSSL_SERVER
  47470. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  47471. #else
  47472. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47473. #endif
  47474. #ifndef NO_RSA
  47475. testCertFile = svrCertFile;
  47476. testKeyFile = svrKeyFile;
  47477. #elif defined(HAVE_ECC)
  47478. testCertFile = eccCertFile;
  47479. testKeyFile = eccKeyFile;
  47480. #else
  47481. testCertFile = NULL;
  47482. testKeyFile = NULL;
  47483. #endif
  47484. if (testCertFile != NULL && testKeyFile != NULL) {
  47485. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  47486. SSL_FILETYPE_PEM));
  47487. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  47488. SSL_FILETYPE_PEM));
  47489. }
  47490. ssl = SSL_new(ctx);
  47491. AssertNotNull(ssl);
  47492. AssertIntEQ(SSL_in_init(ssl), 1);
  47493. SSL_CTX_free(ctx);
  47494. SSL_free(ssl);
  47495. #else
  47496. (void)ctx;
  47497. (void)ssl;
  47498. (void)testCertFile;
  47499. (void)testKeyFile;
  47500. #endif
  47501. printf(resultFmt, passed);
  47502. #endif
  47503. return 0;
  47504. }
  47505. static int test_wolfSSL_EC_curve(void)
  47506. {
  47507. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47508. int nid = NID_secp160k1;
  47509. const char* nid_name;
  47510. printf(testingFmt, "test_wolfSSL_EC_curve()");
  47511. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  47512. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  47513. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  47514. printf(resultFmt, passed);
  47515. #endif
  47516. return 0;
  47517. }
  47518. static int test_wolfSSL_CTX_set_timeout(void)
  47519. {
  47520. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  47521. int timeout;
  47522. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  47523. (void)timeout;
  47524. printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
  47525. AssertNotNull(ctx);
  47526. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47527. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  47528. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  47529. */
  47530. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  47531. /* giving 0 as timeout value sets default timeout */
  47532. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  47533. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  47534. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  47535. #else
  47536. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  47537. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  47538. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  47539. #endif
  47540. wolfSSL_CTX_free(ctx);
  47541. printf(resultFmt, passed);
  47542. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  47543. return 0;
  47544. }
  47545. static int test_wolfSSL_OpenSSL_version(void)
  47546. {
  47547. #if defined(OPENSSL_EXTRA)
  47548. const char* ver;
  47549. printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
  47550. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47551. AssertNotNull(ver = OpenSSL_version(0));
  47552. #else
  47553. AssertNotNull(ver = OpenSSL_version());
  47554. #endif
  47555. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  47556. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  47557. printf(resultFmt, passed);
  47558. #endif
  47559. return 0;
  47560. }
  47561. static int test_CONF_CTX_CMDLINE(void)
  47562. {
  47563. #if defined(OPENSSL_ALL)
  47564. SSL_CTX* ctx = NULL;
  47565. SSL_CONF_CTX* cctx = NULL;
  47566. printf(testingFmt, "test_CONF_CTX_CMDLINE");
  47567. AssertNotNull(cctx = SSL_CONF_CTX_new());
  47568. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47569. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  47570. AssertTrue(1);
  47571. /* set flags */
  47572. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  47573. WOLFSSL_CONF_FLAG_CMDLINE);
  47574. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  47575. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  47576. /* cmd invalid command */
  47577. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  47578. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  47579. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  47580. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  47581. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  47582. /* cmd Certificate and Private Key*/
  47583. {
  47584. #if !defined(NO_CERTS) && !defined(NO_RSA)
  47585. const char* ourCert = svrCertFile;
  47586. const char* ourKey = svrKeyFile;
  47587. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  47588. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  47589. WOLFSSL_SUCCESS);
  47590. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  47591. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  47592. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47593. #endif
  47594. }
  47595. /* cmd curves */
  47596. {
  47597. #if defined(HAVE_ECC)
  47598. const char* curve = "secp256r1";
  47599. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  47600. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  47601. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47602. #endif
  47603. }
  47604. /* cmd CipherString */
  47605. {
  47606. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  47607. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  47608. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  47609. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47610. }
  47611. /* cmd DH parameter */
  47612. {
  47613. #if !defined(NO_DH) && !defined(NO_BIO)
  47614. const char* ourdhcert = "./certs/dh2048.pem";
  47615. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  47616. -3);
  47617. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  47618. WOLFSSL_SUCCESS);
  47619. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47620. #endif
  47621. }
  47622. SSL_CTX_free(ctx);
  47623. SSL_CONF_CTX_free(cctx);
  47624. printf(resultFmt, passed);
  47625. #endif /* OPENSSL_EXTRA */
  47626. return 0;
  47627. }
  47628. static int test_CONF_CTX_FILE(void)
  47629. {
  47630. #if defined(OPENSSL_ALL)
  47631. SSL_CTX* ctx = NULL;
  47632. SSL_CONF_CTX* cctx = NULL;
  47633. printf(testingFmt, "test_CONF_CTX_FILE");
  47634. AssertNotNull(cctx = SSL_CONF_CTX_new());
  47635. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  47636. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  47637. AssertTrue(1);
  47638. /* set flags */
  47639. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  47640. WOLFSSL_CONF_FLAG_FILE);
  47641. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  47642. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  47643. /* sanity check */
  47644. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  47645. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  47646. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  47647. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  47648. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  47649. /* cmd Certificate and Private Key*/
  47650. {
  47651. #if !defined(NO_CERTS) && !defined(NO_RSA)
  47652. const char* ourCert = svrCertFile;
  47653. const char* ourKey = svrKeyFile;
  47654. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  47655. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  47656. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  47657. WOLFSSL_SUCCESS);
  47658. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  47659. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47660. #endif
  47661. }
  47662. /* cmd curves */
  47663. {
  47664. #if defined(HAVE_ECC)
  47665. const char* curve = "secp256r1";
  47666. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  47667. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  47668. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47669. #endif
  47670. }
  47671. /* cmd CipherString */
  47672. {
  47673. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  47674. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  47675. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  47676. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47677. }
  47678. /* cmd DH parameter */
  47679. {
  47680. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  47681. const char* ourdhcert = "./certs/dh3072.pem";
  47682. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  47683. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  47684. WOLFSSL_SUCCESS);
  47685. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  47686. #endif
  47687. }
  47688. SSL_CTX_free(ctx);
  47689. SSL_CONF_CTX_free(cctx);
  47690. printf(resultFmt, passed);
  47691. #endif /* OPENSSL_EXTRA */
  47692. return 0;
  47693. }
  47694. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  47695. {
  47696. #ifdef HAVE_EX_DATA
  47697. int idx1,idx2;
  47698. printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
  47699. /* test for unsupported class index */
  47700. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  47701. 0,NULL, NULL, NULL, NULL ), -1);
  47702. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  47703. 0,NULL, NULL, NULL, NULL ), -1);
  47704. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  47705. 0,NULL, NULL, NULL, NULL ), -1);
  47706. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  47707. 0,NULL, NULL, NULL, NULL ), -1);
  47708. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  47709. 0,NULL, NULL, NULL, NULL ), -1);
  47710. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  47711. 0,NULL, NULL, NULL, NULL ), -1);
  47712. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  47713. 0,NULL, NULL, NULL, NULL ), -1);
  47714. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  47715. 0,NULL, NULL, NULL, NULL ), -1);
  47716. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  47717. 0,NULL, NULL, NULL, NULL ), -1);
  47718. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  47719. 0,NULL, NULL, NULL, NULL ), -1);
  47720. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  47721. 0,NULL, NULL, NULL, NULL ), -1);
  47722. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  47723. 0,NULL, NULL, NULL, NULL ), -1);
  47724. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  47725. /* test for supported class index */
  47726. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  47727. 0,NULL, NULL, NULL, NULL );
  47728. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  47729. 0,NULL, NULL, NULL, NULL );
  47730. AssertIntNE(idx1, -1);
  47731. AssertIntNE(idx2, -1);
  47732. AssertIntNE(idx1, idx2);
  47733. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  47734. 0,NULL, NULL, NULL, NULL );
  47735. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  47736. 0,NULL, NULL, NULL, NULL );
  47737. AssertIntNE(idx1, -1);
  47738. AssertIntNE(idx2, -1);
  47739. AssertIntNE(idx1, idx2);
  47740. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  47741. 0,NULL, NULL, NULL, NULL );
  47742. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  47743. 0,NULL, NULL, NULL, NULL );
  47744. AssertIntNE(idx1, -1);
  47745. AssertIntNE(idx2, -1);
  47746. AssertIntNE(idx1, idx2);
  47747. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  47748. 0,NULL, NULL, NULL, NULL );
  47749. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  47750. 0,NULL, NULL, NULL, NULL );
  47751. AssertIntNE(idx1, -1);
  47752. AssertIntNE(idx2, -1);
  47753. AssertIntNE(idx1, idx2);
  47754. printf(resultFmt, "passed");
  47755. #endif /* HAVE_EX_DATA */
  47756. return 0;
  47757. }
  47758. static int test_wolfSSL_set_psk_use_session_callback(void)
  47759. {
  47760. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  47761. SSL_CTX* ctx;
  47762. SSL* ssl;
  47763. const char* testCertFile;
  47764. const char* testKeyFile;
  47765. printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
  47766. #ifdef WOLFSSL_TLS13
  47767. #ifdef NO_WOLFSSL_SERVER
  47768. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  47769. #else
  47770. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  47771. #endif
  47772. #ifndef NO_RSA
  47773. testCertFile = svrCertFile;
  47774. testKeyFile = svrKeyFile;
  47775. #elif defined(HAVE_ECC)
  47776. testCertFile = eccCertFile;
  47777. testKeyFile = eccKeyFile;
  47778. #else
  47779. testCertFile = NULL;
  47780. testKeyFile = NULL;
  47781. #endif
  47782. if (testCertFile != NULL && testKeyFile != NULL) {
  47783. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  47784. SSL_FILETYPE_PEM));
  47785. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  47786. SSL_FILETYPE_PEM));
  47787. }
  47788. ssl = SSL_new(ctx);
  47789. AssertNotNull(ssl);
  47790. SSL_set_psk_use_session_callback(ssl,
  47791. my_psk_use_session_cb);
  47792. AssertTrue(1);
  47793. SSL_CTX_free(ctx);
  47794. SSL_free(ssl);
  47795. #else
  47796. (void)ctx;
  47797. (void)ssl;
  47798. (void)testCertFile;
  47799. (void)testKeyFile;
  47800. #endif
  47801. printf(resultFmt, passed);
  47802. #endif
  47803. return 0;
  47804. }
  47805. static int test_wolfSSL_DH(void)
  47806. {
  47807. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47808. DH *dh = NULL;
  47809. BIGNUM* p;
  47810. BIGNUM* q;
  47811. BIGNUM* g;
  47812. BIGNUM* pub;
  47813. BIGNUM* priv;
  47814. #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
  47815. #if !defined(HAVE_FIPS) || \
  47816. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47817. FILE* f = NULL;
  47818. unsigned char buf[268];
  47819. const unsigned char* pt = buf;
  47820. long len = 0;
  47821. dh = NULL;
  47822. XMEMSET(buf, 0, sizeof(buf));
  47823. /* Test 2048 bit parameters */
  47824. f = XFOPEN("./certs/dh2048.der", "rb");
  47825. AssertTrue(f != XBADFILE);
  47826. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47827. XFCLOSE(f);
  47828. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47829. AssertNotNull(dh->p);
  47830. AssertNotNull(dh->g);
  47831. AssertTrue(pt != buf);
  47832. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  47833. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47834. (const BIGNUM**)&q,
  47835. (const BIGNUM**) &g);
  47836. AssertPtrEq(p, dh->p);
  47837. AssertPtrEq(q, dh->q);
  47838. AssertPtrEq(g, dh->g);
  47839. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47840. AssertPtrEq(pub, dh->pub_key);
  47841. AssertPtrEq(priv, dh->priv_key);
  47842. AssertNotNull(pub = BN_new());
  47843. AssertNotNull(priv = BN_new());
  47844. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47845. AssertPtrEq(pub, dh->pub_key);
  47846. AssertPtrEq(priv, dh->priv_key);
  47847. DH_free(dh);
  47848. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  47849. DH_free(dh);
  47850. #endif
  47851. #endif
  47852. (void)dh;
  47853. (void)p;
  47854. (void)q;
  47855. (void)g;
  47856. (void)pub;
  47857. (void)priv;
  47858. printf(testingFmt, "test_wolfSSL_DH");
  47859. dh = wolfSSL_DH_new();
  47860. AssertNotNull(dh);
  47861. /* invalid parameters test */
  47862. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  47863. (const BIGNUM**)&q,
  47864. (const BIGNUM**)&g);
  47865. DH_get0_pqg(dh, NULL,
  47866. (const BIGNUM**)&q,
  47867. (const BIGNUM**)&g);
  47868. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  47869. DH_get0_pqg(dh, NULL, NULL, NULL);
  47870. AssertTrue(1);
  47871. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47872. (const BIGNUM**)&q,
  47873. (const BIGNUM**)&g);
  47874. AssertPtrEq(p, NULL);
  47875. AssertPtrEq(q, NULL);
  47876. AssertPtrEq(g, NULL);
  47877. DH_free(dh);
  47878. /* Test DH_up_ref() */
  47879. dh = wolfSSL_DH_new();
  47880. AssertNotNull(dh);
  47881. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  47882. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  47883. DH_free(dh); /* decrease ref count */
  47884. DH_free(dh); /* free WOLFSSL_DH */
  47885. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  47886. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  47887. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  47888. #ifdef HAVE_FFDHE_2048
  47889. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47890. DH_free(dh);
  47891. #endif
  47892. #ifdef HAVE_FFDHE_3072
  47893. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  47894. DH_free(dh);
  47895. #endif
  47896. #ifdef HAVE_FFDHE_4096
  47897. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  47898. DH_free(dh);
  47899. #endif
  47900. #else
  47901. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47902. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  47903. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  47904. printf(resultFmt, passed);
  47905. #endif /* OPENSSL_EXTRA && !NO_DH */
  47906. return 0;
  47907. }
  47908. static int test_wolfSSL_ERR_strings(void)
  47909. {
  47910. const char* err1 = "unsupported cipher suite";
  47911. const char* err2 = "wolfSSL PEM routines";
  47912. const char* err = NULL;
  47913. (void)err;
  47914. (void)err1;
  47915. (void)err2;
  47916. #if !defined(NO_ERROR_STRINGS)
  47917. printf(testingFmt, "test_wolfSSL_ERR_strings");
  47918. #if defined(OPENSSL_EXTRA)
  47919. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  47920. AssertTrue(err != NULL);
  47921. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  47922. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  47923. AssertTrue(err != NULL);
  47924. AssertIntEQ((*err == '\0'), 1);
  47925. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  47926. AssertTrue(err != NULL);
  47927. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  47928. #else
  47929. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  47930. AssertTrue(err != NULL);
  47931. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  47932. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  47933. AssertTrue(err != NULL);
  47934. AssertIntEQ((*err == '\0'), 1);
  47935. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  47936. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  47937. AssertTrue(err != NULL);
  47938. AssertIntEQ((*err == '\0'), 1);
  47939. #endif
  47940. printf(resultFmt, passed);
  47941. #endif
  47942. return 0;
  47943. }
  47944. static int test_wolfSSL_EVP_shake128(void)
  47945. {
  47946. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  47947. defined(WOLFSSL_SHAKE128)
  47948. printf(testingFmt, "test_wolfSSL_EVP_shake128");
  47949. const EVP_MD* md = NULL;
  47950. md = EVP_shake128();
  47951. AssertTrue(md != NULL);
  47952. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  47953. printf(resultFmt, passed);
  47954. #endif
  47955. return 0;
  47956. }
  47957. static int test_wolfSSL_EVP_shake256(void)
  47958. {
  47959. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  47960. defined(WOLFSSL_SHAKE256)
  47961. const EVP_MD* md = NULL;
  47962. printf(testingFmt, "test_wolfSSL_EVP_shake256");
  47963. md = EVP_shake256();
  47964. AssertTrue(md != NULL);
  47965. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  47966. printf(resultFmt, passed);
  47967. #endif
  47968. return 0;
  47969. }
  47970. static int test_EVP_blake2(void)
  47971. {
  47972. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  47973. const EVP_MD* md = NULL;
  47974. (void)md;
  47975. printf(testingFmt, "test_EVP_blake2");
  47976. #if defined(HAVE_BLAKE2)
  47977. md = EVP_blake2b512();
  47978. AssertTrue(md != NULL);
  47979. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  47980. #endif
  47981. #if defined(HAVE_BLAKE2S)
  47982. md = EVP_blake2s256();
  47983. AssertTrue(md != NULL);
  47984. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  47985. #endif
  47986. printf(resultFmt, passed);
  47987. #endif
  47988. return 0;
  47989. }
  47990. #if defined(OPENSSL_EXTRA)
  47991. static void list_md_fn(const EVP_MD* m, const char* from,
  47992. const char* to, void* arg)
  47993. {
  47994. const char* mn;
  47995. BIO *bio;
  47996. (void) from;
  47997. (void) to;
  47998. (void) arg;
  47999. (void) mn;
  48000. (void) bio;
  48001. if (!m) {
  48002. /* alias */
  48003. AssertNull(m);
  48004. AssertNotNull(to);
  48005. }
  48006. else {
  48007. AssertNotNull(m);
  48008. AssertNull(to);
  48009. }
  48010. AssertNotNull(from);
  48011. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  48012. mn = EVP_get_digestbyname(from);
  48013. /* print to stdout */
  48014. AssertNotNull(arg);
  48015. bio = BIO_new(BIO_s_file());
  48016. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  48017. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  48018. BIO_free(bio);
  48019. #endif
  48020. }
  48021. #endif
  48022. static int test_EVP_MD_do_all(void)
  48023. {
  48024. #if defined(OPENSSL_EXTRA)
  48025. printf(testingFmt, "test_EVP_MD_do_all");
  48026. EVP_MD_do_all(NULL, stdout);
  48027. /* to confirm previous call gives no harm */
  48028. AssertTrue(1);
  48029. EVP_MD_do_all(list_md_fn, stdout);
  48030. /* to confirm previous call gives no harm */
  48031. AssertTrue(1);
  48032. printf(resultFmt, passed);
  48033. #endif
  48034. return 0;
  48035. }
  48036. #if defined(OPENSSL_EXTRA)
  48037. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  48038. {
  48039. (void)arg;
  48040. (void)nm;
  48041. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  48042. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  48043. /* print to stdout */
  48044. AssertNotNull(arg);
  48045. bio = BIO_new(BIO_s_file());
  48046. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  48047. BIO_printf(bio, "%s\n", mn);
  48048. BIO_free(bio);
  48049. #endif
  48050. }
  48051. #endif
  48052. static int test_OBJ_NAME_do_all(void)
  48053. {
  48054. #if defined(OPENSSL_EXTRA)
  48055. printf(testingFmt, "test_OBJ_NAME_do_all");
  48056. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  48057. /* to confirm previous call gives no harm */
  48058. AssertTrue(1);
  48059. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
  48060. /* to confirm previous call gives no harm */
  48061. AssertTrue(1);
  48062. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
  48063. AssertTrue(1);
  48064. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
  48065. AssertTrue(1);
  48066. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
  48067. AssertTrue(1);
  48068. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
  48069. AssertTrue(1);
  48070. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
  48071. AssertTrue(1);
  48072. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
  48073. AssertTrue(1);
  48074. OBJ_NAME_do_all(-1, obj_name_t, stdout);
  48075. AssertTrue(1);
  48076. printf(resultFmt, passed);
  48077. #endif
  48078. return 0;
  48079. }
  48080. static int test_SSL_CIPHER_get_xxx(void)
  48081. {
  48082. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  48083. !defined(NO_FILESYSTEM)
  48084. const SSL_CIPHER* cipher = NULL;
  48085. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  48086. int i, numCiphers = 0;
  48087. SSL_CTX* ctx = NULL;
  48088. SSL* ssl = NULL;
  48089. const char* testCertFile;
  48090. const char* testKeyFile;
  48091. char buf[256] = {0};
  48092. const char* cipher_id = NULL;
  48093. int expect_nid1 = NID_undef;
  48094. int expect_nid2 = NID_undef;
  48095. int expect_nid3 = NID_undef;
  48096. int expect_nid4 = NID_undef;
  48097. int expect_nid5 = 0;
  48098. const char* cipher_id2 = NULL;
  48099. int expect_nid21 = NID_undef;
  48100. int expect_nid22 = NID_undef;
  48101. int expect_nid23 = NID_undef;
  48102. int expect_nid24 = NID_undef;
  48103. int expect_nid25 = 0;
  48104. (void)cipher;
  48105. (void)supportedCiphers;
  48106. (void)i;
  48107. (void)numCiphers;
  48108. (void)ctx;
  48109. (void)ssl;
  48110. (void)testCertFile;
  48111. (void)testKeyFile;
  48112. printf(testingFmt, "test_SSL_CIPHER_get_xxx");
  48113. #if defined(WOLFSSL_TLS13)
  48114. cipher_id = "TLS13-AES128-GCM-SHA256";
  48115. expect_nid1 = NID_auth_rsa;
  48116. expect_nid2 = NID_aes_128_gcm;
  48117. expect_nid3 = NID_sha256;
  48118. expect_nid4 = NID_kx_any;
  48119. expect_nid5 = 1;
  48120. #if !defined(WOLFSSL_NO_TLS12)
  48121. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  48122. expect_nid21 = NID_auth_rsa;
  48123. expect_nid22 = NID_aes_256_gcm;
  48124. expect_nid23 = NID_sha384;
  48125. expect_nid24 = NID_kx_ecdhe;
  48126. expect_nid25 = 1;
  48127. #endif
  48128. #endif
  48129. #ifdef NO_WOLFSSL_SERVER
  48130. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48131. #else
  48132. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48133. #endif
  48134. if (cipher_id) {
  48135. #ifndef NO_RSA
  48136. testCertFile = svrCertFile;
  48137. testKeyFile = svrKeyFile;
  48138. #elif defined(HAVE_ECC)
  48139. testCertFile = eccCertFile;
  48140. testKeyFile = eccKeyFile;
  48141. #else
  48142. testCertFile = NULL;
  48143. testKeyFile = NULL;
  48144. #endif
  48145. if (testCertFile != NULL && testKeyFile != NULL) {
  48146. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  48147. SSL_FILETYPE_PEM));
  48148. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  48149. SSL_FILETYPE_PEM));
  48150. }
  48151. ssl = SSL_new(ctx);
  48152. AssertNotNull(ssl);
  48153. AssertIntEQ(SSL_in_init(ssl), 1);
  48154. supportedCiphers = SSL_get_ciphers(ssl);
  48155. numCiphers = sk_num(supportedCiphers);
  48156. for (i = 0; i < numCiphers; ++i) {
  48157. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  48158. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  48159. }
  48160. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  48161. break;
  48162. }
  48163. }
  48164. /* test case for */
  48165. if (i != numCiphers) {
  48166. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  48167. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  48168. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  48169. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  48170. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  48171. }
  48172. if (cipher_id2) {
  48173. for (i = 0; i < numCiphers; ++i) {
  48174. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  48175. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  48176. }
  48177. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  48178. break;
  48179. }
  48180. }
  48181. /* test case for */
  48182. if (i != numCiphers) {
  48183. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  48184. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  48185. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  48186. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  48187. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  48188. }
  48189. }
  48190. }
  48191. if (ctx)
  48192. SSL_CTX_free(ctx);
  48193. if(ssl)
  48194. SSL_free(ssl);
  48195. printf(resultFmt, passed);
  48196. #endif
  48197. return 0;
  48198. }
  48199. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  48200. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  48201. int* keyFormat)
  48202. {
  48203. int ret;
  48204. byte* key_buf = NULL;
  48205. size_t key_sz = 0;
  48206. EncryptedInfo encInfo;
  48207. XMEMSET(&encInfo, 0, sizeof(encInfo));
  48208. ret = load_file(privKeyFile, &key_buf, &key_sz);
  48209. if (ret == 0) {
  48210. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  48211. NULL, &encInfo, keyFormat);
  48212. }
  48213. if (key_buf != NULL) {
  48214. free(key_buf); key_buf = NULL;
  48215. }
  48216. (void)encInfo; /* not used in this test */
  48217. #ifdef DEBUG_WOLFSSL
  48218. printf("%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  48219. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  48220. (*pDer)->length);
  48221. #endif
  48222. return ret;
  48223. }
  48224. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  48225. {
  48226. int ret = CRYPTOCB_UNAVAILABLE;
  48227. const char* privKeyFile = (const char*)ctx;
  48228. DerBuffer* pDer = NULL;
  48229. int keyFormat = 0;
  48230. if (info->algo_type == WC_ALGO_TYPE_PK) {
  48231. #ifdef DEBUG_WOLFSSL
  48232. printf("test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  48233. #endif
  48234. #ifndef NO_RSA
  48235. if (info->pk.type == WC_PK_TYPE_RSA) {
  48236. switch (info->pk.rsa.type) {
  48237. case RSA_PUBLIC_ENCRYPT:
  48238. case RSA_PUBLIC_DECRYPT:
  48239. /* perform software based RSA public op */
  48240. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  48241. break;
  48242. case RSA_PRIVATE_ENCRYPT:
  48243. case RSA_PRIVATE_DECRYPT:
  48244. {
  48245. RsaKey key;
  48246. /* perform software based RSA private op */
  48247. #ifdef DEBUG_WOLFSSL
  48248. printf("test_CryptoCb_Func: RSA Priv\n");
  48249. #endif
  48250. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  48251. &keyFormat);
  48252. if (ret != 0) {
  48253. return ret;
  48254. }
  48255. ret = wc_InitRsaKey(&key, HEAP_HINT);
  48256. if (ret == 0) {
  48257. word32 keyIdx = 0;
  48258. /* load RSA private key and perform private transform */
  48259. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  48260. &key, pDer->length);
  48261. if (ret == 0) {
  48262. ret = wc_RsaFunction(
  48263. info->pk.rsa.in, info->pk.rsa.inLen,
  48264. info->pk.rsa.out, info->pk.rsa.outLen,
  48265. info->pk.rsa.type, &key, info->pk.rsa.rng);
  48266. }
  48267. else {
  48268. /* if decode fails, then fall-back to software based crypto */
  48269. printf("test_CryptoCb_Func: RSA private key decode "
  48270. "failed %d, falling back to software\n", ret);
  48271. ret = CRYPTOCB_UNAVAILABLE;
  48272. }
  48273. wc_FreeRsaKey(&key);
  48274. }
  48275. wc_FreeDer(&pDer); pDer = NULL;
  48276. break;
  48277. }
  48278. }
  48279. #ifdef DEBUG_WOLFSSL
  48280. printf("test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  48281. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  48282. #endif
  48283. }
  48284. #endif /* !NO_RSA */
  48285. #ifdef HAVE_ECC
  48286. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  48287. /* mark this key as ephemeral */
  48288. if (info->pk.eckg.key != NULL) {
  48289. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  48290. sizeof(info->pk.eckg.key->label));
  48291. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  48292. }
  48293. }
  48294. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  48295. ecc_key key;
  48296. /* perform software based ECC sign */
  48297. #ifdef DEBUG_WOLFSSL
  48298. printf("test_CryptoCb_Func: ECC Sign\n");
  48299. #endif
  48300. if (info->pk.eccsign.key != NULL &&
  48301. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  48302. /* this is an empheral key */
  48303. #ifdef DEBUG_WOLFSSL
  48304. printf("test_CryptoCb_Func: skipping signing op on ephemeral key\n");
  48305. #endif
  48306. return CRYPTOCB_UNAVAILABLE;
  48307. }
  48308. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  48309. if (ret != 0) {
  48310. return ret;
  48311. }
  48312. ret = wc_ecc_init(&key);
  48313. if (ret == 0) {
  48314. word32 keyIdx = 0;
  48315. /* load ECC private key and perform private transform */
  48316. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  48317. &key, pDer->length);
  48318. if (ret == 0) {
  48319. ret = wc_ecc_sign_hash(
  48320. info->pk.eccsign.in, info->pk.eccsign.inlen,
  48321. info->pk.eccsign.out, info->pk.eccsign.outlen,
  48322. info->pk.eccsign.rng, &key);
  48323. }
  48324. else {
  48325. /* if decode fails, then fall-back to software based crypto */
  48326. printf("test_CryptoCb_Func: ECC private key decode "
  48327. "failed %d, falling back to software\n", ret);
  48328. ret = CRYPTOCB_UNAVAILABLE;
  48329. }
  48330. wc_ecc_free(&key);
  48331. }
  48332. wc_FreeDer(&pDer); pDer = NULL;
  48333. #ifdef DEBUG_WOLFSSL
  48334. printf("test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  48335. ret, *info->pk.eccsign.outlen);
  48336. #endif
  48337. }
  48338. #endif /* HAVE_ECC */
  48339. #ifdef HAVE_ED25519
  48340. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  48341. ed25519_key key;
  48342. /* perform software based ED25519 sign */
  48343. #ifdef DEBUG_WOLFSSL
  48344. printf("test_CryptoCb_Func: ED25519 Sign\n");
  48345. #endif
  48346. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  48347. if (ret != 0) {
  48348. return ret;
  48349. }
  48350. ret = wc_ed25519_init(&key);
  48351. if (ret == 0) {
  48352. word32 keyIdx = 0;
  48353. /* load ED25519 private key and perform private transform */
  48354. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  48355. &key, pDer->length);
  48356. if (ret == 0) {
  48357. /* calculate public key */
  48358. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  48359. if (ret == 0) {
  48360. key.pubKeySet = 1;
  48361. ret = wc_ed25519_sign_msg_ex(
  48362. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  48363. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  48364. &key, info->pk.ed25519sign.type,
  48365. info->pk.ed25519sign.context,
  48366. info->pk.ed25519sign.contextLen);
  48367. }
  48368. }
  48369. else {
  48370. /* if decode fails, then fall-back to software based crypto */
  48371. printf("test_CryptoCb_Func: ED25519 private key decode "
  48372. "failed %d, falling back to software\n", ret);
  48373. ret = CRYPTOCB_UNAVAILABLE;
  48374. }
  48375. wc_ed25519_free(&key);
  48376. }
  48377. wc_FreeDer(&pDer); pDer = NULL;
  48378. #ifdef DEBUG_WOLFSSL
  48379. printf("test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  48380. ret, *info->pk.ed25519sign.outLen);
  48381. #endif
  48382. }
  48383. #endif /* HAVE_ED25519 */
  48384. }
  48385. (void)thisDevId;
  48386. (void)keyFormat;
  48387. return ret;
  48388. }
  48389. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  48390. static void test_wc_CryptoCb_TLS(int tlsVer,
  48391. const char* cliCaPemFile, const char* cliCertPemFile,
  48392. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  48393. const char* svrCaPemFile, const char* svrCertPemFile,
  48394. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  48395. {
  48396. callback_functions client_cbf;
  48397. callback_functions server_cbf;
  48398. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  48399. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  48400. if (tlsVer == WOLFSSL_TLSV1_3) {
  48401. #ifdef WOLFSSL_TLS13
  48402. server_cbf.method = wolfTLSv1_3_server_method;
  48403. client_cbf.method = wolfTLSv1_3_client_method;
  48404. #endif
  48405. }
  48406. else if (tlsVer == WOLFSSL_TLSV1_2) {
  48407. #ifndef WOLFSSL_NO_TLS12
  48408. server_cbf.method = wolfTLSv1_2_server_method;
  48409. client_cbf.method = wolfTLSv1_2_client_method;
  48410. #endif
  48411. }
  48412. else if (tlsVer == WOLFSSL_TLSV1_1) {
  48413. #ifndef NO_OLD_TLS
  48414. server_cbf.method = wolfTLSv1_1_server_method;
  48415. client_cbf.method = wolfTLSv1_1_client_method;
  48416. #endif
  48417. }
  48418. else if (tlsVer == WOLFSSL_TLSV1) {
  48419. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  48420. server_cbf.method = wolfTLSv1_server_method;
  48421. client_cbf.method = wolfTLSv1_client_method;
  48422. #endif
  48423. }
  48424. else if (tlsVer == WOLFSSL_SSLV3) {
  48425. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  48426. defined(WOLFSSL_STATIC_RSA)
  48427. server_cbf.method = wolfSSLv3_server_method;
  48428. client_cbf.method = wolfSSLv3_client_method;
  48429. #endif
  48430. }
  48431. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  48432. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  48433. server_cbf.method = wolfDTLSv1_2_server_method;
  48434. client_cbf.method = wolfDTLSv1_2_client_method;
  48435. #endif
  48436. }
  48437. else if (tlsVer == WOLFSSL_DTLSV1) {
  48438. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  48439. server_cbf.method = wolfDTLSv1_server_method;
  48440. client_cbf.method = wolfDTLSv1_client_method;
  48441. #endif
  48442. }
  48443. if (server_cbf.method == NULL) {
  48444. /* not enabled */
  48445. return;
  48446. }
  48447. /* Setup the keys for the TLS test */
  48448. client_cbf.certPemFile = cliCertPemFile;
  48449. client_cbf.keyPemFile = cliPubKeyPemFile;
  48450. client_cbf.caPemFile = cliCaPemFile;
  48451. server_cbf.certPemFile = svrCertPemFile;
  48452. server_cbf.keyPemFile = svrPubKeyPemFile;
  48453. server_cbf.caPemFile = svrCaPemFile;
  48454. /* Setup a crypto callback with pointer to private key file for testing */
  48455. client_cbf.devId = 1;
  48456. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  48457. (void*)cliPrivKeyPemFile);
  48458. server_cbf.devId = 2;
  48459. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  48460. (void*)svrPrivKeyPemFile);
  48461. /* Perform TLS server and client test */
  48462. /* First test is at WOLFSSL_CTX level */
  48463. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48464. /* Check for success */
  48465. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48466. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48467. /* Second test is a WOLFSSL object level */
  48468. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  48469. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48470. /* Check for success */
  48471. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48472. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48473. /* Un register the devId's */
  48474. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  48475. client_cbf.devId = INVALID_DEVID;
  48476. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  48477. server_cbf.devId = INVALID_DEVID;
  48478. }
  48479. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  48480. static int test_wc_CryptoCb(void)
  48481. {
  48482. #ifdef WOLF_CRYPTO_CB
  48483. /* TODO: Add crypto callback API tests */
  48484. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48485. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  48486. int tlsVer;
  48487. #endif
  48488. #ifndef NO_RSA
  48489. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48490. test_wc_CryptoCb_TLS(tlsVer,
  48491. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  48492. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  48493. }
  48494. #endif
  48495. #ifdef HAVE_ECC
  48496. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48497. test_wc_CryptoCb_TLS(tlsVer,
  48498. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  48499. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  48500. }
  48501. #endif
  48502. #ifdef HAVE_ED25519
  48503. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  48504. if (tlsVer == WOLFSSL_DTLSV1) continue;
  48505. test_wc_CryptoCb_TLS(tlsVer,
  48506. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  48507. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  48508. }
  48509. #endif
  48510. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  48511. #endif /* WOLF_CRYPTO_CB */
  48512. return 0;
  48513. }
  48514. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  48515. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  48516. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  48517. const char* cliCaPemFile, const char* cliCertPemFile,
  48518. const char* cliPrivKeyPemFile,
  48519. const char* svrCaPemFile, const char* svrCertPemFile,
  48520. const char* svrPrivKeyPemFile,
  48521. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  48522. {
  48523. callback_functions client_cbf;
  48524. callback_functions server_cbf;
  48525. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  48526. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  48527. if (tlsVer == WOLFSSL_TLSV1_3) {
  48528. #ifdef WOLFSSL_TLS13
  48529. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  48530. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  48531. #endif
  48532. }
  48533. else if (tlsVer == WOLFSSL_TLSV1_2) {
  48534. #ifndef WOLFSSL_NO_TLS12
  48535. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  48536. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  48537. #endif
  48538. }
  48539. else if (tlsVer == WOLFSSL_TLSV1_1) {
  48540. #ifndef NO_OLD_TLS
  48541. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  48542. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  48543. #endif
  48544. }
  48545. else if (tlsVer == WOLFSSL_TLSV1) {
  48546. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  48547. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  48548. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  48549. #endif
  48550. }
  48551. else if (tlsVer == WOLFSSL_SSLV3) {
  48552. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  48553. defined(WOLFSSL_STATIC_RSA)
  48554. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  48555. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  48556. #endif
  48557. }
  48558. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  48559. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  48560. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  48561. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  48562. #endif
  48563. }
  48564. else if (tlsVer == WOLFSSL_DTLSV1) {
  48565. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  48566. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  48567. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  48568. #endif
  48569. }
  48570. if (server_cbf.method_ex == NULL) {
  48571. /* not enabled */
  48572. return;
  48573. }
  48574. /* Setup the keys for the TLS test */
  48575. client_cbf.certPemFile = cliCertPemFile;
  48576. client_cbf.keyPemFile = cliPrivKeyPemFile;
  48577. client_cbf.caPemFile = cliCaPemFile;
  48578. server_cbf.certPemFile = svrCertPemFile;
  48579. server_cbf.keyPemFile = svrPrivKeyPemFile;
  48580. server_cbf.caPemFile = svrCaPemFile;
  48581. client_cbf.mem = cliMem;
  48582. client_cbf.memSz = cliMemSz;
  48583. server_cbf.mem = svrMem;
  48584. server_cbf.memSz = svrMemSz;
  48585. client_cbf.devId = INVALID_DEVID;
  48586. server_cbf.devId = INVALID_DEVID;
  48587. /* Perform TLS server and client test */
  48588. /* First test is at WOLFSSL_CTX level */
  48589. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48590. /* Check for success */
  48591. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48592. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48593. /* Second test is a WOLFSSL object level */
  48594. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  48595. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  48596. /* Check for success */
  48597. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  48598. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  48599. }
  48600. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  48601. #ifdef WOLFSSL_STATIC_MEMORY
  48602. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  48603. defined(SESSION_CERTS)
  48604. #ifdef OPENSSL_EXTRA
  48605. #define TEST_TLS_STATIC_MEMSZ (400000)
  48606. #else
  48607. #define TEST_TLS_STATIC_MEMSZ (320000)
  48608. #endif
  48609. #else
  48610. #define TEST_TLS_STATIC_MEMSZ (80000)
  48611. #endif
  48612. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  48613. {
  48614. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  48615. WOLFSSL_MEM_STATS mem_stats;
  48616. WOLFSSL_MEM_CONN_STATS ssl_stats;
  48617. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  48618. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  48619. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  48620. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  48621. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  48622. #endif
  48623. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  48624. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  48625. /* this should fail because kMaxCtxClients == 2 */
  48626. AssertNull((ssl3 = wolfSSL_new(ctx)));
  48627. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  48628. #ifdef DEBUG_WOLFSSL
  48629. wolfSSL_PrintStatsConn(&ssl_stats);
  48630. #endif
  48631. (void)ssl_stats;
  48632. }
  48633. /* display collected statistics */
  48634. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  48635. #ifdef DEBUG_WOLFSSL
  48636. wolfSSL_PrintStats(&mem_stats);
  48637. #endif
  48638. (void)mem_stats;
  48639. }
  48640. wolfSSL_free(ssl1);
  48641. wolfSSL_free(ssl2);
  48642. return 0;
  48643. }
  48644. #endif /* WOLFSSL_STATIC_MEMORY */
  48645. static int test_wolfSSL_CTX_StaticMemory(void)
  48646. {
  48647. #ifdef WOLFSSL_STATIC_MEMORY
  48648. wolfSSL_method_func method_func;
  48649. WOLFSSL_CTX* ctx;
  48650. const int kMaxCtxClients = 2;
  48651. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48652. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  48653. int tlsVer;
  48654. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  48655. #endif
  48656. #endif
  48657. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  48658. printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
  48659. #ifndef NO_WOLFSSL_SERVER
  48660. #ifndef WOLFSSL_NO_TLS12
  48661. method_func = wolfTLSv1_2_server_method_ex;
  48662. #else
  48663. method_func = wolfTLSv1_3_server_method_ex;
  48664. #endif
  48665. #else
  48666. #ifndef WOLFSSL_NO_TLS12
  48667. method_func = wolfTLSv1_2_client_method_ex;
  48668. #else
  48669. method_func = wolfTLSv1_3_client_method_ex;
  48670. #endif
  48671. #endif
  48672. /* Test creating CTX directly from static memory pool */
  48673. ctx = NULL;
  48674. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  48675. &ctx, method_func, svrMem, sizeof(svrMem),
  48676. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  48677. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  48678. wolfSSL_CTX_free(ctx);
  48679. ctx = NULL;
  48680. /* Test for heap allocated CTX, then assigning static pool to it */
  48681. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  48682. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  48683. NULL, svrMem, sizeof(svrMem),
  48684. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  48685. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  48686. wolfSSL_CTX_free(ctx);
  48687. /* TLS Level Tests using static memory */
  48688. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48689. #ifndef NO_RSA
  48690. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48691. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  48692. svrCertFile, cliCertFile, cliKeyFile,
  48693. cliCertFile, svrCertFile, svrKeyFile,
  48694. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  48695. }
  48696. #endif
  48697. #ifdef HAVE_ECC
  48698. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  48699. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  48700. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  48701. cliEccCertFile, eccCertFile, eccKeyFile,
  48702. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  48703. }
  48704. #endif
  48705. #ifdef HAVE_ED25519
  48706. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  48707. if (tlsVer == WOLFSSL_DTLSV1) continue;
  48708. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  48709. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  48710. cliEdCertFile, edCertFile, edKeyFile,
  48711. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  48712. }
  48713. #endif
  48714. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  48715. printf(resultFmt, passed);
  48716. #endif
  48717. return 0;
  48718. }
  48719. static int test_openssl_FIPS_drbg(void)
  48720. {
  48721. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  48722. DRBG_CTX* dctx;
  48723. byte data1[32], data2[32], zeroData[32];
  48724. byte testSeed[16];
  48725. size_t dlen = sizeof(data1);
  48726. int i;
  48727. XMEMSET(data1, 0, dlen);
  48728. XMEMSET(data2, 0, dlen);
  48729. XMEMSET(zeroData, 0, sizeof(zeroData));
  48730. for (i=0; i<(int)sizeof(testSeed); i++) {
  48731. testSeed[i] = (byte)i;
  48732. }
  48733. printf(testingFmt, "test_openssl_FIPS_drbg()");
  48734. AssertNotNull(dctx = FIPS_get_default_drbg());
  48735. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  48736. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  48737. WOLFSSL_SUCCESS);
  48738. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  48739. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  48740. WOLFSSL_SUCCESS);
  48741. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  48742. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  48743. WOLFSSL_SUCCESS);
  48744. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  48745. WOLFSSL_SUCCESS);
  48746. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  48747. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  48748. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  48749. printf(resultFmt, passed);
  48750. #endif
  48751. return 0;
  48752. }
  48753. static int test_wolfSSL_FIPS_mode(void)
  48754. {
  48755. #if defined(OPENSSL_ALL)
  48756. printf(testingFmt, "test_wolfSSL_FIPS_mode()");
  48757. #ifdef HAVE_FIPS
  48758. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  48759. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  48760. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  48761. #else
  48762. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  48763. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  48764. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  48765. #endif
  48766. printf(resultFmt, passed);
  48767. #endif
  48768. return 0;
  48769. }
  48770. #ifdef WOLFSSL_DTLS
  48771. /* Prints out the current window */
  48772. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  48773. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  48774. int i;
  48775. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  48776. word32 b = w[i];
  48777. int j;
  48778. /* Prints out a 32 bit binary number in big endian order */
  48779. for (j = 0; j < 32; j++, b <<= 1) {
  48780. if (b & (((word32)1) << 31))
  48781. printf("1");
  48782. else
  48783. printf("0");
  48784. }
  48785. printf(" ");
  48786. }
  48787. printf("cur_hi %u cur_lo %u\n", h, l);
  48788. #else
  48789. (void)h;
  48790. (void)l;
  48791. (void)w;
  48792. #endif
  48793. }
  48794. /* a - cur_hi
  48795. * b - cur_lo
  48796. * c - next_hi
  48797. * d - next_lo
  48798. * e - window
  48799. * f - expected next_hi
  48800. * g - expected next_lo
  48801. * h - expected window[1]
  48802. * i - expected window[0]
  48803. */
  48804. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  48805. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  48806. DUW_TEST_print_window_binary((a), (b), (e)); \
  48807. AssertIntEQ((c), (f)); \
  48808. AssertIntEQ((d), (g)); \
  48809. AssertIntEQ((e)[1], (h)); \
  48810. AssertIntEQ((e)[0], (i)); \
  48811. } while (0)
  48812. static int test_wolfSSL_DtlsUpdateWindow(void)
  48813. {
  48814. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  48815. word32 next_lo = 0;
  48816. word16 next_hi = 0;
  48817. printf(testingFmt, "wolfSSL_DtlsUpdateWindow()");
  48818. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  48819. printf("\n");
  48820. #endif
  48821. XMEMSET(window, 0, sizeof window);
  48822. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  48823. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  48824. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  48825. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  48826. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  48827. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  48828. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  48829. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  48830. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  48831. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  48832. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  48833. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  48834. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  48835. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  48836. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  48837. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  48838. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  48839. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  48840. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  48841. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  48842. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  48843. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  48844. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  48845. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  48846. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  48847. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  48848. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  48849. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  48850. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  48851. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  48852. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  48853. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  48854. printf(resultFmt, passed);
  48855. fflush(stdout);
  48856. return 0;
  48857. }
  48858. #endif /* WOLFSSL_DTLS */
  48859. /*----------------------------------------------------------------------------*
  48860. | Main
  48861. *----------------------------------------------------------------------------*/
  48862. typedef int (*TEST_FUNC)(void);
  48863. typedef struct {
  48864. const char *name;
  48865. TEST_FUNC func;
  48866. } TEST_CASE;
  48867. #define TEST_DECL(func) { #func, func }
  48868. TEST_CASE testCases[] = {
  48869. TEST_DECL(test_fileAccess),
  48870. TEST_DECL(test_wolfSSL_Init),
  48871. TEST_DECL(test_wolfSSL_Method_Allocators),
  48872. #ifndef NO_WOLFSSL_SERVER
  48873. TEST_DECL(test_wolfSSL_CTX_new),
  48874. #endif
  48875. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  48876. (!defined(NO_RSA) || defined(HAVE_ECC))
  48877. TEST_DECL(test_for_double_Free),
  48878. #endif
  48879. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48880. TEST_DECL(test_wolfSSL_get_finished),
  48881. TEST_DECL(test_wolfSSL_CTX_add_session),
  48882. #endif
  48883. TEST_DECL(test_SSL_CIPHER_get_xxx),
  48884. TEST_DECL(test_wolfSSL_ERR_strings),
  48885. TEST_DECL(test_wolfSSL_EVP_shake128),
  48886. TEST_DECL(test_wolfSSL_EVP_shake256),
  48887. TEST_DECL(test_EVP_blake2),
  48888. TEST_DECL(test_EVP_MD_do_all),
  48889. TEST_DECL(test_OBJ_NAME_do_all),
  48890. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  48891. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  48892. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  48893. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  48894. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  48895. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  48896. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  48897. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  48898. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  48899. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  48900. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  48901. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  48902. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  48903. TEST_DECL(test_wolfSSL_FPKI),
  48904. TEST_DECL(test_wolfSSL_CertRsaPss),
  48905. TEST_DECL(test_wolfSSL_CertManagerCRL),
  48906. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  48907. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  48908. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  48909. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  48910. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  48911. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  48912. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  48913. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  48914. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  48915. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  48916. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  48917. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  48918. TEST_DECL(test_server_wolfSSL_new),
  48919. TEST_DECL(test_client_wolfSSL_new),
  48920. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  48921. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  48922. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  48923. TEST_DECL(test_SetTmpEC_DHE_Sz),
  48924. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  48925. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  48926. TEST_DECL(test_wolfSSL_dtls_plaintext),
  48927. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  48928. defined(HAVE_IO_TESTS_DEPENDENCIES)
  48929. TEST_DECL(test_wolfSSL_read_write),
  48930. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  48931. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  48932. #endif
  48933. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  48934. TEST_DECL(test_wolfSSL_dtls_export),
  48935. TEST_DECL(test_wolfSSL_tls_export),
  48936. #endif
  48937. TEST_DECL(test_wolfSSL_SetMinVersion),
  48938. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  48939. /* TLS extensions tests */
  48940. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  48941. TEST_DECL(test_wolfSSL_UseSNI),
  48942. #endif
  48943. TEST_DECL(test_wolfSSL_UseTrustedCA),
  48944. TEST_DECL(test_wolfSSL_UseMaxFragment),
  48945. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  48946. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  48947. TEST_DECL(test_wolfSSL_UseALPN),
  48948. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  48949. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  48950. /* X509 tests */
  48951. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  48952. TEST_DECL(test_wolfSSL_PKCS12),
  48953. TEST_DECL(test_wolfSSL_no_password_cb),
  48954. TEST_DECL(test_wolfSSL_PKCS8),
  48955. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  48956. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  48957. TEST_DECL(test_wolfSSL_PKCS5),
  48958. TEST_DECL(test_wolfSSL_URI),
  48959. TEST_DECL(test_wolfSSL_TBS),
  48960. TEST_DECL(test_wolfSSL_X509_verify),
  48961. TEST_DECL(test_wolfSSL_X509_TLS_version),
  48962. TEST_DECL(test_wc_PemToDer),
  48963. TEST_DECL(test_wc_AllocDer),
  48964. TEST_DECL(test_wc_CertPemToDer),
  48965. TEST_DECL(test_wc_PubKeyPemToDer),
  48966. TEST_DECL(test_wc_PemPubKeyToDer),
  48967. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  48968. TEST_DECL(test_wc_CheckCertSigPubKey),
  48969. /* OCSP Stapling */
  48970. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  48971. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  48972. /* Multicast */
  48973. TEST_DECL(test_wolfSSL_mcast),
  48974. /* compatibility tests */
  48975. TEST_DECL(test_wolfSSL_lhash),
  48976. TEST_DECL(test_wolfSSL_X509_NAME),
  48977. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  48978. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  48979. #ifndef NO_BIO
  48980. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  48981. TEST_DECL(test_wolfSSL_X509_INFO),
  48982. #endif
  48983. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  48984. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  48985. TEST_DECL(test_wolfSSL_X509_check_host),
  48986. TEST_DECL(test_wolfSSL_X509_check_email),
  48987. TEST_DECL(test_wolfSSL_DES),
  48988. TEST_DECL(test_wolfSSL_certs),
  48989. TEST_DECL(test_wolfSSL_X509_check_private_key),
  48990. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  48991. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  48992. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  48993. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  48994. TEST_DECL(test_wolfSSL_private_keys),
  48995. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  48996. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  48997. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  48998. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  48999. #ifndef NO_BIO
  49000. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  49001. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  49002. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  49003. TEST_DECL(test_wolfSSL_PEM_RSAPrivateKey),
  49004. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  49005. #endif
  49006. TEST_DECL(test_DSA_do_sign_verify),
  49007. TEST_DECL(test_wolfSSL_tmp_dh),
  49008. TEST_DECL(test_wolfSSL_ctrl),
  49009. TEST_DECL(test_wolfSSL_EVP_MD_size),
  49010. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  49011. TEST_DECL(test_wolfSSL_EVP_Digest),
  49012. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  49013. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  49014. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  49015. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  49016. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  49017. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  49018. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  49019. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  49020. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  49021. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  49022. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  49023. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  49024. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  49025. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  49026. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  49027. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  49028. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  49029. #endif
  49030. #ifndef NO_BIO
  49031. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  49032. TEST_DECL(test_wolfSSL_GetLoggingCb),
  49033. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  49034. TEST_DECL(test_wc_ERR_remove_state),
  49035. TEST_DECL(test_wc_ERR_print_errors_fp),
  49036. #endif
  49037. TEST_DECL(test_wolfSSL_set_options),
  49038. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  49039. TEST_DECL(test_wolfSSL_set1_curves_list),
  49040. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  49041. TEST_DECL(test_wolfSSL_PKCS7_certs),
  49042. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  49043. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  49044. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  49045. TEST_DECL(test_wolfSSL_msgCb),
  49046. TEST_DECL(test_wolfSSL_either_side),
  49047. TEST_DECL(test_wolfSSL_DTLS_either_side),
  49048. TEST_DECL(test_wolfSSL_dtls_fragments),
  49049. TEST_DECL(test_generate_cookie),
  49050. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  49051. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  49052. TEST_DECL(test_wolfSSL_X509_Name_canon),
  49053. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  49054. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  49055. TEST_DECL(test_wolfSSL_X509_NID),
  49056. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  49057. TEST_DECL(test_wolfSSL_get0_param),
  49058. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  49059. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  49060. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  49061. TEST_DECL(test_wolfSSL_X509_STORE),
  49062. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  49063. TEST_DECL(test_X509_STORE_get0_objects),
  49064. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  49065. TEST_DECL(test_wolfSSL_BN),
  49066. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  49067. #ifndef NO_BIO
  49068. TEST_DECL(test_wolfSSL_PEM_read_bio),
  49069. TEST_DECL(test_wolfSSL_BIO),
  49070. #endif
  49071. TEST_DECL(test_wolfSSL_ASN1_STRING),
  49072. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  49073. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  49074. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  49075. TEST_DECL(test_wolfSSL_X509),
  49076. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  49077. TEST_DECL(test_wolfSSL_X509_sign),
  49078. TEST_DECL(test_wolfSSL_X509_sign2),
  49079. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  49080. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  49081. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  49082. TEST_DECL(test_wolfSSL_check_domain),
  49083. #endif
  49084. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  49085. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  49086. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  49087. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  49088. TEST_DECL(test_wolfSSL_RAND),
  49089. TEST_DECL(test_wolfSSL_BUF),
  49090. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  49091. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  49092. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  49093. TEST_DECL(test_wolfSSL_X509_cmp_time),
  49094. TEST_DECL(test_wolfSSL_X509_time_adj),
  49095. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  49096. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  49097. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  49098. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  49099. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  49100. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  49101. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  49102. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  49103. TEST_DECL(test_wolfSSL_Tls13_postauth),
  49104. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  49105. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  49106. TEST_DECL(test_wolfSSL_THREADID_hash),
  49107. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  49108. TEST_DECL(test_wolfSSL_RAND_bytes),
  49109. TEST_DECL(test_wolfSSL_BN_rand),
  49110. TEST_DECL(test_wolfSSL_pseudo_rand),
  49111. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  49112. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  49113. TEST_DECL(test_error_queue_per_thread),
  49114. TEST_DECL(test_wolfSSL_ERR_put_error),
  49115. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  49116. #ifndef NO_BIO
  49117. TEST_DECL(test_wolfSSL_ERR_print_errors),
  49118. #endif
  49119. TEST_DECL(test_wolfSSL_HMAC),
  49120. TEST_DECL(test_wolfSSL_CMAC),
  49121. TEST_DECL(test_wolfSSL_OBJ),
  49122. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  49123. TEST_DECL(test_wolfSSL_OBJ_cmp),
  49124. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  49125. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  49126. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  49127. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  49128. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  49129. TEST_DECL(test_wolfSSL_X509_set_name),
  49130. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  49131. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  49132. TEST_DECL(test_wolfSSL_X509_set_version),
  49133. #ifndef NO_BIO
  49134. TEST_DECL(test_wolfSSL_BIO_gets),
  49135. TEST_DECL(test_wolfSSL_BIO_puts),
  49136. TEST_DECL(test_wolfSSL_BIO_dump),
  49137. TEST_DECL(test_wolfSSL_BIO_should_retry),
  49138. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  49139. TEST_DECL(test_wolfSSL_BIO_write),
  49140. TEST_DECL(test_wolfSSL_BIO_connect),
  49141. TEST_DECL(test_wolfSSL_BIO_accept),
  49142. TEST_DECL(test_wolfSSL_BIO_printf),
  49143. TEST_DECL(test_wolfSSL_BIO_f_md),
  49144. TEST_DECL(test_wolfSSL_BIO_up_ref),
  49145. TEST_DECL(test_wolfSSL_BIO_tls),
  49146. #endif
  49147. TEST_DECL(test_wolfSSL_cert_cb),
  49148. TEST_DECL(test_wolfSSL_SESSION),
  49149. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  49150. TEST_DECL(test_wolfSSL_ticket_keys),
  49151. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  49152. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  49153. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  49154. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  49155. TEST_DECL(test_wolfSSL_MD4),
  49156. TEST_DECL(test_wolfSSL_verify_mode),
  49157. TEST_DECL(test_wolfSSL_verify_depth),
  49158. TEST_DECL(test_wolfSSL_HMAC_CTX),
  49159. TEST_DECL(test_wolfSSL_msg_callback),
  49160. TEST_DECL(test_wolfSSL_SHA),
  49161. TEST_DECL(test_wolfSSL_DH_1536_prime),
  49162. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  49163. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  49164. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  49165. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  49166. TEST_DECL(test_wolfSSL_MD5),
  49167. TEST_DECL(test_wolfSSL_MD5_Transform),
  49168. TEST_DECL(test_wolfSSL_SHA_Transform),
  49169. TEST_DECL(test_wolfSSL_SHA256),
  49170. TEST_DECL(test_wolfSSL_SHA256_Transform),
  49171. TEST_DECL(test_wolfSSL_SHA224),
  49172. TEST_DECL(test_wolfSSL_SHA512_Transform),
  49173. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  49174. TEST_DECL(test_wolfSSL_X509_CRL),
  49175. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  49176. TEST_DECL(test_wolfSSL_PEM_read_X509),
  49177. TEST_DECL(test_wolfSSL_PEM_read),
  49178. #ifndef NO_BIO
  49179. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  49180. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  49181. #endif
  49182. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  49183. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  49184. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  49185. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  49186. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  49187. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  49188. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  49189. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  49190. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  49191. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  49192. TEST_DECL(test_wolfSSL_X509_check_ca),
  49193. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  49194. TEST_DECL(test_wolfSSL_make_cert),
  49195. TEST_DECL(test_wolfSSL_DES_ncbc),
  49196. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  49197. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  49198. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  49199. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  49200. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  49201. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  49202. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  49203. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  49204. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  49205. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  49206. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  49207. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  49208. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  49209. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  49210. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  49211. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  49212. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  49213. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  49214. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  49215. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  49216. TEST_DECL(test_CONF_modules_xxx),
  49217. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  49218. TEST_DECL(test_CRYPTO_THREADID_xxx),
  49219. TEST_DECL(test_ENGINE_cleanup),
  49220. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  49221. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  49222. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  49223. #ifdef OPENSSL_ALL
  49224. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  49225. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  49226. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  49227. TEST_DECL(test_wolfSSL_d2i_DHparams),
  49228. TEST_DECL(test_wolfSSL_i2d_DHparams),
  49229. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  49230. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  49231. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  49232. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  49233. TEST_DECL(test_wolfSSL_DSA_SIG),
  49234. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  49235. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  49236. TEST_DECL(test_wolfSSL_CTX_ctrl),
  49237. TEST_DECL(test_wolfSSL_DH_check),
  49238. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  49239. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  49240. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  49241. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  49242. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  49243. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  49244. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  49245. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  49246. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  49247. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  49248. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  49249. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  49250. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  49251. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  49252. TEST_DECL(test_wolfSSL_EVP_rc4),
  49253. TEST_DECL(test_wolfSSL_EVP_enc_null),
  49254. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  49255. TEST_DECL(test_wolfSSL_EVP_mdc2),
  49256. TEST_DECL(test_wolfSSL_EVP_md4),
  49257. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  49258. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  49259. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  49260. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  49261. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  49262. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  49263. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  49264. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  49265. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  49266. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  49267. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  49268. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  49269. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  49270. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  49271. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  49272. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  49273. TEST_DECL(test_evp_cipher_aes_gcm),
  49274. TEST_DECL(test_wolfSSL_OBJ_ln),
  49275. TEST_DECL(test_wolfSSL_OBJ_sn),
  49276. TEST_DECL(test_wolfSSL_TXT_DB),
  49277. TEST_DECL(test_wolfSSL_NCONF),
  49278. #endif /* OPENSSL_ALL */
  49279. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  49280. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  49281. #ifndef NO_BIO
  49282. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  49283. #endif /* !NO_BIO */
  49284. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  49285. TEST_DECL(test_wolfSSL_X509_CA_num),
  49286. TEST_DECL(test_wolfSSL_X509_get_version),
  49287. #ifndef NO_BIO
  49288. TEST_DECL(test_wolfSSL_X509_print),
  49289. TEST_DECL(test_wolfSSL_X509_CRL_print),
  49290. TEST_DECL(test_wolfSSL_BIO_get_len),
  49291. #endif
  49292. TEST_DECL(test_wolfSSL_RSA),
  49293. TEST_DECL(test_wolfSSL_RSA_DER),
  49294. TEST_DECL(test_wolfSSL_RSA_print),
  49295. #ifndef NO_RSA
  49296. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  49297. #endif
  49298. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  49299. TEST_DECL(test_wolfSSL_RSA_get0_key),
  49300. TEST_DECL(test_wolfSSL_RSA_meth),
  49301. TEST_DECL(test_wolfSSL_RSA_verify),
  49302. TEST_DECL(test_wolfSSL_RSA_sign),
  49303. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  49304. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  49305. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  49306. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  49307. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  49308. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  49309. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  49310. TEST_DECL(test_wolfSSL_RSA_ex_data),
  49311. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  49312. TEST_DECL(test_wolfSSL_RSA_To_Der),
  49313. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  49314. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  49315. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  49316. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  49317. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  49318. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  49319. TEST_DECL(test_wolfSSL_X509V3_EXT),
  49320. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  49321. TEST_DECL(test_wolfSSL_X509_get_ext),
  49322. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  49323. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  49324. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  49325. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  49326. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  49327. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  49328. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  49329. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  49330. TEST_DECL(test_wolfSSL_X509_cmp),
  49331. #ifndef NO_BIO
  49332. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  49333. #endif
  49334. TEST_DECL(test_wolfSSL_ASN1_get_object),
  49335. TEST_DECL(test_openssl_generate_key_and_cert),
  49336. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  49337. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  49338. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  49339. /* test the no op functions for compatibility */
  49340. TEST_DECL(test_no_op_functions),
  49341. /* OpenSSL EVP_PKEY API tests */
  49342. TEST_DECL(test_EVP_PKEY_rsa),
  49343. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  49344. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  49345. TEST_DECL(test_EVP_PKEY_ec),
  49346. TEST_DECL(test_EVP_PKEY_cmp),
  49347. /* OpenSSL error API tests */
  49348. TEST_DECL(test_ERR_load_crypto_strings),
  49349. /* OpenSSL sk_X509 API test */
  49350. TEST_DECL(test_sk_X509),
  49351. /* OpenSSL sk_X509_CRL API test */
  49352. TEST_DECL(test_sk_X509_CRL),
  49353. /* OpenSSL X509 API test */
  49354. TEST_DECL(test_X509_get_signature_nid),
  49355. /* OpenSSL X509 REQ API test */
  49356. TEST_DECL(test_X509_REQ),
  49357. /* OpenSSL PKCS7 API test */
  49358. TEST_DECL(test_wolfssl_PKCS7),
  49359. TEST_DECL(test_wolfSSL_PKCS7_sign),
  49360. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  49361. #ifndef NO_BIO
  49362. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  49363. #ifdef HAVE_SMIME
  49364. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  49365. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  49366. #endif /* HAVE_SMIME */
  49367. #endif /* !NO_BIO */
  49368. /* wolfCrypt ASN tests */
  49369. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  49370. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  49371. TEST_DECL(test_wc_SetSubjectRaw),
  49372. TEST_DECL(test_wc_GetSubjectRaw),
  49373. TEST_DECL(test_wc_SetIssuerRaw),
  49374. TEST_DECL(test_wc_SetIssueBuffer),
  49375. TEST_DECL(test_wc_SetSubjectKeyId),
  49376. TEST_DECL(test_wc_SetSubject),
  49377. TEST_DECL(test_CheckCertSignature),
  49378. TEST_DECL(test_wc_ParseCert),
  49379. TEST_DECL(test_MakeCertWithPathLen),
  49380. /* wolfCrypt ECC tests */
  49381. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  49382. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  49383. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  49384. #ifdef WOLFSSL_TLS13
  49385. /* TLS v1.3 API tests */
  49386. TEST_DECL(test_tls13_apis),
  49387. #endif
  49388. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  49389. !defined(WOLFSSL_NO_CLIENT_AUTH))
  49390. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  49391. /* Bad certificate signature tests */
  49392. TEST_DECL(test_EccSigFailure_cm),
  49393. TEST_DECL(test_RsaSigFailure_cm),
  49394. #endif /* NO_CERTS */
  49395. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  49396. !defined(NO_OLD_TLS))
  49397. TEST_DECL(test_DhCallbacks),
  49398. #endif
  49399. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  49400. TEST_DECL(test_export_keying_material),
  49401. #endif
  49402. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  49403. TEST_DECL(test_wolfSSL_security_level),
  49404. TEST_DECL(test_wolfSSL_SSL_in_init),
  49405. TEST_DECL(test_wolfSSL_EC_curve),
  49406. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  49407. TEST_DECL(test_wolfSSL_OpenSSL_version),
  49408. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  49409. TEST_DECL(test_CONF_CTX_FILE),
  49410. TEST_DECL(test_CONF_CTX_CMDLINE),
  49411. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  49412. TEST_DECL(test_wolfSSL_DH),
  49413. /* wolfcrypt */
  49414. TEST_DECL(test_wolfCrypt_Init),
  49415. TEST_DECL(test_wc_InitMd5),
  49416. TEST_DECL(test_wc_Md5Update),
  49417. TEST_DECL(test_wc_Md5Final),
  49418. TEST_DECL(test_wc_InitSha),
  49419. TEST_DECL(test_wc_ShaUpdate),
  49420. TEST_DECL(test_wc_ShaFinal),
  49421. TEST_DECL(test_wc_InitSha256),
  49422. TEST_DECL(test_wc_Sha256Update),
  49423. TEST_DECL(test_wc_Sha256Final),
  49424. TEST_DECL(test_wc_Sha256FinalRaw),
  49425. TEST_DECL(test_wc_Sha256GetFlags),
  49426. TEST_DECL(test_wc_Sha256Free),
  49427. TEST_DECL(test_wc_Sha256GetHash),
  49428. TEST_DECL(test_wc_Sha256Copy),
  49429. TEST_DECL(test_wc_InitSha512),
  49430. TEST_DECL(test_wc_Sha512Update),
  49431. TEST_DECL(test_wc_Sha512Final),
  49432. TEST_DECL(test_wc_Sha512GetFlags),
  49433. TEST_DECL(test_wc_Sha512FinalRaw),
  49434. TEST_DECL(test_wc_Sha512Free),
  49435. TEST_DECL(test_wc_Sha512GetHash),
  49436. TEST_DECL(test_wc_Sha512Copy),
  49437. TEST_DECL(test_wc_InitSha512_224),
  49438. TEST_DECL(test_wc_Sha512_224Update),
  49439. TEST_DECL(test_wc_Sha512_224Final),
  49440. TEST_DECL(test_wc_Sha512_224GetFlags),
  49441. TEST_DECL(test_wc_Sha512_224FinalRaw),
  49442. TEST_DECL(test_wc_Sha512_224Free),
  49443. TEST_DECL(test_wc_Sha512_224GetHash),
  49444. TEST_DECL(test_wc_Sha512_224Copy),
  49445. TEST_DECL(test_wc_InitSha512_256),
  49446. TEST_DECL(test_wc_Sha512_256Update),
  49447. TEST_DECL(test_wc_Sha512_256Final),
  49448. TEST_DECL(test_wc_Sha512_256GetFlags),
  49449. TEST_DECL(test_wc_Sha512_256FinalRaw),
  49450. TEST_DECL(test_wc_Sha512_256Free),
  49451. TEST_DECL(test_wc_Sha512_256GetHash),
  49452. TEST_DECL(test_wc_Sha512_256Copy),
  49453. TEST_DECL(test_wc_InitSha384),
  49454. TEST_DECL(test_wc_Sha384Update),
  49455. TEST_DECL(test_wc_Sha384Final),
  49456. TEST_DECL(test_wc_Sha384GetFlags),
  49457. TEST_DECL(test_wc_Sha384FinalRaw),
  49458. TEST_DECL(test_wc_Sha384Free),
  49459. TEST_DECL(test_wc_Sha384GetHash),
  49460. TEST_DECL(test_wc_Sha384Copy),
  49461. TEST_DECL(test_wc_InitSha224),
  49462. TEST_DECL(test_wc_Sha224Update),
  49463. TEST_DECL(test_wc_Sha224Final),
  49464. TEST_DECL(test_wc_Sha224SetFlags),
  49465. TEST_DECL(test_wc_Sha224GetFlags),
  49466. TEST_DECL(test_wc_Sha224Free),
  49467. TEST_DECL(test_wc_Sha224GetHash),
  49468. TEST_DECL(test_wc_Sha224Copy),
  49469. TEST_DECL(test_wc_InitBlake2b),
  49470. TEST_DECL(test_wc_InitBlake2b_WithKey),
  49471. TEST_DECL(test_wc_InitBlake2s_WithKey),
  49472. TEST_DECL(test_wc_InitRipeMd),
  49473. TEST_DECL(test_wc_RipeMdUpdate),
  49474. TEST_DECL(test_wc_RipeMdFinal),
  49475. TEST_DECL(test_wc_InitSha3),
  49476. TEST_DECL(testing_wc_Sha3_Update),
  49477. TEST_DECL(test_wc_Sha3_224_Final),
  49478. TEST_DECL(test_wc_Sha3_256_Final),
  49479. TEST_DECL(test_wc_Sha3_384_Final),
  49480. TEST_DECL(test_wc_Sha3_512_Final),
  49481. TEST_DECL(test_wc_Sha3_224_Copy),
  49482. TEST_DECL(test_wc_Sha3_256_Copy),
  49483. TEST_DECL(test_wc_Sha3_384_Copy),
  49484. TEST_DECL(test_wc_Sha3_512_Copy),
  49485. TEST_DECL(test_wc_Sha3_GetFlags),
  49486. TEST_DECL(test_wc_InitShake256),
  49487. TEST_DECL(testing_wc_Shake256_Update),
  49488. TEST_DECL(test_wc_Shake256_Final),
  49489. TEST_DECL(test_wc_Shake256_Copy),
  49490. TEST_DECL(test_wc_Shake256Hash),
  49491. TEST_DECL(test_wc_Md5HmacSetKey),
  49492. TEST_DECL(test_wc_Md5HmacUpdate),
  49493. TEST_DECL(test_wc_Md5HmacFinal),
  49494. TEST_DECL(test_wc_ShaHmacSetKey),
  49495. TEST_DECL(test_wc_ShaHmacUpdate),
  49496. TEST_DECL(test_wc_ShaHmacFinal),
  49497. TEST_DECL(test_wc_Sha224HmacSetKey),
  49498. TEST_DECL(test_wc_Sha224HmacUpdate),
  49499. TEST_DECL(test_wc_Sha224HmacFinal),
  49500. TEST_DECL(test_wc_Sha256HmacSetKey),
  49501. TEST_DECL(test_wc_Sha256HmacUpdate),
  49502. TEST_DECL(test_wc_Sha256HmacFinal),
  49503. TEST_DECL(test_wc_Sha384HmacSetKey),
  49504. TEST_DECL(test_wc_Sha384HmacUpdate),
  49505. TEST_DECL(test_wc_Sha384HmacFinal),
  49506. TEST_DECL(test_wc_HashInit),
  49507. TEST_DECL(test_wc_HashSetFlags),
  49508. TEST_DECL(test_wc_HashGetFlags),
  49509. TEST_DECL(test_wc_InitCmac),
  49510. TEST_DECL(test_wc_CmacUpdate),
  49511. TEST_DECL(test_wc_CmacFinal),
  49512. TEST_DECL(test_wc_AesCmacGenerate),
  49513. TEST_DECL(test_wc_AesGcmStream),
  49514. TEST_DECL(test_wc_Des3_SetIV),
  49515. TEST_DECL(test_wc_Des3_SetKey),
  49516. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  49517. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  49518. TEST_DECL(test_wc_Des3_EcbEncrypt),
  49519. TEST_DECL(test_wc_Chacha_SetKey),
  49520. TEST_DECL(test_wc_Chacha_Process),
  49521. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  49522. TEST_DECL(test_wc_Poly1305SetKey),
  49523. TEST_DECL(test_wc_CamelliaSetKey),
  49524. TEST_DECL(test_wc_CamelliaSetIV),
  49525. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  49526. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  49527. TEST_DECL(test_wc_Arc4SetKey),
  49528. TEST_DECL(test_wc_Arc4Process),
  49529. TEST_DECL(test_wc_Rc2SetKey),
  49530. TEST_DECL(test_wc_Rc2SetIV),
  49531. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  49532. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  49533. TEST_DECL(test_wc_AesSetKey),
  49534. TEST_DECL(test_wc_AesSetIV),
  49535. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  49536. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  49537. TEST_DECL(test_wc_AesGcmSetKey),
  49538. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  49539. TEST_DECL(test_wc_GmacSetKey),
  49540. TEST_DECL(test_wc_GmacUpdate),
  49541. TEST_DECL(test_wc_InitRsaKey),
  49542. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  49543. TEST_DECL(test_wc_RsaPublicKeyDecode),
  49544. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  49545. TEST_DECL(test_wc_MakeRsaKey),
  49546. TEST_DECL(test_wc_SetKeyUsage),
  49547. TEST_DECL(test_wc_CheckProbablePrime),
  49548. TEST_DECL(test_wc_RsaPSS_Verify),
  49549. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  49550. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  49551. TEST_DECL(test_wc_SetMutexCb),
  49552. TEST_DECL(test_wc_LockMutex_ex),
  49553. TEST_DECL(test_wc_RsaKeyToDer),
  49554. TEST_DECL(test_wc_RsaKeyToPublicDer),
  49555. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  49556. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  49557. TEST_DECL(test_wc_RsaEncryptSize),
  49558. TEST_DECL(test_wc_RsaSSL_SignVerify),
  49559. TEST_DECL(test_wc_RsaFlattenPublicKey),
  49560. TEST_DECL(test_RsaDecryptBoundsCheck),
  49561. TEST_DECL(test_wc_AesCcmSetKey),
  49562. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  49563. TEST_DECL(test_wc_InitDsaKey),
  49564. TEST_DECL(test_wc_DsaSignVerify),
  49565. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  49566. TEST_DECL(test_wc_MakeDsaKey),
  49567. TEST_DECL(test_wc_DsaKeyToDer),
  49568. TEST_DECL(test_wc_DsaKeyToPublicDer),
  49569. TEST_DECL(test_wc_DsaImportParamsRaw),
  49570. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  49571. TEST_DECL(test_wc_DsaExportParamsRaw),
  49572. TEST_DECL(test_wc_DsaExportKeyRaw),
  49573. TEST_DECL(test_wc_SignatureGetSize_ecc),
  49574. TEST_DECL(test_wc_SignatureGetSize_rsa),
  49575. /*
  49576. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  49577. * avoid memory leaks.
  49578. */
  49579. TEST_DECL(test_wolfCrypt_Cleanup),
  49580. #ifdef OPENSSL_EXTRA
  49581. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  49582. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  49583. TEST_DECL(test_wolfSSL_EC),
  49584. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  49585. TEST_DECL(test_ECDSA_size_sign),
  49586. TEST_DECL(test_ED25519),
  49587. TEST_DECL(test_ED448),
  49588. TEST_DECL(test_EC_i2d),
  49589. #endif
  49590. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  49591. !defined(HAVE_SELFTEST) && \
  49592. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  49593. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  49594. #endif
  49595. #ifdef HAVE_HASHDRBG
  49596. #ifdef TEST_RESEED_INTERVAL
  49597. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  49598. #endif
  49599. TEST_DECL(test_wc_RNG_GenerateBlock),
  49600. #endif
  49601. TEST_DECL(test_get_rand_digit),
  49602. TEST_DECL(test_get_digit_count),
  49603. TEST_DECL(test_mp_cond_copy),
  49604. TEST_DECL(test_mp_rand),
  49605. TEST_DECL(test_get_digit),
  49606. TEST_DECL(test_wc_export_int),
  49607. TEST_DECL(test_wc_InitRngNonce),
  49608. TEST_DECL(test_wc_InitRngNonce_ex),
  49609. TEST_DECL(test_wc_ed25519_make_key),
  49610. TEST_DECL(test_wc_ed25519_init),
  49611. TEST_DECL(test_wc_ed25519_sign_msg),
  49612. TEST_DECL(test_wc_ed25519_import_public),
  49613. TEST_DECL(test_wc_ed25519_import_private_key),
  49614. TEST_DECL(test_wc_ed25519_export),
  49615. TEST_DECL(test_wc_ed25519_size),
  49616. TEST_DECL(test_wc_ed25519_exportKey),
  49617. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  49618. TEST_DECL(test_wc_curve25519_init),
  49619. TEST_DECL(test_wc_curve25519_size),
  49620. TEST_DECL(test_wc_curve25519_export_key_raw),
  49621. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  49622. TEST_DECL(test_wc_curve25519_make_key),
  49623. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  49624. TEST_DECL(test_wc_curve25519_make_pub),
  49625. TEST_DECL(test_wc_curve25519_export_public_ex),
  49626. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  49627. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  49628. TEST_DECL(test_wc_curve25519_import_private),
  49629. TEST_DECL(test_wc_ed448_make_key),
  49630. TEST_DECL(test_wc_ed448_init),
  49631. TEST_DECL(test_wc_ed448_sign_msg),
  49632. TEST_DECL(test_wc_ed448_import_public),
  49633. TEST_DECL(test_wc_ed448_import_private_key),
  49634. TEST_DECL(test_wc_ed448_export),
  49635. TEST_DECL(test_wc_ed448_size),
  49636. TEST_DECL(test_wc_ed448_exportKey),
  49637. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  49638. TEST_DECL(test_wc_curve448_make_key),
  49639. TEST_DECL(test_wc_curve448_shared_secret_ex),
  49640. TEST_DECL(test_wc_curve448_export_public_ex),
  49641. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  49642. TEST_DECL(test_wc_curve448_export_key_raw),
  49643. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  49644. TEST_DECL(test_wc_curve448_import_private),
  49645. TEST_DECL(test_wc_curve448_init),
  49646. TEST_DECL(test_wc_curve448_size),
  49647. TEST_DECL(test_wc_ecc_make_key),
  49648. TEST_DECL(test_wc_ecc_init),
  49649. TEST_DECL(test_wc_ecc_check_key),
  49650. TEST_DECL(test_wc_ecc_get_generator),
  49651. TEST_DECL(test_wc_ecc_size),
  49652. TEST_DECL(test_wc_ecc_params),
  49653. TEST_DECL(test_wc_ecc_signVerify_hash),
  49654. TEST_DECL(test_wc_ecc_shared_secret),
  49655. TEST_DECL(test_wc_ecc_export_x963),
  49656. TEST_DECL(test_wc_ecc_export_x963_ex),
  49657. TEST_DECL(test_wc_ecc_import_x963),
  49658. TEST_DECL(ecc_import_private_key),
  49659. TEST_DECL(test_wc_ecc_export_private_only),
  49660. TEST_DECL(test_wc_ecc_rs_to_sig),
  49661. TEST_DECL(test_wc_ecc_import_raw),
  49662. TEST_DECL(test_wc_ecc_import_unsigned),
  49663. TEST_DECL(test_wc_ecc_sig_size),
  49664. TEST_DECL(test_wc_ecc_ctx_new),
  49665. TEST_DECL(test_wc_ecc_ctx_reset),
  49666. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  49667. TEST_DECL(test_wc_ecc_ctx_set_info),
  49668. TEST_DECL(test_wc_ecc_encryptDecrypt),
  49669. TEST_DECL(test_wc_ecc_del_point),
  49670. TEST_DECL(test_wc_ecc_pointFns),
  49671. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  49672. TEST_DECL(test_wc_ecc_verify_hash_ex),
  49673. TEST_DECL(test_wc_ecc_mulmod),
  49674. TEST_DECL(test_wc_ecc_is_valid_idx),
  49675. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  49676. TEST_DECL(test_wc_ecc_sig_size_calc),
  49677. TEST_DECL(test_ToTraditional),
  49678. TEST_DECL(test_wc_EccPrivateKeyToDer),
  49679. TEST_DECL(test_wc_DhPublicKeyDecode),
  49680. TEST_DECL(test_wc_Ed25519KeyToDer),
  49681. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  49682. TEST_DECL(test_wc_Ed448KeyToDer),
  49683. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  49684. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  49685. TEST_DECL(test_wc_SetSubjectBuffer),
  49686. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  49687. TEST_DECL(test_wc_PKCS7_New),
  49688. TEST_DECL(test_wc_PKCS7_Init),
  49689. TEST_DECL(test_wc_PKCS7_InitWithCert),
  49690. TEST_DECL(test_wc_PKCS7_EncodeData),
  49691. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  49692. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  49693. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  49694. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  49695. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  49696. TEST_DECL(test_wc_PKCS7_Degenerate),
  49697. TEST_DECL(test_wc_PKCS7_BER),
  49698. TEST_DECL(test_PKCS7_signed_enveloped),
  49699. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  49700. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  49701. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  49702. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  49703. TEST_DECL(test_wc_i2d_PKCS12),
  49704. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  49705. TEST_DECL(test_openssl_FIPS_drbg),
  49706. TEST_DECL(test_wc_CryptoCb),
  49707. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  49708. TEST_DECL(test_wolfSSL_FIPS_mode),
  49709. #ifdef WOLFSSL_DTLS
  49710. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  49711. #endif
  49712. TEST_DECL(test_ForceZero),
  49713. TEST_DECL(test_wolfSSL_Cleanup),
  49714. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  49715. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  49716. !defined(WOLFSSL_NO_CLIENT_AUTH))
  49717. TEST_DECL(test_various_pathlen_chains),
  49718. #endif
  49719. /* If at some point a stub get implemented this test should fail indicating
  49720. * a need to implement a new test case
  49721. */
  49722. TEST_DECL(test_stubs_are_stubs)
  49723. };
  49724. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  49725. static void TestSetup(void)
  49726. {
  49727. /* Stub, for now. Add common test setup code here. */
  49728. }
  49729. static void TestCleanup(void)
  49730. {
  49731. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  49732. /* Clear any errors added to the error queue during the test run. */
  49733. wolfSSL_ERR_clear_error();
  49734. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  49735. }
  49736. void ApiTest(void)
  49737. {
  49738. int i;
  49739. int ret;
  49740. printf(" Begin API Tests\n");
  49741. fflush(stdout);
  49742. for (i = 0; i < TEST_CASE_CNT; ++i) {
  49743. TestSetup();
  49744. ret = testCases[i].func();
  49745. if (ret != 0) {
  49746. fprintf(stderr, "%s failed.\n", testCases[i].name);
  49747. }
  49748. AssertIntEQ(ret, 0);
  49749. TestCleanup();
  49750. }
  49751. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  49752. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  49753. wc_ecc_fp_free(); /* free per thread cache */
  49754. #endif
  49755. wolfSSL_Cleanup();
  49756. (void)testDevId;
  49757. printf(" End API Tests\n");
  49758. fflush(stdout);
  49759. }