api.c 1.9 MB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364
  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)) && !defined(NO_FILESYSTEM)
  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_set_cipher_list_bytes(void)
  744. {
  745. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  746. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  747. (!defined(NO_RSA) || defined(HAVE_ECC))
  748. const char* testCertFile;
  749. const char* testKeyFile;
  750. WOLFSSL_CTX* ctx;
  751. WOLFSSL* ssl;
  752. const byte cipherList[] =
  753. {
  754. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  755. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  756. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  757. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  758. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  759. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  760. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  761. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  762. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  763. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  764. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  765. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  766. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  767. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  768. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  769. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  770. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  771. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  772. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  773. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  774. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  775. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  776. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  777. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  778. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  779. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  780. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  781. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  782. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  783. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  784. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  785. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  786. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  787. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  788. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  789. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  790. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  791. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  792. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  793. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  794. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  795. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  796. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  797. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  798. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  799. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  800. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  801. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  802. /* SHA256 */
  803. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  804. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  805. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  806. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  807. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  808. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  809. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  810. /* SHA384 */
  811. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  812. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  813. /* AES-GCM */
  814. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  815. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  816. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  817. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  818. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  819. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  820. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  821. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  822. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  823. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  824. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  825. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  826. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  827. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  828. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  829. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  830. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  831. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  832. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  833. * also, in some of the other AES-CCM suites
  834. * there will be second byte number conflicts
  835. * with non-ECC AES-GCM */
  836. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  837. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  838. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  839. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  840. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  841. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  842. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  843. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  844. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  845. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  846. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  847. /* Camellia */
  848. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  849. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  850. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  851. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  852. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  853. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  854. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  855. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  856. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  857. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  858. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  859. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  860. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  861. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  862. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  863. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  864. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  865. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  866. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  867. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  868. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  869. /* TLS v1.3 cipher suites */
  870. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  871. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  872. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  873. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  874. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  875. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  876. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  877. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  878. };
  879. #ifndef NO_RSA
  880. testCertFile = svrCertFile;
  881. testKeyFile = svrKeyFile;
  882. #elif defined(HAVE_ECC)
  883. testCertFile = eccCertFile;
  884. testKeyFile = eccKeyFile;
  885. #endif
  886. #ifndef NO_WOLFSSL_SERVER
  887. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  888. AssertNotNull(ctx);
  889. #else
  890. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  891. AssertNotNull(ctx);
  892. #endif
  893. AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  894. sizeof(cipherList)));
  895. wolfSSL_CTX_free(ctx);
  896. #ifndef NO_WOLFSSL_SERVER
  897. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  898. AssertNotNull(ctx);
  899. #else
  900. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  901. AssertNotNull(ctx);
  902. #endif
  903. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  904. WOLFSSL_FILETYPE_PEM));
  905. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  906. WOLFSSL_FILETYPE_PEM));
  907. ssl = wolfSSL_new(ctx);
  908. AssertNotNull(ssl);
  909. AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  910. sizeof(cipherList)));
  911. wolfSSL_free(ssl);
  912. wolfSSL_CTX_free(ctx);
  913. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  914. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  915. return 0;
  916. }
  917. static int test_wolfSSL_CTX_use_certificate_file(void)
  918. {
  919. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  920. WOLFSSL_CTX *ctx;
  921. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  922. /* invalid context */
  923. AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  924. WOLFSSL_FILETYPE_PEM));
  925. /* invalid cert file */
  926. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  927. WOLFSSL_FILETYPE_PEM));
  928. /* invalid cert type */
  929. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  930. #ifdef NO_RSA
  931. /* rsa needed */
  932. AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
  933. #else
  934. /* success */
  935. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  936. #endif
  937. wolfSSL_CTX_free(ctx);
  938. #endif
  939. return 0;
  940. }
  941. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  942. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  943. {
  944. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  945. WOLFSSL_CTX* ctx;
  946. int ret;
  947. printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
  948. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  949. ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  950. server_cert_der_2048);
  951. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  952. wolfSSL_CTX_free(ctx);
  953. if (ret == WOLFSSL_SUCCESS) {
  954. ret = 0;
  955. }
  956. return ret;
  957. #else
  958. return 0;
  959. #endif
  960. }
  961. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  962. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  963. * context using buffer.
  964. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  965. * --enable-testcert flag.
  966. */
  967. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  968. {
  969. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  970. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  971. WOLFSSL_CTX* ctx;
  972. int ret;
  973. printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
  974. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  975. ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  976. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
  977. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  978. wolfSSL_CTX_free(ctx);
  979. if (ret == WOLFSSL_SUCCESS) {
  980. ret = 0;
  981. }
  982. return ret;
  983. #else
  984. return 0;
  985. #endif
  986. } /*END test_wolfSSL_CTX_use_certificate_buffer*/
  987. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  988. {
  989. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  990. WOLFSSL_CTX *ctx;
  991. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  992. /* invalid context */
  993. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  994. WOLFSSL_FILETYPE_PEM));
  995. /* invalid key file */
  996. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  997. WOLFSSL_FILETYPE_PEM));
  998. /* invalid key type */
  999. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1000. /* success */
  1001. #ifdef NO_RSA
  1002. /* rsa needed */
  1003. AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1004. #else
  1005. /* success */
  1006. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  1007. #endif
  1008. wolfSSL_CTX_free(ctx);
  1009. #endif
  1010. return 0;
  1011. }
  1012. /* test both file and buffer versions along with unloading trusted peer certs */
  1013. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1014. {
  1015. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1016. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1017. WOLFSSL_CTX *ctx;
  1018. WOLFSSL* ssl;
  1019. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1020. AssertNotNull(ssl = wolfSSL_new(ctx));
  1021. #if !defined(NO_FILESYSTEM)
  1022. /* invalid file */
  1023. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1024. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1025. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1026. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1027. AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1028. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1029. /* success */
  1030. AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1031. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1032. /* unload cert */
  1033. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1034. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1035. /* invalid file */
  1036. AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1037. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1038. AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1039. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1040. AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1041. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1042. /* success */
  1043. AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1044. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1045. #ifdef WOLFSSL_LOCAL_X509_STORE
  1046. /* unload cert */
  1047. AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1048. AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1049. #endif
  1050. #endif
  1051. /* Test of loading certs from buffers */
  1052. /* invalid buffer */
  1053. AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1054. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1055. /* success */
  1056. #ifdef USE_CERT_BUFFERS_1024
  1057. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1058. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1059. #endif
  1060. #ifdef USE_CERT_BUFFERS_2048
  1061. AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1062. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1063. #endif
  1064. /* unload cert */
  1065. AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1066. AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1067. wolfSSL_free(ssl);
  1068. wolfSSL_CTX_free(ctx);
  1069. #endif
  1070. return 0;
  1071. }
  1072. static int test_wolfSSL_CTX_load_verify_locations(void)
  1073. {
  1074. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1075. WOLFSSL_CTX *ctx;
  1076. #ifndef NO_RSA
  1077. WOLFSSL_CERT_MANAGER* cm;
  1078. #ifdef PERSIST_CERT_CACHE
  1079. int cacheSz;
  1080. #endif
  1081. #endif
  1082. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1083. const char* load_certs_path = "./certs/external";
  1084. const char* load_no_certs_path = "./examples";
  1085. const char* load_expired_path = "./certs/test/expired";
  1086. #endif
  1087. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1088. /* invalid arguments */
  1089. AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
  1090. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
  1091. /* invalid ca file */
  1092. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1093. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1094. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1095. (defined(WOLFSSL_QT) && \
  1096. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1097. /* invalid path */
  1098. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1099. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1100. #endif
  1101. /* load ca cert */
  1102. #ifdef NO_RSA
  1103. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1104. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1105. #else /* Skip the following test without RSA certs. */
  1106. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1107. #ifdef PERSIST_CERT_CACHE
  1108. /* Get cert cache size */
  1109. cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
  1110. #endif
  1111. /* Test unloading CA's */
  1112. AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1113. #ifdef PERSIST_CERT_CACHE
  1114. /* Verify no certs (result is less than cacheSz) */
  1115. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1116. #endif
  1117. /* load ca cert again */
  1118. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
  1119. /* Test getting CERT_MANAGER */
  1120. AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1121. /* Test unloading CA's using CM */
  1122. AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1123. #ifdef PERSIST_CERT_CACHE
  1124. /* Verify no certs (result is less than cacheSz) */
  1125. AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1126. #endif
  1127. #endif
  1128. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1129. /* Test loading CA certificates using a path */
  1130. #ifdef NO_RSA
  1131. /* failure here okay since certs in external directory are RSA */
  1132. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1133. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1134. #else
  1135. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1136. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1137. #endif
  1138. /* Test loading path with no files */
  1139. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
  1140. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1141. /* Test loading expired CA certificates */
  1142. #ifdef NO_RSA
  1143. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1144. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1145. WOLFSSL_SUCCESS);
  1146. #else
  1147. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
  1148. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1149. WOLFSSL_SUCCESS);
  1150. #endif
  1151. /* Test loading CA certificates and ignoring all errors */
  1152. #ifdef NO_RSA
  1153. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1154. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1155. #else
  1156. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1157. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1158. #endif
  1159. #endif
  1160. wolfSSL_CTX_free(ctx);
  1161. #endif
  1162. return 0;
  1163. }
  1164. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1165. {
  1166. int ret = 0;
  1167. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1168. (!defined(NO_RSA) || defined(HAVE_ECC))
  1169. WOLFSSL_CTX* ctx;
  1170. byte dirValid = 0;
  1171. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1172. if (ctx == NULL) {
  1173. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1174. ret = -1;
  1175. }
  1176. if (ret == 0) {
  1177. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1178. dirValid = 1;
  1179. #else
  1180. word32 numDirs;
  1181. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1182. if (caDirs == NULL || numDirs == 0) {
  1183. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1184. ret = -1;
  1185. }
  1186. else {
  1187. ReadDirCtx dirCtx;
  1188. word32 i;
  1189. for (i = 0; i < numDirs; ++i) {
  1190. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1191. /* Directory isn't empty. */
  1192. dirValid = 1;
  1193. wc_ReadDirClose(&dirCtx);
  1194. break;
  1195. }
  1196. }
  1197. }
  1198. #endif
  1199. }
  1200. /*
  1201. * If the directory isn't empty, we should be able to load CA
  1202. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1203. * usable.
  1204. */
  1205. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1206. WOLFSSL_SUCCESS) {
  1207. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1208. ret = -1;
  1209. }
  1210. #ifdef OPENSSL_EXTRA
  1211. if (ret == 0 &&
  1212. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1213. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1214. ret = -1;
  1215. }
  1216. #endif /* OPENSSL_EXTRA */
  1217. wolfSSL_CTX_free(ctx);
  1218. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1219. return ret;
  1220. }
  1221. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1222. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
  1223. {
  1224. int ret;
  1225. WOLFSSL_CERT_MANAGER* cm;
  1226. cm = wolfSSL_CertManagerNew();
  1227. if (cm == NULL) {
  1228. printf("test_cm_load_ca failed\n");
  1229. return -1;
  1230. }
  1231. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1232. wolfSSL_CertManagerFree(cm);
  1233. return ret;
  1234. }
  1235. static int test_cm_load_ca_file(const char* ca_cert_file)
  1236. {
  1237. int ret = 0;
  1238. byte* cert_buf = NULL;
  1239. size_t cert_sz = 0;
  1240. #if defined(WOLFSSL_PEM_TO_DER)
  1241. DerBuffer* pDer = NULL;
  1242. #endif
  1243. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1244. if (ret == 0) {
  1245. /* normal test */
  1246. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1247. if (ret == WOLFSSL_SUCCESS) {
  1248. /* test including null terminator in length */
  1249. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1250. if (tmp == NULL) {
  1251. ret = MEMORY_E;
  1252. }
  1253. else {
  1254. cert_buf = tmp;
  1255. cert_buf[cert_sz] = '\0';
  1256. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1257. WOLFSSL_FILETYPE_PEM);
  1258. }
  1259. }
  1260. #if defined(WOLFSSL_PEM_TO_DER)
  1261. if (ret == WOLFSSL_SUCCESS) {
  1262. /* test loading DER */
  1263. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1264. if (ret == 0 && pDer != NULL) {
  1265. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1266. WOLFSSL_FILETYPE_ASN1);
  1267. wc_FreeDer(&pDer);
  1268. }
  1269. }
  1270. #endif
  1271. }
  1272. free(cert_buf);
  1273. return ret;
  1274. }
  1275. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1276. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  1277. {
  1278. #if defined(HAVE_OCSP) && !defined(NO_RSA)
  1279. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  1280. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  1281. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  1282. defined(HAVE_LIGHTY)
  1283. WOLFSSL_CERT_MANAGER* cm = NULL;
  1284. /* Raw OCSP response bytes captured using the following setup:
  1285. * - Run responder with
  1286. * openssl ocsp -port 9999 -ndays 9999
  1287. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  1288. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  1289. * -rkey certs/ocsp/ocsp-responder-key.pem
  1290. * -CA certs/ocsp/intermediate1-ca-cert.pem
  1291. * - Run client with
  1292. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  1293. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  1294. * -cert certs/ocsp/server1-cert.pem
  1295. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  1296. * - Copy raw response from Wireshark.
  1297. */
  1298. byte response[] = {
  1299. 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
  1300. 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
  1301. 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  1302. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  1303. 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
  1304. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
  1305. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1306. 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
  1307. 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
  1308. 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
  1309. 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  1310. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  1311. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
  1312. 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
  1313. 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
  1314. 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
  1315. 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
  1316. 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
  1317. 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
  1318. 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
  1319. 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
  1320. 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
  1321. 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  1322. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
  1323. 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
  1324. 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
  1325. 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
  1326. 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
  1327. 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
  1328. 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
  1329. 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
  1330. 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
  1331. 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
  1332. 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
  1333. 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
  1334. 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
  1335. 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
  1336. 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
  1337. 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
  1338. 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
  1339. 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
  1340. 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  1341. 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  1342. 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
  1343. 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
  1344. 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
  1345. 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
  1346. 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
  1347. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
  1348. 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
  1349. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
  1350. 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
  1351. 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
  1352. 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
  1353. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
  1354. 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
  1355. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
  1356. 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
  1357. 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
  1358. 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
  1359. 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
  1360. 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
  1361. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
  1362. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
  1363. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
  1364. 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
  1365. 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
  1366. 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
  1367. 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
  1368. 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
  1369. 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
  1370. 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
  1371. 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
  1372. 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
  1373. 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
  1374. 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
  1375. 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
  1376. 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
  1377. 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
  1378. 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
  1379. 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
  1380. 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
  1381. 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
  1382. 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
  1383. 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
  1384. 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
  1385. 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
  1386. 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
  1387. 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
  1388. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
  1389. 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
  1390. 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
  1391. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
  1392. 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  1393. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
  1394. 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  1395. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  1396. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
  1397. 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
  1398. 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
  1399. 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
  1400. 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
  1401. 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
  1402. 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
  1403. 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
  1404. 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
  1405. 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
  1406. 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
  1407. 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
  1408. 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
  1409. 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
  1410. 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
  1411. 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
  1412. 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
  1413. 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
  1414. 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
  1415. 0x59, 0x59, 0xa0, 0x07
  1416. };
  1417. OcspEntry entry[1];
  1418. CertStatus status[1];
  1419. OcspRequest* request;
  1420. byte serial[] = {0x05};
  1421. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  1422. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  1423. printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
  1424. XMEMSET(entry, 0, sizeof(OcspEntry));
  1425. XMEMSET(status, 0, sizeof(CertStatus));
  1426. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1427. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1428. DYNAMIC_TYPE_OCSP_REQUEST);
  1429. AssertNotNull(request->serial);
  1430. request->serialSz = sizeof(serial);
  1431. XMEMCPY(request->serial, serial, sizeof(serial));
  1432. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1433. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1434. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1435. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1436. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
  1437. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  1438. /* Response should be valid. */
  1439. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1440. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1441. /* Flip a byte in the request serial number, response should be invalid
  1442. * now. */
  1443. request->serial[0] ^= request->serial[0];
  1444. AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
  1445. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  1446. wolfSSL_OCSP_REQUEST_free(request);
  1447. wolfSSL_CertManagerFree(cm);
  1448. printf(resultFmt, passed);
  1449. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  1450. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  1451. #endif /* HAVE_OCSP */
  1452. return 0;
  1453. }
  1454. static int test_wolfSSL_CheckOCSPResponse(void)
  1455. {
  1456. #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
  1457. const char* responseFile = "./certs/ocsp/test-response.der";
  1458. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  1459. const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
  1460. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  1461. OcspResponse* res = NULL;
  1462. byte data[4096];
  1463. const unsigned char* pt;
  1464. int dataSz;
  1465. XFILE f;
  1466. WOLFSSL_OCSP_BASICRESP* bs;
  1467. WOLFSSL_X509_STORE* st;
  1468. WOLFSSL_X509* issuer;
  1469. printf(testingFmt, "wolfSSL_CheckOCSPResponse()");
  1470. f = XFOPEN(responseFile, "rb");
  1471. AssertTrue(f != XBADFILE);
  1472. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1473. AssertIntGT(dataSz, 0);
  1474. XFCLOSE(f);
  1475. pt = data;
  1476. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1477. AssertNotNull(res);
  1478. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1479. AssertNotNull(issuer);
  1480. st = wolfSSL_X509_STORE_new();
  1481. AssertNotNull(st);
  1482. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1483. bs = wolfSSL_OCSP_response_get1_basic(res);
  1484. AssertNotNull(bs);
  1485. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1486. wolfSSL_OCSP_BASICRESP_free(bs);
  1487. wolfSSL_OCSP_RESPONSE_free(res);
  1488. wolfSSL_X509_STORE_free(st);
  1489. wolfSSL_X509_free(issuer);
  1490. /* check loading a response with optional certs */
  1491. f = XFOPEN(responseNoInternFile, "rb");
  1492. AssertTrue(f != XBADFILE);
  1493. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1494. AssertIntGT(dataSz, 0);
  1495. XFCLOSE(f);
  1496. pt = data;
  1497. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1498. AssertNotNull(res);
  1499. wolfSSL_OCSP_RESPONSE_free(res);
  1500. /* check loading a response with multiple certs */
  1501. {
  1502. WOLFSSL_CERT_MANAGER* cm = NULL;
  1503. OcspEntry *entry;
  1504. CertStatus* status;
  1505. OcspRequest* request;
  1506. byte serial1[] = {0x01};
  1507. byte serial[] = {0x02};
  1508. byte issuerHash[] = {
  1509. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  1510. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  1511. 0x37, 0x15, 0x8A, 0x88
  1512. };
  1513. byte issuerKeyHash[] = {
  1514. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  1515. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  1516. 0x7E, 0x72, 0x15, 0x21
  1517. };
  1518. entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  1519. DYNAMIC_TYPE_OPENSSL);
  1520. AssertNotNull(entry);
  1521. status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  1522. DYNAMIC_TYPE_OPENSSL);
  1523. AssertNotNull(status);
  1524. XMEMSET(entry, 0, sizeof(OcspEntry));
  1525. XMEMSET(status, 0, sizeof(CertStatus));
  1526. AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
  1527. request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  1528. DYNAMIC_TYPE_OCSP_REQUEST);
  1529. AssertNotNull(request->serial);
  1530. request->serialSz = sizeof(serial);
  1531. XMEMCPY(request->serial, serial, sizeof(serial));
  1532. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  1533. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  1534. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1535. AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  1536. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  1537. WOLFSSL_SUCCESS);
  1538. f = XFOPEN(responseMultiFile, "rb");
  1539. AssertTrue(f != XBADFILE);
  1540. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1541. AssertIntGT(dataSz, 0);
  1542. XFCLOSE(f);
  1543. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1544. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1545. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1546. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1547. AssertNotNull(entry->status);
  1548. XMEMCPY(request->serial, serial1, sizeof(serial1));
  1549. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1550. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  1551. /* store both status's in the entry to check that "next" is not
  1552. * overwritten */
  1553. status->next = entry->status;
  1554. entry->status = status;
  1555. XMEMCPY(request->serial, serial, sizeof(serial));
  1556. AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  1557. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  1558. AssertNotNull(entry->status->next);
  1559. /* compare the status found */
  1560. AssertIntEQ(status->serialSz, entry->status->serialSz);
  1561. AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
  1562. status->serialSz), 0);
  1563. wolfSSL_OCSP_CERTID_free(entry);
  1564. wolfSSL_OCSP_REQUEST_free(request);
  1565. wolfSSL_CertManagerFree(cm);
  1566. }
  1567. #if defined(WC_RSA_PSS)
  1568. {
  1569. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  1570. /* check loading a response with RSA-PSS signature */
  1571. f = XFOPEN(responsePssFile, "rb");
  1572. AssertTrue(f != XBADFILE);
  1573. dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
  1574. AssertIntGT(dataSz, 0);
  1575. XFCLOSE(f);
  1576. pt = data;
  1577. res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
  1578. AssertNotNull(res);
  1579. /* try to verify the response */
  1580. issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
  1581. AssertNotNull(issuer);
  1582. st = wolfSSL_X509_STORE_new();
  1583. AssertNotNull(st);
  1584. AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  1585. bs = wolfSSL_OCSP_response_get1_basic(res);
  1586. AssertNotNull(bs);
  1587. AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  1588. wolfSSL_OCSP_BASICRESP_free(bs);
  1589. wolfSSL_OCSP_RESPONSE_free(res);
  1590. wolfSSL_X509_STORE_free(st);
  1591. wolfSSL_X509_free(issuer);
  1592. }
  1593. #endif
  1594. printf(resultFmt, passed);
  1595. #endif /* HAVE_OCSP */
  1596. return 0;
  1597. }
  1598. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  1599. {
  1600. int ret;
  1601. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1602. const char* ca_cert = "./certs/ca-cert.pem";
  1603. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  1604. ret = test_cm_load_ca_file(ca_cert);
  1605. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1606. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1607. #elif defined(NO_RSA)
  1608. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1609. #else
  1610. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1611. #endif
  1612. ret = test_cm_load_ca_file(ca_expired_cert);
  1613. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1614. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  1615. if (ret == WOLFSSL_FATAL_ERROR)
  1616. #elif defined(NO_RSA)
  1617. AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
  1618. if (ret == ASN_UNKNOWN_OID_E)
  1619. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  1620. !defined(OPENSSL_COMPATIBLE_DEFAULTS)
  1621. AssertIntEQ(ret, ASN_AFTER_DATE_E);
  1622. if (ret == ASN_AFTER_DATE_E)
  1623. #else
  1624. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1625. if (ret == WOLFSSL_SUCCESS)
  1626. #endif
  1627. #endif
  1628. {
  1629. ret = 0;
  1630. }
  1631. return ret;
  1632. }
  1633. static int test_wolfSSL_CertManagerGetCerts(void)
  1634. {
  1635. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1636. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1637. defined(WOLFSSL_SIGNER_DER_CERT)
  1638. WOLFSSL_CERT_MANAGER* cm = NULL;
  1639. WOLFSSL_STACK* sk = NULL;
  1640. X509* x509 = NULL;
  1641. X509* cert1 = NULL;
  1642. FILE* file1 = NULL;
  1643. #ifdef DEBUG_WOLFSSL_VERBOSE
  1644. WOLFSSL_BIO* bio = NULL;
  1645. #endif
  1646. int i = 0;
  1647. int ret = 0;
  1648. const byte* der;
  1649. int derSz = 0;
  1650. printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
  1651. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  1652. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  1653. fclose(file1);
  1654. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1655. AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1656. AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  1657. ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
  1658. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  1659. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  1660. * and full OpenSSL compatibility */
  1661. AssertIntEQ(ret, ASN_SELF_SIGNED_E);
  1662. #else
  1663. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  1664. #endif
  1665. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  1666. "./certs/ca-cert.pem", NULL));
  1667. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  1668. for (i = 0; i < sk_X509_num(sk); i++) {
  1669. x509 = sk_X509_value(sk, i);
  1670. AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  1671. #ifdef DEBUG_WOLFSSL_VERBOSE
  1672. bio = BIO_new(wolfSSL_BIO_s_file());
  1673. if (bio != NULL) {
  1674. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  1675. X509_print(bio, x509);
  1676. BIO_free(bio);
  1677. }
  1678. #endif /* DEBUG_WOLFSSL_VERBOSE */
  1679. }
  1680. wolfSSL_X509_free(cert1);
  1681. sk_X509_pop_free(sk, NULL);
  1682. wolfSSL_CertManagerFree(cm);
  1683. printf(resultFmt, passed);
  1684. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  1685. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  1686. defined(WOLFSSL_SIGNER_DER_CERT) */
  1687. return 0;
  1688. }
  1689. static int test_wolfSSL_CertManagerSetVerify(void)
  1690. {
  1691. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1692. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1693. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  1694. int ret = 0;
  1695. WOLFSSL_CERT_MANAGER* cm;
  1696. int tmp = myVerifyAction;
  1697. const char* ca_cert = "./certs/ca-cert.pem";
  1698. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  1699. cm = wolfSSL_CertManagerNew();
  1700. AssertNotNull(cm);
  1701. wolfSSL_CertManagerSetVerify(cm, myVerify);
  1702. ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
  1703. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  1704. AssertIntEQ(ret, -1);
  1705. #else
  1706. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1707. #endif
  1708. /* Use the test CB that always accepts certs */
  1709. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  1710. ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
  1711. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  1712. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  1713. {
  1714. const char* verifyCert = "./certs/server-cert.pem";
  1715. /* Use the test CB that always fails certs */
  1716. myVerifyAction = VERIFY_FORCE_FAIL;
  1717. ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
  1718. AssertIntEQ(ret, VERIFY_CERT_ERROR);
  1719. }
  1720. #endif
  1721. wolfSSL_CertManagerFree(cm);
  1722. myVerifyAction = tmp;
  1723. #endif
  1724. return 0;
  1725. }
  1726. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  1727. defined(DEBUG_UNIT_TEST_CERTS)
  1728. /* Used when debugging name constraint tests. Not static to allow use in
  1729. * multiple locations with complex define guards. */
  1730. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  1731. {
  1732. BIO* out = BIO_new_file(fileName, "wb");
  1733. if (out != NULL) {
  1734. PEM_write_bio_X509(out, x509);
  1735. BIO_free(out);
  1736. }
  1737. }
  1738. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  1739. {
  1740. BIO* out = BIO_new_file(fileName, "wb");
  1741. if (out != NULL) {
  1742. BIO_write(out, der, derSz);
  1743. BIO_free(out);
  1744. }
  1745. }
  1746. #else
  1747. #define DEBUG_WRITE_CERT_X509(x509, fileName)
  1748. #define DEBUG_WRITE_DER(der, derSz, fileName)
  1749. #endif
  1750. static int test_wolfSSL_CertManagerNameConstraint(void)
  1751. {
  1752. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1753. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1754. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1755. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  1756. !defined(NO_SHA256)
  1757. WOLFSSL_CERT_MANAGER* cm;
  1758. WOLFSSL_EVP_PKEY *priv;
  1759. WOLFSSL_X509_NAME* name;
  1760. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  1761. const char* server_cert = "./certs/test/server-goodcn.pem";
  1762. int i = 0;
  1763. static const byte extNameConsOid[] = {85, 29, 30};
  1764. RsaKey key;
  1765. WC_RNG rng;
  1766. byte *der;
  1767. int derSz;
  1768. word32 idx = 0;
  1769. byte *pt;
  1770. WOLFSSL_X509 *x509, *ca;
  1771. wc_InitRng(&rng);
  1772. /* load in CA private key for signing */
  1773. AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  1774. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  1775. sizeof_server_key_der_2048), 0);
  1776. /* get ca certificate then alter it */
  1777. AssertNotNull(der =
  1778. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  1779. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  1780. WOLFSSL_FILETYPE_ASN1));
  1781. AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  1782. XMEMCPY(der, pt, derSz);
  1783. /* find the name constraint extension and alter it */
  1784. pt = der;
  1785. for (i = 0; i < derSz - 3; i++) {
  1786. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  1787. pt += 3;
  1788. break;
  1789. }
  1790. pt++;
  1791. }
  1792. AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  1793. /* go to the length value and set it to 0 */
  1794. while (i < derSz && *pt != 0x81) {
  1795. pt++;
  1796. i++;
  1797. }
  1798. AssertIntNE(i, derSz); /* did not place to alter */
  1799. pt++;
  1800. *pt = 0x00;
  1801. /* resign the altered certificate */
  1802. AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  1803. FOURK_BUF, &key, NULL, &rng)), 0);
  1804. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1805. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1806. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  1807. wolfSSL_CertManagerFree(cm);
  1808. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  1809. wolfSSL_X509_free(x509);
  1810. wc_FreeRsaKey(&key);
  1811. wc_FreeRng(&rng);
  1812. /* add email alt name to satisfy constraint */
  1813. pt = (byte*)server_key_der_2048;
  1814. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  1815. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  1816. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1817. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1818. WOLFSSL_FILETYPE_ASN1));
  1819. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  1820. DEBUG_WRITE_DER(der, derSz, "ca.der");
  1821. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1822. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1823. /* Good cert test with proper alt email name */
  1824. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1825. WOLFSSL_FILETYPE_PEM));
  1826. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1827. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1828. AssertNotNull(name = X509_NAME_new());
  1829. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1830. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1831. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1832. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1833. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1834. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1835. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1836. X509_NAME_free(name);
  1837. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1838. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1839. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  1840. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1841. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1842. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1843. wolfSSL_X509_free(x509);
  1844. /* Cert with bad alt name list */
  1845. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1846. WOLFSSL_FILETYPE_PEM));
  1847. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1848. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1849. AssertNotNull(name = X509_NAME_new());
  1850. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  1851. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  1852. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  1853. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  1854. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  1855. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  1856. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  1857. X509_NAME_free(name);
  1858. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  1859. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  1860. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  1861. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  1862. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  1863. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1864. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1865. wolfSSL_CertManagerFree(cm);
  1866. wolfSSL_X509_free(x509);
  1867. wolfSSL_X509_free(ca);
  1868. wolfSSL_EVP_PKEY_free(priv);
  1869. #endif
  1870. return 0;
  1871. }
  1872. static int test_wolfSSL_CertManagerNameConstraint2(void)
  1873. {
  1874. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  1875. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  1876. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  1877. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  1878. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  1879. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  1880. const char* server_cert = "./certs/server-cert.pem";
  1881. WOLFSSL_CERT_MANAGER* cm;
  1882. WOLFSSL_X509 *x509, *ca;
  1883. const unsigned char *der;
  1884. const unsigned char *pt;
  1885. WOLFSSL_EVP_PKEY *priv;
  1886. WOLFSSL_X509_NAME* name;
  1887. int derSz;
  1888. /* C=US*/
  1889. char altName[] = {
  1890. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1891. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  1892. };
  1893. /* C=ID */
  1894. char altNameFail[] = {
  1895. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  1896. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  1897. };
  1898. /* C=US ST=California*/
  1899. char altNameExc[] = {
  1900. 0x30, 0x22,
  1901. 0x31, 0x0B,
  1902. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  1903. 0x31, 0x13,
  1904. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  1905. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  1906. };
  1907. /* load in CA private key for signing */
  1908. pt = ca_key_der_2048;
  1909. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  1910. sizeof_ca_key_der_2048));
  1911. AssertNotNull(cm = wolfSSL_CertManagerNew());
  1912. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  1913. WOLFSSL_FILETYPE_ASN1));
  1914. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  1915. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  1916. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1917. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1918. WOLFSSL_FILETYPE_PEM));
  1919. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1920. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1921. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1922. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1923. #else
  1924. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1925. #endif
  1926. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1927. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1928. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1929. /* add in matching DIR alt name and resign */
  1930. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1931. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1932. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1933. #else
  1934. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1935. #endif
  1936. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1937. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1938. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1939. wolfSSL_X509_free(x509);
  1940. /* check verify fail */
  1941. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1942. WOLFSSL_FILETYPE_PEM));
  1943. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1944. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1945. /* add in miss matching DIR alt name and resign */
  1946. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1947. ASN_DIR_TYPE);
  1948. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1949. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1950. #else
  1951. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1952. #endif
  1953. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1954. #ifndef WOLFSSL_NO_ASN_STRICT
  1955. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1956. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1957. #else
  1958. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1959. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1960. #endif
  1961. /* check that it still fails if one bad altname and one good altname is in
  1962. * the certificate */
  1963. wolfSSL_X509_free(x509);
  1964. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1965. WOLFSSL_FILETYPE_PEM));
  1966. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1967. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1968. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1969. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1970. ASN_DIR_TYPE);
  1971. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1972. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1973. #else
  1974. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1975. #endif
  1976. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1977. #ifndef WOLFSSL_NO_ASN_STRICT
  1978. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1979. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  1980. #else
  1981. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  1982. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1983. #endif
  1984. /* check it fails with switching position of bad altname */
  1985. wolfSSL_X509_free(x509);
  1986. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  1987. WOLFSSL_FILETYPE_PEM));
  1988. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  1989. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  1990. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  1991. ASN_DIR_TYPE);
  1992. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  1993. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  1994. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  1995. #else
  1996. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  1997. #endif
  1998. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  1999. #ifndef WOLFSSL_NO_ASN_STRICT
  2000. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2001. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2002. #else
  2003. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2004. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2005. #endif
  2006. wolfSSL_CertManagerFree(cm);
  2007. wolfSSL_X509_free(x509);
  2008. wolfSSL_X509_free(ca);
  2009. /* now test with excluded name constraint */
  2010. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2011. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2012. WOLFSSL_FILETYPE_ASN1));
  2013. AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2014. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2015. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2016. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2017. WOLFSSL_FILETYPE_PEM));
  2018. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2019. ASN_DIR_TYPE);
  2020. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2021. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2022. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2023. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2024. #else
  2025. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2026. #endif
  2027. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2028. #ifndef WOLFSSL_NO_ASN_STRICT
  2029. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2030. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2031. #else
  2032. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2033. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2034. #endif
  2035. wolfSSL_CertManagerFree(cm);
  2036. wolfSSL_X509_free(x509);
  2037. wolfSSL_X509_free(ca);
  2038. wolfSSL_EVP_PKEY_free(priv);
  2039. #endif
  2040. return 0;
  2041. }
  2042. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2043. {
  2044. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2045. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2046. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2047. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2048. !defined(NO_SHA256)
  2049. WOLFSSL_CERT_MANAGER* cm;
  2050. WOLFSSL_EVP_PKEY *priv;
  2051. WOLFSSL_X509_NAME* name;
  2052. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2053. const char* server_cert = "./certs/test/server-goodcn.pem";
  2054. byte *der;
  2055. int derSz;
  2056. byte *pt;
  2057. WOLFSSL_X509 *x509, *ca;
  2058. pt = (byte*)server_key_der_2048;
  2059. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2060. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2061. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2062. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2063. WOLFSSL_FILETYPE_ASN1));
  2064. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2065. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2066. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2067. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2068. /* check satisfying .wolfssl.com constraint passes */
  2069. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2070. WOLFSSL_FILETYPE_PEM));
  2071. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2072. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2073. AssertNotNull(name = X509_NAME_new());
  2074. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2075. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2076. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2077. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2078. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2079. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2080. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2081. X509_NAME_free(name);
  2082. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2083. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2084. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2085. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2086. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2087. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2088. wolfSSL_X509_free(x509);
  2089. /* check satisfying .random.com constraint passes */
  2090. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2091. WOLFSSL_FILETYPE_PEM));
  2092. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2093. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2094. AssertNotNull(name = X509_NAME_new());
  2095. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2096. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2097. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2098. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2099. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2100. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2101. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2102. X509_NAME_free(name);
  2103. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2104. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2105. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2106. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2107. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2108. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2109. wolfSSL_X509_free(x509);
  2110. /* check fail case when neither constraint is matched */
  2111. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2112. WOLFSSL_FILETYPE_PEM));
  2113. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2114. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2115. AssertNotNull(name = X509_NAME_new());
  2116. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2117. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2118. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2119. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2120. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2121. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2122. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2123. X509_NAME_free(name);
  2124. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2125. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2126. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2127. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2128. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2129. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2130. wolfSSL_CertManagerFree(cm);
  2131. wolfSSL_X509_free(x509);
  2132. wolfSSL_X509_free(ca);
  2133. wolfSSL_EVP_PKEY_free(priv);
  2134. #endif
  2135. return 0;
  2136. }
  2137. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2138. {
  2139. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2140. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2141. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2142. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2143. !defined(NO_SHA256)
  2144. WOLFSSL_CERT_MANAGER* cm;
  2145. WOLFSSL_EVP_PKEY *priv;
  2146. WOLFSSL_X509_NAME* name;
  2147. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2148. const char* server_cert = "./certs/test/server-goodcn.pem";
  2149. byte *der;
  2150. int derSz;
  2151. byte *pt;
  2152. WOLFSSL_X509 *x509, *ca;
  2153. pt = (byte*)server_key_der_2048;
  2154. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2155. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2156. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2157. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2158. WOLFSSL_FILETYPE_ASN1));
  2159. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2160. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2161. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2162. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2163. /* check satisfying wolfssl.com constraint passes */
  2164. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2165. WOLFSSL_FILETYPE_PEM));
  2166. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2167. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2168. AssertNotNull(name = X509_NAME_new());
  2169. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2170. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2171. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2172. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2173. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2174. X509_NAME_free(name);
  2175. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2176. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2177. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2178. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2179. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2180. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2181. wolfSSL_X509_free(x509);
  2182. /* check satisfying example.com constraint passes */
  2183. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2184. WOLFSSL_FILETYPE_PEM));
  2185. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2186. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2187. AssertNotNull(name = X509_NAME_new());
  2188. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2189. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2190. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2191. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2192. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2193. X509_NAME_free(name);
  2194. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2195. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2196. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2197. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2198. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2199. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2200. wolfSSL_X509_free(x509);
  2201. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2202. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2203. WOLFSSL_FILETYPE_PEM));
  2204. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2205. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2206. AssertNotNull(name = X509_NAME_new());
  2207. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2208. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2209. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2210. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2211. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2212. X509_NAME_free(name);
  2213. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2214. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2215. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2216. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2217. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2218. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2219. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2220. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2221. wolfSSL_X509_free(x509);
  2222. /* check fail when one DNS in the list is bad */
  2223. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2224. WOLFSSL_FILETYPE_PEM));
  2225. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2226. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2227. AssertNotNull(name = X509_NAME_new());
  2228. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2229. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2230. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2231. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2232. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2233. X509_NAME_free(name);
  2234. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2235. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2236. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2237. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2238. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2239. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2240. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2241. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2242. wolfSSL_X509_free(x509);
  2243. /* check fail case when neither constraint is matched */
  2244. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2245. WOLFSSL_FILETYPE_PEM));
  2246. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2247. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2248. AssertNotNull(name = X509_NAME_new());
  2249. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2250. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2251. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2252. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2253. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2254. X509_NAME_free(name);
  2255. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2256. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2257. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2258. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2259. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2260. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2261. wolfSSL_CertManagerFree(cm);
  2262. wolfSSL_X509_free(x509);
  2263. wolfSSL_X509_free(ca);
  2264. wolfSSL_EVP_PKEY_free(priv);
  2265. #endif
  2266. return 0;
  2267. }
  2268. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2269. {
  2270. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2271. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2272. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2273. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2274. !defined(NO_SHA256)
  2275. WOLFSSL_CERT_MANAGER* cm;
  2276. WOLFSSL_EVP_PKEY *priv;
  2277. WOLFSSL_X509_NAME* name;
  2278. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2279. const char* server_cert = "./certs/test/server-goodcn.pem";
  2280. byte *der;
  2281. int derSz;
  2282. byte *pt;
  2283. WOLFSSL_X509 *x509, *ca;
  2284. pt = (byte*)server_key_der_2048;
  2285. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2286. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2287. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2288. AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2289. WOLFSSL_FILETYPE_ASN1));
  2290. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2291. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2292. AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2293. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2294. /* check satisfying wolfssl.com constraint passes */
  2295. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2296. WOLFSSL_FILETYPE_PEM));
  2297. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2298. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2299. AssertNotNull(name = X509_NAME_new());
  2300. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2301. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2302. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2303. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2304. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2305. X509_NAME_free(name);
  2306. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2307. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2308. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2309. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2310. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2311. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2312. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2313. wolfSSL_X509_free(x509);
  2314. /* fail with DNS check because of common name */
  2315. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2316. WOLFSSL_FILETYPE_PEM));
  2317. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2318. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2319. AssertNotNull(name = X509_NAME_new());
  2320. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2321. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2322. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2323. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2324. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2325. X509_NAME_free(name);
  2326. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2327. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2328. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2329. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2330. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2331. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2332. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2333. wolfSSL_X509_free(x509);
  2334. /* fail on permitted DNS name constraint */
  2335. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2336. WOLFSSL_FILETYPE_PEM));
  2337. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2338. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2339. AssertNotNull(name = X509_NAME_new());
  2340. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2341. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2342. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2343. X509_NAME_free(name);
  2344. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2345. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2346. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2347. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2348. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2349. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2350. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2351. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2352. wolfSSL_X509_free(x509);
  2353. /* fail on permitted email name constraint */
  2354. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2355. WOLFSSL_FILETYPE_PEM));
  2356. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2357. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2358. AssertNotNull(name = X509_NAME_new());
  2359. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2360. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2361. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2362. X509_NAME_free(name);
  2363. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2364. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2365. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2366. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2367. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2368. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2369. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2370. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2371. wolfSSL_X509_free(x509);
  2372. /* success with empty email name */
  2373. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2374. WOLFSSL_FILETYPE_PEM));
  2375. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2376. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2377. AssertNotNull(name = X509_NAME_new());
  2378. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2379. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2380. AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2381. X509_NAME_free(name);
  2382. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2383. AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2384. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2385. AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2386. AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2387. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2388. wolfSSL_X509_free(x509);
  2389. wolfSSL_CertManagerFree(cm);
  2390. wolfSSL_X509_free(ca);
  2391. wolfSSL_EVP_PKEY_free(priv);
  2392. #endif
  2393. return 0;
  2394. }
  2395. static int test_wolfSSL_FPKI(void)
  2396. {
  2397. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  2398. XFILE f;
  2399. const char* fpkiCert = "./certs/fpki-cert.der";
  2400. DecodedCert cert;
  2401. byte buf[4096];
  2402. byte* uuid;
  2403. byte* fascn;
  2404. word32 fascnSz;
  2405. word32 uuidSz;
  2406. int bytes;
  2407. printf(testingFmt, "test_wolfSSL_FPKI");
  2408. f = XFOPEN(fpkiCert, "rb");
  2409. AssertTrue((f != XBADFILE));
  2410. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2411. XFCLOSE(f);
  2412. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2413. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  2414. AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  2415. fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2416. AssertNotNull(fascn);
  2417. AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  2418. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2419. AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  2420. uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2421. AssertNotNull(uuid);
  2422. AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  2423. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2424. wc_FreeDecodedCert(&cert);
  2425. printf(resultFmt, passed);
  2426. #endif
  2427. return 0;
  2428. }
  2429. static int test_wolfSSL_CertRsaPss(void)
  2430. {
  2431. /* FIPS v2 and below don't support long salts. */
  2432. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  2433. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  2434. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  2435. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  2436. XFILE f;
  2437. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  2438. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  2439. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2440. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  2441. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  2442. #endif
  2443. DecodedCert cert;
  2444. byte buf[4096];
  2445. int bytes;
  2446. WOLFSSL_CERT_MANAGER* cm;
  2447. printf(testingFmt, "test_CertRsaPss");
  2448. cm = wolfSSL_CertManagerNew();
  2449. AssertNotNull(cm);
  2450. AssertIntEQ(WOLFSSL_SUCCESS,
  2451. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  2452. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  2453. AssertIntEQ(WOLFSSL_SUCCESS,
  2454. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  2455. #endif
  2456. f = XFOPEN(rsaPssSha256Cert, "rb");
  2457. AssertTrue((f != XBADFILE));
  2458. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2459. XFCLOSE(f);
  2460. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2461. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2462. wc_FreeDecodedCert(&cert);
  2463. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  2464. RSA_MAX_SIZE >= 3072
  2465. f = XFOPEN(rsaPssSha384Cert, "rb");
  2466. AssertTrue((f != XBADFILE));
  2467. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  2468. XFCLOSE(f);
  2469. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  2470. AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  2471. wc_FreeDecodedCert(&cert);
  2472. #endif
  2473. wolfSSL_CertManagerFree(cm);
  2474. printf(resultFmt, passed);
  2475. #endif
  2476. return 0;
  2477. }
  2478. static int test_wolfSSL_CertManagerCRL(void)
  2479. {
  2480. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2481. !defined(NO_RSA)
  2482. const char* ca_cert = "./certs/ca-cert.pem";
  2483. const char* crl1 = "./certs/crl/crl.pem";
  2484. const char* crl2 = "./certs/crl/crl2.pem";
  2485. WOLFSSL_CERT_MANAGER* cm = NULL;
  2486. AssertNotNull(cm = wolfSSL_CertManagerNew());
  2487. AssertIntEQ(WOLFSSL_SUCCESS,
  2488. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2489. AssertIntEQ(WOLFSSL_SUCCESS,
  2490. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2491. AssertIntEQ(WOLFSSL_SUCCESS,
  2492. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  2493. wolfSSL_CertManagerFreeCRL(cm);
  2494. AssertIntEQ(WOLFSSL_SUCCESS,
  2495. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  2496. AssertIntEQ(WOLFSSL_SUCCESS,
  2497. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  2498. wolfSSL_CertManagerFree(cm);
  2499. #endif
  2500. return 0;
  2501. }
  2502. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  2503. {
  2504. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2505. !defined(NO_WOLFSSL_CLIENT)
  2506. WOLFSSL_CTX* ctx;
  2507. const char* ca_cert = "./certs/ca-cert.pem";
  2508. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2509. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2510. AssertNotNull(ctx);
  2511. /* test good CA */
  2512. AssertTrue(WOLFSSL_SUCCESS ==
  2513. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  2514. WOLFSSL_LOAD_FLAG_NONE));
  2515. /* test expired CA */
  2516. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2517. AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2518. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2519. #else
  2520. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2521. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2522. #endif
  2523. AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  2524. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2525. wolfSSL_CTX_free(ctx);
  2526. #endif
  2527. return 0;
  2528. }
  2529. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  2530. {
  2531. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2532. defined(USE_CERT_BUFFERS_2048)
  2533. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  2534. WOLFSSL_CTX* ctx;
  2535. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  2536. byte ca_expired_cert[TWOK_BUF];
  2537. word32 sizeof_ca_expired_cert;
  2538. XFILE fp;
  2539. #ifndef NO_WOLFSSL_CLIENT
  2540. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2541. #else
  2542. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2543. #endif
  2544. AssertNotNull(ctx);
  2545. /* test good CA */
  2546. AssertTrue(WOLFSSL_SUCCESS ==
  2547. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  2548. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  2549. WOLFSSL_LOAD_FLAG_NONE));
  2550. /* load expired CA */
  2551. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  2552. fp = XFOPEN(ca_expired_cert_file, "rb");
  2553. AssertTrue(fp != XBADFILE);
  2554. sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  2555. sizeof(ca_expired_cert), fp);
  2556. XFCLOSE(fp);
  2557. /* test expired CA failure */
  2558. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  2559. AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2560. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2561. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2562. #else
  2563. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2564. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2565. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  2566. #endif
  2567. /* test expired CA success */
  2568. AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  2569. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  2570. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  2571. wolfSSL_CTX_free(ctx);
  2572. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  2573. #endif
  2574. return 0;
  2575. }
  2576. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  2577. {
  2578. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  2579. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  2580. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2581. WOLFSSL_CTX* ctx;
  2582. #ifndef NO_WOLFSSL_CLIENT
  2583. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2584. #else
  2585. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2586. #endif
  2587. AssertTrue(WOLFSSL_SUCCESS ==
  2588. wolfSSL_CTX_load_verify_chain_buffer_format(ctx, ca_cert_chain_der,
  2589. sizeof_ca_cert_chain_der,
  2590. WOLFSSL_FILETYPE_ASN1));
  2591. wolfSSL_CTX_free(ctx);
  2592. #endif
  2593. return 0;
  2594. }
  2595. static int test_wolfSSL_CTX_add1_chain_cert(void)
  2596. {
  2597. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  2598. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  2599. WOLFSSL_CTX* ctx;
  2600. WOLFSSL* ssl;
  2601. const char *certChain[] = {
  2602. "./certs/intermediate/client-int-cert.pem",
  2603. "./certs/intermediate/ca-int2-cert.pem",
  2604. "./certs/intermediate/ca-int-cert.pem",
  2605. "./certs/ca-cert.pem",
  2606. NULL
  2607. };
  2608. const char** cert;
  2609. WOLFSSL_X509* x509;
  2610. WOLF_STACK_OF(X509)* chain = NULL;
  2611. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2612. AssertNotNull(ssl = wolfSSL_new(ctx));
  2613. for (cert = certChain; *cert != NULL; cert++) {
  2614. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2615. AssertNotNull(x509);
  2616. AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  2617. X509_free(x509);
  2618. }
  2619. for (cert = certChain; *cert != NULL; cert++) {
  2620. x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
  2621. AssertNotNull(x509);
  2622. AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  2623. X509_free(x509);
  2624. }
  2625. AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  2626. AssertIntEQ(sk_X509_num(chain), 3);
  2627. AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  2628. AssertIntEQ(sk_X509_num(chain), 3);
  2629. SSL_free(ssl);
  2630. SSL_CTX_free(ctx);
  2631. #endif
  2632. return 0;
  2633. }
  2634. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  2635. {
  2636. int ret = 0;
  2637. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2638. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2639. const char* server_chain_der = "./certs/server-cert-chain.der";
  2640. const char* client_single_pem = "./certs/client-cert.pem";
  2641. WOLFSSL_CTX* ctx;
  2642. (void)server_chain_der;
  2643. (void)client_single_pem;
  2644. (void)ctx;
  2645. #ifndef NO_WOLFSSL_CLIENT
  2646. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2647. AssertNotNull(ctx);
  2648. #else
  2649. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2650. AssertNotNull(ctx);
  2651. #endif
  2652. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2653. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2654. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  2655. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2656. wolfSSL_CTX_free(ctx);
  2657. #endif
  2658. return ret;
  2659. }
  2660. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  2661. {
  2662. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2663. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2664. WOLFSSL_CTX *ctx;
  2665. (void)ctx;
  2666. #ifndef NO_WOLFSSL_CLIENT
  2667. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2668. #else
  2669. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2670. #endif
  2671. /* invalid context */
  2672. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  2673. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2674. /* invalid dhParamFile file */
  2675. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2676. NULL, WOLFSSL_FILETYPE_PEM));
  2677. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  2678. bogusFile, WOLFSSL_FILETYPE_PEM));
  2679. /* success */
  2680. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  2681. WOLFSSL_FILETYPE_PEM));
  2682. wolfSSL_CTX_free(ctx);
  2683. #endif
  2684. return 0;
  2685. }
  2686. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  2687. {
  2688. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2689. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2690. WOLFSSL_CTX *ctx;
  2691. (void)ctx;
  2692. #ifndef NO_WOLFSSL_CLIENT
  2693. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2694. #else
  2695. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2696. #endif
  2697. /* invalid context */
  2698. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2699. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2700. /* invalid dhParamFile file */
  2701. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  2702. 0, WOLFSSL_FILETYPE_ASN1));
  2703. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
  2704. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2705. /* success */
  2706. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2707. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2708. wolfSSL_CTX_free(ctx);
  2709. #endif
  2710. return 0;
  2711. }
  2712. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  2713. {
  2714. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  2715. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2716. WOLFSSL_CTX *ctx;
  2717. (void)ctx;
  2718. #ifndef NO_WOLFSSL_CLIENT
  2719. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2720. AssertNotNull(ctx);
  2721. #else
  2722. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  2723. AssertNotNull(ctx);
  2724. #endif
  2725. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  2726. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2727. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2728. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  2729. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2730. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2731. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  2732. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2733. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2734. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  2735. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  2736. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2737. wolfSSL_CTX_free(ctx);
  2738. #endif
  2739. return 0;
  2740. }
  2741. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  2742. {
  2743. #if defined(WOLFSSL_DER_LOAD) && \
  2744. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  2745. WOLFSSL_CTX* ctx = NULL;
  2746. const char* derCert = "./certs/server-cert.der";
  2747. const char* nullPath = NULL;
  2748. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  2749. const char* emptyPath = "";
  2750. /* der load Case 1 ctx NULL */
  2751. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2752. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2753. #ifndef NO_WOLFSSL_CLIENT
  2754. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2755. #else
  2756. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2757. #endif
  2758. /* Case 2 filePath NULL */
  2759. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  2760. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2761. /* Case 3 invalid format */
  2762. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2763. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  2764. /* Case 4 filePath not valid */
  2765. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  2766. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2767. /* Case 5 filePath empty */
  2768. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  2769. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  2770. #ifndef NO_RSA
  2771. /* Case 6 success case */
  2772. AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  2773. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2774. #endif
  2775. wolfSSL_CTX_free(ctx);
  2776. #endif
  2777. return 0;
  2778. }
  2779. static int test_wolfSSL_CTX_enable_disable(void)
  2780. {
  2781. #ifndef NO_CERTS
  2782. WOLFSSL_CTX* ctx = NULL;
  2783. #ifdef HAVE_CRL
  2784. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  2785. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  2786. #endif
  2787. #ifdef HAVE_OCSP
  2788. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  2789. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  2790. #endif
  2791. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2792. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2793. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  2794. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  2795. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2796. AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  2797. #endif
  2798. #ifndef NO_WOLFSSL_CLIENT
  2799. #ifdef HAVE_EXTENDED_MASTER
  2800. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  2801. #endif
  2802. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  2803. AssertNotNull(ctx);
  2804. #ifdef HAVE_EXTENDED_MASTER
  2805. AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  2806. #endif
  2807. #elif !defined(NO_WOLFSSL_SERVER)
  2808. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2809. #else
  2810. return 0;
  2811. #endif
  2812. #ifdef HAVE_CRL
  2813. AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  2814. AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  2815. #endif
  2816. #ifdef HAVE_OCSP
  2817. AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  2818. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  2819. WOLFSSL_SUCCESS);
  2820. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  2821. WOLFSSL_SUCCESS);
  2822. AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  2823. WOLFSSL_SUCCESS);
  2824. #endif
  2825. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2826. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2827. AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2828. AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  2829. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2830. AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  2831. #endif
  2832. wolfSSL_CTX_free(ctx);
  2833. #endif /* NO_CERTS */
  2834. return 0;
  2835. }
  2836. static int test_wolfSSL_CTX_ticket_API(void)
  2837. {
  2838. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  2839. WOLFSSL_CTX* ctx = NULL;
  2840. void *userCtx = (void*)"this is my ctx";
  2841. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2842. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  2843. AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  2844. wolfSSL_CTX_free(ctx);
  2845. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  2846. AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  2847. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  2848. return 0;
  2849. }
  2850. static int test_wolfSSL_set_minmax_proto_version(void)
  2851. {
  2852. #ifdef OPENSSL_EXTRA
  2853. WOLFSSL_CTX *ctx;
  2854. WOLFSSL *ssl;
  2855. int ret;
  2856. (void)ret;
  2857. (void)ssl;
  2858. printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
  2859. #ifndef NO_WOLFSSL_CLIENT
  2860. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2861. AssertNotNull(ssl = wolfSSL_new(ctx));
  2862. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2863. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2864. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2865. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2866. AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2867. AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  2868. AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2869. AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  2870. wolfSSL_free(ssl);
  2871. wolfSSL_CTX_free(ctx);
  2872. #endif
  2873. #ifndef NO_WOLFSSL_SERVER
  2874. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2875. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  2876. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  2877. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  2878. AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  2879. wolfSSL_CTX_free(ctx);
  2880. #endif
  2881. printf(resultFmt, passed);
  2882. #endif
  2883. return 0;
  2884. }
  2885. /*----------------------------------------------------------------------------*
  2886. | SSL
  2887. *----------------------------------------------------------------------------*/
  2888. static int test_server_wolfSSL_new(void)
  2889. {
  2890. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2891. !defined(NO_WOLFSSL_SERVER)
  2892. WOLFSSL_CTX *ctx;
  2893. WOLFSSL_CTX *ctx_nocert;
  2894. WOLFSSL *ssl;
  2895. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2896. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2897. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  2898. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  2899. /* invalid context */
  2900. AssertNull(ssl = wolfSSL_new(NULL));
  2901. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
  2902. AssertNull(ssl = wolfSSL_new(ctx_nocert));
  2903. #endif
  2904. /* success */
  2905. AssertNotNull(ssl = wolfSSL_new(ctx));
  2906. wolfSSL_free(ssl);
  2907. wolfSSL_CTX_free(ctx);
  2908. wolfSSL_CTX_free(ctx_nocert);
  2909. #endif
  2910. return 0;
  2911. }
  2912. static int test_client_wolfSSL_new(void)
  2913. {
  2914. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  2915. !defined(NO_WOLFSSL_CLIENT)
  2916. WOLFSSL_CTX *ctx;
  2917. WOLFSSL_CTX *ctx_nocert;
  2918. WOLFSSL *ssl;
  2919. AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2920. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  2921. AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  2922. /* invalid context */
  2923. AssertNull(ssl = wolfSSL_new(NULL));
  2924. /* success */
  2925. AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
  2926. wolfSSL_free(ssl);
  2927. /* success */
  2928. AssertNotNull(ssl = wolfSSL_new(ctx));
  2929. wolfSSL_free(ssl);
  2930. wolfSSL_CTX_free(ctx);
  2931. wolfSSL_CTX_free(ctx_nocert);
  2932. #endif
  2933. return 0;
  2934. }
  2935. static int test_wolfSSL_SetTmpDH_file(void)
  2936. {
  2937. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  2938. !defined(NO_WOLFSSL_SERVER)
  2939. WOLFSSL_CTX *ctx;
  2940. WOLFSSL *ssl;
  2941. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2942. #ifndef NO_RSA
  2943. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  2944. WOLFSSL_FILETYPE_PEM));
  2945. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  2946. WOLFSSL_FILETYPE_PEM));
  2947. #elif defined(HAVE_ECC)
  2948. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  2949. WOLFSSL_FILETYPE_PEM));
  2950. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  2951. WOLFSSL_FILETYPE_PEM));
  2952. #elif defined(HAVE_ED25519)
  2953. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  2954. WOLFSSL_FILETYPE_PEM));
  2955. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  2956. WOLFSSL_FILETYPE_PEM));
  2957. #elif defined(HAVE_ED448)
  2958. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  2959. WOLFSSL_FILETYPE_PEM));
  2960. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  2961. WOLFSSL_FILETYPE_PEM));
  2962. #endif
  2963. AssertNotNull(ssl = wolfSSL_new(ctx));
  2964. /* invalid ssl */
  2965. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  2966. dhParamFile, WOLFSSL_FILETYPE_PEM));
  2967. /* invalid dhParamFile file */
  2968. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2969. NULL, WOLFSSL_FILETYPE_PEM));
  2970. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  2971. bogusFile, WOLFSSL_FILETYPE_PEM));
  2972. /* success */
  2973. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  2974. WOLFSSL_FILETYPE_PEM));
  2975. wolfSSL_free(ssl);
  2976. wolfSSL_CTX_free(ctx);
  2977. #endif
  2978. return 0;
  2979. }
  2980. static int test_wolfSSL_SetTmpDH_buffer(void)
  2981. {
  2982. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  2983. WOLFSSL_CTX *ctx;
  2984. WOLFSSL *ssl;
  2985. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  2986. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  2987. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  2988. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  2989. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2990. AssertNotNull(ssl = wolfSSL_new(ctx));
  2991. /* invalid ssl */
  2992. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  2993. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2994. /* invalid dhParamFile file */
  2995. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  2996. 0, WOLFSSL_FILETYPE_ASN1));
  2997. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  2998. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  2999. /* success */
  3000. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3001. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3002. wolfSSL_free(ssl);
  3003. wolfSSL_CTX_free(ctx);
  3004. #endif
  3005. return 0;
  3006. }
  3007. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  3008. {
  3009. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  3010. WOLFSSL_CTX *ctx, *ctx2;
  3011. WOLFSSL *ssl, *ssl2;
  3012. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3013. AssertNotNull(ctx);
  3014. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  3015. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3016. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  3017. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3018. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3019. ssl = wolfSSL_new(ctx);
  3020. AssertNotNull(ssl);
  3021. ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3022. AssertNotNull(ctx2);
  3023. AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  3024. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  3025. AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  3026. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3027. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3028. ssl2 = wolfSSL_new(ctx2);
  3029. AssertNotNull(ssl2);
  3030. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3031. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3032. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  3033. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3034. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3035. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  3036. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3037. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3038. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3039. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3040. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  3041. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  3042. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3043. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  3044. AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  3045. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3046. wolfSSL_free(ssl2);
  3047. wolfSSL_CTX_free(ctx2);
  3048. wolfSSL_free(ssl);
  3049. wolfSSL_CTX_free(ctx);
  3050. #endif
  3051. return 0;
  3052. }
  3053. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  3054. * allowed.
  3055. * POST: return 1 on success.
  3056. */
  3057. static int test_wolfSSL_SetMinVersion(void)
  3058. {
  3059. int failFlag = WOLFSSL_SUCCESS;
  3060. #ifndef NO_WOLFSSL_CLIENT
  3061. WOLFSSL_CTX* ctx;
  3062. WOLFSSL* ssl;
  3063. int itr;
  3064. #ifndef NO_OLD_TLS
  3065. const int versions[] = {
  3066. #ifdef WOLFSSL_ALLOW_TLSV10
  3067. WOLFSSL_TLSV1,
  3068. #endif
  3069. WOLFSSL_TLSV1_1,
  3070. WOLFSSL_TLSV1_2};
  3071. #elif !defined(WOLFSSL_NO_TLS12)
  3072. const int versions[] = { WOLFSSL_TLSV1_2 };
  3073. #else
  3074. const int versions[] = { WOLFSSL_TLSV1_3 };
  3075. #endif
  3076. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3077. ssl = wolfSSL_new(ctx);
  3078. printf(testingFmt, "wolfSSL_SetMinVersion()");
  3079. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  3080. if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
  3081. failFlag = WOLFSSL_FAILURE;
  3082. }
  3083. }
  3084. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  3085. wolfSSL_free(ssl);
  3086. wolfSSL_CTX_free(ctx);
  3087. #endif
  3088. if (failFlag == WOLFSSL_SUCCESS) {
  3089. failFlag = 0;
  3090. }
  3091. return failFlag;
  3092. } /* END test_wolfSSL_SetMinVersion */
  3093. /*----------------------------------------------------------------------------*
  3094. | EC
  3095. *----------------------------------------------------------------------------*/
  3096. /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
  3097. EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
  3098. */
  3099. #ifdef OPENSSL_EXTRA
  3100. static int test_wolfSSL_EC(void)
  3101. {
  3102. #if !defined(WOLFSSL_SP_MATH) && \
  3103. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  3104. #if defined(HAVE_ECC)
  3105. BN_CTX *ctx;
  3106. EC_GROUP *group;
  3107. EC_GROUP *group2;
  3108. EC_POINT *Gxy, *new_point, *set_point;
  3109. BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
  3110. BIGNUM *X, *Y;
  3111. BIGNUM *set_point_bn;
  3112. char* hexStr;
  3113. int group_bits;
  3114. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
  3115. /* NISTP256R1 Gx/Gy */
  3116. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  3117. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  3118. #ifndef HAVE_SELFTEST
  3119. EC_POINT *tmp;
  3120. size_t bin_len;
  3121. unsigned char* buf = NULL;
  3122. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
  3123. const unsigned char binUncompG[] = {
  3124. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  3125. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  3126. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  3127. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  3128. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  3129. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  3130. };
  3131. #ifdef HAVE_COMP_KEY
  3132. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
  3133. const unsigned char binCompG[] = {
  3134. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  3135. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  3136. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  3137. };
  3138. #endif
  3139. #endif
  3140. AssertNotNull(ctx = BN_CTX_new());
  3141. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  3142. AssertNotNull(group2 = EC_GROUP_dup(group));
  3143. AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  3144. AssertNotNull(Gxy = EC_POINT_new(group));
  3145. AssertNotNull(new_point = EC_POINT_new(group));
  3146. AssertNotNull(set_point = EC_POINT_new(group));
  3147. AssertNotNull(X = BN_new());
  3148. AssertNotNull(Y = BN_new());
  3149. AssertNotNull(set_point_bn = BN_new());
  3150. /* load test values */
  3151. AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  3152. AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  3153. AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  3154. AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  3155. /* populate coordinates for input point */
  3156. Gxy->X = Gx;
  3157. Gxy->Y = Gy;
  3158. Gxy->Z = Gz;
  3159. #ifndef HAVE_SELFTEST
  3160. /* perform point multiplication */
  3161. AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
  3162. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
  3163. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3164. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3165. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3166. AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
  3167. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3168. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3169. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3170. AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
  3171. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3172. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3173. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3174. #else
  3175. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
  3176. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3177. AssertIntEQ(BN_is_zero(new_point->Y), 0);
  3178. AssertIntEQ(BN_is_zero(new_point->Z), 0);
  3179. #endif
  3180. /* check if point X coordinate is zero */
  3181. AssertIntEQ(BN_is_zero(new_point->X), 0);
  3182. #ifdef USE_ECC_B_PARAM
  3183. AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  3184. #endif /* USE_ECC_B_PARAM */
  3185. /* Force non-affine coordinates */
  3186. AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  3187. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  3188. new_point->inSet = 0;
  3189. /* extract the coordinates from point */
  3190. AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
  3191. /* check if point X coordinate is zero */
  3192. AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  3193. /* set the same X and Y points in another object */
  3194. AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
  3195. /* compare points as they should be the same */
  3196. AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  3197. /* Test copying */
  3198. AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
  3199. /* Test inverting */
  3200. AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  3201. AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
  3202. set_point_bn, ctx), set_point_bn);
  3203. /* check bn2hex */
  3204. hexStr = BN_bn2hex(k);
  3205. AssertStrEQ(hexStr, kTest);
  3206. #ifndef NO_FILESYSTEM
  3207. BN_print_fp(stdout, k);
  3208. printf("\n");
  3209. #endif
  3210. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3211. hexStr = BN_bn2hex(Gx);
  3212. AssertStrEQ(hexStr, kGx);
  3213. #ifndef NO_FILESYSTEM
  3214. BN_print_fp(stdout, Gx);
  3215. printf("\n");
  3216. #endif
  3217. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3218. hexStr = BN_bn2hex(Gy);
  3219. AssertStrEQ(hexStr, kGy);
  3220. #ifndef NO_FILESYSTEM
  3221. BN_print_fp(stdout, Gy);
  3222. printf("\n");
  3223. #endif
  3224. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3225. #ifndef HAVE_SELFTEST
  3226. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  3227. AssertStrEQ(hexStr, uncompG);
  3228. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3229. #ifdef HAVE_COMP_KEY
  3230. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  3231. AssertStrEQ(hexStr, compG);
  3232. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  3233. #endif
  3234. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
  3235. AssertIntEQ(bin_len, sizeof(binUncompG));
  3236. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  3237. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
  3238. bin_len, ctx), bin_len);
  3239. AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  3240. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  3241. #ifdef HAVE_COMP_KEY
  3242. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
  3243. AssertIntEQ(bin_len, sizeof(binCompG));
  3244. AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
  3245. AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  3246. bin_len, ctx), bin_len);
  3247. AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  3248. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  3249. #endif
  3250. AssertNotNull(tmp = EC_POINT_new(group));
  3251. AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
  3252. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  3253. EC_POINT_free(tmp);
  3254. #ifdef HAVE_COMP_KEY
  3255. AssertNotNull(tmp = EC_POINT_new(group));
  3256. AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
  3257. AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  3258. EC_POINT_free(tmp);
  3259. #endif
  3260. #endif
  3261. /* test BN_mod_add */
  3262. AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  3263. (WOLFSSL_BIGNUM*)BN_value_one(),
  3264. (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
  3265. AssertIntEQ(BN_is_zero(new_point->Z), 1);
  3266. /* cleanup */
  3267. BN_free(X);
  3268. BN_free(Y);
  3269. BN_free(k);
  3270. BN_free(set_point_bn);
  3271. EC_POINT_free(new_point);
  3272. EC_POINT_free(set_point);
  3273. EC_POINT_free(Gxy);
  3274. EC_GROUP_free(group);
  3275. EC_GROUP_free(group2);
  3276. BN_CTX_free(ctx);
  3277. #endif /* HAVE_ECC */
  3278. #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  3279. return 0;
  3280. }
  3281. #endif /* OPENSSL_EXTRA */
  3282. #ifndef NO_BIO
  3283. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  3284. {
  3285. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  3286. EC_GROUP *group;
  3287. BIO* bio;
  3288. AssertNotNull(bio = BIO_new(BIO_s_file()));
  3289. AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  3290. AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  3291. AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  3292. EC_GROUP_free(group);
  3293. BIO_free(bio);
  3294. #endif /* HAVE_ECC */
  3295. return 0;
  3296. }
  3297. #endif /* !NO_BIO */
  3298. # if defined(OPENSSL_EXTRA)
  3299. static int test_wolfSSL_ECDSA_SIG(void)
  3300. {
  3301. #ifdef HAVE_ECC
  3302. WOLFSSL_ECDSA_SIG* sig = NULL;
  3303. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  3304. const unsigned char* cp;
  3305. unsigned char* p;
  3306. unsigned char outSig[8];
  3307. unsigned char sigData[8] =
  3308. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  3309. sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
  3310. AssertNull(sig);
  3311. cp = sigData;
  3312. AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  3313. AssertIntEQ((cp == sigData + 8), 1);
  3314. cp = sigData;
  3315. AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  3316. AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  3317. AssertIntEQ((sig == sig2), 1);
  3318. cp = outSig;
  3319. p = outSig;
  3320. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  3321. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  3322. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  3323. AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  3324. AssertIntEQ((p == outSig + 8), 1);
  3325. AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  3326. wolfSSL_ECDSA_SIG_free(sig);
  3327. #endif /* HAVE_ECC */
  3328. return 0;
  3329. }
  3330. static int test_EC_i2d(void)
  3331. {
  3332. #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
  3333. EC_KEY *key;
  3334. EC_KEY *copy;
  3335. int len;
  3336. unsigned char *buf = NULL;
  3337. const unsigned char *tmp = NULL;
  3338. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  3339. AssertIntEQ(EC_KEY_generate_key(key), 1);
  3340. AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  3341. AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
  3342. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3343. buf = NULL;
  3344. AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  3345. AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
  3346. tmp = buf;
  3347. AssertNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  3348. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3349. buf = NULL;
  3350. AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
  3351. tmp = buf;
  3352. AssertNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  3353. AssertIntEQ(EC_KEY_check_key(key), 1);
  3354. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  3355. EC_KEY_free(key);
  3356. EC_KEY_free(copy);
  3357. #endif /* HAVE_ECC */
  3358. return 0;
  3359. }
  3360. static int test_ECDSA_size_sign(void)
  3361. {
  3362. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  3363. EC_KEY *key;
  3364. int id;
  3365. byte hash[WC_MAX_DIGEST_SIZE];
  3366. byte sig[ECC_MAX_SIG_SIZE];
  3367. unsigned int sigSz = sizeof(sig);
  3368. XMEMSET(hash, 123, sizeof(hash));
  3369. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  3370. AssertIntEQ(id, ECC_SECP256R1);
  3371. AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  3372. AssertIntEQ(EC_KEY_generate_key(key), 1);
  3373. AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  3374. AssertIntGE(ECDSA_size(key), sigSz);
  3375. AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  3376. EC_KEY_free(key);
  3377. #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
  3378. return 0;
  3379. }
  3380. static int test_ED25519(void)
  3381. {
  3382. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  3383. defined(WOLFSSL_KEY_GEN)
  3384. byte priv[ED25519_PRV_KEY_SIZE];
  3385. unsigned int privSz = (unsigned int)sizeof(priv);
  3386. byte pub[ED25519_PUB_KEY_SIZE];
  3387. unsigned int pubSz = (unsigned int)sizeof(pub);
  3388. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3389. const char* msg = TEST_STRING;
  3390. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3391. byte sig[ED25519_SIG_SIZE];
  3392. unsigned int sigSz = (unsigned int)sizeof(sig);
  3393. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3394. AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  3395. WOLFSSL_SUCCESS);
  3396. AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  3397. AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  3398. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  3399. AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  3400. &sigSz), WOLFSSL_SUCCESS);
  3401. AssertIntEQ(sigSz, ED25519_SIG_SIZE);
  3402. #ifdef HAVE_ED25519_VERIFY
  3403. AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  3404. sigSz), WOLFSSL_SUCCESS);
  3405. #endif /* HAVE_ED25519_VERIFY */
  3406. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  3407. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3408. return 0;
  3409. }
  3410. static int test_ED448(void)
  3411. {
  3412. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  3413. defined(WOLFSSL_KEY_GEN)
  3414. byte priv[ED448_PRV_KEY_SIZE];
  3415. unsigned int privSz = (unsigned int)sizeof(priv);
  3416. byte pub[ED448_PUB_KEY_SIZE];
  3417. unsigned int pubSz = (unsigned int)sizeof(pub);
  3418. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3419. const char* msg = TEST_STRING;
  3420. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  3421. byte sig[ED448_SIG_SIZE];
  3422. unsigned int sigSz = (unsigned int)sizeof(sig);
  3423. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3424. AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
  3425. WOLFSSL_SUCCESS);
  3426. AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
  3427. AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  3428. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  3429. AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  3430. &sigSz), WOLFSSL_SUCCESS);
  3431. AssertIntEQ(sigSz, ED448_SIG_SIZE);
  3432. #ifdef HAVE_ED448_VERIFY
  3433. AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  3434. sigSz), WOLFSSL_SUCCESS);
  3435. #endif /* HAVE_ED448_VERIFY */
  3436. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  3437. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  3438. return 0;
  3439. }
  3440. #endif /* OPENSSL_EXTRA */
  3441. #include <wolfssl/openssl/pem.h>
  3442. /*----------------------------------------------------------------------------*
  3443. | EVP
  3444. *----------------------------------------------------------------------------*/
  3445. static int test_wolfSSL_EVP_PKEY_print_public(void)
  3446. {
  3447. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  3448. WOLFSSL_BIO* rbio = NULL;
  3449. WOLFSSL_BIO* wbio = NULL;
  3450. WOLFSSL_EVP_PKEY* pkey = NULL;
  3451. char line[256] = { 0 };
  3452. char line1[256] = { 0 };
  3453. int i;
  3454. printf(testingFmt, "EVP_PKEY_print_public()");
  3455. /* test error cases */
  3456. AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  3457. /*
  3458. * test RSA public key print
  3459. * in this test, pass '3' for indent
  3460. */
  3461. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  3462. rbio = BIO_new_mem_buf( client_keypub_der_1024,
  3463. sizeof_client_keypub_der_1024);
  3464. AssertNotNull(rbio);
  3465. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3466. AssertNotNull(pkey);
  3467. wbio = BIO_new(BIO_s_mem());
  3468. AssertNotNull(wbio);
  3469. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  3470. BIO_gets(wbio, line, sizeof(line));
  3471. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  3472. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3473. BIO_gets(wbio, line, sizeof(line));
  3474. strcpy(line1, " Modulus:\n");
  3475. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3476. BIO_gets(wbio, line, sizeof(line));
  3477. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  3478. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3479. /* skip to the end of modulus element*/
  3480. for( i = 0; i < 8 ;i++) {
  3481. BIO_gets(wbio, line, sizeof(line));
  3482. }
  3483. BIO_gets(wbio, line, sizeof(line));
  3484. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  3485. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3486. /* should reach EOF */
  3487. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3488. EVP_PKEY_free(pkey);
  3489. pkey = NULL;
  3490. BIO_free(rbio);
  3491. BIO_free(wbio);
  3492. rbio = NULL;
  3493. wbio = NULL;
  3494. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  3495. /*
  3496. * test DSA public key print
  3497. */
  3498. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  3499. rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  3500. sizeof_dsa_pub_key_der_2048);
  3501. AssertNotNull(rbio);
  3502. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3503. AssertNotNull(pkey);
  3504. wbio = BIO_new(BIO_s_mem());
  3505. AssertNotNull(wbio);
  3506. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3507. BIO_gets(wbio, line, sizeof(line));
  3508. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  3509. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3510. BIO_gets(wbio, line, sizeof(line));
  3511. strcpy(line1, "pub:\n");
  3512. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3513. BIO_gets(wbio, line, sizeof(line));
  3514. strcpy(line1,
  3515. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  3516. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3517. /* skip to the end of pub element*/
  3518. for( i = 0; i < 17 ;i++) {
  3519. BIO_gets(wbio, line, sizeof(line));
  3520. }
  3521. BIO_gets(wbio, line, sizeof(line));
  3522. strcpy(line1, "P:\n");
  3523. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3524. /* skip to the end of P element*/
  3525. for( i = 0; i < 18 ;i++) {
  3526. BIO_gets(wbio, line, sizeof(line));
  3527. }
  3528. BIO_gets(wbio, line, sizeof(line));
  3529. strcpy(line1, "Q:\n");
  3530. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3531. /* skip to the end of Q element*/
  3532. for( i = 0; i < 3 ;i++) {
  3533. BIO_gets(wbio, line, sizeof(line));
  3534. }
  3535. BIO_gets(wbio, line, sizeof(line));
  3536. strcpy(line1, "G:\n");
  3537. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3538. /* skip to the end of G element*/
  3539. for( i = 0; i < 18 ;i++) {
  3540. BIO_gets(wbio, line, sizeof(line));
  3541. }
  3542. /* should reach EOF */
  3543. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3544. EVP_PKEY_free(pkey);
  3545. pkey = NULL;
  3546. BIO_free(rbio);
  3547. BIO_free(wbio);
  3548. rbio = NULL;
  3549. wbio = NULL;
  3550. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  3551. /*
  3552. * test ECC public key print
  3553. */
  3554. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  3555. rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  3556. sizeof_ecc_clikeypub_der_256);
  3557. AssertNotNull(rbio);
  3558. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3559. AssertNotNull(pkey);
  3560. wbio = BIO_new(BIO_s_mem());
  3561. AssertNotNull(wbio);
  3562. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3563. BIO_gets(wbio, line, sizeof(line));
  3564. strcpy(line1, "Public-Key: (256 bit)\n");
  3565. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3566. BIO_gets(wbio, line, sizeof(line));
  3567. strcpy(line1, "pub:\n");
  3568. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3569. BIO_gets(wbio, line, sizeof(line));
  3570. strcpy(line1,
  3571. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  3572. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3573. /* skip to the end of pub element*/
  3574. for( i = 0; i < 4 ;i++) {
  3575. BIO_gets(wbio, line, sizeof(line));
  3576. }
  3577. BIO_gets(wbio, line, sizeof(line));
  3578. strcpy(line1, "ASN1 OID: prime256v1\n");
  3579. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3580. BIO_gets(wbio, line, sizeof(line));
  3581. strcpy(line1, "NIST CURVE: P-256\n");
  3582. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3583. /* should reach EOF */
  3584. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3585. EVP_PKEY_free(pkey);
  3586. pkey = NULL;
  3587. BIO_free(rbio);
  3588. BIO_free(wbio);
  3589. rbio = NULL;
  3590. wbio = NULL;
  3591. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  3592. /*
  3593. * test DH public key print
  3594. */
  3595. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  3596. rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  3597. sizeof_dh_pub_key_der_2048);
  3598. AssertNotNull(rbio);
  3599. wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
  3600. AssertNotNull(pkey);
  3601. wbio = BIO_new(BIO_s_mem());
  3602. AssertNotNull(wbio);
  3603. AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  3604. BIO_gets(wbio, line, sizeof(line));
  3605. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  3606. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3607. BIO_gets(wbio, line, sizeof(line));
  3608. strcpy(line1, "public-key:\n");
  3609. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3610. BIO_gets(wbio, line, sizeof(line));
  3611. strcpy(line1,
  3612. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  3613. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3614. /* skip to the end of public-key element*/
  3615. for( i = 0; i < 17 ;i++) {
  3616. BIO_gets(wbio, line, sizeof(line));
  3617. }
  3618. BIO_gets(wbio, line, sizeof(line));
  3619. strcpy(line1, "prime:\n");
  3620. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3621. BIO_gets(wbio, line, sizeof(line));
  3622. strcpy(line1,
  3623. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  3624. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3625. /* skip to the end of prime element*/
  3626. for( i = 0; i < 17 ;i++) {
  3627. BIO_gets(wbio, line, sizeof(line));
  3628. }
  3629. BIO_gets(wbio, line, sizeof(line));
  3630. strcpy(line1, "generator: 2 (0x02)\n");
  3631. AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  3632. /* should reach EOF */
  3633. AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
  3634. EVP_PKEY_free(pkey);
  3635. pkey = NULL;
  3636. BIO_free(rbio);
  3637. BIO_free(wbio);
  3638. rbio = NULL;
  3639. wbio = NULL;
  3640. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  3641. /* to prevent "unused variable" warning */
  3642. (void)pkey;
  3643. (void)wbio;
  3644. (void)rbio;
  3645. (void)line;
  3646. (void)line1;
  3647. (void)i;
  3648. printf(resultFmt, passed);
  3649. #endif /* OPENSSL_EXTRA */
  3650. return 0;
  3651. }
  3652. /* Test functions for base64 encode/decode */
  3653. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  3654. {
  3655. #if defined(OPENSSL_EXTRA) && \
  3656. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3657. EVP_ENCODE_CTX* ctx = NULL;
  3658. printf(testingFmt, "EVP_ENCODE_CTX_new()");
  3659. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3660. AssertIntEQ( ctx->remaining,0);
  3661. AssertIntEQ( ctx->data[0],0);
  3662. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3663. EVP_ENCODE_CTX_free(ctx);
  3664. printf(resultFmt, passed);
  3665. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3666. return 0;
  3667. }
  3668. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  3669. {
  3670. #if defined(OPENSSL_EXTRA) && \
  3671. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  3672. EVP_ENCODE_CTX* ctx = NULL;
  3673. printf(testingFmt, "EVP_ENCODE_CTX_free()");
  3674. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3675. EVP_ENCODE_CTX_free(ctx);
  3676. printf(resultFmt, passed);
  3677. #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
  3678. return 0;
  3679. }
  3680. static int test_wolfSSL_EVP_EncodeInit(void)
  3681. {
  3682. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3683. EVP_ENCODE_CTX* ctx = NULL;
  3684. printf(testingFmt, "EVP_EncodeInit()");
  3685. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3686. AssertIntEQ( ctx->remaining,0);
  3687. AssertIntEQ( ctx->data[0],0);
  3688. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3689. /* make ctx dirty */
  3690. ctx->remaining = 10;
  3691. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3692. EVP_EncodeInit(ctx);
  3693. AssertIntEQ( ctx->remaining,0);
  3694. AssertIntEQ( ctx->data[0],0);
  3695. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3696. EVP_ENCODE_CTX_free(ctx);
  3697. printf(resultFmt, passed);
  3698. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3699. return 0;
  3700. }
  3701. static int test_wolfSSL_EVP_EncodeUpdate(void)
  3702. {
  3703. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3704. int outl;
  3705. int total;
  3706. const unsigned char plain0[] = {"Th"};
  3707. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  3708. const unsigned char plain2[] = {"This is additional data."};
  3709. const unsigned char enc0[] = {"VGg=\n"};
  3710. /* expected encoded result for the first output 64 chars plus trailing LF*/
  3711. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  3712. const unsigned char enc2[] =
  3713. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  3714. unsigned char encOutBuff[300];
  3715. EVP_ENCODE_CTX* ctx = NULL;
  3716. printf(testingFmt, "EVP_EncodeUpdate()");
  3717. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3718. EVP_EncodeInit(ctx);
  3719. /* illegal parameter test */
  3720. AssertIntEQ(
  3721. EVP_EncodeUpdate(
  3722. NULL, /* pass NULL as ctx */
  3723. encOutBuff,
  3724. &outl,
  3725. plain1,
  3726. sizeof(plain1)-1),
  3727. 0 /* expected result code 0: fail */
  3728. );
  3729. AssertIntEQ(
  3730. EVP_EncodeUpdate(
  3731. ctx,
  3732. NULL, /* pass NULL as out buff */
  3733. &outl,
  3734. plain1,
  3735. sizeof(plain1)-1),
  3736. 0 /* expected result code 0: fail */
  3737. );
  3738. AssertIntEQ(
  3739. EVP_EncodeUpdate(
  3740. ctx,
  3741. encOutBuff,
  3742. NULL, /* pass NULL as outl */
  3743. plain1,
  3744. sizeof(plain1)-1),
  3745. 0 /* expected result code 0: fail */
  3746. );
  3747. AssertIntEQ(
  3748. EVP_EncodeUpdate(
  3749. ctx,
  3750. encOutBuff,
  3751. &outl,
  3752. NULL, /* pass NULL as in */
  3753. sizeof(plain1)-1),
  3754. 0 /* expected result code 0: fail */
  3755. );
  3756. AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  3757. /* meaningless parameter test */
  3758. AssertIntEQ(
  3759. EVP_EncodeUpdate(
  3760. ctx,
  3761. encOutBuff,
  3762. &outl,
  3763. plain1,
  3764. 0), /* pass zero input */
  3765. 1 /* expected result code 1: success */
  3766. );
  3767. /* very small data encoding test */
  3768. EVP_EncodeInit(ctx);
  3769. AssertIntEQ(
  3770. EVP_EncodeUpdate(
  3771. ctx,
  3772. encOutBuff,
  3773. &outl,
  3774. plain0,
  3775. sizeof(plain0)-1),
  3776. 1 /* expected result code 1: success */
  3777. );
  3778. AssertIntEQ(outl,0);
  3779. EVP_EncodeFinal(
  3780. ctx,
  3781. encOutBuff + outl,
  3782. &outl);
  3783. AssertIntEQ( outl, sizeof(enc0)-1);
  3784. AssertIntEQ(
  3785. XSTRNCMP(
  3786. (const char*)encOutBuff,
  3787. (const char*)enc0,sizeof(enc0) ),
  3788. 0);
  3789. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3790. AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  3791. sizeof(enc0)-1);
  3792. AssertIntEQ(
  3793. XSTRNCMP(
  3794. (const char*)encOutBuff,
  3795. (const char*)enc0,sizeof(enc0) ),
  3796. 0);
  3797. /* pass small size( < 48bytes ) input, then make sure they are not
  3798. * encoded and just stored in ctx
  3799. */
  3800. EVP_EncodeInit(ctx);
  3801. total = 0;
  3802. outl = 0;
  3803. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  3804. AssertIntEQ(
  3805. EVP_EncodeUpdate(
  3806. ctx,
  3807. encOutBuff, /* buffer for output */
  3808. &outl, /* size of output */
  3809. plain1, /* input */
  3810. sizeof(plain1)-1), /* size of input */
  3811. 1); /* expected result code 1:success */
  3812. total += outl;
  3813. AssertIntEQ(outl, 0); /* no output expected */
  3814. AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
  3815. AssertTrue(
  3816. XSTRNCMP((const char*)(ctx->data),
  3817. (const char*)plain1,
  3818. ctx->remaining) ==0 );
  3819. AssertTrue(encOutBuff[0] == 0);
  3820. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  3821. * the stored data and the new input together
  3822. */
  3823. AssertIntEQ(
  3824. EVP_EncodeUpdate(
  3825. ctx,
  3826. encOutBuff + outl, /* buffer for output */
  3827. &outl, /* size of output */
  3828. plain2, /* additional input */
  3829. sizeof(plain2) -1), /* size of additional input */
  3830. 1); /* expected result code 1:success */
  3831. total += outl;
  3832. AssertIntNE(outl, 0); /* some output is expected this time*/
  3833. AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  3834. AssertIntEQ(
  3835. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  3836. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  3837. EVP_EncodeFinal(
  3838. ctx,
  3839. encOutBuff + outl,
  3840. &outl);
  3841. total += outl;
  3842. AssertIntNE(total,0);
  3843. AssertIntNE(outl,0);
  3844. AssertIntEQ(XSTRNCMP(
  3845. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  3846. /* test with illeagal parameters */
  3847. outl = 1;
  3848. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  3849. AssertIntEQ(outl, 0);
  3850. outl = 1;
  3851. EVP_EncodeFinal(ctx, NULL, &outl);
  3852. AssertIntEQ(outl, 0);
  3853. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  3854. EVP_EncodeFinal(NULL, NULL, NULL);
  3855. EVP_ENCODE_CTX_free(ctx);
  3856. printf(resultFmt, passed);
  3857. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3858. return 0;
  3859. }
  3860. static int test_wolfSSL_EVP_EncodeFinal(void)
  3861. {
  3862. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  3863. printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
  3864. /* tests for wolfSSL_EVP_EncodeFinal are included in
  3865. * test_wolfSSL_EVP_EncodeUpdate
  3866. */
  3867. printf(resultFmt, passed);
  3868. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  3869. return 0;
  3870. }
  3871. static int test_wolfSSL_EVP_DecodeInit(void)
  3872. {
  3873. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3874. EVP_ENCODE_CTX* ctx = NULL;
  3875. printf(testingFmt, "EVP_DecodeInit()");
  3876. AssertNotNull( ctx = EVP_ENCODE_CTX_new());
  3877. AssertIntEQ( ctx->remaining,0);
  3878. AssertIntEQ( ctx->data[0],0);
  3879. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3880. /* make ctx dirty */
  3881. ctx->remaining = 10;
  3882. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  3883. EVP_DecodeInit(ctx);
  3884. AssertIntEQ( ctx->remaining,0);
  3885. AssertIntEQ( ctx->data[0],0);
  3886. AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  3887. EVP_ENCODE_CTX_free(ctx);
  3888. printf(resultFmt, passed);
  3889. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  3890. return 0;
  3891. }
  3892. static int test_wolfSSL_EVP_DecodeUpdate(void)
  3893. {
  3894. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  3895. int outl;
  3896. unsigned char decOutBuff[300];
  3897. EVP_ENCODE_CTX* ctx;
  3898. static const unsigned char enc1[] =
  3899. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3900. /* const unsigned char plain1[] =
  3901. {"This is a base64 decoding test."} */
  3902. printf(testingFmt, "EVP_DecodeUpdate()");
  3903. ctx = EVP_ENCODE_CTX_new();
  3904. EVP_DecodeInit(ctx);
  3905. /* illegal parameter tests */
  3906. /* pass NULL as ctx */
  3907. AssertIntEQ(
  3908. EVP_DecodeUpdate(
  3909. NULL, /* pass NULL as ctx */
  3910. decOutBuff,
  3911. &outl,
  3912. enc1,
  3913. sizeof(enc1)-1),
  3914. -1 /* expected result code -1: fail */
  3915. );
  3916. AssertIntEQ( outl, 0);
  3917. /* pass NULL as output */
  3918. AssertIntEQ(
  3919. EVP_DecodeUpdate(
  3920. ctx,
  3921. NULL, /* pass NULL as out buff */
  3922. &outl,
  3923. enc1,
  3924. sizeof(enc1)-1),
  3925. -1 /* expected result code -1: fail */
  3926. );
  3927. AssertIntEQ( outl, 0);
  3928. /* pass NULL as outl */
  3929. AssertIntEQ(
  3930. EVP_DecodeUpdate(
  3931. ctx,
  3932. decOutBuff,
  3933. NULL, /* pass NULL as outl */
  3934. enc1,
  3935. sizeof(enc1)-1),
  3936. -1 /* expected result code -1: fail */
  3937. );
  3938. /* pass NULL as input */
  3939. AssertIntEQ(
  3940. EVP_DecodeUpdate(
  3941. ctx,
  3942. decOutBuff,
  3943. &outl,
  3944. NULL, /* pass NULL as in */
  3945. sizeof(enc1)-1),
  3946. -1 /* expected result code -1: fail */
  3947. );
  3948. AssertIntEQ( outl, 0);
  3949. AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  3950. /* pass zero length input */
  3951. AssertIntEQ(
  3952. EVP_DecodeUpdate(
  3953. ctx,
  3954. decOutBuff,
  3955. &outl,
  3956. enc1,
  3957. 0), /* pass zero as input len */
  3958. 1 /* expected result code 1: success */
  3959. );
  3960. /* decode correct base64 string */
  3961. {
  3962. static const unsigned char enc2[] =
  3963. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  3964. static const unsigned char plain2[] =
  3965. {"This is a base64 decoding test."};
  3966. EVP_EncodeInit(ctx);
  3967. AssertIntEQ(
  3968. EVP_DecodeUpdate(
  3969. ctx,
  3970. decOutBuff,
  3971. &outl,
  3972. enc2,
  3973. sizeof(enc2)-1),
  3974. 0 /* expected result code 0: success */
  3975. );
  3976. AssertIntEQ(outl,sizeof(plain2) -1);
  3977. AssertIntEQ(
  3978. EVP_DecodeFinal(
  3979. ctx,
  3980. decOutBuff + outl,
  3981. &outl),
  3982. 1 /* expected result code 1: success */
  3983. );
  3984. AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
  3985. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3986. sizeof(plain2) -1 ),0);
  3987. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  3988. sizeof(plain2)-1);
  3989. AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  3990. sizeof(plain2) -1 ),0);
  3991. }
  3992. /* decode correct base64 string which does not have '\n' in its last*/
  3993. {
  3994. static const unsigned char enc3[] =
  3995. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  3996. static const unsigned char plain3[] =
  3997. {"This is a base64 decoding test."}; /* 31 chars */
  3998. EVP_EncodeInit(ctx);
  3999. AssertIntEQ(
  4000. EVP_DecodeUpdate(
  4001. ctx,
  4002. decOutBuff,
  4003. &outl,
  4004. enc3,
  4005. sizeof(enc3)-1),
  4006. 0 /* expected result code 0: success */
  4007. );
  4008. AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  4009. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4010. sizeof(plain3) -1 ),0);
  4011. AssertIntEQ(
  4012. EVP_DecodeFinal(
  4013. ctx,
  4014. decOutBuff + outl,
  4015. &outl),
  4016. 1 /* expected result code 1: success */
  4017. );
  4018. AssertIntEQ(outl,0 );
  4019. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  4020. sizeof(plain3)-1);
  4021. AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  4022. sizeof(plain3) -1 ),0);
  4023. }
  4024. /* decode string which has a padding char ('=') in the illegal position*/
  4025. {
  4026. static const unsigned char enc4[] =
  4027. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  4028. EVP_EncodeInit(ctx);
  4029. AssertIntEQ(
  4030. EVP_DecodeUpdate(
  4031. ctx,
  4032. decOutBuff,
  4033. &outl,
  4034. enc4,
  4035. sizeof(enc4)-1),
  4036. -1 /* expected result code -1: error */
  4037. );
  4038. AssertIntEQ(outl,0);
  4039. AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  4040. }
  4041. /* small data decode test */
  4042. {
  4043. static const unsigned char enc00[] = {"VG"};
  4044. static const unsigned char enc01[] = {"g=\n"};
  4045. static const unsigned char plain4[] = {"Th"};
  4046. EVP_EncodeInit(ctx);
  4047. AssertIntEQ(
  4048. EVP_DecodeUpdate(
  4049. ctx,
  4050. decOutBuff,
  4051. &outl,
  4052. enc00,
  4053. sizeof(enc00)-1),
  4054. 1 /* expected result code 1: success */
  4055. );
  4056. AssertIntEQ(outl,0);
  4057. AssertIntEQ(
  4058. EVP_DecodeUpdate(
  4059. ctx,
  4060. decOutBuff + outl,
  4061. &outl,
  4062. enc01,
  4063. sizeof(enc01)-1),
  4064. 0 /* expected result code 0: success */
  4065. );
  4066. AssertIntEQ(outl,sizeof(plain4)-1);
  4067. /* test with illegal parameters */
  4068. AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  4069. AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  4070. AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  4071. AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  4072. EVP_DecodeFinal(
  4073. ctx,
  4074. decOutBuff + outl,
  4075. &outl);
  4076. AssertIntEQ( outl, 0);
  4077. AssertIntEQ(
  4078. XSTRNCMP(
  4079. (const char*)decOutBuff,
  4080. (const char*)plain4,sizeof(plain4)-1 ),
  4081. 0);
  4082. }
  4083. EVP_ENCODE_CTX_free(ctx);
  4084. printf(resultFmt, passed);
  4085. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4086. return 0;
  4087. }
  4088. static int test_wolfSSL_EVP_DecodeFinal(void)
  4089. {
  4090. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  4091. printf(testingFmt, "EVP_DecodeFinal()");
  4092. /* tests for wolfSSL_EVP_DecodeFinal are included in
  4093. * test_wolfSSL_EVP_DecodeUpdate
  4094. */
  4095. printf(resultFmt, passed);
  4096. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  4097. return 0;
  4098. }
  4099. /* Test function for wolfSSL_EVP_get_cipherbynid.
  4100. */
  4101. #ifdef OPENSSL_EXTRA
  4102. static int test_wolfSSL_EVP_get_cipherbynid(void)
  4103. {
  4104. #ifndef NO_AES
  4105. const WOLFSSL_EVP_CIPHER* c;
  4106. c = wolfSSL_EVP_get_cipherbynid(419);
  4107. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4108. defined(WOLFSSL_AES_128)
  4109. AssertNotNull(c);
  4110. AssertNotNull(strcmp("EVP_AES_128_CBC", c));
  4111. #else
  4112. AssertNull(c);
  4113. #endif
  4114. c = wolfSSL_EVP_get_cipherbynid(423);
  4115. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4116. defined(WOLFSSL_AES_192)
  4117. AssertNotNull(c);
  4118. AssertNotNull(strcmp("EVP_AES_192_CBC", c));
  4119. #else
  4120. AssertNull(c);
  4121. #endif
  4122. c = wolfSSL_EVP_get_cipherbynid(427);
  4123. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  4124. defined(WOLFSSL_AES_256)
  4125. AssertNotNull(c);
  4126. AssertNotNull(strcmp("EVP_AES_256_CBC", c));
  4127. #else
  4128. AssertNull(c);
  4129. #endif
  4130. c = wolfSSL_EVP_get_cipherbynid(904);
  4131. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  4132. AssertNotNull(c);
  4133. AssertNotNull(strcmp("EVP_AES_128_CTR", c));
  4134. #else
  4135. AssertNull(c);
  4136. #endif
  4137. c = wolfSSL_EVP_get_cipherbynid(905);
  4138. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  4139. AssertNotNull(c);
  4140. AssertNotNull(strcmp("EVP_AES_192_CTR", c));
  4141. #else
  4142. AssertNull(c);
  4143. #endif
  4144. c = wolfSSL_EVP_get_cipherbynid(906);
  4145. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  4146. AssertNotNull(c);
  4147. AssertNotNull(strcmp("EVP_AES_256_CTR", c));
  4148. #else
  4149. AssertNull(c);
  4150. #endif
  4151. c = wolfSSL_EVP_get_cipherbynid(418);
  4152. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  4153. AssertNotNull(c);
  4154. AssertNotNull(strcmp("EVP_AES_128_ECB", c));
  4155. #else
  4156. AssertNull(c);
  4157. #endif
  4158. c = wolfSSL_EVP_get_cipherbynid(422);
  4159. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  4160. AssertNotNull(c);
  4161. AssertNotNull(strcmp("EVP_AES_192_ECB", c));
  4162. #else
  4163. AssertNull(c);
  4164. #endif
  4165. c = wolfSSL_EVP_get_cipherbynid(426);
  4166. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  4167. AssertNotNull(c);
  4168. AssertNotNull(strcmp("EVP_AES_256_ECB", c));
  4169. #else
  4170. AssertNull(c);
  4171. #endif
  4172. #endif /* !NO_AES */
  4173. #ifndef NO_DES3
  4174. AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  4175. #ifdef WOLFSSL_DES_ECB
  4176. AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  4177. #endif
  4178. AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  4179. #ifdef WOLFSSL_DES_ECB
  4180. AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  4181. #endif
  4182. #endif /* !NO_DES3 */
  4183. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  4184. AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  4185. #endif
  4186. /* test for nid is out of range */
  4187. AssertNull(wolfSSL_EVP_get_cipherbynid(1));
  4188. return 0;
  4189. }
  4190. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  4191. {
  4192. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  4193. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  4194. const EVP_CIPHER *init = EVP_aes_128_cbc();
  4195. const EVP_CIPHER *test;
  4196. byte key[AES_BLOCK_SIZE] = {0};
  4197. byte iv[AES_BLOCK_SIZE] = {0};
  4198. AssertNotNull(ctx);
  4199. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  4200. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  4201. test = EVP_CIPHER_CTX_cipher(ctx);
  4202. AssertTrue(init == test);
  4203. AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  4204. AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  4205. AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  4206. EVP_CIPHER_CTX_free(ctx);
  4207. /* test EVP_CIPHER_CTX_cleanup with NULL */
  4208. AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  4209. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  4210. return 0;
  4211. }
  4212. #endif /* OPENSSL_EXTRA */
  4213. /*----------------------------------------------------------------------------*
  4214. | IO
  4215. *----------------------------------------------------------------------------*/
  4216. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  4217. !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
  4218. !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
  4219. #define HAVE_IO_TESTS_DEPENDENCIES
  4220. #endif
  4221. /* helper functions */
  4222. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  4223. #ifdef WOLFSSL_SESSION_EXPORT
  4224. #ifdef WOLFSSL_DTLS
  4225. /* set up function for sending session information */
  4226. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  4227. {
  4228. WOLFSSL_CTX* ctx = NULL;
  4229. WOLFSSL* ssl = NULL;
  4230. AssertNotNull(inSsl);
  4231. AssertNotNull(buf);
  4232. AssertIntNE(0, sz);
  4233. /* Set ctx to DTLS 1.2 */
  4234. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  4235. AssertNotNull(ctx);
  4236. ssl = wolfSSL_new(ctx);
  4237. AssertNotNull(ssl);
  4238. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  4239. wolfSSL_free(ssl);
  4240. wolfSSL_CTX_free(ctx);
  4241. (void)userCtx;
  4242. return 0;
  4243. }
  4244. #endif
  4245. /* returns negative value on fail and positive (including 0) on success */
  4246. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  4247. {
  4248. int ret, err, loop_count, count, timeout = 10;
  4249. char msg[] = "I hear you fa shizzle!";
  4250. char input[1024];
  4251. loop_count = ((func_args*)args)->argc;
  4252. #ifdef WOLFSSL_ASYNC_CRYPT
  4253. err = 0; /* Reset error */
  4254. #endif
  4255. do {
  4256. #ifdef WOLFSSL_ASYNC_CRYPT
  4257. if (err == WC_PENDING_E) {
  4258. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4259. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4260. }
  4261. #endif
  4262. ret = wolfSSL_accept(ssl);
  4263. err = wolfSSL_get_error(ssl, 0);
  4264. if (err == WOLFSSL_ERROR_WANT_READ ||
  4265. err == WOLFSSL_ERROR_WANT_WRITE) {
  4266. int select_ret;
  4267. err = WC_PENDING_E;
  4268. select_ret = tcp_select(*sockfd, timeout);
  4269. if (select_ret == TEST_TIMEOUT) {
  4270. return WOLFSSL_FATAL_ERROR;
  4271. }
  4272. }
  4273. } while (err == WC_PENDING_E);
  4274. if (ret != WOLFSSL_SUCCESS) {
  4275. char buff[WOLFSSL_MAX_ERROR_SZ];
  4276. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4277. return ret;
  4278. }
  4279. for (count = 0; count < loop_count; count++) {
  4280. int select_ret;
  4281. select_ret = tcp_select(*sockfd, timeout);
  4282. if (select_ret == TEST_TIMEOUT) {
  4283. ret = WOLFSSL_FATAL_ERROR;
  4284. break;
  4285. }
  4286. do {
  4287. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  4288. if (ret > 0) {
  4289. input[ret] = '\0';
  4290. printf("Client message: %s\n", input);
  4291. }
  4292. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4293. do {
  4294. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  4295. return WOLFSSL_FATAL_ERROR;
  4296. }
  4297. err = wolfSSL_get_error(ssl, ret);
  4298. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  4299. }
  4300. return ret;
  4301. }
  4302. #endif /* WOLFSSL_SESSION_EXPORT */
  4303. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  4304. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  4305. defined(HAVE_AES_CBC)
  4306. typedef struct openssl_key_ctx {
  4307. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  4308. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  4309. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  4310. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  4311. } openssl_key_ctx;
  4312. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  4313. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  4314. static WC_INLINE int OpenSSLTicketInit(void)
  4315. {
  4316. int ret = wc_InitRng(&myOpenSSLKey_rng);
  4317. if (ret != 0) return ret;
  4318. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  4319. sizeof(myOpenSSLKey_ctx.name));
  4320. if (ret != 0) return ret;
  4321. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  4322. sizeof(myOpenSSLKey_ctx.key));
  4323. if (ret != 0) return ret;
  4324. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  4325. sizeof(myOpenSSLKey_ctx.hmacKey));
  4326. if (ret != 0) return ret;
  4327. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  4328. sizeof(myOpenSSLKey_ctx.iv));
  4329. if (ret != 0) return ret;
  4330. return 0;
  4331. }
  4332. static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  4333. byte name[WOLFSSL_TICKET_NAME_SZ],
  4334. byte iv[WOLFSSL_TICKET_IV_SZ],
  4335. WOLFSSL_EVP_CIPHER_CTX *ectx,
  4336. WOLFSSL_HMAC_CTX *hctx, int enc) {
  4337. (void)ssl;
  4338. if (enc) {
  4339. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  4340. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  4341. }
  4342. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  4343. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  4344. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  4345. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  4346. return 0;
  4347. }
  4348. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  4349. if (enc)
  4350. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4351. else
  4352. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  4353. return 1;
  4354. }
  4355. static WC_INLINE void OpenSSLTicketCleanup(void)
  4356. {
  4357. wc_FreeRng(&myOpenSSLKey_rng);
  4358. }
  4359. #endif
  4360. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4361. #ifdef WC_SHA512_DIGEST_SIZE
  4362. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  4363. #else
  4364. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  4365. #endif
  4366. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  4367. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  4368. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  4369. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  4370. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4371. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  4372. {
  4373. SOCKET_T sockfd = 0;
  4374. SOCKET_T clientfd = 0;
  4375. word16 port;
  4376. callback_functions* cbf;
  4377. WOLFSSL_CTX* ctx = 0;
  4378. WOLFSSL* ssl = 0;
  4379. func_args* opts = (func_args*)args;
  4380. char msg[] = "I hear you fa shizzle!";
  4381. char input[1024];
  4382. int idx;
  4383. int ret, err = 0;
  4384. int sharedCtx = 0;
  4385. int doUdp = 0;
  4386. SOCKADDR_IN_T cliAddr;
  4387. socklen_t cliLen;
  4388. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4389. size_t msg_len = 0;
  4390. #endif
  4391. #ifdef WOLFSSL_TIRTOS
  4392. fdOpenSession(Task_self());
  4393. #endif
  4394. opts->return_code = TEST_FAIL;
  4395. cbf = opts->callbacks;
  4396. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4397. if (cbf != NULL && cbf->ctx) {
  4398. ctx = cbf->ctx;
  4399. sharedCtx = 1;
  4400. }
  4401. else
  4402. #endif
  4403. {
  4404. WOLFSSL_METHOD* method = NULL;
  4405. if (cbf != NULL && cbf->method != NULL) {
  4406. method = cbf->method();
  4407. }
  4408. else {
  4409. method = wolfSSLv23_server_method();
  4410. }
  4411. ctx = wolfSSL_CTX_new(method);
  4412. }
  4413. if (ctx == NULL) {
  4414. goto done;
  4415. }
  4416. if (cbf == NULL || !cbf->ticNoInit) {
  4417. #if defined(HAVE_SESSION_TICKET) && \
  4418. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4419. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4420. OpenSSLTicketInit();
  4421. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  4422. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4423. TicketInit();
  4424. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  4425. #endif
  4426. #endif
  4427. }
  4428. #if defined(USE_WINDOWS_API)
  4429. port = opts->signal->port;
  4430. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4431. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4432. /* Let tcp_listen assign port */
  4433. port = 0;
  4434. #else
  4435. /* Use default port */
  4436. port = wolfSSLPort;
  4437. #endif
  4438. if (cbf != NULL)
  4439. doUdp = cbf->doUdp;
  4440. /* do it here to detect failure */
  4441. tcp_accept(
  4442. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  4443. if (doUdp) {
  4444. cliLen = sizeof(cliAddr);
  4445. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  4446. (struct sockaddr*)&cliAddr, &cliLen);
  4447. AssertIntGT(idx, 0);
  4448. }
  4449. else {
  4450. CloseSocket(sockfd);
  4451. }
  4452. wolfSSL_CTX_set_verify(ctx,
  4453. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4454. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4455. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4456. #endif
  4457. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4458. != WOLFSSL_SUCCESS) {
  4459. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4460. goto done;
  4461. }
  4462. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4463. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4464. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4465. #else
  4466. if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4467. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4468. #endif
  4469. /*err_sys("can't load server cert chain file, "
  4470. "Please run from wolfSSL home dir");*/
  4471. goto done;
  4472. }
  4473. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4474. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4475. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4476. #else
  4477. if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4478. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4479. #endif
  4480. /*err_sys("can't load server key file, "
  4481. "Please run from wolfSSL home dir");*/
  4482. goto done;
  4483. }
  4484. /* call ctx setup callback */
  4485. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4486. cbf->ctx_ready(ctx);
  4487. }
  4488. ssl = wolfSSL_new(ctx);
  4489. if (ssl == NULL) {
  4490. goto done;
  4491. }
  4492. if (doUdp) {
  4493. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  4494. if (err != WOLFSSL_SUCCESS)
  4495. goto done;
  4496. }
  4497. #ifdef WOLFSSL_SESSION_EXPORT
  4498. /* only add in more complex nonblocking case with session export tests */
  4499. if (args && opts->argc > 0) {
  4500. /* set as nonblock and time out for waiting on read/write */
  4501. tcp_set_nonblocking(&clientfd);
  4502. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  4503. }
  4504. #endif
  4505. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4506. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4507. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4508. #else
  4509. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  4510. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4511. #endif
  4512. /*err_sys("can't load server cert chain file, "
  4513. "Please run from wolfSSL home dir");*/
  4514. goto done;
  4515. }
  4516. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4517. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4518. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4519. #else
  4520. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4521. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4522. #endif
  4523. /*err_sys("can't load server key file, "
  4524. "Please run from wolfSSL home dir");*/
  4525. goto done;
  4526. }
  4527. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4528. /*err_sys("SSL_set_fd failed");*/
  4529. goto done;
  4530. }
  4531. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4532. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4533. #elif !defined(NO_DH)
  4534. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4535. #endif
  4536. /* call ssl setup callback */
  4537. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4538. cbf->ssl_ready(ssl);
  4539. }
  4540. #ifdef WOLFSSL_SESSION_EXPORT
  4541. /* only add in more complex nonblocking case with session export tests */
  4542. if (opts->argc > 0) {
  4543. ret = nonblocking_accept_read(args, ssl, &clientfd);
  4544. if (ret >= 0) {
  4545. opts->return_code = TEST_SUCCESS;
  4546. }
  4547. #ifdef WOLFSSL_TIRTOS
  4548. Task_yield();
  4549. #endif
  4550. goto done;
  4551. }
  4552. #endif
  4553. #ifdef WOLFSSL_ASYNC_CRYPT
  4554. err = 0; /* Reset error */
  4555. #endif
  4556. do {
  4557. #ifdef WOLFSSL_ASYNC_CRYPT
  4558. if (err == WC_PENDING_E) {
  4559. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4560. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4561. }
  4562. #endif
  4563. ret = wolfSSL_accept(ssl);
  4564. err = wolfSSL_get_error(ssl, 0);
  4565. } while (err == WC_PENDING_E);
  4566. if (ret != WOLFSSL_SUCCESS) {
  4567. char buff[WOLFSSL_MAX_ERROR_SZ];
  4568. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4569. /*err_sys("SSL_accept failed");*/
  4570. goto done;
  4571. }
  4572. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  4573. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  4574. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  4575. AssertIntGE(msg_len, 0);
  4576. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  4577. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  4578. AssertIntGE(msg_len, 0);
  4579. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  4580. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4581. if (idx > 0) {
  4582. input[idx] = '\0';
  4583. printf("Client message: %s\n", input);
  4584. }
  4585. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4586. /*err_sys("SSL_write failed");*/
  4587. #ifdef WOLFSSL_TIRTOS
  4588. return;
  4589. #else
  4590. return 0;
  4591. #endif
  4592. }
  4593. if (cbf != NULL && cbf->on_result != NULL)
  4594. cbf->on_result(ssl);
  4595. #ifdef WOLFSSL_TIRTOS
  4596. Task_yield();
  4597. #endif
  4598. opts->return_code = TEST_SUCCESS;
  4599. done:
  4600. if (cbf != NULL)
  4601. cbf->last_err = err;
  4602. wolfSSL_shutdown(ssl);
  4603. wolfSSL_free(ssl);
  4604. if (!sharedCtx)
  4605. wolfSSL_CTX_free(ctx);
  4606. CloseSocket(clientfd);
  4607. #ifdef WOLFSSL_TIRTOS
  4608. fdCloseSession(Task_self());
  4609. #endif
  4610. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4611. && defined(HAVE_THREAD_LS)
  4612. wc_ecc_fp_free(); /* free per thread cache */
  4613. #endif
  4614. if (cbf == NULL || !cbf->ticNoInit) {
  4615. #if defined(HAVE_SESSION_TICKET) && \
  4616. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  4617. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  4618. OpenSSLTicketCleanup();
  4619. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  4620. TicketCleanup();
  4621. #endif
  4622. #endif
  4623. }
  4624. #ifndef WOLFSSL_TIRTOS
  4625. return 0;
  4626. #endif
  4627. }
  4628. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  4629. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  4630. {
  4631. SOCKET_T sockfd = 0;
  4632. SOCKET_T clientfd = 0;
  4633. word16 port;
  4634. callback_functions* cbf;
  4635. WOLFSSL_CTX* ctx = 0;
  4636. WOLFSSL* ssl = 0;
  4637. char msg[] = "I hear you fa shizzle!";
  4638. char input[1024];
  4639. int idx;
  4640. int ret, err = 0;
  4641. int sharedCtx = 0;
  4642. int loop_count = ((func_args*)args)->argc;
  4643. int count = 0;
  4644. #ifdef WOLFSSL_TIRTOS
  4645. fdOpenSession(Task_self());
  4646. #endif
  4647. ((func_args*)args)->return_code = TEST_FAIL;
  4648. cbf = ((func_args*)args)->callbacks;
  4649. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4650. if (cbf != NULL && cbf->ctx) {
  4651. ctx = cbf->ctx;
  4652. sharedCtx = 1;
  4653. }
  4654. else
  4655. #endif
  4656. {
  4657. WOLFSSL_METHOD* method = NULL;
  4658. if (cbf != NULL && cbf->method != NULL) {
  4659. method = cbf->method();
  4660. }
  4661. else {
  4662. method = wolfSSLv23_server_method();
  4663. }
  4664. ctx = wolfSSL_CTX_new(method);
  4665. }
  4666. #if defined(USE_WINDOWS_API)
  4667. port = ((func_args*)args)->signal->port;
  4668. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  4669. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  4670. /* Let tcp_listen assign port */
  4671. port = 0;
  4672. #else
  4673. /* Use default port */
  4674. port = wolfSSLPort;
  4675. #endif
  4676. wolfSSL_CTX_set_verify(ctx,
  4677. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  4678. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4679. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4680. #endif
  4681. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  4682. != WOLFSSL_SUCCESS) {
  4683. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4684. goto done;
  4685. }
  4686. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4687. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4688. /*err_sys("can't load server cert chain file, "
  4689. "Please run from wolfSSL home dir");*/
  4690. goto done;
  4691. }
  4692. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4693. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4694. /*err_sys("can't load server key file, "
  4695. "Please run from wolfSSL home dir");*/
  4696. goto done;
  4697. }
  4698. /* call ctx setup callback */
  4699. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4700. cbf->ctx_ready(ctx);
  4701. }
  4702. while(count != loop_count) {
  4703. ssl = wolfSSL_new(ctx);
  4704. if (ssl == NULL) {
  4705. goto done;
  4706. }
  4707. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  4708. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4709. /*err_sys("can't load server cert chain file, "
  4710. "Please run from wolfSSL home dir");*/
  4711. goto done;
  4712. }
  4713. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  4714. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4715. /*err_sys("can't load server key file, "
  4716. "Please run from wolfSSL home dir");*/
  4717. goto done;
  4718. }
  4719. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  4720. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  4721. #elif !defined(NO_DH)
  4722. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  4723. #endif
  4724. /* call ssl setup callback */
  4725. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4726. cbf->ssl_ready(ssl);
  4727. }
  4728. /* do it here to detect failure */
  4729. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  4730. CloseSocket(sockfd);
  4731. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  4732. /*err_sys("SSL_set_fd failed");*/
  4733. goto done;
  4734. }
  4735. #ifdef WOLFSSL_ASYNC_CRYPT
  4736. err = 0; /* Reset error */
  4737. #endif
  4738. do {
  4739. #ifdef WOLFSSL_ASYNC_CRYPT
  4740. if (err == WC_PENDING_E) {
  4741. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4742. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4743. }
  4744. #endif
  4745. ret = wolfSSL_accept(ssl);
  4746. err = wolfSSL_get_error(ssl, 0);
  4747. } while (err == WC_PENDING_E);
  4748. if (ret != WOLFSSL_SUCCESS) {
  4749. char buff[WOLFSSL_MAX_ERROR_SZ];
  4750. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4751. /*err_sys("SSL_accept failed");*/
  4752. goto done;
  4753. }
  4754. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  4755. if (idx > 0) {
  4756. input[idx] = '\0';
  4757. printf("Client message: %s\n", input);
  4758. }
  4759. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  4760. /*err_sys("SSL_write failed");*/
  4761. #ifdef WOLFSSL_TIRTOS
  4762. return;
  4763. #else
  4764. return 0;
  4765. #endif
  4766. }
  4767. /* free ssl for this connection */
  4768. wolfSSL_shutdown(ssl);
  4769. wolfSSL_free(ssl); ssl = NULL;
  4770. CloseSocket(clientfd);
  4771. count++;
  4772. }
  4773. #ifdef WOLFSSL_TIRTOS
  4774. Task_yield();
  4775. #endif
  4776. ((func_args*)args)->return_code = TEST_SUCCESS;
  4777. done:
  4778. if(ssl != NULL) {
  4779. wolfSSL_shutdown(ssl);
  4780. wolfSSL_free(ssl);
  4781. }
  4782. if (!sharedCtx)
  4783. wolfSSL_CTX_free(ctx);
  4784. CloseSocket(clientfd);
  4785. #ifdef WOLFSSL_TIRTOS
  4786. fdCloseSession(Task_self());
  4787. #endif
  4788. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4789. && defined(HAVE_THREAD_LS)
  4790. wc_ecc_fp_free(); /* free per thread cache */
  4791. #endif
  4792. #ifndef WOLFSSL_TIRTOS
  4793. return 0;
  4794. #endif
  4795. }
  4796. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  4797. typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
  4798. static int test_client_nofail(void* args, cbType cb)
  4799. {
  4800. #if !defined(NO_WOLFSSL_CLIENT)
  4801. SOCKET_T sockfd = 0;
  4802. callback_functions* cbf;
  4803. WOLFSSL_CTX* ctx = 0;
  4804. WOLFSSL* ssl = 0;
  4805. WOLFSSL_CIPHER* cipher;
  4806. char msg[64] = "hello wolfssl!";
  4807. char reply[1024];
  4808. int input;
  4809. int msgSz = (int)XSTRLEN(msg);
  4810. int ret, err = 0;
  4811. int cipherSuite;
  4812. int sharedCtx = 0;
  4813. int doUdp = 0;
  4814. const char* cipherName1, *cipherName2;
  4815. #ifdef WOLFSSL_TIRTOS
  4816. fdOpenSession(Task_self());
  4817. #endif
  4818. ((func_args*)args)->return_code = TEST_FAIL;
  4819. cbf = ((func_args*)args)->callbacks;
  4820. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4821. if (cbf != NULL && cbf->ctx) {
  4822. ctx = cbf->ctx;
  4823. sharedCtx = cbf->isSharedCtx;
  4824. }
  4825. else
  4826. #endif
  4827. {
  4828. WOLFSSL_METHOD* method = NULL;
  4829. if (cbf != NULL && cbf->method != NULL) {
  4830. method = cbf->method();
  4831. }
  4832. else {
  4833. method = wolfSSLv23_client_method();
  4834. }
  4835. ctx = wolfSSL_CTX_new(method);
  4836. }
  4837. if (cbf != NULL)
  4838. doUdp = cbf->doUdp;
  4839. #ifdef WOLFSSL_ENCRYPTED_KEYS
  4840. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  4841. #endif
  4842. /* Do connect here so server detects failures */
  4843. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  4844. doUdp, 0, NULL);
  4845. /* Connect the socket so that we don't have to set the peer later on */
  4846. if (doUdp)
  4847. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  4848. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  4849. {
  4850. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  4851. goto done;
  4852. }
  4853. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4854. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4855. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4856. #else
  4857. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  4858. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4859. #endif
  4860. /*err_sys("can't load client cert file, "
  4861. "Please run from wolfSSL home dir");*/
  4862. goto done;
  4863. }
  4864. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4865. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4866. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4867. #else
  4868. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  4869. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4870. #endif
  4871. /*err_sys("can't load client key file, "
  4872. "Please run from wolfSSL home dir");*/
  4873. goto done;
  4874. }
  4875. /* call ctx setup callback */
  4876. if (cbf != NULL && cbf->ctx_ready != NULL) {
  4877. cbf->ctx_ready(ctx);
  4878. }
  4879. ssl = wolfSSL_new(ctx);
  4880. if (ssl == NULL) {
  4881. goto done;
  4882. }
  4883. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4884. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  4885. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4886. #else
  4887. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  4888. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4889. #endif
  4890. /*err_sys("can't load client cert file, "
  4891. "Please run from wolfSSL home dir");*/
  4892. goto done;
  4893. }
  4894. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  4895. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4896. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4897. #else
  4898. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  4899. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  4900. #endif
  4901. /*err_sys("can't load client key file, "
  4902. "Please run from wolfSSL home dir");*/
  4903. goto done;
  4904. }
  4905. if (!doUdp) {
  4906. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4907. /*err_sys("SSL_set_fd failed");*/
  4908. goto done;
  4909. }
  4910. }
  4911. else {
  4912. #ifdef WOLFSSL_DTLS
  4913. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  4914. /*err_sys("SSL_set_fd failed");*/
  4915. goto done;
  4916. }
  4917. #else
  4918. goto done;
  4919. #endif
  4920. }
  4921. /* call ssl setup callback */
  4922. if (cbf != NULL && cbf->ssl_ready != NULL) {
  4923. cbf->ssl_ready(ssl);
  4924. }
  4925. #ifdef WOLFSSL_ASYNC_CRYPT
  4926. err = 0; /* Reset error */
  4927. #endif
  4928. do {
  4929. #ifdef WOLFSSL_ASYNC_CRYPT
  4930. if (err == WC_PENDING_E) {
  4931. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  4932. if (ret < 0) { break; } else if (ret == 0) { continue; }
  4933. }
  4934. #endif
  4935. ret = wolfSSL_connect(ssl);
  4936. err = wolfSSL_get_error(ssl, 0);
  4937. } while (err == WC_PENDING_E);
  4938. if (ret != WOLFSSL_SUCCESS) {
  4939. char buff[WOLFSSL_MAX_ERROR_SZ];
  4940. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  4941. /*err_sys("SSL_connect failed");*/
  4942. goto done;
  4943. }
  4944. /* test the various get cipher methods */
  4945. /* Internal cipher suite names */
  4946. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  4947. cipherName1 = wolfSSL_get_cipher_name(ssl);
  4948. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  4949. (cipherSuite >> 8), cipherSuite & 0xFF);
  4950. AssertStrEQ(cipherName1, cipherName2);
  4951. /* IANA Cipher Suites Names */
  4952. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  4953. then it's the internal cipher suite name */
  4954. cipher = wolfSSL_get_current_cipher(ssl);
  4955. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  4956. cipherName2 = wolfSSL_get_cipher(ssl);
  4957. AssertStrEQ(cipherName1, cipherName2);
  4958. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  4959. !defined(WOLFSSL_QT)
  4960. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  4961. (cipherSuite >> 8), cipherSuite & 0xFF);
  4962. AssertStrEQ(cipherName1, cipherName2);
  4963. #endif
  4964. if (cb != NULL)
  4965. (cb)(ctx, ssl);
  4966. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  4967. /*err_sys("SSL_write failed");*/
  4968. goto done;
  4969. }
  4970. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  4971. if (input > 0) {
  4972. reply[input] = '\0';
  4973. printf("Server response: %s\n", reply);
  4974. }
  4975. if (cbf != NULL && cbf->on_result != NULL)
  4976. cbf->on_result(ssl);
  4977. ((func_args*)args)->return_code = TEST_SUCCESS;
  4978. done:
  4979. if (cbf != NULL)
  4980. cbf->last_err = err;
  4981. wolfSSL_free(ssl);
  4982. if (!sharedCtx)
  4983. wolfSSL_CTX_free(ctx);
  4984. CloseSocket(sockfd);
  4985. #ifdef WOLFSSL_TIRTOS
  4986. fdCloseSession(Task_self());
  4987. #endif
  4988. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  4989. && defined(HAVE_THREAD_LS)
  4990. wc_ecc_fp_free(); /* free per thread cache */
  4991. #endif
  4992. #else
  4993. (void)args;
  4994. (void)cb;
  4995. #endif /* !NO_WOLFSSL_CLIENT */
  4996. return 0;
  4997. }
  4998. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  4999. callback_functions* server_cb)
  5000. {
  5001. func_args client_args;
  5002. func_args server_args;
  5003. tcp_ready ready;
  5004. THREAD_TYPE serverThread;
  5005. XMEMSET(&client_args, 0, sizeof(func_args));
  5006. XMEMSET(&server_args, 0, sizeof(func_args));
  5007. #ifdef WOLFSSL_TIRTOS
  5008. fdOpenSession(Task_self());
  5009. #endif
  5010. StartTCP();
  5011. InitTcpReady(&ready);
  5012. #if defined(USE_WINDOWS_API)
  5013. /* use RNG to get random port if using windows */
  5014. ready.port = GetRandomPort();
  5015. #endif
  5016. server_args.signal = &ready;
  5017. server_args.callbacks = server_cb;
  5018. client_args.signal = &ready;
  5019. client_args.callbacks = client_cb;
  5020. start_thread(test_server_nofail, &server_args, &serverThread);
  5021. wait_tcp_ready(&server_args);
  5022. test_client_nofail(&client_args, NULL);
  5023. join_thread(serverThread);
  5024. client_cb->return_code = client_args.return_code;
  5025. server_cb->return_code = server_args.return_code;
  5026. FreeTcpReady(&ready);
  5027. #ifdef WOLFSSL_TIRTOS
  5028. fdOpenSession(Task_self());
  5029. #endif
  5030. }
  5031. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  5032. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
  5033. static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
  5034. {
  5035. SOCKET_T sockfd = 0;
  5036. callback_functions* cbf;
  5037. WOLFSSL_CTX* ctx = 0;
  5038. WOLFSSL* ssl = 0;
  5039. WOLFSSL_SESSION* session = NULL;
  5040. char msg[64] = "hello wolfssl!";
  5041. char reply[1024];
  5042. int input;
  5043. int msgSz = (int)XSTRLEN(msg);
  5044. int ret, err = 0;
  5045. int sharedCtx = 0;
  5046. #ifdef WOLFSSL_TIRTOS
  5047. fdOpenSession(Task_self());
  5048. #endif
  5049. ((func_args*)args)->return_code = TEST_FAIL;
  5050. cbf = ((func_args*)args)->callbacks;
  5051. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5052. if (cbf != NULL && cbf->ctx) {
  5053. ctx = cbf->ctx;
  5054. sharedCtx = 1;
  5055. }
  5056. else
  5057. #endif
  5058. {
  5059. WOLFSSL_METHOD* method = NULL;
  5060. if (cbf != NULL && cbf->method != NULL) {
  5061. method = cbf->method();
  5062. }
  5063. else {
  5064. method = wolfSSLv23_client_method();
  5065. }
  5066. ctx = wolfSSL_CTX_new(method);
  5067. }
  5068. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5069. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5070. #endif
  5071. /* Do connect here so server detects failures */
  5072. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5073. 0, 0, NULL);
  5074. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5075. {
  5076. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5077. goto done;
  5078. }
  5079. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5080. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5081. /*err_sys("can't load client cert file, "
  5082. "Please run from wolfSSL home dir");*/
  5083. goto done;
  5084. }
  5085. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5086. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5087. /*err_sys("can't load client key file, "
  5088. "Please run from wolfSSL home dir");*/
  5089. goto done;
  5090. }
  5091. /* call ctx setup callback */
  5092. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5093. cbf->ctx_ready(ctx);
  5094. }
  5095. ssl = wolfSSL_new(ctx);
  5096. if (ssl == NULL) {
  5097. goto done;
  5098. }
  5099. /* keep handshakre resources for re-using WOLFSSL obj */
  5100. wolfSSL_KeepArrays(ssl);
  5101. if(wolfSSL_KeepHandshakeResources(ssl)) {
  5102. /* err_sys("SSL_KeepHandshakeResources failed"); */
  5103. goto done;
  5104. }
  5105. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  5106. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5107. /*err_sys("can't load client cert file, "
  5108. "Please run from wolfSSL home dir");*/
  5109. goto done;
  5110. }
  5111. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  5112. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5113. /*err_sys("can't load client key file, "
  5114. "Please run from wolfSSL home dir");*/
  5115. goto done;
  5116. }
  5117. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5118. /*err_sys("SSL_set_fd failed");*/
  5119. goto done;
  5120. }
  5121. /* call ssl setup callback */
  5122. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5123. cbf->ssl_ready(ssl);
  5124. }
  5125. #ifdef WOLFSSL_ASYNC_CRYPT
  5126. err = 0; /* Reset error */
  5127. #endif
  5128. do {
  5129. #ifdef WOLFSSL_ASYNC_CRYPT
  5130. if (err == WC_PENDING_E) {
  5131. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5132. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5133. }
  5134. #endif
  5135. ret = wolfSSL_connect(ssl);
  5136. err = wolfSSL_get_error(ssl, 0);
  5137. } while (err == WC_PENDING_E);
  5138. if (ret != WOLFSSL_SUCCESS) {
  5139. char buff[WOLFSSL_MAX_ERROR_SZ];
  5140. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5141. /*err_sys("SSL_connect failed");*/
  5142. goto done;
  5143. }
  5144. /* Build first session */
  5145. if (cb != NULL)
  5146. ((cbType)cb)(ctx, ssl);
  5147. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5148. /*err_sys("SSL_write failed");*/
  5149. goto done;
  5150. }
  5151. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5152. if (input > 0) {
  5153. reply[input] = '\0';
  5154. printf("Server response: %s\n", reply);
  5155. }
  5156. /* Session Resumption by re-using WOLFSSL object */
  5157. wolfSSL_set_quiet_shutdown(ssl, 1);
  5158. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  5159. /* err_sys ("SSL shutdown failed"); */
  5160. goto done;
  5161. }
  5162. session = wolfSSL_get1_session(ssl);
  5163. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  5164. /* err_sys ("SSL_clear failed"); */
  5165. goto done;
  5166. }
  5167. wolfSSL_set_session(ssl, session);
  5168. wolfSSL_SESSION_free(session);
  5169. session = NULL;
  5170. /* close socket once */
  5171. CloseSocket(sockfd);
  5172. sockfd = 0;
  5173. /* wait until server ready */
  5174. wait_tcp_ready((func_args*)server_args);
  5175. printf("session resumption\n");
  5176. /* Do re-connect */
  5177. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5178. 0, 0, NULL);
  5179. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5180. /*err_sys("SSL_set_fd failed");*/
  5181. goto done;
  5182. }
  5183. #ifdef WOLFSSL_ASYNC_CRYPT
  5184. err = 0; /* Reset error */
  5185. #endif
  5186. do {
  5187. #ifdef WOLFSSL_ASYNC_CRYPT
  5188. if (err == WC_PENDING_E) {
  5189. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5190. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5191. }
  5192. #endif
  5193. ret = wolfSSL_connect(ssl);
  5194. err = wolfSSL_get_error(ssl, 0);
  5195. } while (err == WC_PENDING_E);
  5196. if (ret != WOLFSSL_SUCCESS) {
  5197. char buff[WOLFSSL_MAX_ERROR_SZ];
  5198. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5199. /*err_sys("SSL_connect failed");*/
  5200. goto done;
  5201. }
  5202. /* Build first session */
  5203. if (cb != NULL)
  5204. ((cbType)cb)(ctx, ssl);
  5205. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5206. /*err_sys("SSL_write failed");*/
  5207. goto done;
  5208. }
  5209. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5210. if (input > 0) {
  5211. reply[input] = '\0';
  5212. printf("Server response: %s\n", reply);
  5213. }
  5214. ((func_args*)args)->return_code = TEST_SUCCESS;
  5215. done:
  5216. wolfSSL_free(ssl);
  5217. if (!sharedCtx)
  5218. wolfSSL_CTX_free(ctx);
  5219. CloseSocket(sockfd);
  5220. #ifdef WOLFSSL_TIRTOS
  5221. fdCloseSession(Task_self());
  5222. #endif
  5223. return;
  5224. }
  5225. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  5226. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  5227. static int test_client_verifyDepth(void* args)
  5228. {
  5229. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5230. SOCKET_T sockfd = 0;
  5231. callback_functions* cbf;
  5232. WOLFSSL_CTX* ctx = 0;
  5233. WOLFSSL* ssl = 0;
  5234. char msg[64] = "hello wolfssl!";
  5235. char reply[1024];
  5236. int input;
  5237. int msgSz = (int)XSTRLEN(msg);
  5238. int ret, err = 0;
  5239. int verify_depth = ((func_args*)args)->argc;
  5240. ((func_args*)args)->return_code = TEST_FAIL;
  5241. cbf = ((func_args*)args)->callbacks;
  5242. {
  5243. WOLFSSL_METHOD* method = NULL;
  5244. if (cbf != NULL && cbf->method != NULL) {
  5245. method = cbf->method();
  5246. }
  5247. else {
  5248. method = wolfSSLv23_client_method();
  5249. }
  5250. ctx = wolfSSL_CTX_new(method);
  5251. }
  5252. /* Do connect here so server detects failures */
  5253. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5254. 0, 0, NULL);
  5255. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
  5256. != WOLFSSL_SUCCESS)
  5257. {
  5258. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5259. goto done;
  5260. }
  5261. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5262. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5263. /*err_sys("can't load client cert file, "
  5264. "Please run from wolfSSL home dir");*/
  5265. goto done;
  5266. }
  5267. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5268. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5269. /*err_sys("can't load client key file, "
  5270. "Please run from wolfSSL home dir");*/
  5271. goto done;
  5272. }
  5273. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  5274. /* set verify depth */
  5275. if (verify_depth == 0) {
  5276. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  5277. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5278. } else if (verify_depth == -1) {
  5279. myVerifyAction = VERIFY_USE_PREVERFIY;
  5280. SSL_CTX_set_verify_depth(ctx, 0);
  5281. } else if (verify_depth > 0) {
  5282. myVerifyAction = VERIFY_USE_PREVERFIY;
  5283. SSL_CTX_set_verify_depth(ctx, verify_depth);
  5284. }
  5285. ssl = wolfSSL_new(ctx);
  5286. if (ssl == NULL) {
  5287. goto done;
  5288. }
  5289. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5290. /*err_sys("SSL_set_fd failed");*/
  5291. goto done;
  5292. }
  5293. #ifdef WOLFSSL_ASYNC_CRYPT
  5294. err = 0; /* Reset error */
  5295. #endif
  5296. do {
  5297. #ifdef WOLFSSL_ASYNC_CRYPT
  5298. if (err == WC_PENDING_E) {
  5299. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5300. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5301. }
  5302. #endif
  5303. ret = wolfSSL_connect(ssl);
  5304. err = wolfSSL_get_error(ssl, 0);
  5305. } while (err == WC_PENDING_E);
  5306. if (ret != WOLFSSL_SUCCESS) {
  5307. char buff[WOLFSSL_MAX_ERROR_SZ];
  5308. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5309. goto done;
  5310. }
  5311. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  5312. goto done;
  5313. }
  5314. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5315. if (input > 0) {
  5316. reply[input] = '\0';
  5317. printf("Server response: %s\n", reply);
  5318. }
  5319. ((func_args*)args)->return_code = TEST_SUCCESS;
  5320. done:
  5321. wolfSSL_free(ssl);
  5322. wolfSSL_CTX_free(ctx);
  5323. CloseSocket(sockfd);
  5324. #else
  5325. (void)args;
  5326. #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
  5327. return 0;
  5328. }
  5329. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  5330. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  5331. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  5332. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  5333. #define HAVE_ALPN_PROTOS_SUPPORT
  5334. #endif
  5335. /* Generic TLS client / server with callbacks for API unit tests
  5336. * Used by SNI / ALPN / crypto callback helper functions */
  5337. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  5338. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  5339. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  5340. #define ENABLE_TLS_CALLBACK_TEST
  5341. #endif
  5342. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  5343. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  5344. /* TLS server for API unit testing - generic */
  5345. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  5346. {
  5347. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5348. WOLFSSL_CTX* ctx = NULL;
  5349. WOLFSSL* ssl = NULL;
  5350. SOCKET_T sfd = 0;
  5351. SOCKET_T cfd = 0;
  5352. word16 port;
  5353. char msg[] = "I hear you fa shizzle!";
  5354. int len = (int) XSTRLEN(msg);
  5355. char input[1024];
  5356. int idx;
  5357. int ret, err = 0;
  5358. ((func_args*)args)->return_code = TEST_FAIL;
  5359. #ifdef WOLFSSL_STATIC_MEMORY
  5360. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5361. callbacks->memSz > 0) {
  5362. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5363. callbacks->mem, callbacks->memSz, 0, 1);
  5364. if (ret != WOLFSSL_SUCCESS) {
  5365. printf("CTX static new failed %d\n", ret);
  5366. return 0;
  5367. }
  5368. }
  5369. #else
  5370. if (ctx == NULL) {
  5371. ctx = wolfSSL_CTX_new(callbacks->method());
  5372. }
  5373. if (ctx == NULL) {
  5374. printf("CTX new failed\n");
  5375. return 0;
  5376. }
  5377. #endif
  5378. /* set defaults */
  5379. if (callbacks->caPemFile == NULL)
  5380. callbacks->caPemFile = cliCertFile;
  5381. if (callbacks->certPemFile == NULL)
  5382. callbacks->certPemFile = svrCertFile;
  5383. if (callbacks->keyPemFile == NULL)
  5384. callbacks->keyPemFile = svrKeyFile;
  5385. #ifdef WOLFSSL_TIRTOS
  5386. fdOpenSession(Task_self());
  5387. #endif
  5388. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5389. #if defined(USE_WINDOWS_API)
  5390. port = ((func_args*)args)->signal->port;
  5391. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  5392. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  5393. /* Let tcp_listen assign port */
  5394. port = 0;
  5395. #else
  5396. /* Use default port */
  5397. port = wolfSSLPort;
  5398. #endif
  5399. wolfSSL_CTX_set_verify(ctx,
  5400. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5401. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5402. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5403. #endif
  5404. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  5405. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
  5406. #endif
  5407. AssertIntEQ(WOLFSSL_SUCCESS,
  5408. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5409. AssertIntEQ(WOLFSSL_SUCCESS,
  5410. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5411. WOLFSSL_FILETYPE_PEM));
  5412. AssertIntEQ(WOLFSSL_SUCCESS,
  5413. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5414. WOLFSSL_FILETYPE_PEM));
  5415. if (callbacks->ctx_ready)
  5416. callbacks->ctx_ready(ctx);
  5417. ssl = wolfSSL_new(ctx);
  5418. if (ssl == NULL) {
  5419. printf("SSL new failed\n");
  5420. wolfSSL_CTX_free(ctx);
  5421. return 0;
  5422. }
  5423. if (wolfSSL_dtls(ssl)) {
  5424. SOCKADDR_IN_T cliAddr;
  5425. socklen_t cliLen;
  5426. cliLen = sizeof(cliAddr);
  5427. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  5428. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  5429. (struct sockaddr*)&cliAddr, &cliLen);
  5430. AssertIntGT(idx, 0);
  5431. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  5432. }
  5433. else {
  5434. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  5435. CloseSocket(sfd);
  5436. }
  5437. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  5438. if (callbacks->loadToSSL) {
  5439. wolfSSL_SetDevId(ssl, callbacks->devId);
  5440. AssertIntEQ(WOLFSSL_SUCCESS,
  5441. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5442. WOLFSSL_FILETYPE_PEM));
  5443. AssertIntEQ(WOLFSSL_SUCCESS,
  5444. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5445. WOLFSSL_FILETYPE_PEM));
  5446. }
  5447. #ifdef NO_PSK
  5448. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5449. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5450. #elif !defined(NO_DH)
  5451. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  5452. #endif
  5453. #endif
  5454. if (callbacks->ssl_ready)
  5455. callbacks->ssl_ready(ssl);
  5456. #ifdef WOLFSSL_ASYNC_CRYPT
  5457. err = 0; /* Reset error */
  5458. #endif
  5459. do {
  5460. #ifdef WOLFSSL_ASYNC_CRYPT
  5461. if (err == WC_PENDING_E) {
  5462. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5463. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5464. }
  5465. #endif
  5466. ret = wolfSSL_accept(ssl);
  5467. err = wolfSSL_get_error(ssl, ret);
  5468. } while (err == WC_PENDING_E);
  5469. if (ret != WOLFSSL_SUCCESS) {
  5470. char buff[WOLFSSL_MAX_ERROR_SZ];
  5471. printf("accept error = %d, %s\n",
  5472. err, wolfSSL_ERR_error_string(err, buff));
  5473. /*err_sys("SSL_accept failed");*/
  5474. }
  5475. else {
  5476. #ifdef WOLFSSL_ASYNC_CRYPT
  5477. err = 0; /* Reset error */
  5478. #endif
  5479. do {
  5480. #ifdef WOLFSSL_ASYNC_CRYPT
  5481. if (err == WC_PENDING_E) {
  5482. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5483. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5484. }
  5485. #endif
  5486. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  5487. err = wolfSSL_get_error(ssl, idx);
  5488. } while (err == WC_PENDING_E);
  5489. if (idx > 0) {
  5490. input[idx] = 0;
  5491. printf("Client message: %s\n", input);
  5492. }
  5493. #ifdef WOLFSSL_ASYNC_CRYPT
  5494. err = 0; /* Reset error */
  5495. #endif
  5496. do {
  5497. #ifdef WOLFSSL_ASYNC_CRYPT
  5498. if (err == WC_PENDING_E) {
  5499. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5500. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5501. }
  5502. #endif
  5503. ret = wolfSSL_write(ssl, msg, len);
  5504. err = wolfSSL_get_error(ssl, ret);
  5505. } while (err == WC_PENDING_E);
  5506. AssertIntEQ(len, ret);
  5507. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  5508. defined(WOLFSSL_DTLS)
  5509. if (wolfSSL_dtls(ssl)) {
  5510. byte* import;
  5511. word32 sz;
  5512. wolfSSL_dtls_export(ssl, NULL, &sz);
  5513. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5514. AssertNotNull(import);
  5515. idx = wolfSSL_dtls_export(ssl, import, &sz);
  5516. AssertIntGE(idx, 0);
  5517. AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
  5518. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5519. }
  5520. #endif
  5521. #ifdef WOLFSSL_TIRTOS
  5522. Task_yield();
  5523. #endif
  5524. ((func_args*)args)->return_code = TEST_SUCCESS;
  5525. }
  5526. if (callbacks->on_result)
  5527. callbacks->on_result(ssl);
  5528. wolfSSL_shutdown(ssl);
  5529. wolfSSL_free(ssl);
  5530. wolfSSL_CTX_free(ctx);
  5531. CloseSocket(cfd);
  5532. #ifdef WOLFSSL_TIRTOS
  5533. fdCloseSession(Task_self());
  5534. #endif
  5535. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  5536. && defined(HAVE_THREAD_LS)
  5537. wc_ecc_fp_free(); /* free per thread cache */
  5538. #endif
  5539. #ifndef WOLFSSL_TIRTOS
  5540. return 0;
  5541. #endif
  5542. }
  5543. /* TLS Client for API unit testing - generic */
  5544. static void run_wolfssl_client(void* args)
  5545. {
  5546. callback_functions* callbacks = ((func_args*)args)->callbacks;
  5547. WOLFSSL_CTX* ctx = NULL;
  5548. WOLFSSL* ssl = NULL;
  5549. SOCKET_T sfd = 0;
  5550. char msg[] = "hello wolfssl server!";
  5551. int len = (int) XSTRLEN(msg);
  5552. char input[1024];
  5553. int ret, err = 0;
  5554. ((func_args*)args)->return_code = TEST_FAIL;
  5555. /* set defaults */
  5556. if (callbacks->caPemFile == NULL)
  5557. callbacks->caPemFile = caCertFile;
  5558. if (callbacks->certPemFile == NULL)
  5559. callbacks->certPemFile = cliCertFile;
  5560. if (callbacks->keyPemFile == NULL)
  5561. callbacks->keyPemFile = cliKeyFile;
  5562. #ifdef WOLFSSL_STATIC_MEMORY
  5563. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  5564. callbacks->memSz > 0) {
  5565. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  5566. callbacks->mem, callbacks->memSz, 0, 1);
  5567. if (ret != WOLFSSL_SUCCESS) {
  5568. printf("CTX static new failed %d\n", ret);
  5569. return;
  5570. }
  5571. }
  5572. #else
  5573. if (ctx == NULL) {
  5574. ctx = wolfSSL_CTX_new(callbacks->method());
  5575. }
  5576. if (ctx == NULL) {
  5577. printf("CTX new failed\n");
  5578. return;
  5579. }
  5580. #endif
  5581. #ifdef WOLFSSL_TIRTOS
  5582. fdOpenSession(Task_self());
  5583. #endif
  5584. if (!callbacks->loadToSSL) {
  5585. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  5586. }
  5587. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5588. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  5589. #endif
  5590. AssertIntEQ(WOLFSSL_SUCCESS,
  5591. wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
  5592. if (!callbacks->loadToSSL) {
  5593. AssertIntEQ(WOLFSSL_SUCCESS,
  5594. wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  5595. WOLFSSL_FILETYPE_PEM));
  5596. AssertIntEQ(WOLFSSL_SUCCESS,
  5597. wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  5598. WOLFSSL_FILETYPE_PEM));
  5599. }
  5600. if (callbacks->ctx_ready)
  5601. callbacks->ctx_ready(ctx);
  5602. ssl = wolfSSL_new(ctx);
  5603. if (wolfSSL_dtls(ssl)) {
  5604. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5605. 1, 0, ssl);
  5606. }
  5607. else {
  5608. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5609. 0, 0, ssl);
  5610. }
  5611. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  5612. if (callbacks->loadToSSL) {
  5613. wolfSSL_SetDevId(ssl, callbacks->devId);
  5614. AssertIntEQ(WOLFSSL_SUCCESS,
  5615. wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  5616. WOLFSSL_FILETYPE_PEM));
  5617. AssertIntEQ(WOLFSSL_SUCCESS,
  5618. wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  5619. WOLFSSL_FILETYPE_PEM));
  5620. }
  5621. if (callbacks->ssl_ready)
  5622. callbacks->ssl_ready(ssl);
  5623. #ifdef WOLFSSL_ASYNC_CRYPT
  5624. err = 0; /* Reset error */
  5625. #endif
  5626. do {
  5627. #ifdef WOLFSSL_ASYNC_CRYPT
  5628. if (err == WC_PENDING_E) {
  5629. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5630. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5631. }
  5632. #endif
  5633. ret = wolfSSL_connect(ssl);
  5634. err = wolfSSL_get_error(ssl, ret);
  5635. } while (err == WC_PENDING_E);
  5636. if (ret != WOLFSSL_SUCCESS) {
  5637. char buff[WOLFSSL_MAX_ERROR_SZ];
  5638. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5639. /*err_sys("SSL_connect failed");*/
  5640. }
  5641. else {
  5642. #ifdef WOLFSSL_ASYNC_CRYPT
  5643. err = 0; /* Reset error */
  5644. #endif
  5645. do {
  5646. #ifdef WOLFSSL_ASYNC_CRYPT
  5647. if (err == WC_PENDING_E) {
  5648. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5649. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5650. }
  5651. #endif
  5652. ret = wolfSSL_write(ssl, msg, len);
  5653. err = wolfSSL_get_error(ssl, ret);
  5654. } while (err == WC_PENDING_E);
  5655. AssertIntEQ(len, ret);
  5656. #ifdef WOLFSSL_ASYNC_CRYPT
  5657. err = 0; /* Reset error */
  5658. #endif
  5659. do {
  5660. #ifdef WOLFSSL_ASYNC_CRYPT
  5661. if (err == WC_PENDING_E) {
  5662. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5663. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5664. }
  5665. #endif
  5666. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  5667. err = wolfSSL_get_error(ssl, ret);
  5668. } while (err == WC_PENDING_E);
  5669. if (ret > 0) {
  5670. input[ret] = '\0'; /* null term */
  5671. printf("Server response: %s\n", input);
  5672. }
  5673. ((func_args*)args)->return_code = TEST_SUCCESS;
  5674. }
  5675. if (callbacks->on_result)
  5676. callbacks->on_result(ssl);
  5677. wolfSSL_free(ssl);
  5678. wolfSSL_CTX_free(ctx);
  5679. CloseSocket(sfd);
  5680. #ifdef WOLFSSL_TIRTOS
  5681. fdCloseSession(Task_self());
  5682. #endif
  5683. }
  5684. #endif /* ENABLE_TLS_CALLBACK_TEST */
  5685. static int test_wolfSSL_read_write(void)
  5686. {
  5687. /* The unit testing for read and write shall happen simultaneously, since
  5688. * one can't do anything with one without the other. (Except for a failure
  5689. * test case.) This function will call all the others that will set up,
  5690. * execute, and report their test findings.
  5691. *
  5692. * Set up the success case first. This function will become the template
  5693. * for the other tests. This should eventually be renamed
  5694. *
  5695. * The success case isn't interesting, how can this fail?
  5696. * - Do not give the client context a CA certificate. The connect should
  5697. * fail. Do not need server for this?
  5698. * - Using NULL for the ssl object on server. Do not need client for this.
  5699. * - Using NULL for the ssl object on client. Do not need server for this.
  5700. * - Good ssl objects for client and server. Client write() without server
  5701. * read().
  5702. * - Good ssl objects for client and server. Server write() without client
  5703. * read().
  5704. * - Forgetting the password callback?
  5705. */
  5706. tcp_ready ready;
  5707. func_args client_args;
  5708. func_args server_args;
  5709. THREAD_TYPE serverThread;
  5710. XMEMSET(&client_args, 0, sizeof(func_args));
  5711. XMEMSET(&server_args, 0, sizeof(func_args));
  5712. #ifdef WOLFSSL_TIRTOS
  5713. fdOpenSession(Task_self());
  5714. #endif
  5715. StartTCP();
  5716. InitTcpReady(&ready);
  5717. #if defined(USE_WINDOWS_API)
  5718. /* use RNG to get random port if using windows */
  5719. ready.port = GetRandomPort();
  5720. #endif
  5721. server_args.signal = &ready;
  5722. client_args.signal = &ready;
  5723. start_thread(test_server_nofail, &server_args, &serverThread);
  5724. wait_tcp_ready(&server_args);
  5725. test_client_nofail(&client_args, NULL);
  5726. join_thread(serverThread);
  5727. AssertTrue(client_args.return_code);
  5728. AssertTrue(server_args.return_code);
  5729. FreeTcpReady(&ready);
  5730. #ifdef WOLFSSL_TIRTOS
  5731. fdOpenSession(Task_self());
  5732. #endif
  5733. return 0;
  5734. }
  5735. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  5736. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  5737. {
  5738. /* The unit test for session resumption by re-using WOLFSSL object.
  5739. * WOLFSSL object is not cleared after first session. It re-use the obeject
  5740. * for second connection.
  5741. */
  5742. tcp_ready ready;
  5743. func_args client_args;
  5744. func_args server_args;
  5745. THREAD_TYPE serverThread;
  5746. XMEMSET(&client_args, 0, sizeof(func_args));
  5747. XMEMSET(&server_args, 0, sizeof(func_args));
  5748. #ifdef WOLFSSL_TIRTOS
  5749. fdOpenSession(Task_self());
  5750. #endif
  5751. StartTCP();
  5752. InitTcpReady(&ready);
  5753. #if defined(USE_WINDOWS_API)
  5754. /* use RNG to get random port if using windows */
  5755. ready.port = GetRandomPort();
  5756. #endif
  5757. server_args.signal = &ready;
  5758. client_args.signal = &ready;
  5759. /* the var is used for loop number */
  5760. server_args.argc = 2;
  5761. start_thread(test_server_loop, &server_args, &serverThread);
  5762. wait_tcp_ready(&server_args);
  5763. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  5764. join_thread(serverThread);
  5765. AssertTrue(client_args.return_code);
  5766. AssertTrue(server_args.return_code);
  5767. FreeTcpReady(&ready);
  5768. #ifdef WOLFSSL_TIRTOS
  5769. fdOpenSession(Task_self());
  5770. #endif
  5771. return 0;
  5772. }
  5773. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  5774. static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
  5775. {
  5776. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
  5777. /* This unit test is to check set verify Depth */
  5778. tcp_ready ready;
  5779. func_args client_args;
  5780. func_args server_args;
  5781. THREAD_TYPE serverThread;
  5782. callback_functions client_cbf;
  5783. XMEMSET(&client_args, 0, sizeof(func_args));
  5784. XMEMSET(&server_args, 0, sizeof(func_args));
  5785. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  5786. printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
  5787. #ifdef WOLFSSL_TLS13
  5788. client_cbf.method = wolfTLSv1_3_client_method;
  5789. #endif /* WOLFSSL_TLS13 */
  5790. client_args.callbacks = &client_cbf;
  5791. StartTCP();
  5792. InitTcpReady(&ready);
  5793. #if defined(USE_WINDOWS_API)
  5794. /* use RNG to get random port if using windows */
  5795. ready.port = GetRandomPort();
  5796. #endif
  5797. server_args.signal = &ready;
  5798. client_args.signal = &ready;
  5799. /* the var is used for loop number */
  5800. server_args.argc = 1;
  5801. /* test case 1 verify depth is equal to peer chain */
  5802. {
  5803. start_thread(test_server_nofail, &server_args, &serverThread);
  5804. wait_tcp_ready(&server_args);
  5805. /* the var is used for verify depth */
  5806. client_args.argc = 2;
  5807. test_client_verifyDepth(&client_args);
  5808. join_thread(serverThread);
  5809. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5810. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5811. }
  5812. /* test case 2
  5813. * verify depth is zero, number of peer's chain is 2.
  5814. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  5815. * callback.
  5816. */
  5817. /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
  5818. {
  5819. start_thread(test_server_nofail, &server_args, &serverThread);
  5820. wait_tcp_ready(&server_args);
  5821. client_args.argc = 0;
  5822. test_client_verifyDepth(&client_args);
  5823. join_thread(serverThread);
  5824. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5825. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5826. }
  5827. /* test case 3
  5828. * verify depth is zero, number of peer's chain is 2
  5829. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  5830. * therefore, handshake becomes failure.
  5831. */
  5832. /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
  5833. {
  5834. start_thread(test_server_nofail, &server_args, &serverThread);
  5835. wait_tcp_ready(&server_args);
  5836. client_args.argc = -1;
  5837. test_client_verifyDepth(&client_args);
  5838. join_thread(serverThread);
  5839. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  5840. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  5841. }
  5842. FreeTcpReady(&ready);
  5843. printf(resultFmt, passed);
  5844. #else
  5845. (void)test_client_verifyDepth;
  5846. #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
  5847. return 0;
  5848. }
  5849. static int test_client_get_finished(void* args, cbType cb)
  5850. {
  5851. #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
  5852. SOCKET_T sockfd = 0;
  5853. callback_functions* cbf;
  5854. WOLFSSL_CTX* ctx = 0;
  5855. WOLFSSL* ssl = 0;
  5856. char msg[64] = "hello wolfssl!";
  5857. char reply[1024];
  5858. int msgSz = (int)XSTRLEN(msg);
  5859. int ret, err = 0;
  5860. WOLFSSL_METHOD* method = NULL;
  5861. size_t msg_len = 0;
  5862. (void) args;
  5863. (void) cb;
  5864. ((func_args*)args)->return_code = TEST_FAIL;
  5865. cbf = ((func_args*)args)->callbacks;
  5866. if (cbf != NULL && cbf->method != NULL) {
  5867. method = cbf->method();
  5868. }
  5869. else {
  5870. method = wolfSSLv23_client_method();
  5871. }
  5872. ctx = wolfSSL_CTX_new(method);
  5873. /* Do connect here so server detects failures */
  5874. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  5875. 0, 0, NULL);
  5876. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  5877. {
  5878. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  5879. goto done;
  5880. }
  5881. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  5882. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5883. goto done;
  5884. }
  5885. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  5886. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  5887. goto done;
  5888. }
  5889. /* call ctx setup callback */
  5890. if (cbf != NULL && cbf->ctx_ready != NULL) {
  5891. cbf->ctx_ready(ctx);
  5892. }
  5893. ssl = wolfSSL_new(ctx);
  5894. if (ssl == NULL) {
  5895. goto done;
  5896. }
  5897. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  5898. goto done;
  5899. }
  5900. /* call ssl setup callback */
  5901. if (cbf != NULL && cbf->ssl_ready != NULL) {
  5902. cbf->ssl_ready(ssl);
  5903. }
  5904. #ifdef WOLFSSL_ASYNC_CRYPT
  5905. err = 0; /* Reset error */
  5906. #endif
  5907. do {
  5908. #ifdef WOLFSSL_ASYNC_CRYPT
  5909. if (err == WC_PENDING_E) {
  5910. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5911. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5912. }
  5913. #endif
  5914. ret = wolfSSL_connect(ssl);
  5915. err = wolfSSL_get_error(ssl, 0);
  5916. } while (err == WC_PENDING_E);
  5917. if (ret != WOLFSSL_SUCCESS) {
  5918. char buff[WOLFSSL_MAX_ERROR_SZ];
  5919. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  5920. goto done;
  5921. }
  5922. /* get_finished test */
  5923. /* 1. get own sent message */
  5924. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  5925. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  5926. AssertIntGE(msg_len, 0);
  5927. /* 2. get peer message */
  5928. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  5929. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  5930. AssertIntGE(msg_len, 0);
  5931. if (cb != NULL)
  5932. (cb)(ctx, ssl);
  5933. #ifdef WOLFSSL_ASYNC_CRYPT
  5934. err = 0; /* Reset error */
  5935. #endif
  5936. do {
  5937. #ifdef WOLFSSL_ASYNC_CRYPT
  5938. if (err == WC_PENDING_E) {
  5939. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5940. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5941. }
  5942. #endif
  5943. ret = wolfSSL_write(ssl, msg, msgSz);
  5944. err = wolfSSL_get_error(ssl, 0);
  5945. } while (err == WC_PENDING_E);
  5946. if (ret != msgSz) {
  5947. /*err_sys("SSL_write failed");*/
  5948. goto done;
  5949. }
  5950. #ifdef WOLFSSL_ASYNC_CRYPT
  5951. err = 0; /* Reset error */
  5952. #endif
  5953. do {
  5954. #ifdef WOLFSSL_ASYNC_CRYPT
  5955. if (err == WC_PENDING_E) {
  5956. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  5957. if (ret < 0) { break; } else if (ret == 0) { continue; }
  5958. }
  5959. #endif
  5960. ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  5961. err = wolfSSL_get_error(ssl, 0);
  5962. } while (err == WC_PENDING_E);
  5963. if (ret > 0) {
  5964. reply[ret] = '\0';
  5965. printf("Server response: %s\n", reply);
  5966. }
  5967. ((func_args*)args)->return_code = TEST_SUCCESS;
  5968. done:
  5969. wolfSSL_free(ssl);
  5970. wolfSSL_CTX_free(ctx);
  5971. CloseSocket(sockfd);
  5972. #else
  5973. (void)args;
  5974. (void)cb;
  5975. #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
  5976. return 0;
  5977. }
  5978. static int test_wolfSSL_get_finished(void)
  5979. {
  5980. #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
  5981. tcp_ready ready;
  5982. func_args client_args;
  5983. func_args server_args;
  5984. THREAD_TYPE serverThread;
  5985. XMEMSET(&client_args, 0, sizeof(func_args));
  5986. XMEMSET(&server_args, 0, sizeof(func_args));
  5987. StartTCP();
  5988. InitTcpReady(&ready);
  5989. #if defined(USE_WINDOWS_API)
  5990. /* use RNG to get random port if using windows */
  5991. ready.port = GetRandomPort();
  5992. #endif
  5993. server_args.signal = &ready;
  5994. client_args.signal = &ready;
  5995. start_thread(test_server_nofail, &server_args, &serverThread);
  5996. wait_tcp_ready(&server_args);
  5997. test_client_get_finished(&client_args, NULL);
  5998. join_thread(serverThread);
  5999. AssertTrue(client_args.return_code);
  6000. AssertTrue(server_args.return_code);
  6001. /* test received msg vs sent msg */
  6002. AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  6003. AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  6004. FreeTcpReady(&ready);
  6005. #else
  6006. (void)test_client_get_finished;
  6007. #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
  6008. return 0;
  6009. }
  6010. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6011. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6012. !defined(NO_SESSION_CACHE)
  6013. /* Sessions to restore/store */
  6014. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  6015. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  6016. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  6017. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  6018. {
  6019. /* Don't store sessions. Lookup is still enabled. */
  6020. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  6021. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  6022. /* Require both peers to provide certs */
  6023. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  6024. }
  6025. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  6026. {
  6027. WOLFSSL_SESSION** sess;
  6028. if (wolfSSL_is_server(ssl))
  6029. sess = &test_wolfSSL_CTX_add_session_server_sess;
  6030. else
  6031. sess = &test_wolfSSL_CTX_add_session_client_sess;
  6032. if (*sess == NULL) {
  6033. #ifdef NO_SESSION_CACHE_REF
  6034. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6035. #else
  6036. /* Test for backwards compatibility */
  6037. if (wolfSSL_is_server(ssl)) {
  6038. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  6039. }
  6040. else {
  6041. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  6042. }
  6043. #endif
  6044. /* Now save the session in the internal store to make it available
  6045. * for lookup. For TLS 1.3, we can't save the session without
  6046. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  6047. * session from cache. */
  6048. if (wolfSSL_is_server(ssl)
  6049. #ifndef WOLFSSL_TICKET_HAVE_ID
  6050. && wolfSSL_version(ssl) != TLS1_3_VERSION
  6051. #endif
  6052. )
  6053. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  6054. *sess), WOLFSSL_SUCCESS);
  6055. }
  6056. else {
  6057. /* If we have a session retrieved then remaining connections should be
  6058. * resuming on that session */
  6059. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  6060. }
  6061. /* Save CTX to be able to decrypt tickets */
  6062. if (wolfSSL_is_server(ssl) &&
  6063. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  6064. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  6065. = wolfSSL_get_SSL_CTX(ssl));
  6066. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  6067. WOLFSSL_SUCCESS);
  6068. }
  6069. #ifdef SESSION_CERTS
  6070. #ifndef WOLFSSL_TICKET_HAVE_ID
  6071. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  6072. wolfSSL_session_reused(ssl))
  6073. #endif
  6074. {
  6075. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  6076. * for all connections. TLS 1.3 only has tickets so if we don't
  6077. * include the session id in the ticket then the certificates
  6078. * will not be available on resumption. */
  6079. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  6080. AssertNotNull(peer);
  6081. wolfSSL_X509_free(peer);
  6082. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  6083. AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
  6084. }
  6085. #endif
  6086. }
  6087. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  6088. {
  6089. /* Set the session to reuse for the client */
  6090. AssertIntEQ(wolfSSL_set_session(ssl,
  6091. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  6092. }
  6093. #endif
  6094. static int test_wolfSSL_CTX_add_session(void)
  6095. {
  6096. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  6097. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  6098. !defined(NO_SESSION_CACHE)
  6099. tcp_ready ready;
  6100. func_args client_args;
  6101. func_args server_args;
  6102. THREAD_TYPE serverThread;
  6103. callback_functions client_cb;
  6104. callback_functions server_cb;
  6105. method_provider methods[][2] = {
  6106. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  6107. !defined(NO_DES3))
  6108. /* Without AES there are almost no ciphersuites available. This leads
  6109. * to no ciphersuites being available and an error. */
  6110. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  6111. #endif
  6112. #ifndef WOLFSSL_NO_TLS12
  6113. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  6114. #endif
  6115. /* Needs the default ticket callback since it is tied to the
  6116. * connection context and this makes it easy to carry over the ticket
  6117. * crypto context between connections */
  6118. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  6119. defined(HAVE_SESSION_TICKET)
  6120. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  6121. #endif
  6122. };
  6123. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  6124. size_t i, j;
  6125. printf(testingFmt, "wolfSSL_CTX_add_session()");
  6126. for (i = 0; i < methodsLen; i++) {
  6127. /* First run creates a connection while the second+ run will attempt
  6128. * to resume the connection. The trick is that the internal cache
  6129. * is turned off. wolfSSL_CTX_add_session should put the session in
  6130. * the cache anyway. */
  6131. test_wolfSSL_CTX_add_session_client_sess = NULL;
  6132. test_wolfSSL_CTX_add_session_server_sess = NULL;
  6133. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  6134. for (j = 0; j < 5; j++) {
  6135. #ifdef WOLFSSL_TIRTOS
  6136. fdOpenSession(Task_self());
  6137. #endif
  6138. StartTCP();
  6139. InitTcpReady(&ready);
  6140. XMEMSET(&client_args, 0, sizeof(func_args));
  6141. XMEMSET(&server_args, 0, sizeof(func_args));
  6142. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6143. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6144. client_cb.method = methods[i][0];
  6145. server_cb.method = methods[i][1];
  6146. server_args.signal = &ready;
  6147. server_args.callbacks = &server_cb;
  6148. client_args.signal = &ready;
  6149. client_args.callbacks = &client_cb;
  6150. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  6151. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  6152. server_cb.isSharedCtx = 1;
  6153. }
  6154. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6155. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  6156. if (j != 0)
  6157. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  6158. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6159. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  6160. server_cb.ticNoInit = 1; /* Use default builtin */
  6161. start_thread(test_server_nofail, &server_args, &serverThread);
  6162. wait_tcp_ready(&server_args);
  6163. test_client_nofail(&client_args, NULL);
  6164. join_thread(serverThread);
  6165. AssertTrue(client_args.return_code);
  6166. AssertTrue(server_args.return_code);
  6167. FreeTcpReady(&ready);
  6168. }
  6169. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  6170. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  6171. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  6172. }
  6173. printf(resultFmt, passed);
  6174. #endif
  6175. return 0;
  6176. }
  6177. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6178. /* canned export of a session using older version 3 */
  6179. static unsigned char version_3[] = {
  6180. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  6181. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  6182. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6183. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  6184. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6185. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  6187. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  6188. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  6189. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6190. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6191. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  6192. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6193. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  6194. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  6195. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  6196. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  6197. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  6198. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  6199. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  6200. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6201. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6202. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6203. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6204. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6205. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6206. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6207. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6208. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6209. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6210. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6211. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  6212. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  6213. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  6214. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  6215. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  6216. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  6217. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  6218. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  6219. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  6220. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  6221. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6222. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  6223. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  6224. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6225. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  6226. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  6227. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  6228. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  6229. 0x2E, 0x31, 0xED, 0x4F
  6230. };
  6231. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  6232. static int test_wolfSSL_dtls_export(void)
  6233. {
  6234. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  6235. tcp_ready ready;
  6236. func_args client_args;
  6237. func_args server_args;
  6238. THREAD_TYPE serverThread;
  6239. callback_functions server_cbf;
  6240. callback_functions client_cbf;
  6241. #ifdef WOLFSSL_TIRTOS
  6242. fdOpenSession(Task_self());
  6243. #endif
  6244. InitTcpReady(&ready);
  6245. #if defined(USE_WINDOWS_API)
  6246. /* use RNG to get random port if using windows */
  6247. ready.port = GetRandomPort();
  6248. #endif
  6249. /* set using dtls */
  6250. XMEMSET(&client_args, 0, sizeof(func_args));
  6251. XMEMSET(&server_args, 0, sizeof(func_args));
  6252. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6253. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  6254. server_cbf.method = wolfDTLSv1_2_server_method;
  6255. client_cbf.method = wolfDTLSv1_2_client_method;
  6256. server_args.callbacks = &server_cbf;
  6257. client_args.callbacks = &client_cbf;
  6258. server_args.signal = &ready;
  6259. client_args.signal = &ready;
  6260. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6261. wait_tcp_ready(&server_args);
  6262. run_wolfssl_client(&client_args);
  6263. join_thread(serverThread);
  6264. AssertTrue(client_args.return_code);
  6265. AssertTrue(server_args.return_code);
  6266. FreeTcpReady(&ready);
  6267. #ifdef WOLFSSL_TIRTOS
  6268. fdOpenSession(Task_self());
  6269. #endif
  6270. {
  6271. SOCKET_T sockfd = 0;
  6272. WOLFSSL_CTX* ctx;
  6273. WOLFSSL* ssl;
  6274. char msg[64] = "hello wolfssl!";
  6275. char reply[1024];
  6276. int msgSz = (int)XSTRLEN(msg);
  6277. byte *session, *window;
  6278. unsigned int sessionSz, windowSz;
  6279. #ifndef TEST_IPV6
  6280. struct sockaddr_in peerAddr;
  6281. #else
  6282. struct sockaddr_in6 peerAddr;
  6283. #endif /* TEST_IPV6 */
  6284. int i;
  6285. /* Set ctx to DTLS 1.2 */
  6286. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  6287. AssertNotNull(ssl = wolfSSL_new(ctx));
  6288. /* test importing version 3 */
  6289. AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6290. /* test importing bad length and bad version */
  6291. version_3[2] += 1;
  6292. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6293. version_3[2] -= 1; version_3[1] = 0XA0;
  6294. AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  6295. wolfSSL_free(ssl);
  6296. wolfSSL_CTX_free(ctx);
  6297. /* check storing client state after connection and storing window only */
  6298. #ifdef WOLFSSL_TIRTOS
  6299. fdOpenSession(Task_self());
  6300. #endif
  6301. InitTcpReady(&ready);
  6302. #if defined(USE_WINDOWS_API)
  6303. /* use RNG to get random port if using windows */
  6304. ready.port = GetRandomPort();
  6305. #endif
  6306. /* set using dtls */
  6307. XMEMSET(&server_args, 0, sizeof(func_args));
  6308. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6309. server_cbf.method = wolfDTLSv1_2_server_method;
  6310. server_cbf.doUdp = 1;
  6311. server_args.callbacks = &server_cbf;
  6312. server_args.argc = 3; /* set loop_count to 3 */
  6313. server_args.signal = &ready;
  6314. start_thread(test_server_nofail, &server_args, &serverThread);
  6315. wait_tcp_ready(&server_args);
  6316. /* create and connect with client */
  6317. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  6318. AssertIntEQ(WOLFSSL_SUCCESS,
  6319. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  6320. AssertIntEQ(WOLFSSL_SUCCESS,
  6321. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  6322. AssertIntEQ(WOLFSSL_SUCCESS,
  6323. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  6324. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  6325. AssertNotNull(ssl = wolfSSL_new(ctx));
  6326. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6327. /* store server information connected too */
  6328. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  6329. #ifndef TEST_IPV6
  6330. peerAddr.sin_family = AF_INET;
  6331. AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  6332. peerAddr.sin_port = XHTONS(server_args.signal->port);
  6333. #else
  6334. peerAddr.sin6_family = AF_INET6;
  6335. AssertIntEQ(
  6336. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  6337. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  6338. #endif
  6339. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6340. WOLFSSL_SUCCESS);
  6341. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  6342. AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  6343. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6344. AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  6345. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6346. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6347. AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  6348. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6349. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6350. wolfSSL_free(ssl);
  6351. for (i = 1; i < server_args.argc; i++) {
  6352. /* restore state */
  6353. AssertNotNull(ssl = wolfSSL_new(ctx));
  6354. AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  6355. AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  6356. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  6357. AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  6358. WOLFSSL_SUCCESS);
  6359. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6360. AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  6361. AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  6362. wolfSSL_free(ssl);
  6363. }
  6364. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6365. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  6366. wolfSSL_CTX_free(ctx);
  6367. printf("done and waiting for server\n");
  6368. join_thread(serverThread);
  6369. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6370. FreeTcpReady(&ready);
  6371. #ifdef WOLFSSL_TIRTOS
  6372. fdOpenSession(Task_self());
  6373. #endif
  6374. }
  6375. printf(testingFmt, "wolfSSL_dtls_export()");
  6376. printf(resultFmt, passed);
  6377. #endif
  6378. return 0;
  6379. }
  6380. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6381. #ifdef WOLFSSL_TLS13
  6382. static const byte canned_client_tls13_session[] = {
  6383. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  6384. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6385. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  6386. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6387. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6388. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6389. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6390. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  6391. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6392. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6393. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6394. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  6395. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6396. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6397. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6398. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6399. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6400. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6401. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6402. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6403. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6404. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6405. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6406. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6407. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6408. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6409. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6410. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6412. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  6413. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  6414. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  6415. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6416. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6417. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6418. 0x00, 0x03
  6419. };
  6420. static const byte canned_server_tls13_session[] = {
  6421. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  6422. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  6423. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  6424. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6425. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6426. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  6427. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  6428. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  6429. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  6430. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6431. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6432. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  6433. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  6434. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  6435. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  6436. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  6437. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  6438. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  6439. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  6440. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  6441. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  6442. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  6443. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  6444. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  6445. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  6446. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  6447. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  6448. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  6449. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6450. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  6451. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  6452. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  6453. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  6454. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  6455. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6456. 0x00, 0x04
  6457. };
  6458. #endif /* WOLFSSL_TLS13 */
  6459. static const byte canned_client_session[] = {
  6460. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6461. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6462. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  6463. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6464. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  6465. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6466. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6467. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  6468. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6469. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6470. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6471. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  6472. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6473. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6474. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6475. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6476. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6477. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6478. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6479. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6480. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6481. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6482. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6483. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6484. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6485. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6486. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6487. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6488. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6489. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6490. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6491. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6492. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6493. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6494. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6495. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6496. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6497. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6498. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6499. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6500. 0x00, 0x03
  6501. };
  6502. static const byte canned_server_session[] = {
  6503. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  6504. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  6505. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  6506. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  6507. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  6508. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6509. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  6510. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  6511. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  6512. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6513. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6514. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  6515. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  6516. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  6517. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  6518. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  6519. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  6520. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  6521. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  6522. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  6523. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  6524. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  6525. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  6526. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  6527. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  6528. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  6529. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  6530. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  6531. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  6532. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  6533. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6534. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6535. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  6536. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  6537. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  6538. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  6539. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  6540. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  6541. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  6542. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  6543. 0x00, 0x04
  6544. };
  6545. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  6546. {
  6547. SOCKET_T sockfd = 0;
  6548. SOCKET_T clientfd = 0;
  6549. word16 port;
  6550. callback_functions* cbf;
  6551. WOLFSSL_CTX* ctx = 0;
  6552. WOLFSSL* ssl = 0;
  6553. char msg[] = "I hear you fa shizzle!";
  6554. char input[1024];
  6555. int idx;
  6556. #ifdef WOLFSSL_TIRTOS
  6557. fdOpenSession(Task_self());
  6558. #endif
  6559. ((func_args*)args)->return_code = TEST_FAIL;
  6560. cbf = ((func_args*)args)->callbacks;
  6561. {
  6562. WOLFSSL_METHOD* method = NULL;
  6563. if (cbf != NULL && cbf->method != NULL) {
  6564. method = cbf->method();
  6565. }
  6566. else {
  6567. method = wolfTLSv1_2_server_method();
  6568. }
  6569. ctx = wolfSSL_CTX_new(method);
  6570. }
  6571. if (ctx == NULL) {
  6572. goto done;
  6573. }
  6574. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6575. #if defined(USE_WINDOWS_API)
  6576. port = ((func_args*)args)->signal->port;
  6577. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6578. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6579. /* Let tcp_listen assign port */
  6580. port = 0;
  6581. #else
  6582. /* Use default port */
  6583. port = wolfSSLPort;
  6584. #endif
  6585. /* do it here to detect failure */
  6586. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  6587. CloseSocket(sockfd);
  6588. /* call ctx setup callback */
  6589. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6590. cbf->ctx_ready(ctx);
  6591. }
  6592. ssl = wolfSSL_new(ctx);
  6593. if (ssl == NULL) {
  6594. goto done;
  6595. }
  6596. wolfSSL_set_fd(ssl, clientfd);
  6597. /* call ssl setup callback */
  6598. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6599. cbf->ssl_ready(ssl);
  6600. }
  6601. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6602. if (idx > 0) {
  6603. input[idx] = '\0';
  6604. printf("Client message export/import: %s\n", input);
  6605. }
  6606. else {
  6607. printf("ret = %d error = %d\n", idx, wolfSSL_get_error(ssl, idx));
  6608. goto done;
  6609. }
  6610. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6611. /*err_sys("SSL_write failed");*/
  6612. #ifdef WOLFSSL_TIRTOS
  6613. return;
  6614. #else
  6615. return 0;
  6616. #endif
  6617. }
  6618. #ifdef WOLFSSL_TIRTOS
  6619. Task_yield();
  6620. #endif
  6621. ((func_args*)args)->return_code = TEST_SUCCESS;
  6622. done:
  6623. wolfSSL_shutdown(ssl);
  6624. wolfSSL_free(ssl);
  6625. wolfSSL_CTX_free(ctx);
  6626. CloseSocket(clientfd);
  6627. #ifdef WOLFSSL_TIRTOS
  6628. fdCloseSession(Task_self());
  6629. #endif
  6630. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6631. && defined(HAVE_THREAD_LS)
  6632. wc_ecc_fp_free(); /* free per thread cache */
  6633. #endif
  6634. #if defined(HAVE_SESSION_TICKET) && \
  6635. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6636. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  6637. OpenSSLTicketCleanup();
  6638. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6639. TicketCleanup();
  6640. #endif
  6641. #endif
  6642. #ifndef WOLFSSL_TIRTOS
  6643. return 0;
  6644. #endif
  6645. }
  6646. static void load_tls12_canned_server(WOLFSSL* ssl)
  6647. {
  6648. int clientfd = wolfSSL_get_fd(ssl);
  6649. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  6650. sizeof(canned_server_session)), sizeof(canned_server_session));
  6651. wolfSSL_set_fd(ssl, clientfd);
  6652. }
  6653. #ifdef WOLFSSL_TLS13
  6654. static void load_tls13_canned_server(WOLFSSL* ssl)
  6655. {
  6656. int clientfd = wolfSSL_get_fd(ssl);
  6657. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  6658. sizeof(canned_server_tls13_session)),
  6659. sizeof(canned_server_tls13_session));
  6660. wolfSSL_set_fd(ssl, clientfd);
  6661. }
  6662. #endif
  6663. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  6664. static int test_wolfSSL_tls_export_run(int v)
  6665. {
  6666. SOCKET_T sockfd = 0;
  6667. WOLFSSL_CTX* ctx = 0;
  6668. WOLFSSL* ssl = 0;
  6669. char msg[64] = "hello wolfssl!";
  6670. char reply[1024];
  6671. word32 replySz;
  6672. int msgSz = (int)XSTRLEN(msg);
  6673. const byte* clientSession = NULL;
  6674. int clientSessionSz = 0;
  6675. tcp_ready ready;
  6676. func_args server_args;
  6677. THREAD_TYPE serverThread;
  6678. callback_functions server_cbf;
  6679. #ifdef WOLFSSL_TIRTOS
  6680. fdOpenSession(Task_self());
  6681. #endif
  6682. InitTcpReady(&ready);
  6683. #if defined(USE_WINDOWS_API)
  6684. /* use RNG to get random port if using windows */
  6685. ready.port = GetRandomPort();
  6686. #endif
  6687. XMEMSET(&server_args, 0, sizeof(func_args));
  6688. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  6689. switch (v) {
  6690. case WOLFSSL_TLSV1_2:
  6691. server_cbf.method = wolfTLSv1_2_server_method;
  6692. server_cbf.ssl_ready = load_tls12_canned_server;
  6693. /* setup the client side */
  6694. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  6695. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  6696. clientSession = canned_client_session;
  6697. clientSessionSz = sizeof(canned_client_session);
  6698. break;
  6699. #ifdef WOLFSSL_TLS13
  6700. case WOLFSSL_TLSV1_3:
  6701. server_cbf.method = wolfTLSv1_3_server_method;
  6702. server_cbf.ssl_ready = load_tls13_canned_server;
  6703. /* setup the client side */
  6704. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  6705. clientSession = canned_client_tls13_session;
  6706. clientSessionSz = sizeof(canned_client_tls13_session);
  6707. break;
  6708. #endif
  6709. }
  6710. server_args.callbacks = &server_cbf;
  6711. server_args.signal = &ready;
  6712. start_thread(tls_export_server, &server_args, &serverThread);
  6713. wait_tcp_ready(&server_args);
  6714. #ifdef WOLFSSL_TIRTOS
  6715. fdOpenSession(Task_self());
  6716. #endif
  6717. AssertNotNull(ssl = wolfSSL_new(ctx));
  6718. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  6719. AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  6720. clientSessionSz);
  6721. replySz = sizeof(reply);
  6722. AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  6723. #if !defined(NO_PSK) && defined(HAVE_ANON)
  6724. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  6725. AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  6726. #endif
  6727. wolfSSL_set_fd(ssl, sockfd);
  6728. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  6729. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  6730. wolfSSL_free(ssl);
  6731. wolfSSL_CTX_free(ctx);
  6732. CloseSocket(sockfd);
  6733. #ifdef WOLFSSL_TIRTOS
  6734. fdCloseSession(Task_self());
  6735. #endif
  6736. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6737. && defined(HAVE_THREAD_LS)
  6738. wc_ecc_fp_free(); /* free per thread cache */
  6739. #endif
  6740. join_thread(serverThread);
  6741. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  6742. FreeTcpReady(&ready);
  6743. #ifdef WOLFSSL_TIRTOS
  6744. fdOpenSession(Task_self());
  6745. #endif
  6746. return 0;
  6747. }
  6748. #endif
  6749. static int test_wolfSSL_tls_export(void)
  6750. {
  6751. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  6752. printf(testingFmt, "wolfSSL_tls_export()");
  6753. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  6754. #ifdef WOLFSSL_TLS13
  6755. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  6756. #endif
  6757. printf(resultFmt, passed);
  6758. #endif
  6759. return 0;
  6760. }
  6761. /*----------------------------------------------------------------------------*
  6762. | TLS extensions tests
  6763. *----------------------------------------------------------------------------*/
  6764. #ifdef ENABLE_TLS_CALLBACK_TEST
  6765. /* Connection test runner - generic */
  6766. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  6767. callback_functions* server_callbacks)
  6768. {
  6769. tcp_ready ready;
  6770. func_args client_args;
  6771. func_args server_args;
  6772. THREAD_TYPE serverThread;
  6773. XMEMSET(&client_args, 0, sizeof(func_args));
  6774. XMEMSET(&server_args, 0, sizeof(func_args));
  6775. StartTCP();
  6776. client_args.callbacks = client_callbacks;
  6777. server_args.callbacks = server_callbacks;
  6778. #ifdef WOLFSSL_TIRTOS
  6779. fdOpenSession(Task_self());
  6780. #endif
  6781. /* RUN Server side */
  6782. InitTcpReady(&ready);
  6783. #if defined(USE_WINDOWS_API)
  6784. /* use RNG to get random port if using windows */
  6785. ready.port = GetRandomPort();
  6786. #endif
  6787. server_args.signal = &ready;
  6788. client_args.signal = &ready;
  6789. start_thread(run_wolfssl_server, &server_args, &serverThread);
  6790. wait_tcp_ready(&server_args);
  6791. /* RUN Client side */
  6792. run_wolfssl_client(&client_args);
  6793. join_thread(serverThread);
  6794. FreeTcpReady(&ready);
  6795. #ifdef WOLFSSL_TIRTOS
  6796. fdCloseSession(Task_self());
  6797. #endif
  6798. client_callbacks->return_code = client_args.return_code;
  6799. server_callbacks->return_code = server_args.return_code;
  6800. }
  6801. #endif /* ENABLE_TLS_CALLBACK_TEST */
  6802. #ifdef HAVE_SNI
  6803. static int test_wolfSSL_UseSNI_params(void)
  6804. {
  6805. #if !defined(NO_WOLFSSL_CLIENT)
  6806. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  6807. WOLFSSL *ssl = wolfSSL_new(ctx);
  6808. AssertNotNull(ctx);
  6809. AssertNotNull(ssl);
  6810. /* invalid [ctx|ssl] */
  6811. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  6812. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  6813. /* invalid type */
  6814. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  6815. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  6816. /* invalid data */
  6817. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  6818. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  6819. /* success case */
  6820. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  6821. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  6822. wolfSSL_free(ssl);
  6823. wolfSSL_CTX_free(ctx);
  6824. #endif /* !NO_WOLFSSL_CLIENT */
  6825. return 0;
  6826. }
  6827. /* BEGIN of connection tests callbacks */
  6828. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6829. {
  6830. AssertIntEQ(WOLFSSL_SUCCESS,
  6831. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6832. }
  6833. static void use_SNI_at_ssl(WOLFSSL* ssl)
  6834. {
  6835. AssertIntEQ(WOLFSSL_SUCCESS,
  6836. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  6837. }
  6838. static void different_SNI_at_ssl(WOLFSSL* ssl)
  6839. {
  6840. AssertIntEQ(WOLFSSL_SUCCESS,
  6841. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  6842. }
  6843. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  6844. {
  6845. use_SNI_at_ssl(ssl);
  6846. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6847. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  6848. }
  6849. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  6850. {
  6851. use_SNI_at_ssl(ssl);
  6852. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6853. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  6854. }
  6855. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6856. {
  6857. use_SNI_at_ctx(ctx);
  6858. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6859. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6860. }
  6861. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  6862. {
  6863. use_SNI_at_ssl(ssl);
  6864. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  6865. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6866. }
  6867. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  6868. {
  6869. use_SNI_at_ctx(ctx);
  6870. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  6871. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  6872. }
  6873. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  6874. {
  6875. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  6876. }
  6877. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  6878. {
  6879. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  6880. }
  6881. static void verify_SNI_no_matching(WOLFSSL* ssl)
  6882. {
  6883. byte type = WOLFSSL_SNI_HOST_NAME;
  6884. void* request = (void*) &type; /* to be overwritten */
  6885. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  6886. AssertNotNull(request);
  6887. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6888. AssertNull(request);
  6889. }
  6890. static void verify_SNI_real_matching(WOLFSSL* ssl)
  6891. {
  6892. byte type = WOLFSSL_SNI_HOST_NAME;
  6893. void* request = NULL;
  6894. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  6895. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6896. AssertNotNull(request);
  6897. AssertStrEQ("www.wolfssl.com", (char*)request);
  6898. }
  6899. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  6900. {
  6901. byte type = WOLFSSL_SNI_HOST_NAME;
  6902. void* request = NULL;
  6903. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  6904. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  6905. AssertNotNull(request);
  6906. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  6907. }
  6908. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  6909. {
  6910. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  6911. }
  6912. /* END of connection tests callbacks */
  6913. static int test_wolfSSL_UseSNI_connection(void)
  6914. {
  6915. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  6916. callback_functions client_cb;
  6917. callback_functions server_cb;
  6918. size_t i;
  6919. struct {
  6920. method_provider client_meth;
  6921. method_provider server_meth;
  6922. } methods[] = {
  6923. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  6924. {wolfSSLv23_client_method, wolfSSLv23_server_method},
  6925. #endif
  6926. #ifndef WOLFSSL_NO_TLS12
  6927. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
  6928. #endif
  6929. #ifdef WOLFSSL_TLS13
  6930. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
  6931. #endif
  6932. };
  6933. for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
  6934. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  6935. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  6936. client_cb.method = methods[i].client_meth;
  6937. server_cb.method = methods[i].server_meth;
  6938. client_cb.devId = testDevId;
  6939. server_cb.devId = testDevId;
  6940. /* success case at ctx */
  6941. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6942. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6943. test_wolfSSL_client_server(&client_cb, &server_cb);
  6944. /* success case at ssl */
  6945. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  6946. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  6947. test_wolfSSL_client_server(&client_cb, &server_cb);
  6948. /* default mismatch behavior */
  6949. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6950. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  6951. test_wolfSSL_client_server(&client_cb, &server_cb);
  6952. /* continue on mismatch */
  6953. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6954. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  6955. test_wolfSSL_client_server(&client_cb, &server_cb);
  6956. /* fake answer on mismatch */
  6957. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6958. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  6959. test_wolfSSL_client_server(&client_cb, &server_cb);
  6960. /* sni abort - success */
  6961. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6962. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  6963. test_wolfSSL_client_server(&client_cb, &server_cb);
  6964. /* sni abort - abort when absent (ctx) */
  6965. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6966. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6967. test_wolfSSL_client_server(&client_cb, &server_cb);
  6968. /* sni abort - abort when absent (ssl) */
  6969. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  6970. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  6971. test_wolfSSL_client_server(&client_cb, &server_cb);
  6972. /* sni abort - success when overwritten */
  6973. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  6974. 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;
  6975. test_wolfSSL_client_server(&client_cb, &server_cb);
  6976. /* sni abort - success when allowing mismatches */
  6977. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  6978. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  6979. test_wolfSSL_client_server(&client_cb, &server_cb);
  6980. }
  6981. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  6982. return 0;
  6983. }
  6984. static int test_wolfSSL_SNI_GetFromBuffer(void)
  6985. {
  6986. byte buff[] = { /* www.paypal.com */
  6987. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  6988. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  6989. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  6990. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  6991. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  6992. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  6993. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  6994. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  6995. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  6996. };
  6997. byte buff2[] = { /* api.textmate.org */
  6998. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  6999. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  7000. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  7001. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  7002. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  7003. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  7004. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  7005. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  7006. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  7007. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  7008. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  7009. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  7010. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  7011. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  7012. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  7013. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  7014. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  7015. };
  7016. byte buff3[] = { /* no sni extension */
  7017. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  7018. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  7019. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  7020. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  7021. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  7022. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  7023. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  7024. };
  7025. byte buff4[] = { /* last extension has zero size */
  7026. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  7027. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7028. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7029. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7030. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  7031. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  7032. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  7033. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  7034. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  7035. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  7036. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  7037. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  7038. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  7039. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  7040. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  7041. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  7042. 0x12, 0x00, 0x00
  7043. };
  7044. byte buff5[] = { /* SSL v2.0 client hello */
  7045. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  7046. /* dummy bytes bellow, just to pass size check */
  7047. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  7048. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  7049. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  7050. };
  7051. byte result[32] = {0};
  7052. word32 length = 32;
  7053. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  7054. 0, result, &length));
  7055. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  7056. 0, result, &length));
  7057. AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7058. 1, result, &length));
  7059. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7060. 0, result, &length));
  7061. buff[0] = 0x16;
  7062. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7063. 0, result, &length));
  7064. buff[1] = 0x03;
  7065. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  7066. sizeof(buff), 0, result, &length));
  7067. buff[2] = 0x03;
  7068. AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  7069. sizeof(buff), 0, result, &length));
  7070. buff[4] = 0x64;
  7071. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  7072. 0, result, &length));
  7073. result[length] = 0;
  7074. AssertStrEQ("www.paypal.com", (const char*) result);
  7075. length = 32;
  7076. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  7077. 0, result, &length));
  7078. result[length] = 0;
  7079. AssertStrEQ("api.textmate.org", (const char*) result);
  7080. /* SSL v2.0 tests */
  7081. AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  7082. sizeof(buff5), 0, result, &length));
  7083. buff5[2] = 0x02;
  7084. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7085. sizeof(buff5), 0, result, &length));
  7086. buff5[2] = 0x01; buff5[6] = 0x08;
  7087. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7088. sizeof(buff5), 0, result, &length));
  7089. buff5[6] = 0x09; buff5[8] = 0x01;
  7090. AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  7091. sizeof(buff5), 0, result, &length));
  7092. return 0;
  7093. }
  7094. #endif /* HAVE_SNI */
  7095. static int test_wolfSSL_UseSNI(void)
  7096. {
  7097. #ifdef HAVE_SNI
  7098. test_wolfSSL_UseSNI_params();
  7099. test_wolfSSL_UseSNI_connection();
  7100. test_wolfSSL_SNI_GetFromBuffer();
  7101. #endif
  7102. return 0;
  7103. }
  7104. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  7105. static int test_wolfSSL_UseTrustedCA(void)
  7106. {
  7107. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  7108. && !defined(NO_RSA)
  7109. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7110. WOLFSSL_CTX *ctx;
  7111. WOLFSSL *ssl;
  7112. byte id[20];
  7113. #ifndef NO_WOLFSSL_SERVER
  7114. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  7115. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7116. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7117. #else
  7118. AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  7119. #endif
  7120. AssertNotNull((ssl = wolfSSL_new(ctx)));
  7121. XMEMSET(id, 0, sizeof(id));
  7122. /* error cases */
  7123. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  7124. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7125. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  7126. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7127. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  7128. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7129. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  7130. #ifdef NO_SHA
  7131. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7132. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7133. #endif
  7134. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7135. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  7136. /* success cases */
  7137. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7138. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  7139. #ifndef NO_SHA
  7140. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7141. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  7142. #endif
  7143. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  7144. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  7145. wolfSSL_free(ssl);
  7146. wolfSSL_CTX_free(ctx);
  7147. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7148. #endif /* HAVE_TRUSTED_CA */
  7149. return 0;
  7150. }
  7151. static int test_wolfSSL_UseMaxFragment(void)
  7152. {
  7153. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  7154. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7155. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7156. #ifndef NO_WOLFSSL_SERVER
  7157. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7158. #else
  7159. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7160. #endif
  7161. WOLFSSL *ssl;
  7162. #ifdef OPENSSL_EXTRA
  7163. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  7164. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  7165. #else
  7166. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  7167. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  7168. #endif
  7169. #ifndef NO_WOLFSSL_SERVER
  7170. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7171. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7172. #endif
  7173. AssertNotNull(ctx);
  7174. ssl = wolfSSL_new(ctx);
  7175. AssertNotNull(ssl);
  7176. #ifdef OPENSSL_EXTRA
  7177. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  7178. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  7179. #else
  7180. UseMaxFragment = wolfSSL_UseMaxFragment;
  7181. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  7182. #endif
  7183. /* error cases */
  7184. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  7185. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  7186. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  7187. AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  7188. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  7189. AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  7190. /* success case */
  7191. #ifdef OPENSSL_EXTRA
  7192. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7193. #else
  7194. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  7195. #endif
  7196. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  7197. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  7198. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  7199. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  7200. #ifdef OPENSSL_EXTRA
  7201. AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7202. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7203. #else
  7204. AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  7205. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  7206. #endif
  7207. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  7208. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  7209. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  7210. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  7211. #ifdef OPENSSL_EXTRA
  7212. AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7213. #else
  7214. AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  7215. #endif
  7216. wolfSSL_free(ssl);
  7217. wolfSSL_CTX_free(ctx);
  7218. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7219. #endif
  7220. return 0;
  7221. }
  7222. static int test_wolfSSL_UseTruncatedHMAC(void)
  7223. {
  7224. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  7225. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  7226. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7227. #ifndef NO_WOLFSSL_SERVER
  7228. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  7229. #else
  7230. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7231. #endif
  7232. WOLFSSL *ssl;
  7233. AssertNotNull(ctx);
  7234. #ifndef NO_WOLFSSL_SERVER
  7235. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  7236. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  7237. #endif
  7238. ssl = wolfSSL_new(ctx);
  7239. AssertNotNull(ssl);
  7240. /* error cases */
  7241. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  7242. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  7243. /* success case */
  7244. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  7245. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  7246. wolfSSL_free(ssl);
  7247. wolfSSL_CTX_free(ctx);
  7248. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7249. #endif
  7250. return 0;
  7251. }
  7252. static int test_wolfSSL_UseSupportedCurve(void)
  7253. {
  7254. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
  7255. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7256. WOLFSSL *ssl = wolfSSL_new(ctx);
  7257. AssertNotNull(ctx);
  7258. AssertNotNull(ssl);
  7259. /* error cases */
  7260. AssertIntNE(WOLFSSL_SUCCESS,
  7261. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7262. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  7263. AssertIntNE(WOLFSSL_SUCCESS,
  7264. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  7265. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  7266. /* success case */
  7267. AssertIntEQ(WOLFSSL_SUCCESS,
  7268. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  7269. AssertIntEQ(WOLFSSL_SUCCESS,
  7270. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  7271. wolfSSL_free(ssl);
  7272. wolfSSL_CTX_free(ctx);
  7273. #endif
  7274. return 0;
  7275. }
  7276. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  7277. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  7278. {
  7279. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  7280. }
  7281. static void use_ALPN_all(WOLFSSL* ssl)
  7282. {
  7283. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7284. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7285. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7286. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7287. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7288. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7289. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7290. }
  7291. static void use_ALPN_all_continue(WOLFSSL* ssl)
  7292. {
  7293. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7294. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7295. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7296. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7297. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7298. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  7299. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7300. }
  7301. static void use_ALPN_one(WOLFSSL* ssl)
  7302. {
  7303. /* spdy/2 */
  7304. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7305. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7306. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7307. }
  7308. static void use_ALPN_unknown(WOLFSSL* ssl)
  7309. {
  7310. /* http/2.0 */
  7311. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7312. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7313. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7314. }
  7315. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  7316. {
  7317. /* http/2.0 */
  7318. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  7319. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  7320. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7321. }
  7322. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  7323. {
  7324. /* spdy/3 */
  7325. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7326. char *proto = NULL;
  7327. word16 protoSz = 0;
  7328. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7329. /* check value */
  7330. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  7331. if (proto) {
  7332. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  7333. }
  7334. }
  7335. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  7336. {
  7337. char *proto = NULL;
  7338. word16 protoSz = 0;
  7339. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  7340. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7341. /* check value */
  7342. AssertIntEQ(1, (0 == protoSz));
  7343. AssertIntEQ(1, (NULL == proto));
  7344. }
  7345. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  7346. {
  7347. /* http/1.1 */
  7348. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7349. char *proto;
  7350. word16 protoSz = 0;
  7351. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7352. /* check value */
  7353. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7354. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7355. }
  7356. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  7357. {
  7358. /* spdy/2 */
  7359. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7360. char *proto;
  7361. word16 protoSz = 0;
  7362. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  7363. /* check value */
  7364. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7365. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7366. }
  7367. static void verify_ALPN_client_list(WOLFSSL* ssl)
  7368. {
  7369. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  7370. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  7371. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  7372. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  7373. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7374. char *clist = NULL;
  7375. word16 clistSz = 0;
  7376. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  7377. &clistSz));
  7378. /* check value */
  7379. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  7380. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  7381. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  7382. }
  7383. static int test_wolfSSL_UseALPN_connection(void)
  7384. {
  7385. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7386. callback_functions client_cb;
  7387. callback_functions server_cb;
  7388. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7389. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7390. client_cb.method = wolfSSLv23_client_method;
  7391. server_cb.method = wolfSSLv23_server_method;
  7392. client_cb.devId = testDevId;
  7393. server_cb.devId = testDevId;
  7394. /* success case same list */
  7395. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7396. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  7397. test_wolfSSL_client_server(&client_cb, &server_cb);
  7398. /* success case only one for server */
  7399. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7400. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  7401. test_wolfSSL_client_server(&client_cb, &server_cb);
  7402. /* success case only one for client */
  7403. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  7404. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  7405. test_wolfSSL_client_server(&client_cb, &server_cb);
  7406. /* success case none for client */
  7407. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7408. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  7409. test_wolfSSL_client_server(&client_cb, &server_cb);
  7410. /* success case mismatch behavior but option 'continue' set */
  7411. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  7412. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  7413. test_wolfSSL_client_server(&client_cb, &server_cb);
  7414. /* success case read protocol send by client */
  7415. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7416. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  7417. test_wolfSSL_client_server(&client_cb, &server_cb);
  7418. /* mismatch behavior with same list
  7419. * the first and only this one must be taken */
  7420. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7421. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  7422. test_wolfSSL_client_server(&client_cb, &server_cb);
  7423. /* default mismatch behavior */
  7424. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  7425. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  7426. test_wolfSSL_client_server(&client_cb, &server_cb);
  7427. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7428. return 0;
  7429. }
  7430. static int test_wolfSSL_UseALPN_params(void)
  7431. {
  7432. #ifndef NO_WOLFSSL_CLIENT
  7433. /* "http/1.1" */
  7434. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7435. /* "spdy/1" */
  7436. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  7437. /* "spdy/2" */
  7438. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  7439. /* "spdy/3" */
  7440. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7441. char buff[256];
  7442. word32 idx;
  7443. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7444. WOLFSSL *ssl = wolfSSL_new(ctx);
  7445. AssertNotNull(ctx);
  7446. AssertNotNull(ssl);
  7447. /* error cases */
  7448. AssertIntNE(WOLFSSL_SUCCESS,
  7449. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  7450. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7451. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  7452. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7453. /* success case */
  7454. /* http1 only */
  7455. AssertIntEQ(WOLFSSL_SUCCESS,
  7456. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  7457. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7458. /* http1, spdy1 */
  7459. XMEMCPY(buff, http1, sizeof(http1));
  7460. idx = sizeof(http1);
  7461. buff[idx++] = ',';
  7462. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7463. idx += sizeof(spdy1);
  7464. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7465. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7466. /* http1, spdy2, spdy1 */
  7467. XMEMCPY(buff, http1, sizeof(http1));
  7468. idx = sizeof(http1);
  7469. buff[idx++] = ',';
  7470. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7471. idx += sizeof(spdy2);
  7472. buff[idx++] = ',';
  7473. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7474. idx += sizeof(spdy1);
  7475. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7476. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  7477. /* spdy3, http1, spdy2, spdy1 */
  7478. XMEMCPY(buff, spdy3, sizeof(spdy3));
  7479. idx = sizeof(spdy3);
  7480. buff[idx++] = ',';
  7481. XMEMCPY(buff+idx, http1, sizeof(http1));
  7482. idx += sizeof(http1);
  7483. buff[idx++] = ',';
  7484. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  7485. idx += sizeof(spdy2);
  7486. buff[idx++] = ',';
  7487. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  7488. idx += sizeof(spdy1);
  7489. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  7490. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  7491. wolfSSL_free(ssl);
  7492. wolfSSL_CTX_free(ctx);
  7493. #endif
  7494. return 0;
  7495. }
  7496. #endif /* HAVE_ALPN */
  7497. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7498. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  7499. {
  7500. unsigned char p[] = {
  7501. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7502. 6, 's', 'p', 'd', 'y', '/', '2',
  7503. 6, 's', 'p', 'd', 'y', '/', '1',
  7504. };
  7505. unsigned char p_len = sizeof(p);
  7506. int ret;
  7507. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  7508. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7509. AssertIntEQ(ret, 0);
  7510. #else
  7511. AssertIntEQ(ret, SSL_SUCCESS);
  7512. #endif
  7513. }
  7514. static void set_alpn_protos(SSL* ssl)
  7515. {
  7516. unsigned char p[] = {
  7517. 6, 's', 'p', 'd', 'y', '/', '3',
  7518. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  7519. 6, 's', 'p', 'd', 'y', '/', '2',
  7520. 6, 's', 'p', 'd', 'y', '/', '1',
  7521. };
  7522. unsigned char p_len = sizeof(p);
  7523. int ret;
  7524. ret = SSL_set_alpn_protos(ssl, p, p_len);
  7525. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  7526. AssertIntEQ(ret, 0);
  7527. #else
  7528. AssertIntEQ(ret, SSL_SUCCESS);
  7529. #endif
  7530. }
  7531. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  7532. {
  7533. /* "spdy/3" */
  7534. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  7535. const unsigned char *proto;
  7536. unsigned int protoSz = 0;
  7537. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7538. /* check value */
  7539. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7540. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7541. }
  7542. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  7543. {
  7544. /* "http/1.1" */
  7545. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  7546. const unsigned char *proto;
  7547. unsigned int protoSz = 0;
  7548. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  7549. /* check value */
  7550. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  7551. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  7552. }
  7553. static int test_wolfSSL_set_alpn_protos(void)
  7554. {
  7555. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  7556. callback_functions client_cb;
  7557. callback_functions server_cb;
  7558. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7559. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7560. client_cb.method = wolfSSLv23_client_method;
  7561. server_cb.method = wolfSSLv23_server_method;
  7562. client_cb.devId = testDevId;
  7563. server_cb.devId = testDevId;
  7564. /* use CTX_alpn_protos */
  7565. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  7566. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  7567. test_wolfSSL_client_server(&client_cb, &server_cb);
  7568. /* use set_alpn_protos */
  7569. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  7570. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  7571. test_wolfSSL_client_server(&client_cb, &server_cb);
  7572. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  7573. return 0;
  7574. }
  7575. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  7576. static int test_wolfSSL_UseALPN(void)
  7577. {
  7578. #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
  7579. defined(HAVE_IO_TESTS_DEPENDENCIES)
  7580. test_wolfSSL_UseALPN_connection();
  7581. test_wolfSSL_UseALPN_params();
  7582. #endif
  7583. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  7584. test_wolfSSL_set_alpn_protos();
  7585. #endif
  7586. return 0;
  7587. }
  7588. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  7589. {
  7590. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  7591. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7592. WOLFSSL *ssl = wolfSSL_new(ctx);
  7593. AssertNotNull(ctx);
  7594. AssertNotNull(ssl);
  7595. /* error cases */
  7596. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  7597. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  7598. /* success cases */
  7599. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  7600. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  7601. wolfSSL_free(ssl);
  7602. wolfSSL_CTX_free(ctx);
  7603. #endif
  7604. return 0;
  7605. }
  7606. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  7607. {
  7608. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  7609. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  7610. WOLFSSL *ssl = wolfSSL_new(ctx);
  7611. AssertNotNull(ctx);
  7612. AssertNotNull(ssl);
  7613. /* error cases */
  7614. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  7615. AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  7616. /* success cases */
  7617. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  7618. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  7619. wolfSSL_free(ssl);
  7620. wolfSSL_CTX_free(ctx);
  7621. #endif
  7622. return 0;
  7623. }
  7624. /*----------------------------------------------------------------------------*
  7625. | X509 Tests
  7626. *----------------------------------------------------------------------------*/
  7627. static int test_wolfSSL_X509_NAME_get_entry(void)
  7628. {
  7629. #if !defined(NO_CERTS) && !defined(NO_RSA)
  7630. #if defined(OPENSSL_ALL) || \
  7631. (defined(OPENSSL_EXTRA) && \
  7632. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  7633. printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
  7634. {
  7635. /* use openssl like name to test mapping */
  7636. X509_NAME_ENTRY* ne;
  7637. X509_NAME* name;
  7638. X509* x509;
  7639. #ifndef NO_FILESYSTEM
  7640. ASN1_STRING* asn;
  7641. char* subCN = NULL;
  7642. #endif
  7643. int idx;
  7644. ASN1_OBJECT *object = NULL;
  7645. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  7646. #ifndef NO_BIO
  7647. BIO* bio;
  7648. #endif
  7649. #endif
  7650. #ifndef NO_FILESYSTEM
  7651. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  7652. AssertNotNull(x509);
  7653. name = X509_get_subject_name(x509);
  7654. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7655. AssertIntGE(idx, 0);
  7656. ne = X509_NAME_get_entry(name, idx);
  7657. AssertNotNull(ne);
  7658. asn = X509_NAME_ENTRY_get_data(ne);
  7659. AssertNotNull(asn);
  7660. subCN = (char*)ASN1_STRING_data(asn);
  7661. AssertNotNull(subCN);
  7662. wolfSSL_FreeX509(x509);
  7663. #endif
  7664. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  7665. AssertNotNull(x509);
  7666. name = X509_get_subject_name(x509);
  7667. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  7668. AssertIntGE(idx, 0);
  7669. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX)
  7670. #ifndef NO_BIO
  7671. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7672. AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
  7673. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7674. AssertIntEQ(X509_NAME_print_ex_fp(stdout, name, 4,
  7675. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  7676. BIO_free(bio);
  7677. #endif
  7678. #endif
  7679. ne = X509_NAME_get_entry(name, idx);
  7680. AssertNotNull(ne);
  7681. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  7682. wolfSSL_FreeX509(x509);
  7683. }
  7684. printf(resultFmt, passed);
  7685. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  7686. #endif /* !NO_CERTS && !NO_RSA */
  7687. return 0;
  7688. }
  7689. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  7690. static int test_wolfSSL_PKCS12(void)
  7691. {
  7692. /* .p12 file is encrypted with DES3 */
  7693. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  7694. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  7695. * Password Key
  7696. */
  7697. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  7698. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  7699. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  7700. byte buf[6000];
  7701. char file[] = "./certs/test-servercert.p12";
  7702. char order[] = "./certs/ecc-rsa-server.p12";
  7703. #ifdef WC_RC2
  7704. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  7705. #endif
  7706. char pass[] = "a password";
  7707. const char goodPsw[] = "wolfSSL test";
  7708. const char badPsw[] = "bad";
  7709. #ifdef HAVE_ECC
  7710. WOLFSSL_X509_NAME* subject;
  7711. WOLFSSL_X509 *x509;
  7712. #endif
  7713. XFILE f;
  7714. int bytes, ret, goodPswLen, badPswLen;
  7715. WOLFSSL_BIO *bio;
  7716. WOLFSSL_EVP_PKEY *pkey;
  7717. WC_PKCS12 *pkcs12;
  7718. WC_PKCS12 *pkcs12_2;
  7719. WOLFSSL_X509 *cert;
  7720. WOLFSSL_X509 *tmp;
  7721. WOLF_STACK_OF(WOLFSSL_X509) *ca;
  7722. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7723. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7724. WOLFSSL_CTX *ctx;
  7725. WOLFSSL *ssl;
  7726. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  7727. #endif
  7728. printf(testingFmt, "wolfSSL_PKCS12()");
  7729. f = XFOPEN(file, "rb");
  7730. AssertTrue((f != XBADFILE));
  7731. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7732. XFCLOSE(f);
  7733. goodPswLen = (int)XSTRLEN(goodPsw);
  7734. badPswLen = (int)XSTRLEN(badPsw);
  7735. bio = BIO_new_mem_buf((void*)buf, bytes);
  7736. AssertNotNull(bio);
  7737. pkcs12 = d2i_PKCS12_bio(bio, NULL);
  7738. AssertNotNull(pkcs12);
  7739. PKCS12_free(pkcs12);
  7740. AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  7741. d2i_PKCS12_bio(bio, &pkcs12);
  7742. AssertNotNull(pkcs12);
  7743. BIO_free(bio);
  7744. /* check verify MAC directly */
  7745. ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
  7746. AssertIntEQ(ret, 1);
  7747. /* check verify MAC fail case directly */
  7748. ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
  7749. AssertIntEQ(ret, 0);
  7750. /* check verify MAC fail case */
  7751. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7752. AssertIntEQ(ret, 0);
  7753. AssertNull(pkey);
  7754. AssertNull(cert);
  7755. /* check parse with no extra certs kept */
  7756. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7757. AssertIntEQ(ret, 1);
  7758. AssertNotNull(pkey);
  7759. AssertNotNull(cert);
  7760. wolfSSL_EVP_PKEY_free(pkey);
  7761. wolfSSL_X509_free(cert);
  7762. /* check parse with extra certs kept */
  7763. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7764. AssertIntEQ(ret, 1);
  7765. AssertNotNull(pkey);
  7766. AssertNotNull(cert);
  7767. AssertNotNull(ca);
  7768. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7769. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7770. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  7771. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7772. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7773. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7774. #else
  7775. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7776. #endif
  7777. /* Copy stack structure */
  7778. AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
  7779. AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  7780. /* CTX now owns the tmp_ca stack structure */
  7781. tmp_ca = NULL;
  7782. AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  7783. AssertNotNull(tmp_ca);
  7784. AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  7785. /* Check that the main cert is also set */
  7786. AssertNotNull(SSL_CTX_get0_certificate(ctx));
  7787. AssertNotNull(ssl = SSL_new(ctx));
  7788. AssertNotNull(SSL_get_certificate(ssl));
  7789. SSL_free(ssl);
  7790. SSL_CTX_free(ctx);
  7791. #endif
  7792. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7793. /* should be 2 other certs on stack */
  7794. tmp = sk_X509_pop(ca);
  7795. AssertNotNull(tmp);
  7796. X509_free(tmp);
  7797. tmp = sk_X509_pop(ca);
  7798. AssertNotNull(tmp);
  7799. X509_free(tmp);
  7800. AssertNull(sk_X509_pop(ca));
  7801. EVP_PKEY_free(pkey);
  7802. X509_free(cert);
  7803. sk_X509_pop_free(ca, X509_free);
  7804. /* check PKCS12_create */
  7805. AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  7806. AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  7807. SSL_SUCCESS);
  7808. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7809. -1, -1, 100, -1, 0)));
  7810. EVP_PKEY_free(pkey);
  7811. X509_free(cert);
  7812. sk_X509_pop_free(ca, NULL);
  7813. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7814. SSL_SUCCESS);
  7815. PKCS12_free(pkcs12_2);
  7816. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  7817. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7818. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  7819. 2000, 1, 0)));
  7820. EVP_PKEY_free(pkey);
  7821. X509_free(cert);
  7822. sk_X509_pop_free(ca, NULL);
  7823. /* convert to DER then back and parse */
  7824. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  7825. AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  7826. PKCS12_free(pkcs12_2);
  7827. AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  7828. BIO_free(bio);
  7829. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7830. SSL_SUCCESS);
  7831. /* should be 2 other certs on stack */
  7832. tmp = sk_X509_pop(ca);
  7833. AssertNotNull(tmp);
  7834. X509_free(tmp);
  7835. tmp = sk_X509_pop(ca);
  7836. AssertNotNull(tmp);
  7837. X509_free(tmp);
  7838. AssertNull(sk_X509_pop(ca));
  7839. #ifndef NO_RC4
  7840. PKCS12_free(pkcs12_2);
  7841. AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  7842. NID_pbe_WithSHA1And128BitRC4,
  7843. NID_pbe_WithSHA1And128BitRC4,
  7844. 2000, 1, 0)));
  7845. EVP_PKEY_free(pkey);
  7846. X509_free(cert);
  7847. sk_X509_pop_free(ca, NULL);
  7848. AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  7849. SSL_SUCCESS);
  7850. #endif /* NO_RC4 */
  7851. EVP_PKEY_free(pkey);
  7852. X509_free(cert);
  7853. PKCS12_free(pkcs12);
  7854. PKCS12_free(pkcs12_2);
  7855. sk_X509_pop_free(ca, NULL);
  7856. #ifdef HAVE_ECC
  7857. /* test order of parsing */
  7858. f = XFOPEN(order, "rb");
  7859. AssertTrue(f != XBADFILE);
  7860. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7861. XFCLOSE(f);
  7862. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7863. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7864. AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  7865. WOLFSSL_SUCCESS);
  7866. /* check use of pkey after parse */
  7867. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  7868. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  7869. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  7870. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  7871. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  7872. #else
  7873. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  7874. #endif
  7875. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  7876. SSL_CTX_free(ctx);
  7877. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  7878. #endif
  7879. AssertNotNull(pkey);
  7880. AssertNotNull(cert);
  7881. AssertNotNull(ca);
  7882. /* compare subject lines of certificates */
  7883. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7884. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  7885. SSL_FILETYPE_PEM));
  7886. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7887. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7888. X509_free(x509);
  7889. /* test expected fail case */
  7890. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7891. SSL_FILETYPE_PEM));
  7892. AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7893. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7894. X509_free(x509);
  7895. X509_free(cert);
  7896. /* get subject line from ca stack */
  7897. AssertNotNull(cert = sk_X509_pop(ca));
  7898. AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  7899. /* compare subject from certificate in ca to expected */
  7900. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  7901. SSL_FILETYPE_PEM));
  7902. AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  7903. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  7904. EVP_PKEY_free(pkey);
  7905. X509_free(x509);
  7906. X509_free(cert);
  7907. BIO_free(bio);
  7908. PKCS12_free(pkcs12);
  7909. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  7910. /* test order of parsing */
  7911. f = XFOPEN(file, "rb");
  7912. AssertTrue(f != XBADFILE);
  7913. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7914. XFCLOSE(f);
  7915. /* check verify MAC fail case */
  7916. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7917. AssertIntEQ(ret, 0);
  7918. AssertNull(pkey);
  7919. AssertNull(cert);
  7920. /* check parse with no extra certs kept */
  7921. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7922. AssertIntEQ(ret, 1);
  7923. AssertNotNull(pkey);
  7924. AssertNotNull(cert);
  7925. wolfSSL_EVP_PKEY_free(pkey);
  7926. wolfSSL_X509_free(cert);
  7927. /* check parse with extra certs kept */
  7928. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7929. AssertIntEQ(ret, 1);
  7930. AssertNotNull(pkey);
  7931. AssertNotNull(cert);
  7932. AssertNotNull(ca);
  7933. wolfSSL_EVP_PKEY_free(pkey);
  7934. wolfSSL_X509_free(cert);
  7935. sk_X509_pop_free(ca, NULL);
  7936. PKCS12_free(pkcs12);
  7937. #endif /* HAVE_ECC */
  7938. #ifdef WC_RC2
  7939. /* test PKCS#12 with RC2 encryption */
  7940. f = XFOPEN(rc2p12, "rb");
  7941. AssertTrue(f != XBADFILE);
  7942. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  7943. XFCLOSE(f);
  7944. AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  7945. AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  7946. /* check verify MAC fail case */
  7947. ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
  7948. AssertIntEQ(ret, 0);
  7949. AssertNull(pkey);
  7950. AssertNull(cert);
  7951. /* check parse iwth not extra certs kept */
  7952. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
  7953. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7954. AssertNotNull(pkey);
  7955. AssertNotNull(cert);
  7956. /* check parse with extra certs kept */
  7957. ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
  7958. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  7959. AssertNotNull(pkey);
  7960. AssertNotNull(cert);
  7961. AssertNotNull(ca);
  7962. wolfSSL_EVP_PKEY_free(pkey);
  7963. wolfSSL_X509_free(cert);
  7964. sk_X509_pop_free(ca, NULL);
  7965. BIO_free(bio);
  7966. PKCS12_free(pkcs12);
  7967. #endif /* WC_RC2 */
  7968. /* Test i2d_PKCS12_bio */
  7969. f = XFOPEN(file, "rb");
  7970. AssertTrue((f != XBADFILE));
  7971. AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  7972. XFCLOSE(f);
  7973. bio = BIO_new(BIO_s_mem());
  7974. AssertNotNull(bio);
  7975. ret = i2d_PKCS12_bio(bio, pkcs12);
  7976. AssertIntEQ(ret, 1);
  7977. ret = i2d_PKCS12_bio(NULL, pkcs12);
  7978. AssertIntEQ(ret, 0);
  7979. ret = i2d_PKCS12_bio(bio, NULL);
  7980. AssertIntEQ(ret, 0);
  7981. PKCS12_free(pkcs12);
  7982. BIO_free(bio);
  7983. (void)order;
  7984. printf(resultFmt, passed);
  7985. #endif /* OPENSSL_EXTRA */
  7986. #endif /* HAVE_FIPS */
  7987. return 0;
  7988. }
  7989. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  7990. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  7991. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  7992. #define TEST_PKCS8_ENC
  7993. #endif
  7994. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  7995. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  7996. /* used to keep track if FailTestCallback was called */
  7997. static int failTestCallbackCalled = 0;
  7998. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  7999. {
  8000. (void)passwd;
  8001. (void)sz;
  8002. (void)rw;
  8003. (void)userdata;
  8004. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  8005. failTestCallbackCalled = 1;
  8006. return -1;
  8007. }
  8008. #endif
  8009. static int test_wolfSSL_no_password_cb(void)
  8010. {
  8011. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  8012. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  8013. WOLFSSL_CTX* ctx;
  8014. byte buff[FOURK_BUF];
  8015. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8016. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8017. XFILE f;
  8018. int bytes;
  8019. printf(testingFmt, "test_wolfSSL_no_password_cb()");
  8020. #ifndef NO_WOLFSSL_CLIENT
  8021. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  8022. #else
  8023. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  8024. #endif
  8025. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  8026. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  8027. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8028. XFCLOSE(f);
  8029. AssertIntLE(bytes, sizeof(buff));
  8030. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8031. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8032. AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  8033. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8034. XFCLOSE(f);
  8035. AssertIntLE(bytes, sizeof(buff));
  8036. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8037. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8038. wolfSSL_CTX_free(ctx);
  8039. if (failTestCallbackCalled != 0) {
  8040. Fail(("Password callback should not be called by default"),
  8041. ("Password callback was called without attempting "
  8042. "to first decipher private key without password."));
  8043. }
  8044. printf(resultFmt, passed);
  8045. #endif
  8046. return 0;
  8047. }
  8048. #ifdef TEST_PKCS8_ENC
  8049. /* for PKCS8 test case */
  8050. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  8051. {
  8052. int flag = 0;
  8053. (void)rw;
  8054. if (userdata != NULL) {
  8055. flag = *((int*)userdata); /* user set data */
  8056. }
  8057. switch (flag) {
  8058. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  8059. * can be anything the user wishes to be passed to the callback
  8060. * associated with the WOLFSSL_CTX */
  8061. XSTRNCPY(passwd, "yassl123", sz);
  8062. return 8;
  8063. default:
  8064. return BAD_FUNC_ARG;
  8065. }
  8066. }
  8067. #endif /* TEST_PKCS8_ENC */
  8068. /* Testing functions dealing with PKCS8 */
  8069. static int test_wolfSSL_PKCS8(void)
  8070. {
  8071. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
  8072. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8073. byte buff[FOURK_BUF];
  8074. byte der[FOURK_BUF];
  8075. #ifndef NO_RSA
  8076. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  8077. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  8078. #endif
  8079. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  8080. #ifdef HAVE_ECC
  8081. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  8082. #endif
  8083. XFILE f;
  8084. int bytes;
  8085. WOLFSSL_CTX* ctx;
  8086. #if defined(HAVE_ECC) && !defined(NO_CODING)
  8087. int ret;
  8088. ecc_key key;
  8089. word32 x = 0;
  8090. #endif
  8091. #ifdef TEST_PKCS8_ENC
  8092. #if !defined(NO_RSA) && !defined(NO_SHA)
  8093. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  8094. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  8095. #endif
  8096. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8097. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  8098. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  8099. #endif
  8100. int flag;
  8101. #endif
  8102. (void)der;
  8103. printf(testingFmt, "wolfSSL_PKCS8()");
  8104. #ifndef NO_WOLFSSL_CLIENT
  8105. #ifndef WOLFSSL_NO_TLS12
  8106. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  8107. #else
  8108. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  8109. #endif
  8110. #else
  8111. #ifndef WOLFSSL_NO_TLS12
  8112. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  8113. #else
  8114. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  8115. #endif
  8116. #endif
  8117. #ifdef TEST_PKCS8_ENC
  8118. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  8119. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  8120. flag = 1; /* used by password callback as return code */
  8121. #if !defined(NO_RSA) && !defined(NO_SHA)
  8122. /* test loading PEM PKCS8 encrypted file */
  8123. f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
  8124. AssertTrue((f != XBADFILE));
  8125. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8126. XFCLOSE(f);
  8127. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8128. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8129. /* this next case should fail because of password callback return code */
  8130. flag = 0; /* used by password callback as return code */
  8131. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8132. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8133. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8134. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8135. "yassl123"), 0);
  8136. /* test that error value is returned with a bad password */
  8137. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8138. "bad"), 0);
  8139. /* test loading PEM PKCS8 encrypted file */
  8140. f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
  8141. AssertTrue((f != XBADFILE));
  8142. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8143. XFCLOSE(f);
  8144. flag = 1; /* used by password callback as return code */
  8145. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8146. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8147. /* this next case should fail because of password callback return code */
  8148. flag = 0; /* used by password callback as return code */
  8149. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8150. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8151. #endif /* !NO_RSA && !NO_SHA */
  8152. #if defined(HAVE_ECC) && !defined(NO_SHA)
  8153. /* test loading PEM PKCS8 encrypted ECC Key file */
  8154. f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
  8155. AssertTrue((f != XBADFILE));
  8156. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8157. XFCLOSE(f);
  8158. flag = 1; /* used by password callback as return code */
  8159. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8160. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8161. /* this next case should fail because of password callback return code */
  8162. flag = 0; /* used by password callback as return code */
  8163. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8164. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8165. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  8166. AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8167. "yassl123"), 0);
  8168. /* test that error value is returned with a bad password */
  8169. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  8170. "bad"), 0);
  8171. /* test loading DER PKCS8 encrypted ECC Key file */
  8172. f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
  8173. AssertTrue((f != XBADFILE));
  8174. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8175. XFCLOSE(f);
  8176. flag = 1; /* used by password callback as return code */
  8177. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8178. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8179. /* this next case should fail because of password callback return code */
  8180. flag = 0; /* used by password callback as return code */
  8181. AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8182. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8183. /* leave flag as "okay" */
  8184. flag = 1;
  8185. #endif /* HAVE_ECC && !NO_SHA */
  8186. #endif /* TEST_PKCS8_ENC */
  8187. #ifndef NO_RSA
  8188. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  8189. f = XFOPEN(serverKeyPkcs8DerFile, "rb");
  8190. AssertTrue((f != XBADFILE));
  8191. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8192. XFCLOSE(f);
  8193. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8194. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8195. /* test loading PEM PKCS8 private key file (not encrypted) */
  8196. f = XFOPEN(serverKeyPkcs8PemFile, "rb");
  8197. AssertTrue((f != XBADFILE));
  8198. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8199. XFCLOSE(f);
  8200. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8201. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8202. #endif /* !NO_RSA */
  8203. /* Test PKCS8 PEM ECC key no crypt */
  8204. f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
  8205. AssertTrue((f != XBADFILE));
  8206. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8207. XFCLOSE(f);
  8208. #ifdef HAVE_ECC
  8209. /* Test PKCS8 PEM ECC key no crypt */
  8210. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8211. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  8212. #ifndef NO_CODING
  8213. /* decrypt PKCS8 PEM to key in DER format */
  8214. AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  8215. (word32)sizeof(der), NULL)), 0);
  8216. ret = wc_ecc_init(&key);
  8217. if (ret == 0) {
  8218. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  8219. wc_ecc_free(&key);
  8220. }
  8221. AssertIntEQ(ret, 0);
  8222. #endif
  8223. /* Test PKCS8 DER ECC key no crypt */
  8224. f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
  8225. AssertTrue((f != XBADFILE));
  8226. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  8227. XFCLOSE(f);
  8228. /* Test using a PKCS8 ECC PEM */
  8229. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  8230. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8231. #else
  8232. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  8233. AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  8234. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  8235. #endif /* HAVE_ECC */
  8236. wolfSSL_CTX_free(ctx);
  8237. printf(resultFmt, passed);
  8238. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8239. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  8240. return 0;
  8241. }
  8242. static int test_wolfSSL_PKCS8_ED25519(void)
  8243. {
  8244. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8245. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  8246. defined(HAVE_ED25519_KEY_IMPORT)
  8247. const byte encPrivKey[] = \
  8248. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8249. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  8250. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  8251. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  8252. "2L9W4v7swXkV+X+a1ww=\n"
  8253. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8254. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8255. byte der[FOURK_BUF];
  8256. WOLFSSL_CTX* ctx;
  8257. int bytes;
  8258. XMEMSET(der, 0, sizeof(der));
  8259. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8260. (word32)sizeof(der), password)), 0);
  8261. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8262. #ifndef NO_WOLFSSL_SERVER
  8263. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8264. #else
  8265. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8266. #endif
  8267. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8268. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8269. wolfSSL_CTX_free(ctx);
  8270. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8271. #endif
  8272. return 0;
  8273. }
  8274. static int test_wolfSSL_PKCS8_ED448(void)
  8275. {
  8276. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  8277. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  8278. defined(HAVE_ED448_KEY_IMPORT)
  8279. const byte encPrivKey[] = \
  8280. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  8281. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  8282. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  8283. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  8284. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  8285. "-----END ENCRYPTED PRIVATE KEY-----\n";
  8286. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  8287. byte der[FOURK_BUF];
  8288. WOLFSSL_CTX* ctx;
  8289. int bytes;
  8290. XMEMSET(der, 0, sizeof(der));
  8291. AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  8292. (word32)sizeof(der), password)), 0);
  8293. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  8294. #ifndef NO_WOLFSSL_SERVER
  8295. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  8296. #else
  8297. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  8298. #endif
  8299. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  8300. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8301. wolfSSL_CTX_free(ctx);
  8302. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  8303. #endif
  8304. return 0;
  8305. }
  8306. /* Testing functions dealing with PKCS5 */
  8307. static int test_wolfSSL_PKCS5(void)
  8308. {
  8309. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  8310. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  8311. const char* passwd = "myfipsPa$$W0rd";
  8312. #else
  8313. const char *passwd = "pass1234";
  8314. #endif
  8315. const unsigned char *salt = (unsigned char *)"salt1234";
  8316. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  8317. DYNAMIC_TYPE_TMP_BUFFER);
  8318. int ret = 0;
  8319. AssertNotNull(out);
  8320. ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  8321. (int)XSTRLEN((const char *) salt), 10,
  8322. WC_SHA_DIGEST_SIZE,out);
  8323. AssertIntEQ(ret, SSL_SUCCESS);
  8324. #ifdef WOLFSSL_SHA512
  8325. ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  8326. (int)XSTRLEN((const char *) salt), 10,
  8327. wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
  8328. AssertIntEQ(ret, SSL_SUCCESS);
  8329. #endif
  8330. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8331. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  8332. return 0;
  8333. }
  8334. /* test parsing URI from certificate */
  8335. static int test_wolfSSL_URI(void)
  8336. {
  8337. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8338. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  8339. defined(OPENSSL_EXTRA))
  8340. WOLFSSL_X509* x509;
  8341. const char uri[] = "./certs/client-uri-cert.pem";
  8342. const char badUri[] = "./certs/client-relative-uri.pem";
  8343. printf(testingFmt, "wolfSSL URI parse");
  8344. x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
  8345. AssertNotNull(x509);
  8346. wolfSSL_FreeX509(x509);
  8347. x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
  8348. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  8349. && !defined(WOLFSSL_FPKI)
  8350. AssertNull(x509);
  8351. #else
  8352. AssertNotNull(x509);
  8353. wolfSSL_FreeX509(x509);
  8354. #endif
  8355. printf(resultFmt, passed);
  8356. #endif
  8357. return 0;
  8358. }
  8359. static int test_wolfSSL_TBS(void)
  8360. {
  8361. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8362. && defined(OPENSSL_EXTRA)
  8363. WOLFSSL_X509* x509;
  8364. const unsigned char* tbs;
  8365. int tbsSz;
  8366. printf(testingFmt, "wolfSSL TBS");
  8367. AssertNotNull(x509 =
  8368. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8369. AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  8370. AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  8371. AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  8372. AssertIntEQ(tbsSz, 1003);
  8373. wolfSSL_FreeX509(x509);
  8374. printf(resultFmt, passed);
  8375. #endif
  8376. return 0;
  8377. }
  8378. static int test_wolfSSL_X509_verify(void)
  8379. {
  8380. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  8381. && defined(OPENSSL_EXTRA)
  8382. WOLFSSL_X509* ca;
  8383. WOLFSSL_X509* serv;
  8384. WOLFSSL_EVP_PKEY* pkey;
  8385. unsigned char buf[2048];
  8386. const unsigned char* pt = NULL;
  8387. int bufSz;
  8388. printf(testingFmt, "wolfSSL X509 verify");
  8389. AssertNotNull(ca =
  8390. wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
  8391. AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  8392. WOLFSSL_SUCCESS);
  8393. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  8394. WOLFSSL_SUCCESS);
  8395. AssertIntEQ(bufSz, 294);
  8396. bufSz = 2048;
  8397. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  8398. WOLFSSL_SUCCESS);
  8399. AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  8400. AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  8401. AssertNotNull(serv =
  8402. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
  8403. /* success case */
  8404. pt = buf;
  8405. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8406. AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  8407. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  8408. wolfSSL_EVP_PKEY_free(pkey);
  8409. /* fail case */
  8410. bufSz = 2048;
  8411. AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  8412. WOLFSSL_SUCCESS);
  8413. pt = buf;
  8414. AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  8415. AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  8416. AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  8417. AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  8418. wolfSSL_EVP_PKEY_free(pkey);
  8419. wolfSSL_FreeX509(ca);
  8420. wolfSSL_FreeX509(serv);
  8421. printf(resultFmt, passed);
  8422. #endif
  8423. return 0;
  8424. }
  8425. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8426. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8427. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8428. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8429. /* create certificate with version 2 */
  8430. static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
  8431. {
  8432. WOLFSSL_X509 *x509, *x509v2;
  8433. WOLFSSL_EVP_PKEY *priv, *pub;
  8434. unsigned char *der = NULL, *key = NULL, *pt;
  8435. char *header, *name;
  8436. int derSz;
  8437. long keySz;
  8438. XFILE fp;
  8439. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  8440. time_t t;
  8441. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  8442. WOLFSSL_FILETYPE_PEM));
  8443. fp = XFOPEN(cliKeyFile, "rb");
  8444. AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  8445. WOLFSSL_SUCCESS);
  8446. XFCLOSE(fp);
  8447. pt = key;
  8448. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  8449. (const unsigned char**)&pt, keySz));
  8450. /* create the version 2 certificate */
  8451. AssertNotNull(x509v2 = X509_new());
  8452. AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  8453. AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  8454. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  8455. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  8456. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  8457. AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  8458. AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  8459. t = time(NULL);
  8460. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  8461. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  8462. AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  8463. AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  8464. AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  8465. derSz = wolfSSL_i2d_X509(x509v2, &der);
  8466. AssertIntGT(derSz, 0);
  8467. AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  8468. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  8469. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
  8470. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8471. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8472. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8473. wolfSSL_X509_free(x509);
  8474. wolfSSL_X509_free(x509v2);
  8475. wolfSSL_EVP_PKEY_free(priv);
  8476. wolfSSL_EVP_PKEY_free(pub);
  8477. wolfSSL_ASN1_TIME_free(notBefore);
  8478. wolfSSL_ASN1_TIME_free(notAfter);
  8479. }
  8480. /* override certificate version error */
  8481. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  8482. {
  8483. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8484. AssertIntEQ(store->error, ASN_VERSION_E);
  8485. #else
  8486. AssertIntEQ(store->error, 0);
  8487. #endif
  8488. AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  8489. (void)preverify;
  8490. return 1;
  8491. }
  8492. /* set verify callback that will override bad certificate version */
  8493. static void test_set_override_x509(WOLFSSL_CTX* ctx)
  8494. {
  8495. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  8496. }
  8497. #endif
  8498. static int test_wolfSSL_X509_TLS_version(void)
  8499. {
  8500. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  8501. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
  8502. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  8503. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  8504. tcp_ready ready;
  8505. func_args server_args;
  8506. func_args client_args;
  8507. THREAD_TYPE serverThread;
  8508. callback_functions func_cb_client;
  8509. callback_functions func_cb_server;
  8510. printf(testingFmt, "test_wolfSSL_X509_TLS_version");
  8511. /* test server rejects a client certificate that is not version 3 */
  8512. #ifdef WOLFSSL_TIRTOS
  8513. fdOpenSession(Task_self());
  8514. #endif
  8515. XMEMSET(&server_args, 0, sizeof(func_args));
  8516. XMEMSET(&client_args, 0, sizeof(func_args));
  8517. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8518. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8519. StartTCP();
  8520. InitTcpReady(&ready);
  8521. #if defined(USE_WINDOWS_API)
  8522. /* use RNG to get random port if using windows */
  8523. ready.port = GetRandomPort();
  8524. #endif
  8525. server_args.signal = &ready;
  8526. client_args.signal = &ready;
  8527. server_args.return_code = TEST_FAIL;
  8528. client_args.return_code = TEST_FAIL;
  8529. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8530. #ifndef WOLFSSL_NO_TLS12
  8531. func_cb_client.method = wolfTLSv1_2_client_method;
  8532. #else
  8533. func_cb_client.method = wolfTLSv1_3_client_method;
  8534. #endif
  8535. client_args.callbacks = &func_cb_client;
  8536. #ifndef WOLFSSL_NO_TLS12
  8537. func_cb_server.method = wolfTLSv1_2_server_method;
  8538. #else
  8539. func_cb_server.method = wolfTLSv1_3_server_method;
  8540. #endif
  8541. server_args.callbacks = &func_cb_server;
  8542. start_thread(test_server_nofail, &server_args, &serverThread);
  8543. wait_tcp_ready(&server_args);
  8544. test_client_nofail(&client_args, NULL);
  8545. join_thread(serverThread);
  8546. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  8547. AssertIntEQ(client_args.return_code, TEST_FAIL);
  8548. AssertIntEQ(server_args.return_code, TEST_FAIL);
  8549. #else
  8550. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8551. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8552. #endif
  8553. FreeTcpReady(&ready);
  8554. #ifdef WOLFSSL_TIRTOS
  8555. fdCloseSession(Task_self());
  8556. #endif
  8557. /* Now re run but override the bad X509 version */
  8558. #ifdef WOLFSSL_TIRTOS
  8559. fdOpenSession(Task_self());
  8560. #endif
  8561. XMEMSET(&server_args, 0, sizeof(func_args));
  8562. XMEMSET(&client_args, 0, sizeof(func_args));
  8563. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  8564. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  8565. StartTCP();
  8566. InitTcpReady(&ready);
  8567. #if defined(USE_WINDOWS_API)
  8568. /* use RNG to get random port if using windows */
  8569. ready.port = GetRandomPort();
  8570. #endif
  8571. server_args.signal = &ready;
  8572. client_args.signal = &ready;
  8573. server_args.return_code = TEST_FAIL;
  8574. client_args.return_code = TEST_FAIL;
  8575. func_cb_client.ctx_ready = &test_set_x509_badversion;
  8576. func_cb_server.ctx_ready = &test_set_override_x509;
  8577. #ifndef WOLFSSL_NO_TLS12
  8578. func_cb_client.method = wolfTLSv1_2_client_method;
  8579. #else
  8580. func_cb_client.method = wolfTLSv1_3_client_method;
  8581. #endif
  8582. client_args.callbacks = &func_cb_client;
  8583. #ifndef WOLFSSL_NO_TLS12
  8584. func_cb_server.method = wolfTLSv1_2_server_method;
  8585. #else
  8586. func_cb_server.method = wolfTLSv1_3_server_method;
  8587. #endif
  8588. server_args.callbacks = &func_cb_server;
  8589. start_thread(test_server_nofail, &server_args, &serverThread);
  8590. wait_tcp_ready(&server_args);
  8591. test_client_nofail(&client_args, NULL);
  8592. join_thread(serverThread);
  8593. AssertIntEQ(client_args.return_code, TEST_SUCCESS);
  8594. AssertIntEQ(server_args.return_code, TEST_SUCCESS);
  8595. FreeTcpReady(&ready);
  8596. #ifdef WOLFSSL_TIRTOS
  8597. fdCloseSession(Task_self());
  8598. #endif
  8599. printf(resultFmt, passed);
  8600. #endif
  8601. return 0;
  8602. }
  8603. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  8604. * version allowed.
  8605. * POST: 1 on success.
  8606. */
  8607. static int test_wolfSSL_CTX_SetMinVersion(void)
  8608. {
  8609. int failFlag = WOLFSSL_SUCCESS;
  8610. #ifndef NO_WOLFSSL_CLIENT
  8611. WOLFSSL_CTX* ctx;
  8612. int itr;
  8613. #ifndef NO_OLD_TLS
  8614. const int versions[] = {
  8615. #ifdef WOLFSSL_ALLOW_TLSV10
  8616. WOLFSSL_TLSV1,
  8617. #endif
  8618. WOLFSSL_TLSV1_1,
  8619. WOLFSSL_TLSV1_2 };
  8620. #elif !defined(WOLFSSL_NO_TLS12)
  8621. const int versions[] = { WOLFSSL_TLSV1_2 };
  8622. #elif defined(WOLFSSL_TLS13)
  8623. const int versions[] = { WOLFSSL_TLSV1_3 };
  8624. #else
  8625. const int versions[0];
  8626. #endif
  8627. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  8628. printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
  8629. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
  8630. if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
  8631. failFlag = WOLFSSL_FAILURE;
  8632. }
  8633. }
  8634. printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
  8635. wolfSSL_CTX_free(ctx);
  8636. #endif
  8637. if (failFlag == WOLFSSL_SUCCESS) {
  8638. failFlag = 0;
  8639. }
  8640. return failFlag;
  8641. } /* END test_wolfSSL_CTX_SetMinVersion */
  8642. /*----------------------------------------------------------------------------*
  8643. | OCSP Stapling
  8644. *----------------------------------------------------------------------------*/
  8645. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  8646. * need to contact the CA, lowering the cost of cert revocation checking.
  8647. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  8648. * POST: 1 returned for success.
  8649. */
  8650. static int test_wolfSSL_UseOCSPStapling(void)
  8651. {
  8652. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  8653. !defined(NO_WOLFSSL_CLIENT)
  8654. int ret;
  8655. WOLFSSL_CTX* ctx;
  8656. WOLFSSL* ssl;
  8657. #ifndef NO_WOLFSSL_CLIENT
  8658. #ifndef WOLFSSL_NO_TLS12
  8659. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8660. #else
  8661. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8662. #endif
  8663. #else
  8664. #ifndef WOLFSSL_NO_TLS12
  8665. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8666. #else
  8667. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8668. #endif
  8669. #endif
  8670. ssl = wolfSSL_new(ctx);
  8671. printf(testingFmt, "wolfSSL_UseOCSPStapling()");
  8672. ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  8673. WOLFSSL_CSR2_OCSP_USE_NONCE);
  8674. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  8675. wolfSSL_free(ssl);
  8676. wolfSSL_CTX_free(ctx);
  8677. if (ret == WOLFSSL_SUCCESS) {
  8678. ret = 0;
  8679. }
  8680. return ret;
  8681. #else
  8682. return 0;
  8683. #endif
  8684. } /*END test_wolfSSL_UseOCSPStapling */
  8685. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  8686. * stapling eliminates the need to contact the CA and lowers cert revocation
  8687. * check.
  8688. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  8689. */
  8690. static int test_wolfSSL_UseOCSPStaplingV2(void)
  8691. {
  8692. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  8693. !defined(NO_WOLFSSL_CLIENT)
  8694. int ret;
  8695. WOLFSSL_CTX* ctx;
  8696. WOLFSSL* ssl;
  8697. #ifndef NO_WOLFSSL_CLIENT
  8698. #ifndef WOLFSSL_NO_TLS12
  8699. ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  8700. #else
  8701. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  8702. #endif
  8703. #else
  8704. #ifndef WOLFSSL_NO_TLS12
  8705. ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  8706. #else
  8707. ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  8708. #endif
  8709. #endif
  8710. ssl = wolfSSL_new(ctx);
  8711. printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
  8712. ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  8713. WOLFSSL_CSR2_OCSP_USE_NONCE );
  8714. printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
  8715. wolfSSL_free(ssl);
  8716. wolfSSL_CTX_free(ctx);
  8717. if (ret == WOLFSSL_SUCCESS) {
  8718. ret = 0;
  8719. }
  8720. return ret;
  8721. #else
  8722. return 0;
  8723. #endif
  8724. } /*END test_wolfSSL_UseOCSPStaplingV2*/
  8725. /*----------------------------------------------------------------------------*
  8726. | Multicast Tests
  8727. *----------------------------------------------------------------------------*/
  8728. static int test_wolfSSL_mcast(void)
  8729. {
  8730. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  8731. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  8732. WOLFSSL_CTX* ctx;
  8733. WOLFSSL* ssl;
  8734. int result;
  8735. byte preMasterSecret[512];
  8736. byte clientRandom[32];
  8737. byte serverRandom[32];
  8738. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  8739. byte buf[256];
  8740. word16 newId;
  8741. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
  8742. AssertNotNull(ctx);
  8743. result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
  8744. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8745. ssl = wolfSSL_new(ctx);
  8746. AssertNotNull(ssl);
  8747. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  8748. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  8749. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  8750. result = wolfSSL_set_secret(ssl, 23,
  8751. preMasterSecret, sizeof(preMasterSecret),
  8752. clientRandom, serverRandom, suite);
  8753. AssertIntEQ(result, WOLFSSL_SUCCESS);
  8754. result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
  8755. AssertIntLE(result, 0);
  8756. AssertIntLE(newId, 100);
  8757. wolfSSL_free(ssl);
  8758. wolfSSL_CTX_free(ctx);
  8759. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
  8760. return 0;
  8761. }
  8762. /*----------------------------------------------------------------------------*
  8763. | Wolfcrypt
  8764. *----------------------------------------------------------------------------*/
  8765. /*
  8766. * Unit test for the wc_InitBlake2b()
  8767. */
  8768. static int test_wc_InitBlake2b(void)
  8769. {
  8770. int ret = 0;
  8771. #ifdef HAVE_BLAKE2
  8772. Blake2b blake;
  8773. printf(testingFmt, "wc_InitBlake2B()");
  8774. /* Test good arg. */
  8775. ret = wc_InitBlake2b(&blake, 64);
  8776. if (ret != 0) {
  8777. ret = WOLFSSL_FATAL_ERROR;
  8778. }
  8779. /* Test bad arg. */
  8780. if (!ret) {
  8781. ret = wc_InitBlake2b(NULL, 64);
  8782. if (ret == 0) {
  8783. ret = WOLFSSL_FATAL_ERROR;
  8784. } else {
  8785. ret = 0;
  8786. }
  8787. }
  8788. if (!ret) {
  8789. ret = wc_InitBlake2b(NULL, 128);
  8790. if (ret == 0) {
  8791. ret = WOLFSSL_FATAL_ERROR;
  8792. } else {
  8793. ret = 0;
  8794. }
  8795. }
  8796. if (!ret) {
  8797. ret = wc_InitBlake2b(&blake, 128);
  8798. if (ret == 0) {
  8799. ret = WOLFSSL_FATAL_ERROR;
  8800. } else {
  8801. ret = 0;
  8802. }
  8803. }
  8804. if (!ret) {
  8805. ret = wc_InitBlake2b(NULL, 0);
  8806. if (ret == 0) {
  8807. ret = WOLFSSL_FATAL_ERROR;
  8808. } else {
  8809. ret = 0;
  8810. }
  8811. }
  8812. if (!ret) {
  8813. ret = wc_InitBlake2b(&blake, 0);
  8814. if (ret == 0) {
  8815. ret = WOLFSSL_FATAL_ERROR;
  8816. } else {
  8817. ret = 0;
  8818. }
  8819. }
  8820. printf(resultFmt, ret == 0 ? passed : failed);
  8821. #endif
  8822. return ret;
  8823. } /*END test_wc_InitBlake2b*/
  8824. /*
  8825. * Unit test for the wc_InitBlake2b_WithKey()
  8826. */
  8827. static int test_wc_InitBlake2b_WithKey(void)
  8828. {
  8829. int ret = 0;
  8830. #ifdef HAVE_BLAKE2
  8831. Blake2b blake;
  8832. word32 digestSz = BLAKE2B_KEYBYTES;
  8833. byte key[BLAKE2B_KEYBYTES];
  8834. word32 keylen = BLAKE2B_KEYBYTES;
  8835. printf(testingFmt, "wc_InitBlake2b_WithKey()");
  8836. /* Test good arg. */
  8837. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
  8838. if (ret != 0) {
  8839. ret = WOLFSSL_FATAL_ERROR;
  8840. }
  8841. /* Test bad args. */
  8842. if (ret == 0) {
  8843. ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
  8844. if (ret == BAD_FUNC_ARG) {
  8845. ret = 0;
  8846. }
  8847. }
  8848. if (ret == 0) {
  8849. ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
  8850. if (ret == BAD_FUNC_ARG) {
  8851. ret = 0;
  8852. }
  8853. }
  8854. if (ret == 0) {
  8855. ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
  8856. }
  8857. printf(resultFmt, ret == 0 ? passed : failed);
  8858. #endif
  8859. return ret;
  8860. } /*END wc_InitBlake2b_WithKey*/
  8861. /*
  8862. * Unit test for the wc_InitBlake2s_WithKey()
  8863. */
  8864. static int test_wc_InitBlake2s_WithKey(void)
  8865. {
  8866. int ret = 0;
  8867. #ifdef HAVE_BLAKE2S
  8868. Blake2s blake;
  8869. word32 digestSz = BLAKE2S_KEYBYTES;
  8870. byte *key = (byte*)"01234567890123456789012345678901";
  8871. word32 keylen = BLAKE2S_KEYBYTES;
  8872. printf(testingFmt, "wc_InitBlake2s_WithKey()");
  8873. /* Test good arg. */
  8874. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
  8875. if (ret != 0) {
  8876. ret = WOLFSSL_FATAL_ERROR;
  8877. }
  8878. /* Test bad args. */
  8879. if (ret == 0) {
  8880. ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
  8881. if (ret == BAD_FUNC_ARG) {
  8882. ret = 0;
  8883. }
  8884. }
  8885. if (ret == 0) {
  8886. ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
  8887. if (ret == BAD_FUNC_ARG) {
  8888. ret = 0;
  8889. }
  8890. }
  8891. if (ret == 0) {
  8892. ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
  8893. }
  8894. printf(resultFmt, ret == 0 ? passed : failed);
  8895. #endif
  8896. return ret;
  8897. } /*END wc_InitBlake2s_WithKey*/
  8898. /*
  8899. * Unit test for the wc_InitMd5()
  8900. */
  8901. static int test_wc_InitMd5(void)
  8902. {
  8903. int flag = 0;
  8904. #ifndef NO_MD5
  8905. wc_Md5 md5;
  8906. int ret;
  8907. printf(testingFmt, "wc_InitMd5()");
  8908. /* Test good arg. */
  8909. ret = wc_InitMd5(&md5);
  8910. if (ret != 0) {
  8911. flag = WOLFSSL_FATAL_ERROR;
  8912. }
  8913. /* Test bad arg. */
  8914. if (!flag) {
  8915. ret = wc_InitMd5(NULL);
  8916. if (ret != BAD_FUNC_ARG) {
  8917. flag = WOLFSSL_FATAL_ERROR;
  8918. }
  8919. }
  8920. wc_Md5Free(&md5);
  8921. printf(resultFmt, flag == 0 ? passed : failed);
  8922. #endif
  8923. return flag;
  8924. } /* END test_wc_InitMd5 */
  8925. /*
  8926. * Testing wc_UpdateMd5()
  8927. */
  8928. static int test_wc_Md5Update(void)
  8929. {
  8930. int flag = 0;
  8931. #ifndef NO_MD5
  8932. wc_Md5 md5;
  8933. byte hash[WC_MD5_DIGEST_SIZE];
  8934. testVector a, b, c;
  8935. int ret;
  8936. ret = wc_InitMd5(&md5);
  8937. if (ret != 0) {
  8938. flag = ret;
  8939. }
  8940. printf(testingFmt, "wc_Md5Update()");
  8941. /* Input */
  8942. if (!flag) {
  8943. a.input = "a";
  8944. a.inLen = XSTRLEN(a.input);
  8945. ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
  8946. if (ret != 0) {
  8947. flag = ret;
  8948. }
  8949. }
  8950. if (!flag) {
  8951. ret = wc_Md5Final(&md5, hash);
  8952. if (ret != 0) {
  8953. flag = ret;
  8954. }
  8955. }
  8956. /* Update input. */
  8957. if (!flag) {
  8958. a.input = "abc";
  8959. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  8960. "\x72";
  8961. a.inLen = XSTRLEN(a.input);
  8962. a.outLen = XSTRLEN(a.output);
  8963. ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
  8964. if (ret != 0) {
  8965. flag = ret;
  8966. }
  8967. }
  8968. if (!flag) {
  8969. ret = wc_Md5Final(&md5, hash);
  8970. if (ret != 0) {
  8971. flag = ret;
  8972. }
  8973. }
  8974. if (!flag) {
  8975. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  8976. flag = WOLFSSL_FATAL_ERROR;
  8977. }
  8978. }
  8979. /*Pass in bad values. */
  8980. if (!flag) {
  8981. b.input = NULL;
  8982. b.inLen = 0;
  8983. ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
  8984. if (ret != 0) {
  8985. flag = ret;
  8986. }
  8987. }
  8988. if (!flag) {
  8989. c.input = NULL;
  8990. c.inLen = WC_MD5_DIGEST_SIZE;
  8991. ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
  8992. if (ret != BAD_FUNC_ARG) {
  8993. flag = WOLFSSL_FATAL_ERROR;
  8994. }
  8995. }
  8996. if (!flag) {
  8997. ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
  8998. if (ret != BAD_FUNC_ARG) {
  8999. flag = WOLFSSL_FATAL_ERROR;
  9000. }
  9001. }
  9002. wc_Md5Free(&md5);
  9003. printf(resultFmt, flag == 0 ? passed : failed);
  9004. #endif
  9005. return flag;
  9006. } /* END test_wc_Md5Update() */
  9007. /*
  9008. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  9009. */
  9010. static int test_wc_Md5Final(void)
  9011. {
  9012. int flag = 0;
  9013. #ifndef NO_MD5
  9014. /* Instantiate */
  9015. wc_Md5 md5;
  9016. byte* hash_test[3];
  9017. byte hash1[WC_MD5_DIGEST_SIZE];
  9018. byte hash2[2*WC_MD5_DIGEST_SIZE];
  9019. byte hash3[5*WC_MD5_DIGEST_SIZE];
  9020. int times, i, ret;
  9021. /* Initialize */
  9022. ret = wc_InitMd5(&md5);
  9023. if (ret != 0) {
  9024. flag = ret;
  9025. }
  9026. if (!flag) {
  9027. hash_test[0] = hash1;
  9028. hash_test[1] = hash2;
  9029. hash_test[2] = hash3;
  9030. }
  9031. times = sizeof(hash_test)/sizeof(byte*);
  9032. /* Test good args. */
  9033. printf(testingFmt, "wc_Md5Final()");
  9034. for (i = 0; i < times; i++) {
  9035. if (!flag) {
  9036. ret = wc_Md5Final(&md5, hash_test[i]);
  9037. if (ret != 0) {
  9038. flag = WOLFSSL_FATAL_ERROR;
  9039. }
  9040. }
  9041. }
  9042. /* Test bad args. */
  9043. if (!flag) {
  9044. ret = wc_Md5Final(NULL, NULL);
  9045. if (ret != BAD_FUNC_ARG) {
  9046. flag = WOLFSSL_FATAL_ERROR;
  9047. }
  9048. }
  9049. if (!flag) {
  9050. ret = wc_Md5Final(NULL, hash1);
  9051. if (ret != BAD_FUNC_ARG) {
  9052. flag = WOLFSSL_FATAL_ERROR;
  9053. }
  9054. }
  9055. if (!flag) {
  9056. ret = wc_Md5Final(&md5, NULL);
  9057. if (ret != BAD_FUNC_ARG) {
  9058. flag = WOLFSSL_FATAL_ERROR;
  9059. }
  9060. }
  9061. wc_Md5Free(&md5);
  9062. printf(resultFmt, flag == 0 ? passed : failed);
  9063. #endif
  9064. return flag;
  9065. }
  9066. /*
  9067. * Unit test for the wc_InitSha()
  9068. */
  9069. static int test_wc_InitSha(void)
  9070. {
  9071. int flag = 0;
  9072. #ifndef NO_SHA
  9073. wc_Sha sha;
  9074. int ret;
  9075. printf(testingFmt, "wc_InitSha()");
  9076. /* Test good arg. */
  9077. ret = wc_InitSha(&sha);
  9078. if (ret != 0) {
  9079. flag = WOLFSSL_FATAL_ERROR;
  9080. }
  9081. /* Test bad arg. */
  9082. if (!flag) {
  9083. ret = wc_InitSha(NULL);
  9084. if (ret != BAD_FUNC_ARG) {
  9085. flag = WOLFSSL_FATAL_ERROR;
  9086. }
  9087. }
  9088. wc_ShaFree(&sha);
  9089. printf(resultFmt, flag == 0 ? passed : failed);
  9090. #endif
  9091. return flag;
  9092. } /* END test_wc_InitSha */
  9093. /*
  9094. * Tesing wc_ShaUpdate()
  9095. */
  9096. static int test_wc_ShaUpdate(void)
  9097. {
  9098. int flag = 0;
  9099. #ifndef NO_SHA
  9100. wc_Sha sha;
  9101. byte hash[WC_SHA_DIGEST_SIZE];
  9102. testVector a, b, c;
  9103. int ret;
  9104. ret = wc_InitSha(&sha);
  9105. if (ret != 0) {
  9106. flag = ret;
  9107. }
  9108. printf(testingFmt, "wc_ShaUpdate()");
  9109. /* Input. */
  9110. if (!flag) {
  9111. a.input = "a";
  9112. a.inLen = XSTRLEN(a.input);
  9113. ret = wc_ShaUpdate(&sha, NULL, 0);
  9114. if (ret != 0) {
  9115. flag = ret;
  9116. }
  9117. ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
  9118. if (ret != 0) {
  9119. flag = ret;
  9120. }
  9121. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9122. if (ret != 0) {
  9123. flag = ret;
  9124. }
  9125. }
  9126. if (!flag) {
  9127. ret = wc_ShaFinal(&sha, hash);
  9128. if (ret != 0) {
  9129. flag = ret;
  9130. }
  9131. }
  9132. /* Update input. */
  9133. if (!flag) {
  9134. a.input = "abc";
  9135. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  9136. "\x6C\x9C\xD0\xD8\x9D";
  9137. a.inLen = XSTRLEN(a.input);
  9138. a.outLen = XSTRLEN(a.output);
  9139. ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
  9140. if (ret != 0) {
  9141. flag = ret;
  9142. }
  9143. }
  9144. if (!flag) {
  9145. ret = wc_ShaFinal(&sha, hash);
  9146. if (ret !=0) {
  9147. flag = ret;
  9148. }
  9149. }
  9150. if (!flag) {
  9151. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  9152. flag = WOLFSSL_FATAL_ERROR;
  9153. }
  9154. }
  9155. /* Try passing in bad values. */
  9156. if (!flag) {
  9157. b.input = NULL;
  9158. b.inLen = 0;
  9159. ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
  9160. if (ret != 0) {
  9161. flag = ret;
  9162. }
  9163. }
  9164. if (!flag) {
  9165. c.input = NULL;
  9166. c.inLen = WC_SHA_DIGEST_SIZE;
  9167. ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
  9168. if (ret != BAD_FUNC_ARG) {
  9169. flag = WOLFSSL_FATAL_ERROR;
  9170. }
  9171. }
  9172. if (!flag) {
  9173. ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  9174. if (ret != BAD_FUNC_ARG) {
  9175. flag = WOLFSSL_FATAL_ERROR;
  9176. }
  9177. }
  9178. wc_ShaFree(&sha);
  9179. /* If not returned then the unit test passed test vectors. */
  9180. printf(resultFmt, flag == 0 ? passed : failed);
  9181. #endif
  9182. return flag;
  9183. } /* END test_wc_ShaUpdate() */
  9184. /*
  9185. * Unit test on wc_ShaFinal
  9186. */
  9187. static int test_wc_ShaFinal(void)
  9188. {
  9189. int flag = 0;
  9190. #ifndef NO_SHA
  9191. wc_Sha sha;
  9192. byte* hash_test[3];
  9193. byte hash1[WC_SHA_DIGEST_SIZE];
  9194. byte hash2[2*WC_SHA_DIGEST_SIZE];
  9195. byte hash3[5*WC_SHA_DIGEST_SIZE];
  9196. int times, i, ret;
  9197. /*Initialize*/
  9198. ret = wc_InitSha(&sha);
  9199. if (ret) {
  9200. flag = ret;
  9201. }
  9202. if (!flag) {
  9203. hash_test[0] = hash1;
  9204. hash_test[1] = hash2;
  9205. hash_test[2] = hash3;
  9206. }
  9207. times = sizeof(hash_test)/sizeof(byte*);
  9208. /* Good test args. */
  9209. printf(testingFmt, "wc_ShaFinal()");
  9210. for (i = 0; i < times; i++) {
  9211. if (!flag) {
  9212. ret = wc_ShaFinal(&sha, hash_test[i]);
  9213. if (ret != 0) {
  9214. flag = WOLFSSL_FATAL_ERROR;
  9215. }
  9216. }
  9217. }
  9218. /* Test bad args. */
  9219. if (!flag) {
  9220. ret = wc_ShaFinal(NULL, NULL);
  9221. if (ret != BAD_FUNC_ARG) {
  9222. flag = WOLFSSL_FATAL_ERROR;
  9223. }
  9224. }
  9225. if (!flag) {
  9226. ret = wc_ShaFinal(NULL, hash1);
  9227. if (ret != BAD_FUNC_ARG) {
  9228. flag = WOLFSSL_FATAL_ERROR;
  9229. }
  9230. }
  9231. if (!flag) {
  9232. ret = wc_ShaFinal(&sha, NULL);
  9233. if (ret != BAD_FUNC_ARG) {
  9234. flag = WOLFSSL_FATAL_ERROR;
  9235. }
  9236. }
  9237. wc_ShaFree(&sha);
  9238. printf(resultFmt, flag == 0 ? passed : failed);
  9239. #endif
  9240. return flag;
  9241. } /* END test_wc_ShaFinal */
  9242. /*
  9243. * Unit test for wc_InitSha256()
  9244. */
  9245. static int test_wc_InitSha256(void)
  9246. {
  9247. int flag = 0;
  9248. #ifndef NO_SHA256
  9249. wc_Sha256 sha256;
  9250. int ret;
  9251. printf(testingFmt, "wc_InitSha256()");
  9252. /* Test good arg. */
  9253. ret = wc_InitSha256(&sha256);
  9254. if (ret != 0) {
  9255. flag = WOLFSSL_FATAL_ERROR;
  9256. }
  9257. /* Test bad arg. */
  9258. if (!flag) {
  9259. ret = wc_InitSha256(NULL);
  9260. if (ret != BAD_FUNC_ARG) {
  9261. flag = WOLFSSL_FATAL_ERROR;
  9262. }
  9263. }
  9264. wc_Sha256Free(&sha256);
  9265. printf(resultFmt, flag == 0 ? passed : failed);
  9266. #endif
  9267. return flag;
  9268. } /* END test_wc_InitSha256 */
  9269. /*
  9270. * Unit test for wc_Sha256Update()
  9271. */
  9272. static int test_wc_Sha256Update(void)
  9273. {
  9274. int flag = 0;
  9275. #ifndef NO_SHA256
  9276. wc_Sha256 sha256;
  9277. byte hash[WC_SHA256_DIGEST_SIZE];
  9278. testVector a, b, c;
  9279. int ret;
  9280. ret = wc_InitSha256(&sha256);
  9281. if (ret != 0) {
  9282. flag = ret;
  9283. }
  9284. printf(testingFmt, "wc_Sha256Update()");
  9285. /* Input. */
  9286. if (!flag) {
  9287. a.input = "a";
  9288. a.inLen = XSTRLEN(a.input);
  9289. ret = wc_Sha256Update(&sha256, NULL, 0);
  9290. if (ret != 0) {
  9291. flag = ret;
  9292. }
  9293. ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
  9294. if (ret != 0) {
  9295. flag = ret;
  9296. }
  9297. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9298. if (ret != 0) {
  9299. flag = ret;
  9300. }
  9301. }
  9302. if (!flag) {
  9303. ret = wc_Sha256Final(&sha256, hash);
  9304. if (ret != 0) {
  9305. flag = ret;
  9306. }
  9307. }
  9308. /* Update input. */
  9309. if (!flag) {
  9310. a.input = "abc";
  9311. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  9312. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  9313. "\x15\xAD";
  9314. a.inLen = XSTRLEN(a.input);
  9315. a.outLen = XSTRLEN(a.output);
  9316. ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
  9317. if (ret != 0) {
  9318. flag = ret;
  9319. }
  9320. }
  9321. if (!flag) {
  9322. ret = wc_Sha256Final(&sha256, hash);
  9323. if (ret != 0) {
  9324. flag = ret;
  9325. }
  9326. }
  9327. if (!flag) {
  9328. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  9329. flag = WOLFSSL_FATAL_ERROR;
  9330. }
  9331. }
  9332. /* Try passing in bad values */
  9333. if (!flag) {
  9334. b.input = NULL;
  9335. b.inLen = 0;
  9336. ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
  9337. if (ret != 0) {
  9338. flag = ret;
  9339. }
  9340. }
  9341. if (!flag) {
  9342. c.input = NULL;
  9343. c.inLen = WC_SHA256_DIGEST_SIZE;
  9344. ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
  9345. if (ret != BAD_FUNC_ARG) {
  9346. flag = WOLFSSL_FATAL_ERROR;
  9347. }
  9348. }
  9349. if (!flag) {
  9350. ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
  9351. if (ret != BAD_FUNC_ARG) {
  9352. flag = WOLFSSL_FATAL_ERROR;
  9353. }
  9354. }
  9355. wc_Sha256Free(&sha256);
  9356. /* If not returned then the unit test passed. */
  9357. printf(resultFmt, flag == 0 ? passed : failed);
  9358. #endif
  9359. return flag;
  9360. } /* END test_wc_Sha256Update */
  9361. /*
  9362. * Unit test function for wc_Sha256Final()
  9363. */
  9364. static int test_wc_Sha256Final(void)
  9365. {
  9366. int flag = 0;
  9367. #ifndef NO_SHA256
  9368. wc_Sha256 sha256;
  9369. byte* hash_test[3];
  9370. byte hash1[WC_SHA256_DIGEST_SIZE];
  9371. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9372. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9373. int times, i, ret;
  9374. /* Initialize */
  9375. ret = wc_InitSha256(&sha256);
  9376. if (ret != 0) {
  9377. flag = ret;
  9378. }
  9379. if (!flag) {
  9380. hash_test[0] = hash1;
  9381. hash_test[1] = hash2;
  9382. hash_test[2] = hash3;
  9383. }
  9384. times = sizeof(hash_test) / sizeof(byte*);
  9385. /* Good test args. */
  9386. printf(testingFmt, "wc_Sha256Final()");
  9387. for (i = 0; i < times; i++) {
  9388. if (!flag) {
  9389. ret = wc_Sha256Final(&sha256, hash_test[i]);
  9390. if (ret != 0) {
  9391. flag = WOLFSSL_FATAL_ERROR;
  9392. }
  9393. }
  9394. }
  9395. /* Test bad args. */
  9396. if (!flag ) {
  9397. ret = wc_Sha256Final(NULL, NULL);
  9398. if (ret != BAD_FUNC_ARG) {
  9399. flag = WOLFSSL_FATAL_ERROR;
  9400. }
  9401. }
  9402. if (!flag) {
  9403. ret = wc_Sha256Final(NULL, hash1);
  9404. if (ret != BAD_FUNC_ARG) {
  9405. flag = WOLFSSL_FATAL_ERROR;
  9406. }
  9407. }
  9408. if (!flag) {
  9409. ret = wc_Sha256Final(&sha256, NULL);
  9410. if (ret != BAD_FUNC_ARG) {
  9411. flag = WOLFSSL_FATAL_ERROR;
  9412. }
  9413. }
  9414. wc_Sha256Free(&sha256);
  9415. printf(resultFmt, flag == 0 ? passed : failed);
  9416. #endif
  9417. return flag;
  9418. } /* END test_wc_Sha256Final */
  9419. /*
  9420. * Unit test function for wc_Sha256FinalRaw()
  9421. */
  9422. static int test_wc_Sha256FinalRaw(void)
  9423. {
  9424. int flag = 0;
  9425. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  9426. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  9427. !defined(WOLFSSL_NO_HASH_RAW)
  9428. wc_Sha256 sha256;
  9429. byte* hash_test[3];
  9430. byte hash1[WC_SHA256_DIGEST_SIZE];
  9431. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  9432. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  9433. int times, i, ret;
  9434. /* Initialize */
  9435. ret = wc_InitSha256(&sha256);
  9436. if (ret != 0) {
  9437. flag = ret;
  9438. }
  9439. if (!flag) {
  9440. hash_test[0] = hash1;
  9441. hash_test[1] = hash2;
  9442. hash_test[2] = hash3;
  9443. }
  9444. times = sizeof(hash_test) / sizeof(byte*);
  9445. /* Good test args. */
  9446. printf(testingFmt, "wc_Sha256FinalRaw()");
  9447. for (i = 0; i < times; i++) {
  9448. if (!flag) {
  9449. ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
  9450. if (ret != 0) {
  9451. flag = WOLFSSL_FATAL_ERROR;
  9452. }
  9453. }
  9454. }
  9455. /* Test bad args. */
  9456. if (!flag ) {
  9457. ret = wc_Sha256FinalRaw(NULL, NULL);
  9458. if (ret != BAD_FUNC_ARG) {
  9459. flag = WOLFSSL_FATAL_ERROR;
  9460. }
  9461. }
  9462. if (!flag) {
  9463. ret = wc_Sha256FinalRaw(NULL, hash1);
  9464. if (ret != BAD_FUNC_ARG) {
  9465. flag = WOLFSSL_FATAL_ERROR;
  9466. }
  9467. }
  9468. if (!flag) {
  9469. ret = wc_Sha256FinalRaw(&sha256, NULL);
  9470. if (ret != BAD_FUNC_ARG) {
  9471. flag = WOLFSSL_FATAL_ERROR;
  9472. }
  9473. }
  9474. wc_Sha256Free(&sha256);
  9475. printf(resultFmt, flag == 0 ? passed : failed);
  9476. #endif
  9477. return flag;
  9478. } /* END test_wc_Sha256FinalRaw */
  9479. /*
  9480. * Unit test function for wc_Sha256GetFlags()
  9481. */
  9482. static int test_wc_Sha256GetFlags(void)
  9483. {
  9484. int flag = 0;
  9485. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  9486. wc_Sha256 sha256;
  9487. word32 flags = 0;
  9488. printf(testingFmt, "wc_Sha256GetFlags()");
  9489. /* Initialize */
  9490. flag = wc_InitSha256(&sha256);
  9491. if (flag == 0) {
  9492. flag = wc_Sha256GetFlags(&sha256, &flags);
  9493. }
  9494. if (flag == 0) {
  9495. if (flags & WC_HASH_FLAG_ISCOPY) {
  9496. flag = 0;
  9497. }
  9498. }
  9499. wc_Sha256Free(&sha256);
  9500. printf(resultFmt, flag == 0 ? passed : failed);
  9501. #endif
  9502. return flag;
  9503. } /* END test_wc_Sha256GetFlags */
  9504. /*
  9505. * Unit test function for wc_Sha256Free()
  9506. */
  9507. static int test_wc_Sha256Free(void)
  9508. {
  9509. int flag = 0;
  9510. #ifndef NO_SHA256
  9511. printf(testingFmt, "wc_Sha256Free()");
  9512. wc_Sha256Free(NULL);
  9513. printf(resultFmt, flag == 0 ? passed : failed);
  9514. #endif
  9515. return flag;
  9516. } /* END test_wc_Sha256Free */
  9517. /*
  9518. * Unit test function for wc_Sha256GetHash()
  9519. */
  9520. static int test_wc_Sha256GetHash(void)
  9521. {
  9522. int flag = 0;
  9523. #ifndef NO_SHA256
  9524. wc_Sha256 sha256;
  9525. byte hash1[WC_SHA256_DIGEST_SIZE];
  9526. printf(testingFmt, "wc_Sha256GetHash()");
  9527. /* Initialize */
  9528. flag = wc_InitSha256(&sha256);
  9529. if (flag == 0) {
  9530. flag = wc_Sha256GetHash(&sha256, hash1);
  9531. }
  9532. /*test bad arguments*/
  9533. if (flag == 0) {
  9534. flag = wc_Sha256GetHash(NULL, NULL);
  9535. if (flag == BAD_FUNC_ARG) {
  9536. flag = 0;
  9537. }
  9538. }
  9539. if (flag == 0) {
  9540. flag = wc_Sha256GetHash(NULL, hash1);
  9541. if (flag == BAD_FUNC_ARG) {
  9542. flag = 0;
  9543. }
  9544. }
  9545. if (flag == 0) {
  9546. flag = wc_Sha256GetHash(&sha256, NULL);
  9547. if (flag == BAD_FUNC_ARG) {
  9548. flag = 0;
  9549. }
  9550. }
  9551. wc_Sha256Free(&sha256);
  9552. printf(resultFmt, flag == 0 ? passed : failed);
  9553. #endif
  9554. return flag;
  9555. } /* END test_wc_Sha256GetHash */
  9556. /*
  9557. * Unit test function for wc_Sha256Copy()
  9558. */
  9559. static int test_wc_Sha256Copy(void)
  9560. {
  9561. int flag = 0;
  9562. #ifndef NO_SHA256
  9563. wc_Sha256 sha256;
  9564. wc_Sha256 temp;
  9565. printf(testingFmt, "wc_Sha256Copy()");
  9566. /* Initialize */
  9567. flag = wc_InitSha256(&sha256);
  9568. if (flag == 0) {
  9569. flag = wc_InitSha256(&temp);
  9570. }
  9571. if (flag == 0) {
  9572. flag = wc_Sha256Copy(&sha256, &temp);
  9573. }
  9574. /*test bad arguments*/
  9575. if (flag == 0) {
  9576. flag = wc_Sha256Copy(NULL, NULL);
  9577. if (flag == BAD_FUNC_ARG) {
  9578. flag = 0;
  9579. }
  9580. }
  9581. if (flag == 0) {
  9582. flag = wc_Sha256Copy(NULL, &temp);
  9583. if (flag == BAD_FUNC_ARG) {
  9584. flag = 0;
  9585. }
  9586. }
  9587. if (flag == 0) {
  9588. flag = wc_Sha256Copy(&sha256, NULL);
  9589. if (flag == BAD_FUNC_ARG) {
  9590. flag = 0;
  9591. }
  9592. }
  9593. wc_Sha256Free(&sha256);
  9594. wc_Sha256Free(&temp);
  9595. printf(resultFmt, flag == 0 ? passed : failed);
  9596. #endif
  9597. return flag;
  9598. } /* END test_wc_Sha256Copy */
  9599. /*
  9600. * Testing wc_InitSha512()
  9601. */
  9602. static int test_wc_InitSha512(void)
  9603. {
  9604. int flag = 0;
  9605. #ifdef WOLFSSL_SHA512
  9606. wc_Sha512 sha512;
  9607. int ret;
  9608. printf(testingFmt, "wc_InitSha512()");
  9609. /* Test good arg. */
  9610. ret = wc_InitSha512(&sha512);
  9611. if (ret != 0) {
  9612. flag = WOLFSSL_FATAL_ERROR;
  9613. }
  9614. /* Test bad arg. */
  9615. if (!flag) {
  9616. ret = wc_InitSha512(NULL);
  9617. if (ret != BAD_FUNC_ARG) {
  9618. flag = WOLFSSL_FATAL_ERROR;
  9619. }
  9620. }
  9621. wc_Sha512Free(&sha512);
  9622. printf(resultFmt, flag == 0 ? passed : failed);
  9623. #endif
  9624. return flag;
  9625. } /* END test_wc_InitSha512 */
  9626. /*
  9627. * wc_Sha512Update() test.
  9628. */
  9629. static int test_wc_Sha512Update(void)
  9630. {
  9631. int flag = 0;
  9632. #ifdef WOLFSSL_SHA512
  9633. wc_Sha512 sha512;
  9634. byte hash[WC_SHA512_DIGEST_SIZE];
  9635. testVector a, b, c;
  9636. int ret;
  9637. ret = wc_InitSha512(&sha512);
  9638. if (ret != 0) {
  9639. flag = ret;
  9640. }
  9641. printf(testingFmt, "wc_Sha512Update()");
  9642. /* Input. */
  9643. if (!flag) {
  9644. a.input = "a";
  9645. a.inLen = XSTRLEN(a.input);
  9646. ret = wc_Sha512Update(&sha512, NULL, 0);
  9647. if (ret != 0) {
  9648. flag = ret;
  9649. }
  9650. ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
  9651. if (ret != 0) {
  9652. flag = ret;
  9653. }
  9654. ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
  9655. if (ret != 0) {
  9656. flag = ret;
  9657. }
  9658. ret = wc_Sha512Final(&sha512, hash);
  9659. if (ret != 0) {
  9660. flag = ret;
  9661. }
  9662. }
  9663. /* Update input. */
  9664. if (!flag) {
  9665. a.input = "abc";
  9666. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  9667. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  9668. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  9669. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  9670. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  9671. a.inLen = XSTRLEN(a.input);
  9672. a.outLen = XSTRLEN(a.output);
  9673. ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
  9674. if (ret != 0) {
  9675. flag = ret;
  9676. }
  9677. }
  9678. if (!flag) {
  9679. ret = wc_Sha512Final(&sha512, hash);
  9680. if (ret != 0) {
  9681. flag = ret;
  9682. }
  9683. }
  9684. if (!flag) {
  9685. if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
  9686. flag = WOLFSSL_FATAL_ERROR;
  9687. }
  9688. }
  9689. /* Try passing in bad values */
  9690. if (!flag) {
  9691. b.input = NULL;
  9692. b.inLen = 0;
  9693. ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
  9694. if (ret != 0) {
  9695. flag = ret;
  9696. }
  9697. }
  9698. if (!flag) {
  9699. c.input = NULL;
  9700. c.inLen = WC_SHA512_DIGEST_SIZE;
  9701. ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
  9702. if (ret != BAD_FUNC_ARG) {
  9703. flag = WOLFSSL_FATAL_ERROR;
  9704. }
  9705. }
  9706. if (!flag) {
  9707. ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
  9708. if (ret != BAD_FUNC_ARG) {
  9709. flag = WOLFSSL_FATAL_ERROR;
  9710. }
  9711. }
  9712. wc_Sha512Free(&sha512);
  9713. /* If not returned then the unit test passed test vectors. */
  9714. printf(resultFmt, flag == 0 ? passed : failed);
  9715. #endif
  9716. return flag;
  9717. } /* END test_wc_Sha512Update */
  9718. #ifdef WOLFSSL_SHA512
  9719. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9720. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9721. /* Perfoms test for
  9722. * - wc_Sha512Final/wc_Sha512FinalRaw
  9723. * - wc_Sha512_224Final/wc_Sha512_224Final
  9724. * - wc_Sha512_256Final/wc_Sha512_256Final
  9725. * parameter:
  9726. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  9727. * WC_HASH_TYPE_SHA512_256
  9728. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  9729. *return 0 on success
  9730. */
  9731. static int test_Sha512_Family_Final(int type, int isRaw)
  9732. {
  9733. wc_Sha512 sha512;
  9734. byte* hash_test[3];
  9735. byte hash1[WC_SHA512_DIGEST_SIZE];
  9736. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9737. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9738. int times, i, ret;
  9739. int(*initFp)(wc_Sha512*);
  9740. int(*finalFp)(wc_Sha512*, byte*);
  9741. void(*freeFp)(wc_Sha512*);
  9742. if (type == WC_HASH_TYPE_SHA512) {
  9743. initFp = wc_InitSha512;
  9744. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9745. !defined(WOLFSSL_NO_HASH_RAW)
  9746. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  9747. #else
  9748. finalFp = (isRaw)? NULL : wc_Sha512Final;
  9749. #endif
  9750. freeFp = wc_Sha512Free;
  9751. }
  9752. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9753. #if !defined(WOLFSSL_NOSHA512_224)
  9754. else if (type == WC_HASH_TYPE_SHA512_224) {
  9755. initFp = wc_InitSha512_224;
  9756. #if !defined(WOLFSSL_NO_HASH_RAW)
  9757. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  9758. #else
  9759. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  9760. #endif
  9761. freeFp = wc_Sha512_224Free;
  9762. }
  9763. #endif
  9764. #if !defined(WOLFSSL_NOSHA512_256)
  9765. else if (type == WC_HASH_TYPE_SHA512_256) {
  9766. initFp = wc_InitSha512_256;
  9767. #if !defined(WOLFSSL_NO_HASH_RAW)
  9768. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  9769. #else
  9770. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  9771. #endif
  9772. freeFp = wc_Sha512_256Free;
  9773. }
  9774. #endif
  9775. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9776. else
  9777. return BAD_FUNC_ARG;
  9778. /* Initialize */
  9779. ret = initFp(&sha512);
  9780. if (!ret) {
  9781. hash_test[0] = hash1;
  9782. hash_test[1] = hash2;
  9783. hash_test[2] = hash3;
  9784. }
  9785. times = sizeof(hash_test) / sizeof(byte *);
  9786. /* Good test args. */
  9787. for (i = 0; i < times && ret == 0; i++) {
  9788. ret = finalFp(&sha512, hash_test[i]);
  9789. }
  9790. /* Test bad args. */
  9791. if (!ret) {
  9792. if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
  9793. ret = WOLFSSL_FATAL_ERROR;
  9794. }
  9795. }
  9796. if (!ret) {
  9797. if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
  9798. ret = WOLFSSL_FATAL_ERROR;
  9799. }
  9800. }
  9801. if (!ret) {
  9802. if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
  9803. ret = WOLFSSL_FATAL_ERROR;
  9804. }
  9805. }
  9806. freeFp(&sha512);
  9807. return ret;
  9808. }
  9809. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  9810. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  9811. #endif /* WOLFSSL_SHA512 */
  9812. /*
  9813. * Unit test function for wc_Sha512Final()
  9814. */
  9815. static int test_wc_Sha512Final(void)
  9816. {
  9817. int flag = 0;
  9818. #ifdef WOLFSSL_SHA512
  9819. wc_Sha512 sha512;
  9820. byte* hash_test[3];
  9821. byte hash1[WC_SHA512_DIGEST_SIZE];
  9822. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9823. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9824. int times, i, ret;
  9825. /* Initialize */
  9826. ret = wc_InitSha512(&sha512);
  9827. if (ret != 0) {
  9828. flag = ret;
  9829. }
  9830. if (!flag) {
  9831. hash_test[0] = hash1;
  9832. hash_test[1] = hash2;
  9833. hash_test[2] = hash3;
  9834. }
  9835. times = sizeof(hash_test) / sizeof(byte *);
  9836. /* Good test args. */
  9837. printf(testingFmt, "wc_Sha512Final()");
  9838. for (i = 0; i < times; i++) {
  9839. if (!flag) {
  9840. ret = wc_Sha512Final(&sha512, hash_test[i]);
  9841. if (ret != 0) {
  9842. flag = WOLFSSL_FATAL_ERROR;
  9843. }
  9844. }
  9845. }
  9846. /* Test bad args. */
  9847. if (!flag) {
  9848. ret = wc_Sha512Final(NULL, NULL);
  9849. if (ret != BAD_FUNC_ARG) {
  9850. flag = WOLFSSL_FATAL_ERROR;
  9851. }
  9852. if (!flag) {}
  9853. ret = wc_Sha512Final(NULL, hash1);
  9854. if (ret != BAD_FUNC_ARG) {
  9855. flag = WOLFSSL_FATAL_ERROR;
  9856. }
  9857. }
  9858. if (!flag) {
  9859. ret = wc_Sha512Final(&sha512, NULL);
  9860. if (ret != BAD_FUNC_ARG) {
  9861. flag = WOLFSSL_FATAL_ERROR;
  9862. }
  9863. }
  9864. wc_Sha512Free(&sha512);
  9865. printf(resultFmt, flag == 0 ? passed : failed);
  9866. #endif
  9867. return flag;
  9868. } /* END test_wc_Sha512Final */
  9869. /*
  9870. * Unit test function for wc_Sha512GetFlags()
  9871. */
  9872. static int test_wc_Sha512GetFlags(void)
  9873. {
  9874. int flag = 0;
  9875. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  9876. wc_Sha512 sha512;
  9877. word32 flags = 0;
  9878. printf(testingFmt, "wc_Sha512GetFlags()");
  9879. /* Initialize */
  9880. flag = wc_InitSha512(&sha512);
  9881. if (flag == 0) {
  9882. flag = wc_Sha512GetFlags(&sha512, &flags);
  9883. }
  9884. if (flag == 0) {
  9885. if (flags & WC_HASH_FLAG_ISCOPY) {
  9886. flag = 0;
  9887. }
  9888. }
  9889. wc_Sha512Free(&sha512);
  9890. printf(resultFmt, flag == 0 ? passed : failed);
  9891. #endif
  9892. return flag;
  9893. } /* END test_wc_Sha512GetFlags */
  9894. /*
  9895. * Unit test function for wc_Sha512FinalRaw()
  9896. */
  9897. static int test_wc_Sha512FinalRaw(void)
  9898. {
  9899. int flag = 0;
  9900. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  9901. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  9902. !defined(WOLFSSL_NO_HASH_RAW)
  9903. wc_Sha512 sha512;
  9904. byte* hash_test[3];
  9905. byte hash1[WC_SHA512_DIGEST_SIZE];
  9906. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  9907. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  9908. int times, i, ret;
  9909. /* Initialize */
  9910. ret = wc_InitSha512(&sha512);
  9911. if (ret != 0) {
  9912. flag = ret;
  9913. }
  9914. if (!flag) {
  9915. hash_test[0] = hash1;
  9916. hash_test[1] = hash2;
  9917. hash_test[2] = hash3;
  9918. }
  9919. times = sizeof(hash_test) / sizeof(byte*);
  9920. /* Good test args. */
  9921. printf(testingFmt, "wc_Sha512FinalRaw()");
  9922. for (i = 0; i < times; i++) {
  9923. if (!flag) {
  9924. ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
  9925. if (ret != 0) {
  9926. flag = WOLFSSL_FATAL_ERROR;
  9927. }
  9928. }
  9929. }
  9930. /* Test bad args. */
  9931. if (!flag ) {
  9932. ret = wc_Sha512FinalRaw(NULL, NULL);
  9933. if (ret != BAD_FUNC_ARG) {
  9934. flag = WOLFSSL_FATAL_ERROR;
  9935. }
  9936. }
  9937. if (!flag) {
  9938. ret = wc_Sha512FinalRaw(NULL, hash1);
  9939. if (ret != BAD_FUNC_ARG) {
  9940. flag = WOLFSSL_FATAL_ERROR;
  9941. }
  9942. }
  9943. if (!flag) {
  9944. ret = wc_Sha512FinalRaw(&sha512, NULL);
  9945. if (ret != BAD_FUNC_ARG) {
  9946. flag = WOLFSSL_FATAL_ERROR;
  9947. }
  9948. }
  9949. wc_Sha512Free(&sha512);
  9950. printf(resultFmt, flag == 0 ? passed : failed);
  9951. #endif
  9952. return flag;
  9953. } /* END test_wc_Sha512FinalRaw */
  9954. /*
  9955. * Unit test function for wc_Sha512Free()
  9956. */
  9957. static int test_wc_Sha512Free(void)
  9958. {
  9959. int flag = 0;
  9960. #ifdef WOLFSSL_SHA512
  9961. printf(testingFmt, "wc_Sha512Free()");
  9962. wc_Sha512Free(NULL);
  9963. printf(resultFmt, flag == 0 ? passed : failed);
  9964. #endif
  9965. return flag;
  9966. } /* END test_wc_Sha512Free */
  9967. #ifdef WOLFSSL_SHA512
  9968. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  9969. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  9970. static int test_Sha512_Family_GetHash(int type )
  9971. {
  9972. int flag = 0;
  9973. int(*initFp)(wc_Sha512*);
  9974. int(*ghashFp)(wc_Sha512*, byte*);
  9975. wc_Sha512 sha512;
  9976. byte hash1[WC_SHA512_DIGEST_SIZE];
  9977. if (type == WC_HASH_TYPE_SHA512) {
  9978. initFp = wc_InitSha512;
  9979. ghashFp = wc_Sha512GetHash;
  9980. }
  9981. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  9982. #if !defined(WOLFSSL_NOSHA512_224)
  9983. else if (type == WC_HASH_TYPE_SHA512_224) {
  9984. initFp = wc_InitSha512_224;
  9985. ghashFp = wc_Sha512_224GetHash;
  9986. }
  9987. #endif
  9988. #if !defined(WOLFSSL_NOSHA512_256)
  9989. else if (type == WC_HASH_TYPE_SHA512_256) {
  9990. initFp = wc_InitSha512_256;
  9991. ghashFp = wc_Sha512_256GetHash;
  9992. }
  9993. #endif
  9994. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  9995. else {
  9996. initFp = NULL;
  9997. ghashFp = NULL;
  9998. }
  9999. if (initFp == NULL || ghashFp == NULL)
  10000. return WOLFSSL_FATAL_ERROR;
  10001. if (!flag) {
  10002. flag = initFp(&sha512);
  10003. }
  10004. if (!flag) {
  10005. flag = ghashFp(&sha512, hash1);
  10006. }
  10007. /*test bad arguments*/
  10008. if (!flag) {
  10009. if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
  10010. flag = WOLFSSL_FATAL_ERROR;
  10011. }
  10012. if (!flag) {
  10013. if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
  10014. flag = WOLFSSL_FATAL_ERROR;
  10015. }
  10016. if (!flag) {
  10017. if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
  10018. flag = WOLFSSL_FATAL_ERROR;
  10019. }
  10020. wc_Sha512Free(&sha512);
  10021. return flag;
  10022. }
  10023. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  10024. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  10025. #endif /* WOLFSSL_SHA512 */
  10026. /*
  10027. * Unit test function for wc_Sha512GetHash()
  10028. */
  10029. static int test_wc_Sha512GetHash(void)
  10030. {
  10031. int flag = 0;
  10032. #ifdef WOLFSSL_SHA512
  10033. wc_Sha512 sha512;
  10034. byte hash1[WC_SHA512_DIGEST_SIZE];
  10035. printf(testingFmt, "wc_Sha512GetHash()");
  10036. /* Initialize */
  10037. flag = wc_InitSha512(&sha512);
  10038. if (flag == 0) {
  10039. flag = wc_Sha512GetHash(&sha512, hash1);
  10040. }
  10041. /*test bad arguments*/
  10042. if (flag == 0) {
  10043. flag = wc_Sha512GetHash(NULL, NULL);
  10044. if (flag == BAD_FUNC_ARG) {
  10045. flag = 0;
  10046. }
  10047. }
  10048. if (flag == 0) {
  10049. flag = wc_Sha512GetHash(NULL, hash1);
  10050. if (flag == BAD_FUNC_ARG) {
  10051. flag = 0;
  10052. }
  10053. }
  10054. if (flag == 0) {
  10055. flag = wc_Sha512GetHash(&sha512, NULL);
  10056. if (flag == BAD_FUNC_ARG) {
  10057. flag = 0;
  10058. }
  10059. }
  10060. wc_Sha512Free(&sha512);
  10061. printf(resultFmt, flag == 0 ? passed : failed);
  10062. #endif
  10063. return flag;
  10064. } /* END test_wc_Sha512GetHash */
  10065. /*
  10066. * Unit test function for wc_Sha512Copy()
  10067. */
  10068. static int test_wc_Sha512Copy(void)
  10069. {
  10070. int flag = 0;
  10071. #ifdef WOLFSSL_SHA512
  10072. wc_Sha512 sha512;
  10073. wc_Sha512 temp;
  10074. printf(testingFmt, "wc_Sha512Copy()");
  10075. /* Initialize */
  10076. flag = wc_InitSha512(&sha512);
  10077. if (flag == 0) {
  10078. flag = wc_InitSha512(&temp);
  10079. }
  10080. if (flag == 0) {
  10081. flag = wc_Sha512Copy(&sha512, &temp);
  10082. }
  10083. /*test bad arguments*/
  10084. if (flag == 0) {
  10085. flag = wc_Sha512Copy(NULL, NULL);
  10086. if (flag == BAD_FUNC_ARG) {
  10087. flag = 0;
  10088. }
  10089. }
  10090. if (flag == 0) {
  10091. flag = wc_Sha512Copy(NULL, &temp);
  10092. if (flag == BAD_FUNC_ARG) {
  10093. flag = 0;
  10094. }
  10095. }
  10096. if (flag == 0) {
  10097. flag = wc_Sha512Copy(&sha512, NULL);
  10098. if (flag == BAD_FUNC_ARG) {
  10099. flag = 0;
  10100. }
  10101. }
  10102. wc_Sha512Free(&sha512);
  10103. wc_Sha512Free(&temp);
  10104. printf(resultFmt, flag == 0 ? passed : failed);
  10105. #endif
  10106. return flag;
  10107. } /* END test_wc_Sha512Copy */
  10108. static int test_wc_InitSha512_224(void)
  10109. {
  10110. int flag = 0;
  10111. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10112. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10113. wc_Sha512 sha512;
  10114. int ret;
  10115. printf(testingFmt, "wc_InitSha512_224()");
  10116. /* Test good arg. */
  10117. ret = wc_InitSha512_224(&sha512);
  10118. if (ret != 0) {
  10119. flag = WOLFSSL_FATAL_ERROR;
  10120. }
  10121. /* Test bad arg. */
  10122. if (!flag) {
  10123. ret = wc_InitSha512_224(NULL);
  10124. if (ret != BAD_FUNC_ARG) {
  10125. flag = WOLFSSL_FATAL_ERROR;
  10126. }
  10127. }
  10128. wc_Sha512_224Free(&sha512);
  10129. printf(resultFmt, flag == 0 ? passed : failed);
  10130. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10131. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10132. return flag;
  10133. }
  10134. static int test_wc_Sha512_224Update(void)
  10135. {
  10136. int flag = 0;
  10137. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10138. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10139. wc_Sha512 sha512;
  10140. byte hash[WC_SHA512_DIGEST_SIZE];
  10141. testVector a, c;
  10142. int ret;
  10143. ret = wc_InitSha512_224(&sha512);
  10144. if (ret != 0) {
  10145. flag = ret;
  10146. }
  10147. printf(testingFmt, "wc_Sha512_224Update()");
  10148. /* Input. */
  10149. if (!flag) {
  10150. a.input = "a";
  10151. a.inLen = XSTRLEN(a.input);
  10152. ret = wc_Sha512_224Update(&sha512, NULL, 0);
  10153. if (ret != 0) {
  10154. flag = ret;
  10155. }
  10156. ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
  10157. if (ret != 0) {
  10158. flag = ret;
  10159. }
  10160. ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10161. if (ret != 0) {
  10162. flag = ret;
  10163. }
  10164. ret = wc_Sha512_224Final(&sha512, hash);
  10165. if (ret != 0) {
  10166. flag = ret;
  10167. }
  10168. }
  10169. /* Update input. */
  10170. if (!flag) {
  10171. a.input = "abc";
  10172. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  10173. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  10174. a.inLen = XSTRLEN(a.input);
  10175. a.outLen = XSTRLEN(a.output);
  10176. ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10177. if (ret != 0) {
  10178. flag = ret;
  10179. }
  10180. }
  10181. if (!flag) {
  10182. ret = wc_Sha512_224Final(&sha512, hash);
  10183. if (ret != 0) {
  10184. flag = ret;
  10185. }
  10186. }
  10187. if (!flag) {
  10188. if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
  10189. flag = WOLFSSL_FATAL_ERROR;
  10190. }
  10191. }
  10192. if (!flag) {
  10193. c.input = NULL;
  10194. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  10195. ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10196. if (ret != BAD_FUNC_ARG) {
  10197. flag = WOLFSSL_FATAL_ERROR;
  10198. }
  10199. }
  10200. if (!flag) {
  10201. ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
  10202. if (ret != BAD_FUNC_ARG) {
  10203. flag = WOLFSSL_FATAL_ERROR;
  10204. }
  10205. }
  10206. wc_Sha512_224Free(&sha512);
  10207. /* If not returned then the unit test passed test vectors. */
  10208. printf(resultFmt, flag == 0 ? passed : failed);
  10209. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10210. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10211. return flag;
  10212. }
  10213. static int test_wc_Sha512_224Final(void)
  10214. {
  10215. int flag = 0;
  10216. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10217. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10218. printf(testingFmt, "wc_Sha512_224Final()");
  10219. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
  10220. printf(resultFmt, flag == 0 ? passed : failed);
  10221. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  10222. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10223. return flag;
  10224. }
  10225. static int test_wc_Sha512_224GetFlags(void)
  10226. {
  10227. int flag = 0;
  10228. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10229. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  10230. wc_Sha512 sha512, copy;
  10231. word32 flags = 0;
  10232. printf(testingFmt, "wc_Sha512_224GetFlags()");
  10233. /* Initialize */
  10234. flag = wc_InitSha512_224(&sha512);
  10235. if (!flag) {
  10236. flag = wc_InitSha512_224(&copy);
  10237. }
  10238. if (!flag) {
  10239. flag = wc_Sha512_224Copy(&sha512, &copy);
  10240. }
  10241. if (!flag) {
  10242. flag = wc_Sha512_224GetFlags(&copy, &flags);
  10243. }
  10244. if (!flag) {
  10245. if (flags & WC_HASH_FLAG_ISCOPY)
  10246. flag = 0;
  10247. else
  10248. flag = WOLFSSL_FATAL_ERROR;
  10249. }
  10250. wc_Sha512_224Free(&copy);
  10251. wc_Sha512_224Free(&sha512);
  10252. printf(resultFmt, flag == 0 ? passed : failed);
  10253. #endif
  10254. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10255. return flag;
  10256. }
  10257. static int test_wc_Sha512_224FinalRaw(void)
  10258. {
  10259. int flag = 0;
  10260. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10261. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  10262. !defined(WOLFSSL_NO_HASH_RAW)
  10263. printf(testingFmt, "wc_Sha512_224FinalRaw()");
  10264. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
  10265. printf(resultFmt, flag == 0 ? passed : failed);
  10266. #endif
  10267. return flag;
  10268. }
  10269. static int test_wc_Sha512_224Free(void)
  10270. {
  10271. int flag = 0;
  10272. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10273. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10274. printf(testingFmt, "wc_Sha512_224Free()");
  10275. wc_Sha512_224Free(NULL);
  10276. printf(resultFmt, passed);
  10277. #endif
  10278. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10279. return flag;
  10280. }
  10281. static int test_wc_Sha512_224GetHash(void)
  10282. {
  10283. int flag = 0;
  10284. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10285. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10286. printf(testingFmt, "wc_Sha512_224GetHash()");
  10287. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
  10288. printf(resultFmt, flag == 0 ? passed : failed);
  10289. #endif
  10290. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10291. return flag;
  10292. }
  10293. static int test_wc_Sha512_224Copy(void)
  10294. {
  10295. int flag = 0;
  10296. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10297. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  10298. wc_Sha512 sha512;
  10299. wc_Sha512 temp;
  10300. printf(testingFmt, "wc_Sha512_224Copy()");
  10301. /* Initialize */
  10302. flag = wc_InitSha512_224(&sha512);
  10303. if (flag == 0) {
  10304. flag = wc_InitSha512_224(&temp);
  10305. }
  10306. if (flag == 0) {
  10307. flag = wc_Sha512_224Copy(&sha512, &temp);
  10308. }
  10309. /*test bad arguments*/
  10310. if (flag == 0) {
  10311. if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
  10312. flag = WOLFSSL_FATAL_ERROR;
  10313. }
  10314. if (flag == 0) {
  10315. if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
  10316. flag = WOLFSSL_FATAL_ERROR;
  10317. }
  10318. if (flag == 0) {
  10319. if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10320. flag = WOLFSSL_FATAL_ERROR;
  10321. }
  10322. wc_Sha512_224Free(&sha512);
  10323. wc_Sha512_224Free(&temp);
  10324. printf(resultFmt, flag == 0 ? passed : failed);
  10325. #endif
  10326. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10327. return flag;
  10328. }
  10329. static int test_wc_InitSha512_256(void)
  10330. {
  10331. int flag = 0;
  10332. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10333. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10334. wc_Sha512 sha512;
  10335. int ret;
  10336. printf(testingFmt, "wc_InitSha512_256()");
  10337. /* Test good arg. */
  10338. ret = wc_InitSha512_256(&sha512);
  10339. if (ret != 0) {
  10340. flag = WOLFSSL_FATAL_ERROR;
  10341. }
  10342. /* Test bad arg. */
  10343. if (!flag) {
  10344. ret = wc_InitSha512_256(NULL);
  10345. if (ret != BAD_FUNC_ARG) {
  10346. flag = WOLFSSL_FATAL_ERROR;
  10347. }
  10348. }
  10349. wc_Sha512_256Free(&sha512);
  10350. printf(resultFmt, flag == 0 ? passed : failed);
  10351. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10352. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10353. return flag;
  10354. }
  10355. static int test_wc_Sha512_256Update(void)
  10356. {
  10357. int flag = 0;
  10358. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10359. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10360. wc_Sha512 sha512;
  10361. byte hash[WC_SHA512_DIGEST_SIZE];
  10362. testVector a, c;
  10363. int ret;
  10364. ret = wc_InitSha512_256(&sha512);
  10365. if (ret != 0) {
  10366. flag = ret;
  10367. }
  10368. printf(testingFmt, "wc_Sha512_256Update()");
  10369. /* Input. */
  10370. if (!flag) {
  10371. a.input = "a";
  10372. a.inLen = XSTRLEN(a.input);
  10373. ret = wc_Sha512_256Update(&sha512, NULL, 0);
  10374. if (ret != 0) {
  10375. flag = ret;
  10376. }
  10377. ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
  10378. if (ret != 0) {
  10379. flag = ret;
  10380. }
  10381. ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
  10382. if (ret != 0) {
  10383. flag = ret;
  10384. }
  10385. ret = wc_Sha512_256Final(&sha512, hash);
  10386. if (ret != 0) {
  10387. flag = ret;
  10388. }
  10389. }
  10390. /* Update input. */
  10391. if (!flag) {
  10392. a.input = "abc";
  10393. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  10394. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  10395. "\x07\xe7\xaf\x23";
  10396. a.inLen = XSTRLEN(a.input);
  10397. a.outLen = XSTRLEN(a.output);
  10398. ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
  10399. if (ret != 0) {
  10400. flag = ret;
  10401. }
  10402. }
  10403. if (!flag) {
  10404. ret = wc_Sha512_256Final(&sha512, hash);
  10405. if (ret != 0) {
  10406. flag = ret;
  10407. }
  10408. }
  10409. if (!flag) {
  10410. if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
  10411. flag = WOLFSSL_FATAL_ERROR;
  10412. }
  10413. }
  10414. if (!flag) {
  10415. c.input = NULL;
  10416. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  10417. ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
  10418. if (ret != BAD_FUNC_ARG) {
  10419. flag = WOLFSSL_FATAL_ERROR;
  10420. }
  10421. }
  10422. if (!flag) {
  10423. ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
  10424. if (ret != BAD_FUNC_ARG) {
  10425. flag = WOLFSSL_FATAL_ERROR;
  10426. }
  10427. }
  10428. wc_Sha512_256Free(&sha512);
  10429. /* If not returned then the unit test passed test vectors. */
  10430. printf(resultFmt, flag == 0 ? passed : failed);
  10431. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10432. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10433. return flag;
  10434. }
  10435. static int test_wc_Sha512_256Final(void)
  10436. {
  10437. int flag = 0;
  10438. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10439. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10440. printf(testingFmt, "wc_Sha512_256Final()");
  10441. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
  10442. printf(resultFmt, flag == 0 ? passed : failed);
  10443. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  10444. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10445. return flag;
  10446. }
  10447. static int test_wc_Sha512_256GetFlags(void)
  10448. {
  10449. int flag = 0;
  10450. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10451. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  10452. wc_Sha512 sha512, copy;
  10453. word32 flags = 0;
  10454. printf(testingFmt, "wc_Sha512_256GetFlags()");
  10455. /* Initialize */
  10456. flag = wc_InitSha512_256(&sha512);
  10457. if (!flag ) {
  10458. flag = wc_InitSha512_256(&copy);
  10459. }
  10460. if (!flag ) {
  10461. flag = wc_Sha512_256Copy(&sha512, &copy);
  10462. }
  10463. if (!flag ) {
  10464. flag = wc_Sha512_256GetFlags(&copy, &flags);
  10465. }
  10466. if (!flag) {
  10467. if (flags & WC_HASH_FLAG_ISCOPY)
  10468. flag = 0;
  10469. else
  10470. flag = WOLFSSL_FATAL_ERROR;
  10471. }
  10472. wc_Sha512_256Free(&sha512);
  10473. printf(resultFmt, flag == 0 ? passed : failed);
  10474. #endif
  10475. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10476. return flag;
  10477. }
  10478. static int test_wc_Sha512_256FinalRaw(void)
  10479. {
  10480. int flag = 0;
  10481. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  10482. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  10483. !defined(WOLFSSL_NO_HASH_RAW)
  10484. printf(testingFmt, "wc_Sha512_256FinalRaw()");
  10485. flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
  10486. printf(resultFmt, flag == 0 ? passed : failed);
  10487. #endif
  10488. return flag;
  10489. }
  10490. static int test_wc_Sha512_256Free(void)
  10491. {
  10492. int flag = 0;
  10493. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10494. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10495. printf(testingFmt, "wc_Sha512_256Free()");
  10496. wc_Sha512_256Free(NULL);
  10497. printf(resultFmt, passed);
  10498. #endif
  10499. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10500. return flag;
  10501. }
  10502. static int test_wc_Sha512_256GetHash(void)
  10503. {
  10504. int flag = 0;
  10505. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10506. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10507. printf(testingFmt, "wc_Sha512_256GetHash()");
  10508. flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
  10509. printf(resultFmt, flag == 0 ? passed : failed);
  10510. #endif
  10511. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10512. return flag;
  10513. }
  10514. static int test_wc_Sha512_256Copy(void)
  10515. {
  10516. int flag = 0;
  10517. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  10518. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  10519. wc_Sha512 sha512;
  10520. wc_Sha512 temp;
  10521. printf(testingFmt, "wc_Sha512_256Copy()");
  10522. /* Initialize */
  10523. flag = wc_InitSha512_256(&sha512);
  10524. if (flag == 0) {
  10525. flag = wc_InitSha512_256(&temp);
  10526. }
  10527. if (flag == 0) {
  10528. flag = wc_Sha512_256Copy(&sha512, &temp);
  10529. }
  10530. /*test bad arguments*/
  10531. if (flag == 0) {
  10532. if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
  10533. flag = WOLFSSL_FATAL_ERROR;
  10534. }
  10535. if (flag == 0) {
  10536. if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
  10537. flag = WOLFSSL_FATAL_ERROR;
  10538. }
  10539. if (flag == 0) {
  10540. if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
  10541. flag = WOLFSSL_FATAL_ERROR;
  10542. }
  10543. wc_Sha512_256Free(&sha512);
  10544. wc_Sha512_256Free(&temp);
  10545. printf(resultFmt, flag == 0 ? passed : failed);
  10546. #endif
  10547. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  10548. return flag;
  10549. }
  10550. /*
  10551. * Testing wc_InitSha384()
  10552. */
  10553. static int test_wc_InitSha384(void)
  10554. {
  10555. int flag = 0;
  10556. #ifdef WOLFSSL_SHA384
  10557. wc_Sha384 sha384;
  10558. int ret;
  10559. printf(testingFmt, "wc_InitSha384()");
  10560. /* Test good arg. */
  10561. ret = wc_InitSha384(&sha384);
  10562. if (ret != 0) {
  10563. flag = WOLFSSL_FATAL_ERROR;
  10564. }
  10565. /* Test bad arg. */
  10566. if (!flag) {
  10567. ret = wc_InitSha384(NULL);
  10568. if (ret != BAD_FUNC_ARG) {
  10569. flag = WOLFSSL_FATAL_ERROR;
  10570. }
  10571. }
  10572. wc_Sha384Free(&sha384);
  10573. printf(resultFmt, flag == 0 ? passed : failed);
  10574. #endif
  10575. return flag;
  10576. } /* END test_wc_InitSha384 */
  10577. /*
  10578. * test wc_Sha384Update()
  10579. */
  10580. static int test_wc_Sha384Update(void)
  10581. {
  10582. int flag = 0;
  10583. #ifdef WOLFSSL_SHA384
  10584. wc_Sha384 sha384;
  10585. byte hash[WC_SHA384_DIGEST_SIZE];
  10586. testVector a, b, c;
  10587. int ret;
  10588. ret = wc_InitSha384(&sha384);
  10589. if (ret != 0) {
  10590. flag = ret;
  10591. }
  10592. printf(testingFmt, "wc_Sha384Update()");
  10593. /* Input */
  10594. if (!flag) {
  10595. a.input = "a";
  10596. a.inLen = XSTRLEN(a.input);
  10597. ret = wc_Sha384Update(&sha384, NULL, 0);
  10598. if (ret != 0) {
  10599. flag = ret;
  10600. }
  10601. ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
  10602. if (ret != 0) {
  10603. flag = ret;
  10604. }
  10605. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10606. if (ret != 0) {
  10607. flag = ret;
  10608. }
  10609. }
  10610. if (!flag) {
  10611. ret = wc_Sha384Final(&sha384, hash);
  10612. if (ret != 0) {
  10613. flag = ret;
  10614. }
  10615. }
  10616. /* Update input. */
  10617. if (!flag) {
  10618. a.input = "abc";
  10619. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  10620. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  10621. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  10622. "\xc8\x25\xa7";
  10623. a.inLen = XSTRLEN(a.input);
  10624. a.outLen = XSTRLEN(a.output);
  10625. ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
  10626. if (ret != 0) {
  10627. flag = ret;
  10628. }
  10629. }
  10630. if (!flag) {
  10631. ret = wc_Sha384Final(&sha384, hash);
  10632. if (ret != 0) {
  10633. flag = ret;
  10634. }
  10635. }
  10636. if (!flag) {
  10637. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  10638. flag = WOLFSSL_FATAL_ERROR;
  10639. }
  10640. }
  10641. /* Pass in bad values. */
  10642. if (!flag) {
  10643. b.input = NULL;
  10644. b.inLen = 0;
  10645. ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
  10646. if (ret != 0) {
  10647. flag = ret;
  10648. }
  10649. }
  10650. if (!flag) {
  10651. c.input = NULL;
  10652. c.inLen = WC_SHA384_DIGEST_SIZE;
  10653. ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
  10654. if (ret != BAD_FUNC_ARG) {
  10655. flag = WOLFSSL_FATAL_ERROR;
  10656. }
  10657. }
  10658. if (!flag) {
  10659. ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
  10660. if (ret != BAD_FUNC_ARG) {
  10661. flag = WOLFSSL_FATAL_ERROR;
  10662. }
  10663. }
  10664. wc_Sha384Free(&sha384);
  10665. /* If not returned then the unit test passed test vectors. */
  10666. printf(resultFmt, flag == 0 ? passed : failed);
  10667. #endif
  10668. return flag;
  10669. } /* END test_wc_Sha384Update */
  10670. /*
  10671. * Unit test function for wc_Sha384Final();
  10672. */
  10673. static int test_wc_Sha384Final(void)
  10674. {
  10675. int flag = 0;
  10676. #ifdef WOLFSSL_SHA384
  10677. wc_Sha384 sha384;
  10678. byte* hash_test[3];
  10679. byte hash1[WC_SHA384_DIGEST_SIZE];
  10680. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10681. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10682. int times, i, ret;
  10683. /* Initialize */
  10684. ret = wc_InitSha384(&sha384);
  10685. if (ret) {
  10686. flag = ret;
  10687. }
  10688. if (!flag) {
  10689. hash_test[0] = hash1;
  10690. hash_test[1] = hash2;
  10691. hash_test[2] = hash3;
  10692. }
  10693. times = sizeof(hash_test) / sizeof(byte*);
  10694. /* Good test args. */
  10695. printf(testingFmt, "wc_Sha384Final()");
  10696. for (i = 0; i < times; i++) {
  10697. if (!flag) {
  10698. ret = wc_Sha384Final(&sha384, hash_test[i]);
  10699. if (ret != 0) {
  10700. flag = WOLFSSL_FATAL_ERROR;
  10701. }
  10702. }
  10703. }
  10704. /* Test bad args. */
  10705. if (!flag) {
  10706. ret = wc_Sha384Final(NULL, NULL);
  10707. if (ret != BAD_FUNC_ARG) {
  10708. flag = WOLFSSL_FATAL_ERROR;
  10709. }
  10710. }
  10711. if (!flag) {
  10712. ret = wc_Sha384Final(NULL, hash1);
  10713. if (ret != BAD_FUNC_ARG) {
  10714. flag = WOLFSSL_FATAL_ERROR;
  10715. }
  10716. }
  10717. if (!flag) {
  10718. ret = wc_Sha384Final(&sha384, NULL);
  10719. if (ret != BAD_FUNC_ARG) {
  10720. flag = WOLFSSL_FATAL_ERROR;
  10721. }
  10722. }
  10723. wc_Sha384Free(&sha384);
  10724. printf(resultFmt, flag == 0 ? passed : failed);
  10725. #endif
  10726. return flag;
  10727. } /* END test_wc_Sha384Final */
  10728. /*
  10729. * Unit test function for wc_Sha384GetFlags()
  10730. */
  10731. static int test_wc_Sha384GetFlags(void)
  10732. {
  10733. int flag = 0;
  10734. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  10735. wc_Sha384 sha384;
  10736. word32 flags = 0;
  10737. printf(testingFmt, "wc_Sha384GetFlags()");
  10738. /* Initialize */
  10739. flag = wc_InitSha384(&sha384);
  10740. if (flag == 0) {
  10741. flag = wc_Sha384GetFlags(&sha384, &flags);
  10742. }
  10743. if (flag == 0) {
  10744. if (flags & WC_HASH_FLAG_ISCOPY) {
  10745. flag = 0;
  10746. }
  10747. }
  10748. wc_Sha384Free(&sha384);
  10749. printf(resultFmt, flag == 0 ? passed : failed);
  10750. #endif
  10751. return flag;
  10752. } /* END test_wc_Sha384GetFlags */
  10753. /*
  10754. * Unit test function for wc_Sha384FinalRaw()
  10755. */
  10756. static int test_wc_Sha384FinalRaw(void)
  10757. {
  10758. int flag = 0;
  10759. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  10760. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  10761. !defined(WOLFSSL_NO_HASH_RAW)
  10762. wc_Sha384 sha384;
  10763. byte* hash_test[3];
  10764. byte hash1[WC_SHA384_DIGEST_SIZE];
  10765. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  10766. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  10767. int times, i, ret;
  10768. /* Initialize */
  10769. ret = wc_InitSha384(&sha384);
  10770. if (ret != 0) {
  10771. flag = ret;
  10772. }
  10773. if (!flag) {
  10774. hash_test[0] = hash1;
  10775. hash_test[1] = hash2;
  10776. hash_test[2] = hash3;
  10777. }
  10778. times = sizeof(hash_test) / sizeof(byte*);
  10779. /* Good test args. */
  10780. printf(testingFmt, "wc_Sha384FinalRaw()");
  10781. for (i = 0; i < times; i++) {
  10782. if (!flag) {
  10783. ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
  10784. if (ret != 0) {
  10785. flag = WOLFSSL_FATAL_ERROR;
  10786. }
  10787. }
  10788. }
  10789. /* Test bad args. */
  10790. if (!flag ) {
  10791. ret = wc_Sha384FinalRaw(NULL, NULL);
  10792. if (ret != BAD_FUNC_ARG) {
  10793. flag = WOLFSSL_FATAL_ERROR;
  10794. }
  10795. }
  10796. if (!flag) {
  10797. ret = wc_Sha384FinalRaw(NULL, hash1);
  10798. if (ret != BAD_FUNC_ARG) {
  10799. flag = WOLFSSL_FATAL_ERROR;
  10800. }
  10801. }
  10802. if (!flag) {
  10803. ret = wc_Sha384FinalRaw(&sha384, NULL);
  10804. if (ret != BAD_FUNC_ARG) {
  10805. flag = WOLFSSL_FATAL_ERROR;
  10806. }
  10807. }
  10808. wc_Sha384Free(&sha384);
  10809. printf(resultFmt, flag == 0 ? passed : failed);
  10810. #endif
  10811. return flag;
  10812. } /* END test_wc_Sha384FinalRaw */
  10813. /*
  10814. * Unit test function for wc_Sha384Free()
  10815. */
  10816. static int test_wc_Sha384Free(void)
  10817. {
  10818. int flag = 0;
  10819. #ifdef WOLFSSL_SHA384
  10820. printf(testingFmt, "wc_Sha384Free()");
  10821. wc_Sha384Free(NULL);
  10822. printf(resultFmt, flag == 0 ? passed : failed);
  10823. #endif
  10824. return flag;
  10825. } /* END test_wc_Sha384Free */
  10826. /*
  10827. * Unit test function for wc_Sha384GetHash()
  10828. */
  10829. static int test_wc_Sha384GetHash(void)
  10830. {
  10831. int flag = 0;
  10832. #ifdef WOLFSSL_SHA384
  10833. wc_Sha384 sha384;
  10834. byte hash1[WC_SHA384_DIGEST_SIZE];
  10835. printf(testingFmt, "wc_Sha384GetHash()");
  10836. /* Initialize */
  10837. flag = wc_InitSha384(&sha384);
  10838. if (flag == 0) {
  10839. flag = wc_Sha384GetHash(&sha384, hash1);
  10840. }
  10841. /*test bad arguments*/
  10842. if (flag == 0) {
  10843. flag = wc_Sha384GetHash(NULL, NULL);
  10844. if (flag == BAD_FUNC_ARG) {
  10845. flag = 0;
  10846. }
  10847. }
  10848. if (flag == 0) {
  10849. flag = wc_Sha384GetHash(NULL, hash1);
  10850. if (flag == BAD_FUNC_ARG) {
  10851. flag = 0;
  10852. }
  10853. }
  10854. if (flag == 0) {
  10855. flag = wc_Sha384GetHash(&sha384, NULL);
  10856. if (flag == BAD_FUNC_ARG) {
  10857. flag = 0;
  10858. }
  10859. }
  10860. wc_Sha384Free(&sha384);
  10861. printf(resultFmt, flag == 0 ? passed : failed);
  10862. #endif
  10863. return flag;
  10864. } /* END test_wc_Sha384GetHash */
  10865. /*
  10866. * Unit test function for wc_Sha384Copy()
  10867. */
  10868. static int test_wc_Sha384Copy(void)
  10869. {
  10870. int flag = 0;
  10871. #ifdef WOLFSSL_SHA384
  10872. wc_Sha384 sha384;
  10873. wc_Sha384 temp;
  10874. printf(testingFmt, "wc_Sha384Copy()");
  10875. /* Initialize */
  10876. flag = wc_InitSha384(&sha384);
  10877. if (flag == 0) {
  10878. flag = wc_InitSha384(&temp);
  10879. }
  10880. if (flag == 0) {
  10881. flag = wc_Sha384Copy(&sha384, &temp);
  10882. }
  10883. /*test bad arguments*/
  10884. if (flag == 0) {
  10885. flag = wc_Sha384Copy(NULL, NULL);
  10886. if (flag == BAD_FUNC_ARG) {
  10887. flag = 0;
  10888. }
  10889. }
  10890. if (flag == 0) {
  10891. flag = wc_Sha384Copy(NULL, &temp);
  10892. if (flag == BAD_FUNC_ARG) {
  10893. flag = 0;
  10894. }
  10895. }
  10896. if (flag == 0) {
  10897. flag = wc_Sha384Copy(&sha384, NULL);
  10898. if (flag == BAD_FUNC_ARG) {
  10899. flag = 0;
  10900. }
  10901. }
  10902. wc_Sha384Free(&sha384);
  10903. wc_Sha384Free(&temp);
  10904. printf(resultFmt, flag == 0 ? passed : failed);
  10905. #endif
  10906. return flag;
  10907. } /* END test_wc_Sha384Copy */
  10908. /*
  10909. * Testing wc_InitSha224();
  10910. */
  10911. static int test_wc_InitSha224(void)
  10912. {
  10913. int flag = 0;
  10914. #ifdef WOLFSSL_SHA224
  10915. wc_Sha224 sha224;
  10916. int ret;
  10917. printf(testingFmt, "wc_InitSha224()");
  10918. /* Test good arg. */
  10919. ret = wc_InitSha224(&sha224);
  10920. if (ret != 0) {
  10921. flag = WOLFSSL_FATAL_ERROR;
  10922. }
  10923. /* Test bad arg. */
  10924. if (!flag) {
  10925. ret = wc_InitSha224(NULL);
  10926. if (ret != BAD_FUNC_ARG) {
  10927. flag = WOLFSSL_FATAL_ERROR;
  10928. }
  10929. }
  10930. wc_Sha224Free(&sha224);
  10931. printf(resultFmt, flag == 0 ? passed : failed);
  10932. #endif
  10933. return flag;
  10934. } /* END test_wc_InitSha224 */
  10935. /*
  10936. * Unit test on wc_Sha224Update
  10937. */
  10938. static int test_wc_Sha224Update(void)
  10939. {
  10940. int flag = 0;
  10941. #ifdef WOLFSSL_SHA224
  10942. wc_Sha224 sha224;
  10943. byte hash[WC_SHA224_DIGEST_SIZE];
  10944. testVector a, b, c;
  10945. int ret;
  10946. ret = wc_InitSha224(&sha224);
  10947. if (ret != 0) {
  10948. flag = ret;
  10949. }
  10950. printf(testingFmt, "wc_Sha224Update()");
  10951. /* Input. */
  10952. if (!flag) {
  10953. a.input = "a";
  10954. a.inLen = XSTRLEN(a.input);
  10955. ret = wc_Sha224Update(&sha224, NULL, 0);
  10956. if (ret != 0) {
  10957. flag = ret;
  10958. }
  10959. ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
  10960. if (ret != 0) {
  10961. flag = ret;
  10962. }
  10963. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10964. if (ret != 0) {
  10965. flag = ret;
  10966. }
  10967. }
  10968. if (!flag) {
  10969. ret = wc_Sha224Final(&sha224, hash);
  10970. if (ret != 0) {
  10971. flag = ret;
  10972. }
  10973. }
  10974. /* Update input. */
  10975. if (!flag) {
  10976. a.input = "abc";
  10977. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  10978. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  10979. a.inLen = XSTRLEN(a.input);
  10980. a.outLen = XSTRLEN(a.output);
  10981. ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
  10982. if (ret != 0) {
  10983. flag = ret;
  10984. }
  10985. }
  10986. if (!flag) {
  10987. ret = wc_Sha224Final(&sha224, hash);
  10988. if (ret != 0) {
  10989. flag = ret;
  10990. }
  10991. }
  10992. if (!flag) {
  10993. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  10994. flag = WOLFSSL_FATAL_ERROR;
  10995. }
  10996. }
  10997. /* Pass in bad values. */
  10998. if (!flag) {
  10999. b.input = NULL;
  11000. b.inLen = 0;
  11001. ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
  11002. if (ret != 0) {
  11003. flag = ret;
  11004. }
  11005. }
  11006. if (!flag) {
  11007. c.input = NULL;
  11008. c.inLen = WC_SHA224_DIGEST_SIZE;
  11009. ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
  11010. if (ret != BAD_FUNC_ARG) {
  11011. flag = WOLFSSL_FATAL_ERROR;
  11012. }
  11013. }
  11014. if (!flag) {
  11015. ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
  11016. if (ret != BAD_FUNC_ARG) {
  11017. flag = WOLFSSL_FATAL_ERROR;
  11018. }
  11019. }
  11020. wc_Sha224Free(&sha224);
  11021. /* If not returned then the unit test passed test vectors. */
  11022. printf(resultFmt, flag == 0 ? passed : failed);
  11023. #endif
  11024. return flag;
  11025. } /* END test_wc_Sha224Update */
  11026. /*
  11027. * Unit test for wc_Sha224Final();
  11028. */
  11029. static int test_wc_Sha224Final(void)
  11030. {
  11031. int flag = 0;
  11032. #ifdef WOLFSSL_SHA224
  11033. wc_Sha224 sha224;
  11034. byte* hash_test[3];
  11035. byte hash1[WC_SHA224_DIGEST_SIZE];
  11036. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  11037. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  11038. int times, i, ret;
  11039. /* Initialize */
  11040. ret = wc_InitSha224(&sha224);
  11041. if (ret) {
  11042. flag = ret;
  11043. }
  11044. if (!flag) {
  11045. hash_test[0] = hash1;
  11046. hash_test[1] = hash2;
  11047. hash_test[2] = hash3;
  11048. }
  11049. times = sizeof(hash_test) / sizeof(byte*);
  11050. /* Good test args. */
  11051. printf(testingFmt, "wc_sha224Final()");
  11052. /* Testing oversized buffers. */
  11053. for (i = 0; i < times; i++) {
  11054. if (!flag) {
  11055. ret = wc_Sha224Final(&sha224, hash_test[i]);
  11056. if (ret != 0) {
  11057. flag = WOLFSSL_FATAL_ERROR;
  11058. }
  11059. }
  11060. }
  11061. /* Test bad args. */
  11062. if (!flag) {
  11063. ret = wc_Sha224Final(NULL, NULL);
  11064. if (ret != BAD_FUNC_ARG) {
  11065. flag = WOLFSSL_FATAL_ERROR;
  11066. }
  11067. }
  11068. if (!flag) {
  11069. ret = wc_Sha224Final(NULL, hash1);
  11070. if (ret != BAD_FUNC_ARG) {
  11071. flag = WOLFSSL_FATAL_ERROR;
  11072. }
  11073. }
  11074. if (!flag) {
  11075. ret = wc_Sha224Final(&sha224, NULL);
  11076. if (ret != BAD_FUNC_ARG) {
  11077. flag = WOLFSSL_FATAL_ERROR;
  11078. }
  11079. }
  11080. wc_Sha224Free(&sha224);
  11081. printf(resultFmt, flag == 0 ? passed : failed);
  11082. #endif
  11083. return flag;
  11084. } /* END test_wc_Sha224Final */
  11085. /*
  11086. * Unit test function for wc_Sha224SetFlags()
  11087. */
  11088. static int test_wc_Sha224SetFlags(void)
  11089. {
  11090. int flag = 0;
  11091. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11092. wc_Sha224 sha224;
  11093. word32 flags = 0;
  11094. printf(testingFmt, "wc_Sha224SetFlags()");
  11095. /* Initialize */
  11096. flag = wc_InitSha224(&sha224);
  11097. if (flag == 0) {
  11098. flag = wc_Sha224SetFlags(&sha224, flags);
  11099. }
  11100. if (flag == 0) {
  11101. if (flags & WC_HASH_FLAG_ISCOPY) {
  11102. flag = 0;
  11103. }
  11104. }
  11105. wc_Sha224Free(&sha224);
  11106. printf(resultFmt, flag == 0 ? passed : failed);
  11107. #endif
  11108. return flag;
  11109. } /* END test_wc_Sha224SetFlags */
  11110. /*
  11111. * Unit test function for wc_Sha224GetFlags()
  11112. */
  11113. static int test_wc_Sha224GetFlags(void)
  11114. {
  11115. int flag = 0;
  11116. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  11117. wc_Sha224 sha224;
  11118. word32 flags = 0;
  11119. printf(testingFmt, "wc_Sha224GetFlags()");
  11120. /* Initialize */
  11121. flag = wc_InitSha224(&sha224);
  11122. if (flag == 0) {
  11123. flag = wc_Sha224GetFlags(&sha224, &flags);
  11124. }
  11125. if (flag == 0) {
  11126. if (flags & WC_HASH_FLAG_ISCOPY) {
  11127. flag = 0;
  11128. }
  11129. }
  11130. wc_Sha224Free(&sha224);
  11131. printf(resultFmt, flag == 0 ? passed : failed);
  11132. #endif
  11133. return flag;
  11134. } /* END test_wc_Sha224GetFlags */
  11135. /*
  11136. * Unit test function for wc_Sha224Free()
  11137. */
  11138. static int test_wc_Sha224Free(void)
  11139. {
  11140. int flag = 0;
  11141. #ifdef WOLFSSL_SHA224
  11142. printf(testingFmt, "wc_Sha224Free()");
  11143. wc_Sha224Free(NULL);
  11144. printf(resultFmt, flag == 0 ? passed : failed);
  11145. #endif
  11146. return flag;
  11147. } /* END test_wc_Sha224Free */
  11148. /*
  11149. * Unit test function for wc_Sha224GetHash()
  11150. */
  11151. static int test_wc_Sha224GetHash(void)
  11152. {
  11153. int flag = 0;
  11154. #ifdef WOLFSSL_SHA224
  11155. wc_Sha224 sha224;
  11156. byte hash1[WC_SHA224_DIGEST_SIZE];
  11157. printf(testingFmt, "wc_Sha224GetHash()");
  11158. /* Initialize */
  11159. flag = wc_InitSha224(&sha224);
  11160. if (flag == 0) {
  11161. flag = wc_Sha224GetHash(&sha224, hash1);
  11162. }
  11163. /*test bad arguments*/
  11164. if (flag == 0) {
  11165. flag = wc_Sha224GetHash(NULL, NULL);
  11166. if (flag == BAD_FUNC_ARG) {
  11167. flag = 0;
  11168. }
  11169. }
  11170. if (flag == 0) {
  11171. flag = wc_Sha224GetHash(NULL, hash1);
  11172. if (flag == BAD_FUNC_ARG) {
  11173. flag = 0;
  11174. }
  11175. }
  11176. if (flag == 0) {
  11177. flag = wc_Sha224GetHash(&sha224, NULL);
  11178. if (flag == BAD_FUNC_ARG) {
  11179. flag = 0;
  11180. }
  11181. }
  11182. wc_Sha224Free(&sha224);
  11183. printf(resultFmt, flag == 0 ? passed : failed);
  11184. #endif
  11185. return flag;
  11186. } /* END test_wc_Sha224GetHash */
  11187. /*
  11188. * Unit test function for wc_Sha224Copy()
  11189. */
  11190. static int test_wc_Sha224Copy(void)
  11191. {
  11192. int flag = 0;
  11193. #ifdef WOLFSSL_SHA224
  11194. wc_Sha224 sha224;
  11195. wc_Sha224 temp;
  11196. printf(testingFmt, "wc_Sha224Copy()");
  11197. /* Initialize */
  11198. flag = wc_InitSha224(&sha224);
  11199. if (flag == 0) {
  11200. flag = wc_InitSha224(&temp);
  11201. }
  11202. if (flag == 0) {
  11203. flag = wc_Sha224Copy(&sha224, &temp);
  11204. }
  11205. /*test bad arguments*/
  11206. if (flag == 0) {
  11207. flag = wc_Sha224Copy(NULL, NULL);
  11208. if (flag == BAD_FUNC_ARG) {
  11209. flag = 0;
  11210. }
  11211. }
  11212. if (flag == 0) {
  11213. flag = wc_Sha224Copy(NULL, &temp);
  11214. if (flag == BAD_FUNC_ARG) {
  11215. flag = 0;
  11216. }
  11217. }
  11218. if (flag == 0) {
  11219. flag = wc_Sha224Copy(&sha224, NULL);
  11220. if (flag == BAD_FUNC_ARG) {
  11221. flag = 0;
  11222. }
  11223. }
  11224. wc_Sha224Free(&sha224);
  11225. wc_Sha224Free(&temp);
  11226. printf(resultFmt, flag == 0 ? passed : failed);
  11227. #endif
  11228. return flag;
  11229. } /* END test_wc_Sha224Copy */
  11230. /*
  11231. * Testing wc_InitRipeMd()
  11232. */
  11233. static int test_wc_InitRipeMd(void)
  11234. {
  11235. int flag = 0;
  11236. #ifdef WOLFSSL_RIPEMD
  11237. RipeMd ripemd;
  11238. int ret;
  11239. printf(testingFmt, "wc_InitRipeMd()");
  11240. /* Test good arg. */
  11241. ret = wc_InitRipeMd(&ripemd);
  11242. if (ret != 0) {
  11243. flag = WOLFSSL_FATAL_ERROR;
  11244. }
  11245. /* Test bad arg. */
  11246. if (!flag) {
  11247. ret = wc_InitRipeMd(NULL);
  11248. if (ret != BAD_FUNC_ARG) {
  11249. flag = WOLFSSL_FATAL_ERROR;
  11250. }
  11251. }
  11252. printf(resultFmt, flag == 0 ? passed : failed);
  11253. #endif
  11254. return flag;
  11255. } /* END test_wc_InitRipeMd */
  11256. /*
  11257. * Testing wc_RipeMdUpdate()
  11258. */
  11259. static int test_wc_RipeMdUpdate(void)
  11260. {
  11261. int flag = 0;
  11262. #ifdef WOLFSSL_RIPEMD
  11263. RipeMd ripemd;
  11264. byte hash[RIPEMD_DIGEST_SIZE];
  11265. testVector a, b, c;
  11266. int ret;
  11267. ret = wc_InitRipeMd(&ripemd);
  11268. if (ret != 0) {
  11269. flag = ret;
  11270. }
  11271. printf(testingFmt, "wc_RipeMdUpdate()");
  11272. /* Input */
  11273. if (!flag) {
  11274. a.input = "a";
  11275. a.inLen = XSTRLEN(a.input);
  11276. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11277. if (ret != 0) {
  11278. flag = ret;
  11279. }
  11280. }
  11281. if (!flag) {
  11282. ret = wc_RipeMdFinal(&ripemd, hash);
  11283. if (ret != 0) {
  11284. flag = ret;
  11285. }
  11286. }
  11287. /* Update input. */
  11288. if (!flag) {
  11289. a.input = "abc";
  11290. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  11291. "\xb0\x87\xf1\x5a\x0b\xfc";
  11292. a.inLen = XSTRLEN(a.input);
  11293. a.outLen = XSTRLEN(a.output);
  11294. ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
  11295. if (ret != 0) {
  11296. flag = ret;
  11297. }
  11298. }
  11299. if (!flag) {
  11300. ret = wc_RipeMdFinal(&ripemd, hash);
  11301. if (ret != 0) {
  11302. flag = ret;
  11303. }
  11304. }
  11305. if (!flag) {
  11306. if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
  11307. flag = WOLFSSL_FATAL_ERROR;
  11308. }
  11309. }
  11310. /* Pass in bad values. */
  11311. if (!flag) {
  11312. b.input = NULL;
  11313. b.inLen = 0;
  11314. ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
  11315. if (ret != 0) {
  11316. flag = ret;
  11317. }
  11318. }
  11319. if (!flag) {
  11320. c.input = NULL;
  11321. c.inLen = RIPEMD_DIGEST_SIZE;
  11322. ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
  11323. if (ret != BAD_FUNC_ARG) {
  11324. flag = WOLFSSL_FATAL_ERROR;
  11325. }
  11326. }
  11327. if (!flag) {
  11328. ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  11329. if (ret != BAD_FUNC_ARG) {
  11330. flag = WOLFSSL_FATAL_ERROR;
  11331. }
  11332. }
  11333. printf(resultFmt, flag == 0 ? passed : failed);
  11334. #endif
  11335. return flag;
  11336. } /* END test_wc_RipeMdUdpate */
  11337. /*
  11338. * Unit test function for wc_RipeMdFinal()
  11339. */
  11340. static int test_wc_RipeMdFinal(void)
  11341. {
  11342. int flag = 0;
  11343. #ifdef WOLFSSL_RIPEMD
  11344. RipeMd ripemd;
  11345. byte* hash_test[3];
  11346. byte hash1[RIPEMD_DIGEST_SIZE];
  11347. byte hash2[2*RIPEMD_DIGEST_SIZE];
  11348. byte hash3[5*RIPEMD_DIGEST_SIZE];
  11349. int times, i, ret;
  11350. /* Initialize */
  11351. ret = wc_InitRipeMd(&ripemd);
  11352. if (ret != 0) {
  11353. flag = ret;
  11354. }
  11355. if (!flag) {
  11356. hash_test[0] = hash1;
  11357. hash_test[1] = hash2;
  11358. hash_test[2] = hash3;
  11359. }
  11360. times = sizeof(hash_test) / sizeof(byte*);
  11361. /* Good test args. */
  11362. printf(testingFmt, "wc_RipeMdFinal()");
  11363. /* Testing oversized buffers. */
  11364. for (i = 0; i < times; i++) {
  11365. if (!flag) {
  11366. ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
  11367. if (ret != 0) {
  11368. flag = WOLFSSL_FATAL_ERROR;
  11369. }
  11370. }
  11371. }
  11372. /* Test bad args. */
  11373. if (!flag) {
  11374. ret = wc_RipeMdFinal(NULL, NULL);
  11375. if (ret != BAD_FUNC_ARG) {
  11376. flag = WOLFSSL_FATAL_ERROR;
  11377. }
  11378. }
  11379. if (!flag) {
  11380. ret = wc_RipeMdFinal(NULL, hash1);
  11381. if (ret != BAD_FUNC_ARG) {
  11382. flag = WOLFSSL_FATAL_ERROR;
  11383. }
  11384. }
  11385. if (!flag) {
  11386. ret = wc_RipeMdFinal(&ripemd, NULL);
  11387. if (ret != BAD_FUNC_ARG) {
  11388. flag = WOLFSSL_FATAL_ERROR;
  11389. }
  11390. }
  11391. printf(resultFmt, flag == 0 ? passed : failed);
  11392. #endif
  11393. return flag;
  11394. } /* END test_wc_RipeMdFinal */
  11395. /*
  11396. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  11397. * wc_InitSha3_512
  11398. */
  11399. static int test_wc_InitSha3(void)
  11400. {
  11401. int ret = 0;
  11402. #if defined(WOLFSSL_SHA3)
  11403. wc_Sha3 sha3;
  11404. (void)sha3;
  11405. #if !defined(WOLFSSL_NOSHA3_224)
  11406. printf(testingFmt, "wc_InitSha3_224()");
  11407. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11408. /* Test bad args. */
  11409. if (ret == 0) {
  11410. ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
  11411. if (ret == BAD_FUNC_ARG) {
  11412. ret = 0;
  11413. } else if (ret == 0) {
  11414. ret = WOLFSSL_FATAL_ERROR;
  11415. }
  11416. }
  11417. wc_Sha3_224_Free(&sha3);
  11418. printf(resultFmt, ret == 0 ? passed : failed);
  11419. #endif /* NOSHA3_224 */
  11420. #if !defined(WOLFSSL_NOSHA3_256)
  11421. if (ret == 0) {
  11422. printf(testingFmt, "wc_InitSha3_256()");
  11423. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11424. /* Test bad args. */
  11425. if (ret == 0) {
  11426. ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
  11427. if (ret == BAD_FUNC_ARG) {
  11428. ret = 0;
  11429. } else if (ret == 0) {
  11430. ret = WOLFSSL_FATAL_ERROR;
  11431. }
  11432. }
  11433. wc_Sha3_256_Free(&sha3);
  11434. printf(resultFmt, ret == 0 ? passed : failed);
  11435. } /* END sha3_256 */
  11436. #endif /* NOSHA3_256 */
  11437. #if !defined(WOLFSSL_NOSHA3_384)
  11438. if (ret == 0) {
  11439. printf(testingFmt, "wc_InitSha3_384()");
  11440. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11441. /* Test bad args. */
  11442. if (ret == 0) {
  11443. ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
  11444. if (ret == BAD_FUNC_ARG) {
  11445. ret = 0;
  11446. } else if (ret == 0) {
  11447. ret = WOLFSSL_FATAL_ERROR;
  11448. }
  11449. }
  11450. wc_Sha3_384_Free(&sha3);
  11451. printf(resultFmt, ret == 0 ? passed : failed);
  11452. } /* END sha3_384 */
  11453. #endif /* NOSHA3_384 */
  11454. #if !defined(WOLFSSL_NOSHA3_512)
  11455. if (ret == 0) {
  11456. printf(testingFmt, "wc_InitSha3_512()");
  11457. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11458. /* Test bad args. */
  11459. if (ret == 0) {
  11460. ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
  11461. if (ret == BAD_FUNC_ARG) {
  11462. ret = 0;
  11463. } else if (ret == 0) {
  11464. ret = WOLFSSL_FATAL_ERROR;
  11465. }
  11466. }
  11467. wc_Sha3_512_Free(&sha3);
  11468. printf(resultFmt, ret == 0 ? passed : failed);
  11469. } /* END sha3_512 */
  11470. #endif /* NOSHA3_512 */
  11471. #endif
  11472. return ret;
  11473. } /* END test_wc_InitSha3 */
  11474. /*
  11475. * Testing wc_Sha3_Update()
  11476. */
  11477. static int testing_wc_Sha3_Update(void)
  11478. {
  11479. int ret = 0;
  11480. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  11481. !defined(WOLFSSL_AFALG_XILINX)
  11482. wc_Sha3 sha3;
  11483. byte msg[] = "Everybody's working for the weekend.";
  11484. byte msg2[] = "Everybody gets Friday off.";
  11485. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  11486. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  11487. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  11488. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  11489. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  11490. word32 msglen = sizeof(msg) - 1;
  11491. word32 msg2len = sizeof(msg2);
  11492. word32 msgCmplen = sizeof(msgCmp);
  11493. #if !defined(WOLFSSL_NOSHA3_224)
  11494. printf(testingFmt, "wc_Sha3_224_Update()");
  11495. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11496. if (ret != 0) {
  11497. return ret;
  11498. }
  11499. ret = wc_Sha3_224_Update(&sha3, msg, msglen);
  11500. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11501. ret = WOLFSSL_FATAL_ERROR;
  11502. }
  11503. if (ret == 0) {
  11504. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11505. if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11506. ret = WOLFSSL_FATAL_ERROR;
  11507. }
  11508. }
  11509. /* Pass bad args. */
  11510. if (ret == 0) {
  11511. ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
  11512. if (ret == BAD_FUNC_ARG) {
  11513. ret = wc_Sha3_224_Update(&sha3, NULL, 5);
  11514. }
  11515. if (ret == BAD_FUNC_ARG) {
  11516. wc_Sha3_224_Free(&sha3);
  11517. if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
  11518. return ret;
  11519. }
  11520. ret = wc_Sha3_224_Update(&sha3, NULL, 0);
  11521. if (ret == 0) {
  11522. ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
  11523. }
  11524. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11525. ret = WOLFSSL_FATAL_ERROR;
  11526. }
  11527. }
  11528. }
  11529. wc_Sha3_224_Free(&sha3);
  11530. printf(resultFmt, ret == 0 ? passed : failed);
  11531. #endif /* SHA3_224 */
  11532. #if !defined(WOLFSSL_NOSHA3_256)
  11533. if (ret == 0) {
  11534. printf(testingFmt, "wc_Sha3_256_Update()");
  11535. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11536. if (ret != 0) {
  11537. return ret;
  11538. }
  11539. ret = wc_Sha3_256_Update(&sha3, msg, msglen);
  11540. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11541. ret = WOLFSSL_FATAL_ERROR;
  11542. }
  11543. if (ret == 0) {
  11544. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11545. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11546. ret = WOLFSSL_FATAL_ERROR;
  11547. }
  11548. }
  11549. /* Pass bad args. */
  11550. if (ret == 0) {
  11551. ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
  11552. if (ret == BAD_FUNC_ARG) {
  11553. ret = wc_Sha3_256_Update(&sha3, NULL, 5);
  11554. }
  11555. if (ret == BAD_FUNC_ARG) {
  11556. wc_Sha3_256_Free(&sha3);
  11557. if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
  11558. return ret;
  11559. }
  11560. ret = wc_Sha3_256_Update(&sha3, NULL, 0);
  11561. if (ret == 0) {
  11562. ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
  11563. }
  11564. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11565. ret = WOLFSSL_FATAL_ERROR;
  11566. }
  11567. }
  11568. }
  11569. wc_Sha3_256_Free(&sha3);
  11570. printf(resultFmt, ret == 0 ? passed : failed);
  11571. }
  11572. #endif /* SHA3_256 */
  11573. #if !defined(WOLFSSL_NOSHA3_384)
  11574. if (ret == 0) {
  11575. printf(testingFmt, "wc_Sha3_384_Update()");
  11576. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11577. if (ret != 0) {
  11578. return ret;
  11579. }
  11580. ret = wc_Sha3_384_Update(&sha3, msg, msglen);
  11581. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11582. ret = WOLFSSL_FATAL_ERROR;
  11583. }
  11584. if (ret == 0) {
  11585. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11586. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11587. ret = WOLFSSL_FATAL_ERROR;
  11588. }
  11589. }
  11590. /* Pass bad args. */
  11591. if (ret == 0) {
  11592. ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
  11593. if (ret == BAD_FUNC_ARG) {
  11594. ret = wc_Sha3_384_Update(&sha3, NULL, 5);
  11595. }
  11596. if (ret == BAD_FUNC_ARG) {
  11597. wc_Sha3_384_Free(&sha3);
  11598. if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
  11599. return ret;
  11600. }
  11601. ret = wc_Sha3_384_Update(&sha3, NULL, 0);
  11602. if (ret == 0) {
  11603. ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
  11604. }
  11605. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11606. ret = WOLFSSL_FATAL_ERROR;
  11607. }
  11608. }
  11609. }
  11610. wc_Sha3_384_Free(&sha3);
  11611. printf(resultFmt, ret == 0 ? passed : failed);
  11612. }
  11613. #endif /* SHA3_384 */
  11614. #if !defined(WOLFSSL_NOSHA3_512)
  11615. if (ret == 0) {
  11616. printf(testingFmt, "wc_Sha3_512_Update()");
  11617. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11618. if (ret != 0) {
  11619. return ret;
  11620. }
  11621. ret = wc_Sha3_512_Update(&sha3, msg, msglen);
  11622. if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
  11623. ret = WOLFSSL_FATAL_ERROR;
  11624. }
  11625. if (ret == 0) {
  11626. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11627. if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
  11628. ret = WOLFSSL_FATAL_ERROR;
  11629. }
  11630. }
  11631. /* Pass bad args. */
  11632. if (ret == 0) {
  11633. ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
  11634. if (ret == BAD_FUNC_ARG) {
  11635. ret = wc_Sha3_512_Update(&sha3, NULL, 5);
  11636. }
  11637. if (ret == BAD_FUNC_ARG) {
  11638. wc_Sha3_512_Free(&sha3);
  11639. if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
  11640. return ret;
  11641. }
  11642. ret = wc_Sha3_512_Update(&sha3, NULL, 0);
  11643. if (ret == 0) {
  11644. ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
  11645. }
  11646. if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
  11647. ret = WOLFSSL_FATAL_ERROR;
  11648. }
  11649. }
  11650. }
  11651. wc_Sha3_512_Free(&sha3);
  11652. printf(resultFmt, ret == 0 ? passed : failed);
  11653. }
  11654. #endif /* SHA3_512 */
  11655. #endif /* WOLFSSL_SHA3 */
  11656. return ret;
  11657. } /* END testing_wc_Sha3_Update */
  11658. /*
  11659. * Testing wc_Sha3_224_Final()
  11660. */
  11661. static int test_wc_Sha3_224_Final(void)
  11662. {
  11663. int ret = 0;
  11664. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11665. wc_Sha3 sha3;
  11666. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11667. "nopnopq";
  11668. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  11669. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  11670. "\x64\xea\xd0\xfc\xce\x33";
  11671. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11672. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  11673. /* Init stack variables. */
  11674. XMEMSET(hash, 0, sizeof(hash));
  11675. printf(testingFmt, "wc_Sha3_224_Final()");
  11676. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11677. if (ret != 0) {
  11678. return ret;
  11679. }
  11680. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11681. if (ret == 0) {
  11682. ret = wc_Sha3_224_Final(&sha3, hash);
  11683. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11684. ret = WOLFSSL_FATAL_ERROR;
  11685. }
  11686. }
  11687. /* Test bad args. */
  11688. if (ret == 0) {
  11689. ret = wc_Sha3_224_Final(NULL, hash);
  11690. if (ret == 0) {
  11691. ret = wc_Sha3_224_Final(&sha3, NULL);
  11692. }
  11693. if (ret == BAD_FUNC_ARG) {
  11694. ret = 0;
  11695. } else if (ret == 0) {
  11696. ret = WOLFSSL_FATAL_ERROR;
  11697. }
  11698. }
  11699. wc_Sha3_224_Free(&sha3);
  11700. printf(resultFmt, ret == 0 ? passed : failed);
  11701. if (ret == 0) {
  11702. printf(testingFmt, "wc_Sha3_224_GetHash()");
  11703. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11704. if (ret != 0) {
  11705. return ret;
  11706. }
  11707. /* Init stack variables. */
  11708. XMEMSET(hash, 0, sizeof(hash));
  11709. XMEMSET(hashRet, 0, sizeof(hashRet));
  11710. ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11711. if (ret == 0) {
  11712. ret = wc_Sha3_224_GetHash(&sha3, hashRet);
  11713. }
  11714. if (ret == 0) {
  11715. ret = wc_Sha3_224_Final(&sha3, hash);
  11716. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
  11717. ret = WOLFSSL_FATAL_ERROR;
  11718. }
  11719. }
  11720. if (ret == 0) {
  11721. /* Test bad args. */
  11722. ret = wc_Sha3_224_GetHash(NULL, hashRet);
  11723. if (ret == BAD_FUNC_ARG) {
  11724. ret = wc_Sha3_224_GetHash(&sha3, NULL);
  11725. }
  11726. if (ret == BAD_FUNC_ARG) {
  11727. ret = 0;
  11728. } else if (ret == 0) {
  11729. ret = WOLFSSL_FATAL_ERROR;
  11730. }
  11731. }
  11732. printf(resultFmt, ret == 0 ? passed : failed);
  11733. }
  11734. wc_Sha3_224_Free(&sha3);
  11735. #endif
  11736. return ret;
  11737. } /* END test_wc_Sha3_224_Final */
  11738. /*
  11739. * Testing wc_Sha3_256_Final()
  11740. */
  11741. static int test_wc_Sha3_256_Final(void)
  11742. {
  11743. int ret = 0;
  11744. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  11745. wc_Sha3 sha3;
  11746. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11747. "nopnopq";
  11748. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  11749. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  11750. "\xdd\x97\x49\x6d\x33\x76";
  11751. byte hash[WC_SHA3_256_DIGEST_SIZE];
  11752. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  11753. /* Init stack variables. */
  11754. XMEMSET(hash, 0, sizeof(hash));
  11755. printf(testingFmt, "wc_Sha3_256_Final()");
  11756. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11757. if (ret != 0) {
  11758. return ret;
  11759. }
  11760. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11761. if (ret == 0) {
  11762. ret = wc_Sha3_256_Final(&sha3, hash);
  11763. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11764. ret = WOLFSSL_FATAL_ERROR;
  11765. }
  11766. }
  11767. /* Test bad args. */
  11768. if (ret == 0) {
  11769. ret = wc_Sha3_256_Final(NULL, hash);
  11770. if (ret == 0) {
  11771. ret = wc_Sha3_256_Final(&sha3, NULL);
  11772. }
  11773. if (ret == BAD_FUNC_ARG) {
  11774. ret = 0;
  11775. } else if (ret == 0) {
  11776. ret = WOLFSSL_FATAL_ERROR;
  11777. }
  11778. }
  11779. wc_Sha3_256_Free(&sha3);
  11780. printf(resultFmt, ret == 0 ? passed : failed);
  11781. if (ret == 0) {
  11782. printf(testingFmt, "wc_Sha3_256_GetHash()");
  11783. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  11784. if (ret != 0) {
  11785. return ret;
  11786. }
  11787. /* Init stack variables. */
  11788. XMEMSET(hash, 0, sizeof(hash));
  11789. XMEMSET(hashRet, 0, sizeof(hashRet));
  11790. ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11791. if (ret == 0) {
  11792. ret = wc_Sha3_256_GetHash(&sha3, hashRet);
  11793. }
  11794. if (ret == 0) {
  11795. ret = wc_Sha3_256_Final(&sha3, hash);
  11796. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
  11797. ret = WOLFSSL_FATAL_ERROR;
  11798. }
  11799. }
  11800. if (ret == 0) {
  11801. /* Test bad args. */
  11802. ret = wc_Sha3_256_GetHash(NULL, hashRet);
  11803. if (ret == BAD_FUNC_ARG) {
  11804. ret = wc_Sha3_256_GetHash(&sha3, NULL);
  11805. }
  11806. if (ret == BAD_FUNC_ARG) {
  11807. ret = 0;
  11808. } else if (ret == 0) {
  11809. ret = WOLFSSL_FATAL_ERROR;
  11810. }
  11811. }
  11812. printf(resultFmt, ret == 0 ? passed : failed);
  11813. }
  11814. wc_Sha3_256_Free(&sha3);
  11815. #endif
  11816. return ret;
  11817. } /* END test_wc_Sha3_256_Final */
  11818. /*
  11819. * Testing wc_Sha3_384_Final()
  11820. */
  11821. static int test_wc_Sha3_384_Final(void)
  11822. {
  11823. int ret = 0;
  11824. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  11825. wc_Sha3 sha3;
  11826. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11827. "nopnopq";
  11828. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  11829. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  11830. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  11831. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  11832. byte hash[WC_SHA3_384_DIGEST_SIZE];
  11833. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  11834. /* Init stack variables. */
  11835. XMEMSET(hash, 0, sizeof(hash));
  11836. printf(testingFmt, "wc_Sha3_384_Final()");
  11837. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11838. if (ret != 0) {
  11839. return ret;
  11840. }
  11841. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11842. if (ret == 0) {
  11843. ret = wc_Sha3_384_Final(&sha3, hash);
  11844. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11845. ret = WOLFSSL_FATAL_ERROR;
  11846. }
  11847. }
  11848. /* Test bad args. */
  11849. if (ret == 0) {
  11850. ret = wc_Sha3_384_Final(NULL, hash);
  11851. if (ret == 0) {
  11852. ret = wc_Sha3_384_Final(&sha3, NULL);
  11853. }
  11854. if (ret == BAD_FUNC_ARG) {
  11855. ret = 0;
  11856. } else if (ret == 0) {
  11857. ret = WOLFSSL_FATAL_ERROR;
  11858. }
  11859. }
  11860. wc_Sha3_384_Free(&sha3);
  11861. printf(resultFmt, ret == 0 ? passed : failed);
  11862. if (ret == 0) {
  11863. printf(testingFmt, "wc_Sha3_384_GetHash()");
  11864. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  11865. if (ret != 0) {
  11866. return ret;
  11867. }
  11868. /* Init stack variables. */
  11869. XMEMSET(hash, 0, sizeof(hash));
  11870. XMEMSET(hashRet, 0, sizeof(hashRet));
  11871. ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11872. if (ret == 0) {
  11873. ret = wc_Sha3_384_GetHash(&sha3, hashRet);
  11874. }
  11875. if (ret == 0) {
  11876. ret = wc_Sha3_384_Final(&sha3, hash);
  11877. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
  11878. ret = WOLFSSL_FATAL_ERROR;
  11879. }
  11880. }
  11881. if (ret == 0) {
  11882. /* Test bad args. */
  11883. ret = wc_Sha3_384_GetHash(NULL, hashRet);
  11884. if (ret == BAD_FUNC_ARG) {
  11885. ret = wc_Sha3_384_GetHash(&sha3, NULL);
  11886. }
  11887. if (ret == BAD_FUNC_ARG) {
  11888. ret = 0;
  11889. } else if (ret == 0) {
  11890. ret = WOLFSSL_FATAL_ERROR;
  11891. }
  11892. }
  11893. printf(resultFmt, ret == 0 ? passed : failed);
  11894. }
  11895. wc_Sha3_384_Free(&sha3);
  11896. #endif
  11897. return ret;
  11898. } /* END test_wc_Sha3_384_Final */
  11899. /*
  11900. * Testing wc_Sha3_512_Final()
  11901. */
  11902. static int test_wc_Sha3_512_Final(void)
  11903. {
  11904. int ret = 0;
  11905. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  11906. !defined(WOLFSSL_NOSHA3_384)
  11907. wc_Sha3 sha3;
  11908. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  11909. "nopnopq";
  11910. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  11911. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  11912. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  11913. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  11914. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  11915. byte hash[WC_SHA3_512_DIGEST_SIZE];
  11916. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  11917. /* Init stack variables. */
  11918. XMEMSET(hash, 0, sizeof(hash));
  11919. printf(testingFmt, "wc_Sha3_512_Final()");
  11920. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11921. if (ret != 0) {
  11922. return ret;
  11923. }
  11924. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11925. if (ret == 0) {
  11926. ret = wc_Sha3_512_Final(&sha3, hash);
  11927. if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11928. ret = WOLFSSL_FATAL_ERROR;
  11929. }
  11930. }
  11931. /* Test bad args. */
  11932. if (ret == 0) {
  11933. ret = wc_Sha3_512_Final(NULL, hash);
  11934. if (ret == 0) {
  11935. ret = wc_Sha3_384_Final(&sha3, NULL);
  11936. }
  11937. if (ret == BAD_FUNC_ARG) {
  11938. ret = 0;
  11939. } else if (ret == 0) {
  11940. ret = WOLFSSL_FATAL_ERROR;
  11941. }
  11942. }
  11943. wc_Sha3_512_Free(&sha3);
  11944. printf(resultFmt, ret == 0 ? passed : failed);
  11945. if (ret == 0) {
  11946. printf(testingFmt, "wc_Sha3_512_GetHash()");
  11947. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  11948. if (ret != 0) {
  11949. return ret;
  11950. }
  11951. /* Init stack variables. */
  11952. XMEMSET(hash, 0, sizeof(hash));
  11953. XMEMSET(hashRet, 0, sizeof(hashRet));
  11954. ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
  11955. if (ret == 0) {
  11956. ret = wc_Sha3_512_GetHash(&sha3, hashRet);
  11957. }
  11958. if (ret == 0) {
  11959. ret = wc_Sha3_512_Final(&sha3, hash);
  11960. if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
  11961. ret = WOLFSSL_FATAL_ERROR;
  11962. }
  11963. }
  11964. if (ret == 0) {
  11965. /* Test bad args. */
  11966. ret = wc_Sha3_512_GetHash(NULL, hashRet);
  11967. if (ret == BAD_FUNC_ARG) {
  11968. ret = wc_Sha3_512_GetHash(&sha3, NULL);
  11969. }
  11970. if (ret == BAD_FUNC_ARG) {
  11971. ret = 0;
  11972. } else if (ret == 0) {
  11973. ret = WOLFSSL_FATAL_ERROR;
  11974. }
  11975. }
  11976. printf(resultFmt, ret == 0 ? passed : failed);
  11977. }
  11978. wc_Sha3_512_Free(&sha3);
  11979. #endif
  11980. return ret;
  11981. } /* END test_wc_Sha3_512_Final */
  11982. /*
  11983. * Testing wc_Sha3_224_Copy()
  11984. */
  11985. static int test_wc_Sha3_224_Copy(void)
  11986. {
  11987. int ret = 0;
  11988. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  11989. wc_Sha3 sha3, sha3Cpy;
  11990. const char* msg = TEST_STRING;
  11991. word32 msglen = (word32)TEST_STRING_SZ;
  11992. byte hash[WC_SHA3_224_DIGEST_SIZE];
  11993. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  11994. XMEMSET(hash, 0, sizeof(hash));
  11995. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  11996. printf(testingFmt, "wc_Sha3_224_Copy()");
  11997. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  11998. if (ret != 0) {
  11999. return ret;
  12000. }
  12001. ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
  12002. if (ret != 0) {
  12003. wc_Sha3_224_Free(&sha3);
  12004. return ret;
  12005. }
  12006. ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
  12007. if (ret == 0) {
  12008. ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
  12009. if (ret == 0) {
  12010. ret = wc_Sha3_224_Final(&sha3, hash);
  12011. if (ret == 0) {
  12012. ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
  12013. }
  12014. }
  12015. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12016. ret = WOLFSSL_FATAL_ERROR;
  12017. }
  12018. }
  12019. /* Test bad args. */
  12020. if (ret == 0) {
  12021. ret = wc_Sha3_224_Copy(NULL, &sha3);
  12022. if (ret == BAD_FUNC_ARG) {
  12023. ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
  12024. }
  12025. if (ret == BAD_FUNC_ARG) {
  12026. ret = 0;
  12027. } else if (ret == 0) {
  12028. ret = WOLFSSL_FATAL_ERROR;
  12029. }
  12030. }
  12031. printf(resultFmt, ret == 0 ? passed : failed);
  12032. #endif
  12033. return ret;
  12034. } /* END test_wc_Sha3_224_Copy */
  12035. /*
  12036. * Testing wc_Sha3_256_Copy()
  12037. */
  12038. static int test_wc_Sha3_256_Copy(void)
  12039. {
  12040. int ret = 0;
  12041. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  12042. wc_Sha3 sha3, sha3Cpy;
  12043. const char* msg = TEST_STRING;
  12044. word32 msglen = (word32)TEST_STRING_SZ;
  12045. byte hash[WC_SHA3_256_DIGEST_SIZE];
  12046. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  12047. XMEMSET(hash, 0, sizeof(hash));
  12048. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12049. printf(testingFmt, "wc_Sha3_256_Copy()");
  12050. ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
  12051. if (ret != 0) {
  12052. return ret;
  12053. }
  12054. ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
  12055. if (ret != 0) {
  12056. wc_Sha3_256_Free(&sha3);
  12057. return ret;
  12058. }
  12059. ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
  12060. if (ret == 0) {
  12061. ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
  12062. if (ret == 0) {
  12063. ret = wc_Sha3_256_Final(&sha3, hash);
  12064. if (ret == 0) {
  12065. ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
  12066. }
  12067. }
  12068. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12069. ret = WOLFSSL_FATAL_ERROR;
  12070. }
  12071. }
  12072. /* Test bad args. */
  12073. if (ret == 0) {
  12074. ret = wc_Sha3_256_Copy(NULL, &sha3);
  12075. if (ret == BAD_FUNC_ARG) {
  12076. ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
  12077. }
  12078. if (ret == BAD_FUNC_ARG) {
  12079. ret = 0;
  12080. } else if (ret == 0) {
  12081. ret = WOLFSSL_FATAL_ERROR;
  12082. }
  12083. }
  12084. printf(resultFmt, ret == 0 ? passed : failed);
  12085. #endif
  12086. return ret;
  12087. } /* END test_wc_Sha3_256_Copy */
  12088. /*
  12089. * Testing wc_Sha3_384_Copy()
  12090. */
  12091. static int test_wc_Sha3_384_Copy(void)
  12092. {
  12093. int ret = 0;
  12094. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  12095. wc_Sha3 sha3, sha3Cpy;
  12096. const char* msg = TEST_STRING;
  12097. word32 msglen = (word32)TEST_STRING_SZ;
  12098. byte hash[WC_SHA3_384_DIGEST_SIZE];
  12099. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  12100. XMEMSET(hash, 0, sizeof(hash));
  12101. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12102. printf(testingFmt, "wc_Sha3_384_Copy()");
  12103. ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
  12104. if (ret != 0) {
  12105. return ret;
  12106. }
  12107. ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
  12108. if (ret != 0) {
  12109. wc_Sha3_384_Free(&sha3);
  12110. return ret;
  12111. }
  12112. ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
  12113. if (ret == 0) {
  12114. ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
  12115. if (ret == 0) {
  12116. ret = wc_Sha3_384_Final(&sha3, hash);
  12117. if (ret == 0) {
  12118. ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
  12119. }
  12120. }
  12121. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12122. ret = WOLFSSL_FATAL_ERROR;
  12123. }
  12124. }
  12125. /* Test bad args. */
  12126. if (ret == 0) {
  12127. ret = wc_Sha3_384_Copy(NULL, &sha3);
  12128. if (ret == BAD_FUNC_ARG) {
  12129. ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
  12130. }
  12131. if (ret == BAD_FUNC_ARG) {
  12132. ret = 0;
  12133. } else if (ret == 0) {
  12134. ret = WOLFSSL_FATAL_ERROR;
  12135. }
  12136. }
  12137. printf(resultFmt, ret == 0 ? passed : failed);
  12138. #endif
  12139. return ret;
  12140. } /* END test_wc_Sha3_384_Copy */
  12141. /*
  12142. * Testing wc_Sha3_512_Copy()
  12143. */
  12144. static int test_wc_Sha3_512_Copy(void)
  12145. {
  12146. int ret = 0;
  12147. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  12148. wc_Sha3 sha3, sha3Cpy;
  12149. const char* msg = TEST_STRING;
  12150. word32 msglen = (word32)TEST_STRING_SZ;
  12151. byte hash[WC_SHA3_512_DIGEST_SIZE];
  12152. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  12153. XMEMSET(hash, 0, sizeof(hash));
  12154. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12155. printf(testingFmt, "wc_Sha3_512_Copy()");
  12156. ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
  12157. if (ret != 0) {
  12158. return ret;
  12159. }
  12160. ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
  12161. if (ret != 0) {
  12162. wc_Sha3_512_Free(&sha3);
  12163. return ret;
  12164. }
  12165. ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
  12166. if (ret == 0) {
  12167. ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
  12168. if (ret == 0) {
  12169. ret = wc_Sha3_512_Final(&sha3, hash);
  12170. if (ret == 0) {
  12171. ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
  12172. }
  12173. }
  12174. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12175. ret = WOLFSSL_FATAL_ERROR;
  12176. }
  12177. }
  12178. /* Test bad args. */
  12179. if (ret == 0) {
  12180. ret = wc_Sha3_512_Copy(NULL, &sha3);
  12181. if (ret == BAD_FUNC_ARG) {
  12182. ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
  12183. }
  12184. if (ret == BAD_FUNC_ARG) {
  12185. ret = 0;
  12186. } else if (ret == 0) {
  12187. ret = WOLFSSL_FATAL_ERROR;
  12188. }
  12189. }
  12190. printf(resultFmt, ret == 0 ? passed : failed);
  12191. #endif
  12192. return ret;
  12193. } /* END test_wc_Sha3_512_Copy */
  12194. /*
  12195. * Unit test function for wc_Sha3_GetFlags()
  12196. */
  12197. static int test_wc_Sha3_GetFlags(void)
  12198. {
  12199. int ret = 0;
  12200. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  12201. wc_Sha3 sha3;
  12202. word32 flags = 0;
  12203. printf(testingFmt, "wc_Sha3_GetFlags()");
  12204. /* Initialize */
  12205. ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
  12206. if (ret != 0) {
  12207. return ret;
  12208. }
  12209. if (ret == 0) {
  12210. ret = wc_Sha3_GetFlags(&sha3, &flags);
  12211. }
  12212. if (ret == 0) {
  12213. if (flags & WC_HASH_FLAG_ISCOPY) {
  12214. ret = 0;
  12215. }
  12216. }
  12217. wc_Sha3_224_Free(&sha3);
  12218. printf(resultFmt, ret == 0 ? passed : failed);
  12219. #endif
  12220. return ret;
  12221. } /* END test_wc_Sha3_GetFlags */
  12222. static int test_wc_InitShake256(void)
  12223. {
  12224. int ret = 0;
  12225. #ifdef WOLFSSL_SHAKE256
  12226. wc_Shake shake;
  12227. printf(testingFmt, "wc_InitShake256()");
  12228. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12229. /* Test bad args. */
  12230. if (ret == 0) {
  12231. ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
  12232. if (ret == BAD_FUNC_ARG) {
  12233. ret = 0;
  12234. } else if (ret == 0) {
  12235. ret = WOLFSSL_FATAL_ERROR;
  12236. }
  12237. }
  12238. wc_Shake256_Free(&shake);
  12239. printf(resultFmt, ret == 0 ? passed : failed);
  12240. #endif
  12241. return ret;
  12242. } /* END test_wc_InitSha3 */
  12243. static int testing_wc_Shake256_Update(void)
  12244. {
  12245. int ret = 0;
  12246. #ifdef WOLFSSL_SHAKE256
  12247. wc_Shake shake;
  12248. byte msg[] = "Everybody's working for the weekend.";
  12249. byte msg2[] = "Everybody gets Friday off.";
  12250. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  12251. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  12252. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  12253. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  12254. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  12255. word32 msglen = sizeof(msg) - 1;
  12256. word32 msg2len = sizeof(msg2);
  12257. word32 msgCmplen = sizeof(msgCmp);
  12258. printf(testingFmt, "wc_Shake256_Update()");
  12259. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12260. if (ret != 0) {
  12261. return ret;
  12262. }
  12263. ret = wc_Shake256_Update(&shake, msg, msglen);
  12264. if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
  12265. ret = WOLFSSL_FATAL_ERROR;
  12266. }
  12267. if (ret == 0) {
  12268. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12269. if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
  12270. ret = WOLFSSL_FATAL_ERROR;
  12271. }
  12272. }
  12273. /* Pass bad args. */
  12274. if (ret == 0) {
  12275. ret = wc_Shake256_Update(NULL, msg2, msg2len);
  12276. if (ret == BAD_FUNC_ARG) {
  12277. ret = wc_Shake256_Update(&shake, NULL, 5);
  12278. }
  12279. if (ret == BAD_FUNC_ARG) {
  12280. wc_Shake256_Free(&shake);
  12281. if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
  12282. return ret;
  12283. }
  12284. ret = wc_Shake256_Update(&shake, NULL, 0);
  12285. if (ret == 0) {
  12286. ret = wc_Shake256_Update(&shake, msg2, msg2len);
  12287. }
  12288. if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
  12289. ret = WOLFSSL_FATAL_ERROR;
  12290. }
  12291. }
  12292. }
  12293. wc_Shake256_Free(&shake);
  12294. printf(resultFmt, ret == 0 ? passed : failed);
  12295. #endif /* WOLFSSL_SHAKE256 */
  12296. return ret;
  12297. }
  12298. static int test_wc_Shake256_Final(void)
  12299. {
  12300. int ret = 0;
  12301. #ifdef WOLFSSL_SHAKE256
  12302. wc_Shake shake;
  12303. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  12304. "nopnopq";
  12305. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  12306. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  12307. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  12308. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  12309. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  12310. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  12311. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  12312. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  12313. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  12314. byte hash[114];
  12315. /* Init stack variables. */
  12316. XMEMSET(hash, 0, sizeof(hash));
  12317. printf(testingFmt, "wc_Shake256_Final()");
  12318. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12319. if (ret != 0) {
  12320. return ret;
  12321. }
  12322. ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
  12323. if (ret == 0) {
  12324. ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
  12325. if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
  12326. ret = WOLFSSL_FATAL_ERROR;
  12327. }
  12328. }
  12329. /* Test bad args. */
  12330. if (ret == 0) {
  12331. ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
  12332. if (ret == 0) {
  12333. ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
  12334. }
  12335. if (ret == BAD_FUNC_ARG) {
  12336. ret = 0;
  12337. } else if (ret == 0) {
  12338. ret = WOLFSSL_FATAL_ERROR;
  12339. }
  12340. }
  12341. wc_Shake256_Free(&shake);
  12342. printf(resultFmt, ret == 0 ? passed : failed);
  12343. #endif
  12344. return ret;
  12345. }
  12346. /*
  12347. * Testing wc_Shake256_Copy()
  12348. */
  12349. static int test_wc_Shake256_Copy(void)
  12350. {
  12351. int ret = 0;
  12352. #ifdef WOLFSSL_SHAKE256
  12353. wc_Shake shake, shakeCpy;
  12354. const char* msg = TEST_STRING;
  12355. word32 msglen = (word32)TEST_STRING_SZ;
  12356. byte hash[144];
  12357. byte hashCpy[144];
  12358. word32 hashLen = sizeof(hash);
  12359. word32 hashLenCpy = sizeof(hashCpy);
  12360. XMEMSET(hash, 0, sizeof(hash));
  12361. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  12362. printf(testingFmt, "wc_Shake256_Copy()");
  12363. ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
  12364. if (ret != 0) {
  12365. return ret;
  12366. }
  12367. ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
  12368. if (ret != 0) {
  12369. wc_Shake256_Free(&shake);
  12370. return ret;
  12371. }
  12372. ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
  12373. if (ret == 0) {
  12374. ret = wc_Shake256_Copy(&shakeCpy, &shake);
  12375. if (ret == 0) {
  12376. ret = wc_Shake256_Final(&shake, hash, hashLen);
  12377. if (ret == 0) {
  12378. ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
  12379. }
  12380. }
  12381. if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
  12382. ret = WOLFSSL_FATAL_ERROR;
  12383. }
  12384. }
  12385. /* Test bad args. */
  12386. if (ret == 0) {
  12387. ret = wc_Shake256_Copy(NULL, &shake);
  12388. if (ret == BAD_FUNC_ARG) {
  12389. ret = wc_Shake256_Copy(&shakeCpy, NULL);
  12390. }
  12391. if (ret == BAD_FUNC_ARG) {
  12392. ret = 0;
  12393. } else if (ret == 0) {
  12394. ret = WOLFSSL_FATAL_ERROR;
  12395. }
  12396. }
  12397. wc_Shake256_Free(&shake);
  12398. printf(resultFmt, ret == 0 ? passed : failed);
  12399. #endif
  12400. return ret;
  12401. } /* END test_wc_Shake256_Copy */
  12402. /*
  12403. * Unit test function for wc_Shake256Hash()
  12404. */
  12405. static int test_wc_Shake256Hash(void)
  12406. {
  12407. int ret = 0;
  12408. #ifdef WOLFSSL_SHAKE256
  12409. const byte data[] = { /* Hello World */
  12410. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  12411. 0x72,0x6c,0x64
  12412. };
  12413. word32 len = sizeof(data);
  12414. byte hash[144];
  12415. word32 hashLen = sizeof(hash);
  12416. printf(testingFmt, "wc_Shake256Hash()");
  12417. ret = wc_Shake256Hash(data, len, hash, hashLen);
  12418. printf(resultFmt, ret == 0 ? passed : failed);
  12419. #endif
  12420. return ret;
  12421. } /* END test_wc_Shake256Hash */
  12422. /*
  12423. * Test function for wc_HmacSetKey
  12424. */
  12425. static int test_wc_Md5HmacSetKey(void)
  12426. {
  12427. int flag = 0;
  12428. #if !defined(NO_HMAC) && !defined(NO_MD5)
  12429. Hmac hmac;
  12430. int ret, times, itr;
  12431. const char* keys[]=
  12432. {
  12433. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  12434. #ifndef HAVE_FIPS
  12435. "Jefe", /* smaller than minimum FIPS key size */
  12436. #endif
  12437. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12438. };
  12439. times = sizeof(keys) / sizeof(char*);
  12440. flag = 0;
  12441. printf(testingFmt, "wc_HmacSetKey() with MD5");
  12442. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12443. if (ret != 0)
  12444. return ret;
  12445. for (itr = 0; itr < times; itr++) {
  12446. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  12447. (word32)XSTRLEN(keys[itr]));
  12448. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  12449. wc_HmacFree(&hmac);
  12450. if (ret == BAD_FUNC_ARG)
  12451. return 0;
  12452. else {
  12453. return WOLFSSL_FATAL_ERROR;
  12454. }
  12455. #else
  12456. if (ret != 0) {
  12457. flag = ret;
  12458. }
  12459. #endif
  12460. }
  12461. /* Bad args. */
  12462. if (!flag) {
  12463. ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  12464. (word32)XSTRLEN(keys[0]));
  12465. if (ret != BAD_FUNC_ARG) {
  12466. flag = WOLFSSL_FATAL_ERROR;
  12467. }
  12468. }
  12469. if (!flag) {
  12470. ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
  12471. if (ret != BAD_FUNC_ARG) {
  12472. flag = WOLFSSL_FATAL_ERROR;
  12473. }
  12474. }
  12475. if (!flag) {
  12476. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12477. (word32)XSTRLEN(keys[0]));
  12478. if (ret != BAD_FUNC_ARG) {
  12479. flag = WOLFSSL_FATAL_ERROR;
  12480. }
  12481. }
  12482. if (!flag) {
  12483. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  12484. #ifdef HAVE_FIPS
  12485. if (ret != HMAC_MIN_KEYLEN_E) {
  12486. flag = WOLFSSL_FATAL_ERROR;
  12487. }
  12488. #else
  12489. if (ret != 0) {
  12490. flag = WOLFSSL_FATAL_ERROR;
  12491. }
  12492. #endif
  12493. }
  12494. wc_HmacFree(&hmac);
  12495. printf(resultFmt, flag == 0 ? passed : failed);
  12496. #endif
  12497. return flag;
  12498. } /* END test_wc_Md5HmacSetKey */
  12499. /*
  12500. * testing wc_HmacSetKey() on wc_Sha hash.
  12501. */
  12502. static int test_wc_ShaHmacSetKey(void)
  12503. {
  12504. int flag = 0;
  12505. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12506. Hmac hmac;
  12507. int ret, times, itr;
  12508. const char* keys[]=
  12509. {
  12510. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12511. "\x0b\x0b\x0b",
  12512. #ifndef HAVE_FIPS
  12513. "Jefe", /* smaller than minimum FIPS key size */
  12514. #endif
  12515. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12516. "\xAA\xAA\xAA"
  12517. };
  12518. times = sizeof(keys) / sizeof(char*);
  12519. flag = 0;
  12520. printf(testingFmt, "wc_HmacSetKey() with SHA");
  12521. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12522. if (ret != 0)
  12523. return ret;
  12524. for (itr = 0; itr < times; itr++) {
  12525. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  12526. (word32)XSTRLEN(keys[itr]));
  12527. if (ret != 0) {
  12528. flag = ret;
  12529. }
  12530. }
  12531. /* Bad args. */
  12532. if (!flag) {
  12533. ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  12534. (word32)XSTRLEN(keys[0]));
  12535. if (ret != BAD_FUNC_ARG) {
  12536. flag = WOLFSSL_FATAL_ERROR;
  12537. }
  12538. }
  12539. if (!flag) {
  12540. ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
  12541. if (ret != BAD_FUNC_ARG) {
  12542. flag = WOLFSSL_FATAL_ERROR;
  12543. }
  12544. }
  12545. if (!flag) {
  12546. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12547. (word32)XSTRLEN(keys[0]));
  12548. if (ret != BAD_FUNC_ARG) {
  12549. flag = WOLFSSL_FATAL_ERROR;
  12550. }
  12551. }
  12552. if (!flag) {
  12553. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  12554. #ifdef HAVE_FIPS
  12555. if (ret != HMAC_MIN_KEYLEN_E) {
  12556. flag = WOLFSSL_FATAL_ERROR;
  12557. }
  12558. #else
  12559. if (ret != 0) {
  12560. flag = WOLFSSL_FATAL_ERROR;
  12561. }
  12562. #endif
  12563. }
  12564. wc_HmacFree(&hmac);
  12565. printf(resultFmt, flag == 0 ? passed : failed);
  12566. #endif
  12567. return flag;
  12568. } /* END test_wc_ShaHmacSetKey() */
  12569. /*
  12570. * testing wc_HmacSetKey() on Sha224 hash.
  12571. */
  12572. static int test_wc_Sha224HmacSetKey(void)
  12573. {
  12574. int flag = 0;
  12575. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12576. Hmac hmac;
  12577. int ret, times, itr;
  12578. const char* keys[]=
  12579. {
  12580. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12581. "\x0b\x0b\x0b",
  12582. #ifndef HAVE_FIPS
  12583. "Jefe", /* smaller than minimum FIPS key size */
  12584. #endif
  12585. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12586. "\xAA\xAA\xAA"
  12587. };
  12588. times = sizeof(keys) / sizeof(char*);
  12589. flag = 0;
  12590. printf(testingFmt, "wc_HmacSetKey() with SHA 224");
  12591. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12592. if (ret != 0)
  12593. return ret;
  12594. for (itr = 0; itr < times; itr++) {
  12595. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  12596. (word32)XSTRLEN(keys[itr]));
  12597. if (ret != 0) {
  12598. flag = ret;
  12599. }
  12600. }
  12601. /* Bad args. */
  12602. if (!flag) {
  12603. ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  12604. (word32)XSTRLEN(keys[0]));
  12605. if (ret != BAD_FUNC_ARG) {
  12606. flag = WOLFSSL_FATAL_ERROR;
  12607. }
  12608. }
  12609. if (!flag) {
  12610. ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
  12611. if (ret != BAD_FUNC_ARG) {
  12612. flag = WOLFSSL_FATAL_ERROR;
  12613. }
  12614. }
  12615. if (!flag) {
  12616. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12617. (word32)XSTRLEN(keys[0]));
  12618. if (ret != BAD_FUNC_ARG) {
  12619. flag = WOLFSSL_FATAL_ERROR;
  12620. }
  12621. }
  12622. if (!flag) {
  12623. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  12624. #ifdef HAVE_FIPS
  12625. if (ret != HMAC_MIN_KEYLEN_E) {
  12626. flag = WOLFSSL_FATAL_ERROR;
  12627. }
  12628. #else
  12629. if (ret != 0) {
  12630. flag = WOLFSSL_FATAL_ERROR;
  12631. }
  12632. #endif
  12633. }
  12634. wc_HmacFree(&hmac);
  12635. printf(resultFmt, flag == 0 ? passed : failed);
  12636. #endif
  12637. return flag;
  12638. } /* END test_wc_Sha224HmacSetKey() */
  12639. /*
  12640. * testing wc_HmacSetKey() on Sha256 hash
  12641. */
  12642. static int test_wc_Sha256HmacSetKey(void)
  12643. {
  12644. int flag = 0;
  12645. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12646. Hmac hmac;
  12647. int ret, times, itr;
  12648. const char* keys[]=
  12649. {
  12650. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12651. "\x0b\x0b\x0b",
  12652. #ifndef HAVE_FIPS
  12653. "Jefe", /* smaller than minimum FIPS key size */
  12654. #endif
  12655. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12656. "\xAA\xAA\xAA"
  12657. };
  12658. times = sizeof(keys) / sizeof(char*);
  12659. flag = 0;
  12660. printf(testingFmt, "wc_HmacSetKey() with SHA256");
  12661. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12662. if (ret != 0)
  12663. return ret;
  12664. for (itr = 0; itr < times; itr++) {
  12665. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  12666. (word32)XSTRLEN(keys[itr]));
  12667. if (ret != 0) {
  12668. flag = ret;
  12669. }
  12670. }
  12671. /* Bad args. */
  12672. if (!flag) {
  12673. ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  12674. (word32)XSTRLEN(keys[0]));
  12675. if (ret != BAD_FUNC_ARG) {
  12676. flag = WOLFSSL_FATAL_ERROR;
  12677. }
  12678. }
  12679. if (!flag) {
  12680. ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
  12681. if (ret != BAD_FUNC_ARG) {
  12682. flag = WOLFSSL_FATAL_ERROR;
  12683. }
  12684. }
  12685. if (!flag) {
  12686. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12687. (word32)XSTRLEN(keys[0]));
  12688. if (ret != BAD_FUNC_ARG) {
  12689. flag = WOLFSSL_FATAL_ERROR;
  12690. }
  12691. }
  12692. if (!flag) {
  12693. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  12694. #ifdef HAVE_FIPS
  12695. if (ret != HMAC_MIN_KEYLEN_E) {
  12696. flag = WOLFSSL_FATAL_ERROR;
  12697. }
  12698. #else
  12699. if (ret != 0) {
  12700. flag = WOLFSSL_FATAL_ERROR;
  12701. }
  12702. #endif
  12703. }
  12704. wc_HmacFree(&hmac);
  12705. printf(resultFmt, flag == 0 ? passed : failed);
  12706. #endif
  12707. return flag;
  12708. } /* END test_wc_Sha256HmacSetKey() */
  12709. /*
  12710. * testing wc_HmacSetKey on Sha384 hash.
  12711. */
  12712. static int test_wc_Sha384HmacSetKey(void)
  12713. {
  12714. int flag = 0;
  12715. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  12716. Hmac hmac;
  12717. int ret, times, itr;
  12718. const char* keys[]=
  12719. {
  12720. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  12721. "\x0b\x0b\x0b",
  12722. #ifndef HAVE_FIPS
  12723. "Jefe", /* smaller than minimum FIPS key size */
  12724. #endif
  12725. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  12726. "\xAA\xAA\xAA"
  12727. };
  12728. times = sizeof(keys) / sizeof(char*);
  12729. flag = 0;
  12730. printf(testingFmt, "wc_HmacSetKey() with SHA384");
  12731. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12732. if (ret != 0)
  12733. return ret;
  12734. for (itr = 0; itr < times; itr++) {
  12735. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  12736. (word32)XSTRLEN(keys[itr]));
  12737. if (ret != 0) {
  12738. flag = ret;
  12739. }
  12740. }
  12741. /* Bad args. */
  12742. if (!flag) {
  12743. ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  12744. (word32)XSTRLEN(keys[0]));
  12745. if (ret != BAD_FUNC_ARG) {
  12746. flag = WOLFSSL_FATAL_ERROR;
  12747. }
  12748. }
  12749. if (!flag) {
  12750. ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
  12751. if (ret != BAD_FUNC_ARG) {
  12752. flag = WOLFSSL_FATAL_ERROR;
  12753. }
  12754. }
  12755. if (!flag) {
  12756. ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
  12757. (word32)XSTRLEN(keys[0]));
  12758. if (ret != BAD_FUNC_ARG) {
  12759. flag = WOLFSSL_FATAL_ERROR;
  12760. }
  12761. }
  12762. if (!flag) {
  12763. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  12764. #ifdef HAVE_FIPS
  12765. if (ret != HMAC_MIN_KEYLEN_E) {
  12766. flag = WOLFSSL_FATAL_ERROR;
  12767. }
  12768. #else
  12769. if (ret != 0) {
  12770. flag = WOLFSSL_FATAL_ERROR;
  12771. }
  12772. #endif
  12773. }
  12774. wc_HmacFree(&hmac);
  12775. printf(resultFmt, flag == 0 ? passed : failed);
  12776. #endif
  12777. return flag;
  12778. } /* END test_wc_Sha384HmacSetKey() */
  12779. /*
  12780. * testing wc_HmacUpdate on wc_Md5 hash.
  12781. */
  12782. static int test_wc_Md5HmacUpdate(void)
  12783. {
  12784. int flag = 0;
  12785. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  12786. Hmac hmac;
  12787. testVector a, b;
  12788. int ret;
  12789. #ifdef HAVE_FIPS
  12790. const char* keys =
  12791. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12792. #else
  12793. const char* keys = "Jefe";
  12794. #endif
  12795. a.input = "what do ya want for nothing?";
  12796. a.inLen = XSTRLEN(a.input);
  12797. b.input = "Hi There";
  12798. b.inLen = XSTRLEN(b.input);
  12799. flag = 0;
  12800. printf(testingFmt, "wc_HmacUpdate() with MD5");
  12801. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12802. if (ret != 0)
  12803. return ret;
  12804. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
  12805. if (ret != 0) {
  12806. flag = ret;
  12807. }
  12808. if (!flag) {
  12809. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12810. if (ret != 0) {
  12811. flag = ret;
  12812. }
  12813. }
  12814. /* Update Hmac. */
  12815. if (!flag) {
  12816. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12817. if (ret != 0) {
  12818. flag = ret;
  12819. }
  12820. }
  12821. /* Test bad args. */
  12822. if (!flag) {
  12823. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12824. if (ret != BAD_FUNC_ARG) {
  12825. flag = WOLFSSL_FATAL_ERROR;
  12826. }
  12827. }
  12828. if (!flag) {
  12829. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12830. if (ret != BAD_FUNC_ARG) {
  12831. flag = WOLFSSL_FATAL_ERROR;
  12832. }
  12833. }
  12834. if (!flag) {
  12835. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12836. if (ret != 0) {
  12837. flag = ret;
  12838. }
  12839. }
  12840. wc_HmacFree(&hmac);
  12841. printf(resultFmt, flag == 0 ? passed : failed);
  12842. #endif
  12843. return flag;
  12844. } /* END test_wc_Md5HmacUpdate */
  12845. /*
  12846. * testing wc_HmacUpdate on SHA hash.
  12847. */
  12848. static int test_wc_ShaHmacUpdate(void)
  12849. {
  12850. int flag = 0;
  12851. #if !defined(NO_HMAC) && !defined(NO_SHA)
  12852. Hmac hmac;
  12853. testVector a, b;
  12854. int ret;
  12855. #ifdef HAVE_FIPS
  12856. const char* keys =
  12857. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12858. #else
  12859. const char* keys = "Jefe";
  12860. #endif
  12861. a.input = "what do ya want for nothing?";
  12862. a.inLen = XSTRLEN(a.input);
  12863. b.input = "Hi There";
  12864. b.inLen = XSTRLEN(b.input);
  12865. flag = 0;
  12866. printf(testingFmt, "wc_HmacUpdate() with SHA");
  12867. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12868. if (ret != 0)
  12869. return ret;
  12870. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
  12871. if (ret != 0) {
  12872. flag = ret;
  12873. }
  12874. if (!flag) {
  12875. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12876. if (ret != 0) {
  12877. flag = ret;
  12878. }
  12879. }
  12880. /* Update Hmac. */
  12881. if (!flag) {
  12882. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12883. if (ret != 0) {
  12884. flag = ret;
  12885. }
  12886. }
  12887. /* Test bad args. */
  12888. if (!flag) {
  12889. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12890. if (ret != BAD_FUNC_ARG) {
  12891. flag = WOLFSSL_FATAL_ERROR;
  12892. }
  12893. }
  12894. if (!flag) {
  12895. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12896. if (ret != BAD_FUNC_ARG) {
  12897. flag = WOLFSSL_FATAL_ERROR;
  12898. }
  12899. }
  12900. if (!flag) {
  12901. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12902. if (ret != 0) {
  12903. flag = ret;
  12904. }
  12905. }
  12906. wc_HmacFree(&hmac);
  12907. printf(resultFmt, flag == 0 ? passed : failed);
  12908. #endif
  12909. return flag;
  12910. } /* END test_wc_ShaHmacUpdate */
  12911. /*
  12912. * testing wc_HmacUpdate on SHA224 hash.
  12913. */
  12914. static int test_wc_Sha224HmacUpdate(void)
  12915. {
  12916. int flag = 0;
  12917. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  12918. Hmac hmac;
  12919. testVector a, b;
  12920. int ret;
  12921. #ifdef HAVE_FIPS
  12922. const char* keys =
  12923. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12924. #else
  12925. const char* keys = "Jefe";
  12926. #endif
  12927. a.input = "what do ya want for nothing?";
  12928. a.inLen = XSTRLEN(a.input);
  12929. b.input = "Hi There";
  12930. b.inLen = XSTRLEN(b.input);
  12931. flag = 0;
  12932. printf(testingFmt, "wc_HmacUpdate() with SHA224");
  12933. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  12934. if (ret != 0)
  12935. return ret;
  12936. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
  12937. if (ret != 0) {
  12938. flag = ret;
  12939. }
  12940. if (!flag) {
  12941. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  12942. if (ret != 0) {
  12943. flag = ret;
  12944. }
  12945. }
  12946. /* Update Hmac. */
  12947. if (!flag) {
  12948. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  12949. if (ret != 0) {
  12950. flag = ret;
  12951. }
  12952. }
  12953. /* Test bad args. */
  12954. if (!flag) {
  12955. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  12956. if (ret != BAD_FUNC_ARG) {
  12957. flag = WOLFSSL_FATAL_ERROR;
  12958. }
  12959. }
  12960. if (!flag) {
  12961. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  12962. if (ret != BAD_FUNC_ARG) {
  12963. flag = WOLFSSL_FATAL_ERROR;
  12964. }
  12965. }
  12966. if (!flag) {
  12967. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  12968. if (ret != 0) {
  12969. flag = ret;
  12970. }
  12971. }
  12972. wc_HmacFree(&hmac);
  12973. printf(resultFmt, flag == 0 ? passed : failed);
  12974. #endif
  12975. return flag;
  12976. } /* END test_wc_Sha224HmacUpdate */
  12977. /*
  12978. * testing wc_HmacUpdate on SHA256 hash.
  12979. */
  12980. static int test_wc_Sha256HmacUpdate(void)
  12981. {
  12982. int flag = 0;
  12983. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  12984. Hmac hmac;
  12985. testVector a, b;
  12986. int ret;
  12987. #ifdef HAVE_FIPS
  12988. const char* keys =
  12989. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  12990. #else
  12991. const char* keys = "Jefe";
  12992. #endif
  12993. a.input = "what do ya want for nothing?";
  12994. a.inLen = XSTRLEN(a.input);
  12995. b.input = "Hi There";
  12996. b.inLen = XSTRLEN(b.input);
  12997. flag = 0;
  12998. printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
  12999. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13000. if (ret != 0)
  13001. return ret;
  13002. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
  13003. if (ret != 0) {
  13004. flag = ret;
  13005. }
  13006. if (!flag) {
  13007. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13008. if (ret != 0) {
  13009. flag = ret;
  13010. }
  13011. }
  13012. /* Update Hmac. */
  13013. if (!flag) {
  13014. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13015. if (ret != 0) {
  13016. flag = ret;
  13017. }
  13018. }
  13019. /* Test bad args. */
  13020. if (!flag) {
  13021. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13022. if (ret != BAD_FUNC_ARG) {
  13023. flag = WOLFSSL_FATAL_ERROR;
  13024. }
  13025. }
  13026. if (!flag) {
  13027. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13028. if (ret != BAD_FUNC_ARG) {
  13029. flag = WOLFSSL_FATAL_ERROR;
  13030. }
  13031. }
  13032. if (!flag) {
  13033. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13034. if (ret != 0) {
  13035. flag = ret;
  13036. }
  13037. }
  13038. wc_HmacFree(&hmac);
  13039. printf(resultFmt, flag == 0 ? passed : failed);
  13040. #endif
  13041. return flag;
  13042. } /* END test_wc_Sha256HmacUpdate */
  13043. /*
  13044. * testing wc_HmacUpdate on SHA384 hash.
  13045. */
  13046. static int test_wc_Sha384HmacUpdate(void)
  13047. {
  13048. int flag = 0;
  13049. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13050. Hmac hmac;
  13051. testVector a, b;
  13052. int ret;
  13053. #ifdef HAVE_FIPS
  13054. const char* keys =
  13055. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13056. #else
  13057. const char* keys = "Jefe";
  13058. #endif
  13059. a.input = "what do ya want for nothing?";
  13060. a.inLen = XSTRLEN(a.input);
  13061. b.input = "Hi There";
  13062. b.inLen = XSTRLEN(b.input);
  13063. flag = 0;
  13064. printf(testingFmt, "wc_HmacUpdate() with SHA384");
  13065. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13066. if (ret != 0)
  13067. return ret;
  13068. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
  13069. if (ret != 0) {
  13070. flag = ret;
  13071. }
  13072. if (!flag) {
  13073. ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
  13074. if (ret != 0) {
  13075. flag = ret;
  13076. }
  13077. }
  13078. /* Update Hmac. */
  13079. if (!flag) {
  13080. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13081. if (ret != 0) {
  13082. flag = ret;
  13083. }
  13084. }
  13085. /* Test bad args. */
  13086. if (!flag) {
  13087. ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
  13088. if (ret != BAD_FUNC_ARG) {
  13089. flag = WOLFSSL_FATAL_ERROR;
  13090. }
  13091. }
  13092. if (!flag) {
  13093. ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
  13094. if (ret != BAD_FUNC_ARG) {
  13095. flag = WOLFSSL_FATAL_ERROR;
  13096. }
  13097. }
  13098. if (!flag) {
  13099. ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
  13100. if (ret != 0) {
  13101. flag = ret;
  13102. }
  13103. }
  13104. wc_HmacFree(&hmac);
  13105. printf(resultFmt, flag == 0 ? passed : failed);
  13106. #endif
  13107. return flag;
  13108. } /* END test_wc_Sha384HmacUpdate */
  13109. /*
  13110. * Testing wc_HmacFinal() with MD5
  13111. */
  13112. static int test_wc_Md5HmacFinal(void)
  13113. {
  13114. int flag = 0;
  13115. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  13116. Hmac hmac;
  13117. byte hash[WC_MD5_DIGEST_SIZE];
  13118. testVector a;
  13119. int ret;
  13120. const char* key;
  13121. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  13122. a.input = "Hi There";
  13123. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  13124. "\x9d";
  13125. a.inLen = XSTRLEN(a.input);
  13126. a.outLen = XSTRLEN(a.output);
  13127. flag = 0;
  13128. printf(testingFmt, "wc_HmacFinal() with MD5");
  13129. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13130. if (ret != 0)
  13131. return ret;
  13132. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
  13133. if (ret != 0) {
  13134. flag = ret;
  13135. }
  13136. if (!flag) {
  13137. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13138. if (ret != 0) {
  13139. flag = ret;
  13140. }
  13141. }
  13142. if (!flag) {
  13143. ret = wc_HmacFinal(&hmac, hash);
  13144. if (ret != 0) {
  13145. flag = ret;
  13146. }
  13147. }
  13148. if (!flag) {
  13149. if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
  13150. flag = WOLFSSL_FATAL_ERROR;
  13151. }
  13152. }
  13153. /* Try bad parameters. */
  13154. if (!flag) {
  13155. ret = wc_HmacFinal(NULL, hash);
  13156. if (ret != BAD_FUNC_ARG) {
  13157. flag = WOLFSSL_FATAL_ERROR;
  13158. }
  13159. }
  13160. #ifndef HAVE_FIPS
  13161. if (!flag) {
  13162. ret = wc_HmacFinal(&hmac, NULL);
  13163. if (ret != BAD_FUNC_ARG) {
  13164. flag = WOLFSSL_FATAL_ERROR;
  13165. }
  13166. }
  13167. #endif
  13168. wc_HmacFree(&hmac);
  13169. printf(resultFmt, flag == 0 ? passed : failed);
  13170. #endif
  13171. return flag;
  13172. } /* END test_wc_Md5HmacFinal */
  13173. /*
  13174. * Testing wc_HmacFinal() with SHA
  13175. */
  13176. static int test_wc_ShaHmacFinal(void)
  13177. {
  13178. int flag = 0;
  13179. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13180. Hmac hmac;
  13181. byte hash[WC_SHA_DIGEST_SIZE];
  13182. testVector a;
  13183. int ret;
  13184. const char* key;
  13185. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13186. "\x0b\x0b\x0b";
  13187. a.input = "Hi There";
  13188. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  13189. "\x8e\xf1\x46\xbe\x00";
  13190. a.inLen = XSTRLEN(a.input);
  13191. a.outLen = XSTRLEN(a.output);
  13192. flag = 0;
  13193. printf(testingFmt, "wc_HmacFinal() with SHA");
  13194. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13195. if (ret != 0)
  13196. return ret;
  13197. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
  13198. if (ret != 0) {
  13199. flag = ret;
  13200. }
  13201. if (!flag) {
  13202. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13203. if (ret != 0) {
  13204. flag = ret;
  13205. }
  13206. }
  13207. if (!flag) {
  13208. ret = wc_HmacFinal(&hmac, hash);
  13209. if (ret != 0) {
  13210. flag = ret;
  13211. }
  13212. }
  13213. if (!flag) {
  13214. if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
  13215. flag = WOLFSSL_FATAL_ERROR;
  13216. }
  13217. }
  13218. /* Try bad parameters. */
  13219. if (!flag) {
  13220. ret = wc_HmacFinal(NULL, hash);
  13221. if (ret != BAD_FUNC_ARG) {
  13222. flag = WOLFSSL_FATAL_ERROR;
  13223. }
  13224. }
  13225. #ifndef HAVE_FIPS
  13226. if (!flag) {
  13227. ret = wc_HmacFinal(&hmac, NULL);
  13228. if (ret != BAD_FUNC_ARG) {
  13229. flag = WOLFSSL_FATAL_ERROR;
  13230. }
  13231. }
  13232. #endif
  13233. wc_HmacFree(&hmac);
  13234. printf(resultFmt, flag == 0 ? passed : failed);
  13235. #endif
  13236. return flag;
  13237. } /* END test_wc_ShaHmacFinal */
  13238. /*
  13239. * Testing wc_HmacFinal() with SHA224
  13240. */
  13241. static int test_wc_Sha224HmacFinal(void)
  13242. {
  13243. int flag = 0;
  13244. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13245. Hmac hmac;
  13246. byte hash[WC_SHA224_DIGEST_SIZE];
  13247. testVector a;
  13248. int ret;
  13249. const char* key;
  13250. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13251. "\x0b\x0b\x0b";
  13252. a.input = "Hi There";
  13253. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  13254. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  13255. a.inLen = XSTRLEN(a.input);
  13256. a.outLen = XSTRLEN(a.output);
  13257. flag = 0;
  13258. printf(testingFmt, "wc_HmacFinal() with SHA224");
  13259. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13260. if (ret != 0)
  13261. return ret;
  13262. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
  13263. if (ret != 0) {
  13264. flag = ret;
  13265. }
  13266. if (!flag) {
  13267. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13268. if (ret != 0) {
  13269. flag = ret;
  13270. }
  13271. }
  13272. if (!flag) {
  13273. ret = wc_HmacFinal(&hmac, hash);
  13274. if (ret != 0) {
  13275. flag = ret;
  13276. }
  13277. }
  13278. if (!flag) {
  13279. if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
  13280. flag = WOLFSSL_FATAL_ERROR;
  13281. }
  13282. }
  13283. /* Try bad parameters. */
  13284. if (!flag) {
  13285. ret = wc_HmacFinal(NULL, hash);
  13286. if (ret != BAD_FUNC_ARG) {
  13287. flag = WOLFSSL_FATAL_ERROR;
  13288. }
  13289. }
  13290. #ifndef HAVE_FIPS
  13291. if (!flag) {
  13292. ret = wc_HmacFinal(&hmac, NULL);
  13293. if (ret != BAD_FUNC_ARG) {
  13294. flag = WOLFSSL_FATAL_ERROR;
  13295. }
  13296. }
  13297. #endif
  13298. wc_HmacFree(&hmac);
  13299. printf(resultFmt, flag == 0 ? passed : failed);
  13300. #endif
  13301. return flag;
  13302. } /* END test_wc_Sha224HmacFinal */
  13303. /*
  13304. * Testing wc_HmacFinal() with SHA256
  13305. */
  13306. static int test_wc_Sha256HmacFinal(void)
  13307. {
  13308. int flag = 0;
  13309. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13310. Hmac hmac;
  13311. byte hash[WC_SHA256_DIGEST_SIZE];
  13312. testVector a;
  13313. int ret;
  13314. const char* key;
  13315. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13316. "\x0b\x0b\x0b";
  13317. a.input = "Hi There";
  13318. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  13319. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  13320. "\xcf\xf7";
  13321. a.inLen = XSTRLEN(a.input);
  13322. a.outLen = XSTRLEN(a.output);
  13323. flag = 0;
  13324. printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
  13325. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13326. if (ret != 0)
  13327. return ret;
  13328. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
  13329. if (ret != 0) {
  13330. flag = ret;
  13331. }
  13332. if (!flag) {
  13333. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13334. if (ret != 0) {
  13335. flag = ret;
  13336. }
  13337. }
  13338. if (!flag) {
  13339. ret = wc_HmacFinal(&hmac, hash);
  13340. if (ret != 0) {
  13341. flag = ret;
  13342. }
  13343. }
  13344. if (!flag) {
  13345. if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
  13346. flag = WOLFSSL_FATAL_ERROR;
  13347. }
  13348. }
  13349. /* Try bad parameters. */
  13350. if (!flag) {
  13351. ret = wc_HmacFinal(NULL, hash);
  13352. if (ret != BAD_FUNC_ARG) {
  13353. flag = WOLFSSL_FATAL_ERROR;
  13354. }
  13355. }
  13356. #ifndef HAVE_FIPS
  13357. if (!flag) {
  13358. ret = wc_HmacFinal(&hmac, NULL);
  13359. if (ret != BAD_FUNC_ARG) {
  13360. flag = WOLFSSL_FATAL_ERROR;
  13361. }
  13362. }
  13363. #endif
  13364. wc_HmacFree(&hmac);
  13365. printf(resultFmt, flag == 0 ? passed : failed);
  13366. #endif
  13367. return flag;
  13368. } /* END test_wc_Sha256HmacFinal */
  13369. /*
  13370. * Testing wc_HmacFinal() with SHA384
  13371. */
  13372. static int test_wc_Sha384HmacFinal(void)
  13373. {
  13374. int flag = 0;
  13375. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13376. Hmac hmac;
  13377. byte hash[WC_SHA384_DIGEST_SIZE];
  13378. testVector a;
  13379. int ret;
  13380. const char* key;
  13381. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13382. "\x0b\x0b\x0b";
  13383. a.input = "Hi There";
  13384. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  13385. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  13386. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  13387. "\xfa\x9c\xb6";
  13388. a.inLen = XSTRLEN(a.input);
  13389. a.outLen = XSTRLEN(a.output);
  13390. flag = 0;
  13391. printf(testingFmt, "wc_HmacFinal() with SHA384");
  13392. ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
  13393. if (ret != 0)
  13394. return ret;
  13395. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
  13396. if (ret != 0) {
  13397. flag = ret;
  13398. }
  13399. if (!flag) {
  13400. ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
  13401. if (ret != 0) {
  13402. flag = ret;
  13403. }
  13404. }
  13405. if (!flag) {
  13406. ret = wc_HmacFinal(&hmac, hash);
  13407. if (ret != 0) {
  13408. flag = ret;
  13409. }
  13410. }
  13411. if (!flag) {
  13412. if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
  13413. flag = WOLFSSL_FATAL_ERROR;
  13414. }
  13415. }
  13416. /* Try bad parameters. */
  13417. if (!flag) {
  13418. ret = wc_HmacFinal(NULL, hash);
  13419. if (ret != BAD_FUNC_ARG) {
  13420. flag = WOLFSSL_FATAL_ERROR;
  13421. }
  13422. }
  13423. #ifndef HAVE_FIPS
  13424. if (!flag) {
  13425. ret = wc_HmacFinal(&hmac, NULL);
  13426. if (ret != BAD_FUNC_ARG) {
  13427. flag = WOLFSSL_FATAL_ERROR;
  13428. }
  13429. }
  13430. #endif
  13431. wc_HmacFree(&hmac);
  13432. printf(resultFmt, flag == 0 ? passed : failed);
  13433. #endif
  13434. return flag;
  13435. } /* END test_wc_Sha384HmacFinal */
  13436. /*
  13437. * Testing wc_InitCmac()
  13438. */
  13439. static int test_wc_InitCmac(void)
  13440. {
  13441. int ret = 0;
  13442. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  13443. Cmac cmac1, cmac2, cmac3;
  13444. /* AES 128 key. */
  13445. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13446. "\x09\x10\x11\x12\x13\x14\x15\x16";
  13447. /* AES 192 key. */
  13448. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13449. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13450. "\x01\x02\x03\x04\x05\x06\x07\x08";
  13451. /* AES 256 key. */
  13452. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  13453. "\x09\x01\x11\x12\x13\x14\x15\x16"
  13454. "\x01\x02\x03\x04\x05\x06\x07\x08"
  13455. "\x09\x01\x11\x12\x13\x14\x15\x16";
  13456. word32 key1Sz = (word32)sizeof(key1) - 1;
  13457. word32 key2Sz = (word32)sizeof(key2) - 1;
  13458. word32 key3Sz = (word32)sizeof(key3) - 1;
  13459. int type = WC_CMAC_AES;
  13460. printf(testingFmt, "wc_InitCmac()");
  13461. #ifdef WOLFSSL_AES_128
  13462. ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
  13463. #endif
  13464. #ifdef WOLFSSL_AES_192
  13465. if (ret == 0) {
  13466. wc_AesFree(&cmac1.aes);
  13467. ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
  13468. }
  13469. #endif
  13470. #ifdef WOLFSSL_AES_256
  13471. if (ret == 0) {
  13472. wc_AesFree(&cmac2.aes);
  13473. ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
  13474. }
  13475. #endif
  13476. /* Test bad args. */
  13477. if (ret == 0) {
  13478. wc_AesFree(&cmac3.aes);
  13479. ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
  13480. if (ret == BAD_FUNC_ARG) {
  13481. ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
  13482. }
  13483. if (ret == BAD_FUNC_ARG) {
  13484. ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
  13485. }
  13486. if (ret == BAD_FUNC_ARG) {
  13487. ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
  13488. }
  13489. if (ret == BAD_FUNC_ARG) {
  13490. ret = 0;
  13491. } else {
  13492. ret = WOLFSSL_FATAL_ERROR;
  13493. }
  13494. }
  13495. (void)key1;
  13496. (void)key1Sz;
  13497. (void)key2;
  13498. (void)key2Sz;
  13499. (void)cmac1;
  13500. (void)cmac2;
  13501. printf(resultFmt, ret == 0 ? passed : failed);
  13502. #endif
  13503. return ret;
  13504. } /* END test_wc_InitCmac */
  13505. /*
  13506. * Testing wc_CmacUpdate()
  13507. */
  13508. static int test_wc_CmacUpdate(void)
  13509. {
  13510. int ret = 0;
  13511. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13512. Cmac cmac;
  13513. byte key[] =
  13514. {
  13515. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13516. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13517. };
  13518. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  13519. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  13520. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  13521. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  13522. "\xf4";
  13523. word32 inSz = (word32)sizeof(in) - 1;
  13524. word32 keySz = (word32)sizeof(key);
  13525. int type = WC_CMAC_AES;
  13526. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13527. if (ret != 0) {
  13528. return ret;
  13529. }
  13530. printf(testingFmt, "wc_CmacUpdate()");
  13531. ret = wc_CmacUpdate(&cmac, in, inSz);
  13532. /* Test bad args. */
  13533. if (ret == 0) {
  13534. ret = wc_CmacUpdate(NULL, in, inSz);
  13535. if (ret == BAD_FUNC_ARG) {
  13536. ret = wc_CmacUpdate(&cmac, NULL, 30);
  13537. }
  13538. if (ret == BAD_FUNC_ARG) {
  13539. ret = 0;
  13540. } else if (ret == 0) {
  13541. ret = WOLFSSL_FATAL_ERROR;
  13542. }
  13543. wc_AesFree(&cmac.aes);
  13544. }
  13545. printf(resultFmt, ret == 0 ? passed : failed);
  13546. #endif
  13547. return ret;
  13548. } /* END test_wc_CmacUpdate */
  13549. /*
  13550. * Testing wc_CmacFinal()
  13551. */
  13552. static int test_wc_CmacFinal(void)
  13553. {
  13554. int ret = 0;
  13555. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13556. Cmac cmac;
  13557. byte key[] =
  13558. {
  13559. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  13560. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  13561. };
  13562. byte msg[] =
  13563. {
  13564. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  13565. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  13566. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  13567. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  13568. 0xf4
  13569. };
  13570. /* Test vectors from CMACGenAES128.rsp from
  13571. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  13572. * Per RFC4493 truncation of lsb is possible.
  13573. */
  13574. byte expMac[] =
  13575. {
  13576. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  13577. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  13578. };
  13579. byte mac[AES_BLOCK_SIZE];
  13580. word32 msgSz = (word32)sizeof(msg);
  13581. word32 keySz = (word32)sizeof(key);
  13582. word32 macSz = sizeof(mac);
  13583. word32 badMacSz = 17;
  13584. int expMacSz = sizeof(expMac);
  13585. int type = WC_CMAC_AES;
  13586. XMEMSET(mac, 0, macSz);
  13587. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13588. if (ret != 0) {
  13589. return ret;
  13590. }
  13591. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13592. printf(testingFmt, "wc_CmacFinal()");
  13593. if (ret == 0) {
  13594. ret = wc_CmacFinal(&cmac, mac, &macSz);
  13595. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13596. ret = WOLFSSL_FATAL_ERROR;
  13597. }
  13598. /* Pass in bad args. */
  13599. if (ret == 0) {
  13600. ret = wc_CmacFinal(NULL, mac, &macSz);
  13601. if (ret == BAD_FUNC_ARG) {
  13602. ret = wc_CmacFinal(&cmac, NULL, &macSz);
  13603. }
  13604. if (ret == BAD_FUNC_ARG) {
  13605. ret = wc_CmacFinal(&cmac, mac, &badMacSz);
  13606. if (ret == BUFFER_E) {
  13607. ret = 0;
  13608. }
  13609. } else if (ret == 0) {
  13610. ret = WOLFSSL_FATAL_ERROR;
  13611. }
  13612. }
  13613. }
  13614. printf(resultFmt, ret == 0 ? passed : failed);
  13615. #endif
  13616. return ret;
  13617. } /* END test_wc_CmacFinal */
  13618. /*
  13619. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  13620. */
  13621. static int test_wc_AesCmacGenerate(void)
  13622. {
  13623. int ret = 0;
  13624. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  13625. Cmac cmac;
  13626. byte key[] =
  13627. {
  13628. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  13629. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  13630. };
  13631. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  13632. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  13633. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  13634. "\x3d\x32\x65\x4c\x66\x23\xc5";
  13635. byte mac[AES_BLOCK_SIZE];
  13636. word32 keySz = sizeof(key);
  13637. word32 macSz = sizeof(mac);
  13638. word32 msgSz = sizeof(msg) - 1;
  13639. word32 expMacSz = sizeof(expMac) - 1;
  13640. int type = WC_CMAC_AES;
  13641. XMEMSET(mac, 0, macSz);
  13642. ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
  13643. if (ret != 0) {
  13644. return ret;
  13645. }
  13646. ret = wc_CmacUpdate(&cmac, msg, msgSz);
  13647. if (ret != 0) {
  13648. return ret;
  13649. } else {
  13650. wc_AesFree(&cmac.aes);
  13651. }
  13652. printf(testingFmt, "wc_AesCmacGenerate()");
  13653. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
  13654. if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
  13655. ret = WOLFSSL_FATAL_ERROR;
  13656. }
  13657. /* Pass in bad args. */
  13658. if (ret == 0) {
  13659. ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
  13660. if (ret == BAD_FUNC_ARG) {
  13661. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
  13662. }
  13663. if (ret == BAD_FUNC_ARG) {
  13664. ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
  13665. }
  13666. if (ret == BAD_FUNC_ARG) {
  13667. ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
  13668. }
  13669. if (ret == BAD_FUNC_ARG) {
  13670. ret = 0;
  13671. } else if (ret == 0) {
  13672. ret = WOLFSSL_FATAL_ERROR;
  13673. }
  13674. }
  13675. printf(resultFmt, ret == 0 ? passed : failed);
  13676. if (ret == 0) {
  13677. printf(testingFmt, "wc_AesCmacVerify()");
  13678. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
  13679. /* Test bad args. */
  13680. if (ret == 0) {
  13681. ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
  13682. if (ret == BAD_FUNC_ARG) {
  13683. ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
  13684. }
  13685. if (ret == BAD_FUNC_ARG) {
  13686. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
  13687. }
  13688. if (ret == BAD_FUNC_ARG) {
  13689. ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
  13690. }
  13691. if (ret == BAD_FUNC_ARG) {
  13692. ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
  13693. }
  13694. if (ret == BAD_FUNC_ARG) {
  13695. ret = 0;
  13696. } else if (ret == 0) {
  13697. ret = WOLFSSL_FATAL_ERROR;
  13698. }
  13699. }
  13700. printf(resultFmt, ret == 0 ? passed : failed);
  13701. }
  13702. #endif
  13703. return ret;
  13704. } /* END test_wc_AesCmacGenerate */
  13705. /*
  13706. * Testing streaming AES-GCM API.
  13707. */
  13708. static int test_wc_AesGcmStream(void)
  13709. {
  13710. int ret = 0;
  13711. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  13712. defined(WOLFSSL_AESGCM_STREAM)
  13713. int i;
  13714. WC_RNG rng[1];
  13715. Aes aesEnc[1];
  13716. Aes aesDec[1];
  13717. byte tag[AES_BLOCK_SIZE];
  13718. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13719. byte out[AES_BLOCK_SIZE * 3 + 2];
  13720. byte plain[AES_BLOCK_SIZE * 3 + 2];
  13721. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  13722. byte key[AES_128_KEY_SIZE] = { 0, };
  13723. byte iv[AES_IV_SIZE] = { 1, };
  13724. byte ivOut[AES_IV_SIZE];
  13725. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  13726. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  13727. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  13728. };
  13729. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  13730. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  13731. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  13732. };
  13733. static const byte expTag[AES_BLOCK_SIZE] = {
  13734. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  13735. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  13736. };
  13737. /* Create a random for generating IV/nonce. */
  13738. AssertIntEQ(wc_InitRng(rng), 0);
  13739. /* Initialize data structures. */
  13740. AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  13741. AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  13742. /* BadParameters to streaming init. */
  13743. AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13744. AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  13745. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  13746. BAD_FUNC_ARG);
  13747. AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  13748. BAD_FUNC_ARG);
  13749. /* Bad parameters to encrypt update. */
  13750. AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13751. BAD_FUNC_ARG);
  13752. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  13753. BAD_FUNC_ARG);
  13754. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  13755. BAD_FUNC_ARG);
  13756. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  13757. BAD_FUNC_ARG);
  13758. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  13759. BAD_FUNC_ARG);
  13760. /* Bad parameters to decrypt update. */
  13761. AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  13762. BAD_FUNC_ARG);
  13763. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  13764. BAD_FUNC_ARG);
  13765. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  13766. BAD_FUNC_ARG);
  13767. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  13768. BAD_FUNC_ARG);
  13769. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  13770. BAD_FUNC_ARG);
  13771. /* Bad parameters to encrypt final. */
  13772. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13773. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13774. AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13775. BAD_FUNC_ARG);
  13776. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  13777. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  13778. BAD_FUNC_ARG);
  13779. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  13780. BAD_FUNC_ARG);
  13781. /* Bad parameters to decrypt final. */
  13782. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  13783. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  13784. AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  13785. BAD_FUNC_ARG);
  13786. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  13787. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  13788. BAD_FUNC_ARG);
  13789. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  13790. BAD_FUNC_ARG);
  13791. /* Check calling final before setting key fails. */
  13792. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  13793. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  13794. /* Check calling update before setting key else fails. */
  13795. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13796. MISSING_KEY);
  13797. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13798. MISSING_KEY);
  13799. /* Set key but not IV. */
  13800. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  13801. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  13802. /* Check calling final before setting IV fails. */
  13803. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  13804. AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  13805. /* Check calling update before setting IV else fails. */
  13806. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  13807. MISSING_IV);
  13808. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  13809. MISSING_IV);
  13810. /* Set IV using fixed part IV and external IV APIs. */
  13811. AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  13812. rng), 0);
  13813. AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  13814. GCM_NONCE_MID_SZ), 0);
  13815. AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  13816. AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  13817. /* Encrypt and decrypt data. */
  13818. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  13819. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  13820. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13821. /* Finalize and check tag matches. */
  13822. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13823. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13824. /* Set key and IV through streaming init API. */
  13825. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13826. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13827. /* Encrypt/decrypt one block and AAD of one block. */
  13828. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  13829. AES_BLOCK_SIZE), 0);
  13830. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  13831. AES_BLOCK_SIZE), 0);
  13832. AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  13833. /* Finalize and check tag matches. */
  13834. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13835. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13836. /* Set key and IV through streaming init API. */
  13837. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13838. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13839. /* No data to encrypt/decrypt one byte of AAD. */
  13840. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  13841. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  13842. /* Finalize and check tag matches. */
  13843. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13844. AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  13845. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13846. /* Set key and IV through streaming init API. */
  13847. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13848. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13849. /* Encrypt/decrypt one byte and no AAD. */
  13850. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  13851. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  13852. AssertIntEQ(XMEMCMP(plain, in, 1), 0);
  13853. /* Finalize and check tag matches. */
  13854. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13855. AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  13856. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13857. /* Set key and IV through streaming init API. */
  13858. AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13859. AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  13860. /* Encryption AES is one byte at a time */
  13861. for (i = 0; i < (int)sizeof(aad); i++) {
  13862. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  13863. 0);
  13864. }
  13865. for (i = 0; i < (int)sizeof(in); i++) {
  13866. AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  13867. 0);
  13868. }
  13869. /* Decryption AES is two bytes at a time */
  13870. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13871. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  13872. 0);
  13873. }
  13874. for (i = 0; i < (int)sizeof(aad); i += 2) {
  13875. AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  13876. 0), 0);
  13877. }
  13878. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13879. /* Finalize and check tag matches. */
  13880. AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  13881. AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  13882. AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  13883. /* Check streaming encryption can be decrypted with one shot. */
  13884. AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  13885. AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  13886. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  13887. AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  13888. wc_AesFree(aesEnc);
  13889. wc_AesFree(aesDec);
  13890. wc_FreeRng(rng);
  13891. #endif
  13892. return ret;
  13893. } /* END test_wc_AesGcmStream */
  13894. /*
  13895. * unit test for wc_Des3_SetIV()
  13896. */
  13897. static int test_wc_Des3_SetIV(void)
  13898. {
  13899. int ret = 0;
  13900. #ifndef NO_DES3
  13901. Des3 des;
  13902. const byte key[] =
  13903. {
  13904. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13905. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13906. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13907. };
  13908. const byte iv[] =
  13909. {
  13910. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13911. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13912. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13913. };
  13914. printf(testingFmt, "wc_Des3_SetIV()");
  13915. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13916. if (ret != 0)
  13917. return ret;
  13918. /* DES_ENCRYPTION or DES_DECRYPTION */
  13919. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13920. if (ret == 0) {
  13921. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13922. ret = WOLFSSL_FATAL_ERROR;
  13923. }
  13924. }
  13925. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  13926. /* Test explicitly wc_Des3_SetIV() */
  13927. if (ret == 0) {
  13928. ret = wc_Des3_SetIV(NULL, iv);
  13929. if (ret == BAD_FUNC_ARG) {
  13930. ret = wc_Des3_SetIV(&des, NULL);
  13931. } else if (ret == 0) {
  13932. ret = WOLFSSL_FATAL_ERROR;
  13933. }
  13934. }
  13935. #endif
  13936. wc_Des3Free(&des);
  13937. printf(resultFmt, ret == 0 ? passed : failed);
  13938. #endif
  13939. return ret;
  13940. } /* END test_wc_Des3_SetIV */
  13941. /*
  13942. * unit test for wc_Des3_SetKey()
  13943. */
  13944. static int test_wc_Des3_SetKey(void)
  13945. {
  13946. int ret = 0;
  13947. #ifndef NO_DES3
  13948. Des3 des;
  13949. const byte key[] =
  13950. {
  13951. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  13952. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  13953. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  13954. };
  13955. const byte iv[] =
  13956. {
  13957. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  13958. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  13959. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  13960. };
  13961. printf(testingFmt, "wc_Des3_SetKey()");
  13962. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  13963. if (ret != 0)
  13964. return ret;
  13965. /* DES_ENCRYPTION or DES_DECRYPTION */
  13966. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  13967. if (ret == 0) {
  13968. if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
  13969. ret = WOLFSSL_FATAL_ERROR;
  13970. }
  13971. }
  13972. /* Test bad args. */
  13973. if (ret == 0) {
  13974. ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
  13975. if (ret == BAD_FUNC_ARG) {
  13976. ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
  13977. }
  13978. if (ret == BAD_FUNC_ARG) {
  13979. ret = wc_Des3_SetKey(&des, key, iv, -1);
  13980. }
  13981. if (ret == BAD_FUNC_ARG) {
  13982. /* Default case. Should return 0. */
  13983. ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
  13984. }
  13985. } /* END if ret != 0 */
  13986. wc_Des3Free(&des);
  13987. printf(resultFmt, ret == 0 ? passed : failed);
  13988. #endif
  13989. return ret;
  13990. } /* END test_wc_Des3_SetKey */
  13991. /*
  13992. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  13993. */
  13994. static int test_wc_Des3_CbcEncryptDecrypt(void)
  13995. {
  13996. int ret = 0;
  13997. #ifndef NO_DES3
  13998. Des3 des;
  13999. byte cipher[24];
  14000. byte plain[24];
  14001. const byte key[] =
  14002. {
  14003. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14004. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14005. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14006. };
  14007. const byte iv[] =
  14008. {
  14009. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14010. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14011. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14012. };
  14013. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14014. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14015. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14016. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14017. };
  14018. printf(testingFmt, "wc_Des3_CbcEncrypt()");
  14019. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14020. if (ret != 0)
  14021. return ret;
  14022. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14023. if (ret == 0) {
  14024. ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
  14025. if (ret == 0) {
  14026. ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
  14027. }
  14028. if (ret == 0) {
  14029. ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
  14030. }
  14031. }
  14032. if (ret == 0) {
  14033. if (XMEMCMP(plain, vector, 24) != 0) {
  14034. ret = WOLFSSL_FATAL_ERROR;
  14035. }
  14036. }
  14037. /* Pass in bad args. */
  14038. if (ret == 0) {
  14039. ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
  14040. if (ret == BAD_FUNC_ARG) {
  14041. ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
  14042. }
  14043. if (ret == BAD_FUNC_ARG) {
  14044. ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
  14045. }
  14046. if (ret != BAD_FUNC_ARG) {
  14047. ret = WOLFSSL_FATAL_ERROR;
  14048. } else {
  14049. ret = 0;
  14050. }
  14051. }
  14052. if (ret == 0) {
  14053. ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
  14054. if (ret == BAD_FUNC_ARG) {
  14055. ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
  14056. }
  14057. if (ret == BAD_FUNC_ARG) {
  14058. ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
  14059. }
  14060. if (ret != BAD_FUNC_ARG) {
  14061. ret = WOLFSSL_FATAL_ERROR;
  14062. } else {
  14063. ret = 0;
  14064. }
  14065. }
  14066. wc_Des3Free(&des);
  14067. printf(resultFmt, ret == 0 ? passed : failed);
  14068. #endif
  14069. return ret;
  14070. } /* END wc_Des3_CbcEncrypt */
  14071. /*
  14072. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  14073. */
  14074. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  14075. {
  14076. int ret = 0;
  14077. #ifndef NO_DES3
  14078. word32 vectorSz, cipherSz;
  14079. byte cipher[24];
  14080. byte plain[24];
  14081. byte vector[] = /* Now is the time for all w/o trailing 0 */
  14082. {
  14083. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14084. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14085. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14086. };
  14087. byte key[] =
  14088. {
  14089. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14090. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14091. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14092. };
  14093. byte iv[] =
  14094. {
  14095. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14096. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14097. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14098. };
  14099. vectorSz = sizeof(byte) * 24;
  14100. cipherSz = sizeof(byte) * 24;
  14101. printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
  14102. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
  14103. if (ret == 0) {
  14104. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
  14105. if (ret == 0) {
  14106. if (XMEMCMP(plain, vector, 24) != 0) {
  14107. ret = WOLFSSL_FATAL_ERROR;
  14108. }
  14109. }
  14110. }
  14111. /* pass in bad args. */
  14112. if (ret == 0) {
  14113. ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
  14114. if (ret == BAD_FUNC_ARG) {
  14115. ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
  14116. }
  14117. if (ret == BAD_FUNC_ARG) {
  14118. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
  14119. }
  14120. if (ret == BAD_FUNC_ARG) {
  14121. ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
  14122. key, NULL);
  14123. } else {
  14124. /* Return code catch. */
  14125. ret = WOLFSSL_FAILURE;
  14126. }
  14127. }
  14128. if (ret == 0) {
  14129. ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
  14130. if (ret == BAD_FUNC_ARG) {
  14131. ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
  14132. }
  14133. if (ret == BAD_FUNC_ARG) {
  14134. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
  14135. }
  14136. if (ret == BAD_FUNC_ARG) {
  14137. ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
  14138. } else {
  14139. ret = WOLFSSL_FAILURE;
  14140. }
  14141. }
  14142. printf(resultFmt, ret == 0 ? passed : failed);
  14143. #endif
  14144. return ret;
  14145. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  14146. /*
  14147. * Unit test for wc_Des3_EcbEncrypt
  14148. */
  14149. static int test_wc_Des3_EcbEncrypt(void)
  14150. {
  14151. int ret = 0;
  14152. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  14153. Des3 des;
  14154. byte cipher[24];
  14155. word32 cipherSz = sizeof(cipher);
  14156. const byte key[] =
  14157. {
  14158. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  14159. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  14160. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  14161. };
  14162. const byte iv[] =
  14163. {
  14164. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  14165. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  14166. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  14167. };
  14168. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  14169. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14170. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14171. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  14172. };
  14173. printf(testingFmt, "wc_Des3_EcbEncrypt()");
  14174. ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
  14175. if (ret != 0) {
  14176. return ret;
  14177. }
  14178. if (ret == 0 ) {
  14179. ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
  14180. }
  14181. /* Bad Cases */
  14182. if (ret == 0) {
  14183. ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
  14184. if (ret == BAD_FUNC_ARG) {
  14185. ret = 0;
  14186. }
  14187. }
  14188. if (ret == 0) {
  14189. ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
  14190. if (ret == BAD_FUNC_ARG) {
  14191. ret = 0;
  14192. }
  14193. }
  14194. if (ret == 0) {
  14195. ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
  14196. if (ret == BAD_FUNC_ARG) {
  14197. ret = 0;
  14198. }
  14199. }
  14200. if (ret == 0) {
  14201. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
  14202. if (ret == BAD_FUNC_ARG) {
  14203. ret = 0;
  14204. }
  14205. }
  14206. if (ret == 0) {
  14207. ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
  14208. if (ret == BAD_FUNC_ARG) {
  14209. ret = 0;
  14210. }
  14211. }
  14212. /* Good Cases */
  14213. if (ret == 0) {
  14214. ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
  14215. }
  14216. wc_Des3Free(&des);
  14217. printf(resultFmt, ret == 0 ? passed : failed);
  14218. #endif
  14219. return ret;
  14220. } /* END test_wc_Des3_EcbEncrypt */
  14221. /*
  14222. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  14223. */
  14224. static int test_wc_Chacha_SetKey(void)
  14225. {
  14226. int ret = 0;
  14227. #ifdef HAVE_CHACHA
  14228. ChaCha ctx;
  14229. const byte key[] =
  14230. {
  14231. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14232. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14233. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14234. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14235. };
  14236. byte cipher[128];
  14237. printf(testingFmt, "wc_Chacha_SetKey()");
  14238. ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14239. /* Test bad args. */
  14240. if (ret == 0) {
  14241. ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14242. if (ret == BAD_FUNC_ARG) {
  14243. ret = wc_Chacha_SetKey(&ctx, key, 18);
  14244. }
  14245. if (ret == BAD_FUNC_ARG) {
  14246. ret = 0;
  14247. } else {
  14248. ret = WOLFSSL_FATAL_ERROR;
  14249. }
  14250. }
  14251. printf(resultFmt, ret == 0 ? passed : failed);
  14252. if (ret != 0) {
  14253. return ret;
  14254. }
  14255. printf(testingFmt, "wc_Chacha_SetIV");
  14256. ret = wc_Chacha_SetIV(&ctx, cipher, 0);
  14257. if (ret == 0) {
  14258. /* Test bad args. */
  14259. ret = wc_Chacha_SetIV(NULL, cipher, 0);
  14260. if (ret == BAD_FUNC_ARG) {
  14261. ret = 0;
  14262. } else {
  14263. ret = WOLFSSL_FAILURE;
  14264. }
  14265. }
  14266. printf(resultFmt, ret == 0 ? passed : failed);
  14267. #endif
  14268. return ret;
  14269. } /* END test_wc_Chacha_SetKey */
  14270. /*
  14271. * unit test for wc_Poly1305SetKey()
  14272. */
  14273. static int test_wc_Poly1305SetKey(void)
  14274. {
  14275. int ret = 0;
  14276. #ifdef HAVE_POLY1305
  14277. Poly1305 ctx;
  14278. const byte key[] =
  14279. {
  14280. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14281. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14282. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14283. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14284. };
  14285. printf(testingFmt, "wc_Poly1305_SetKey()");
  14286. ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
  14287. /* Test bad args. */
  14288. if (ret == 0) {
  14289. ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
  14290. if(ret == BAD_FUNC_ARG) {
  14291. ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
  14292. }
  14293. if (ret == BAD_FUNC_ARG) {
  14294. ret = wc_Poly1305SetKey(&ctx, key, 18);
  14295. }
  14296. if (ret == BAD_FUNC_ARG) {
  14297. ret = 0;
  14298. } else {
  14299. ret = WOLFSSL_FATAL_ERROR;
  14300. }
  14301. }
  14302. printf(resultFmt, ret == 0 ? passed : failed);
  14303. #endif
  14304. return ret;
  14305. } /* END test_wc_Poly1305_SetKey() */
  14306. /*
  14307. * Testing wc_Chacha_Process()
  14308. */
  14309. static int test_wc_Chacha_Process(void)
  14310. {
  14311. int ret = 0;
  14312. #ifdef HAVE_CHACHA
  14313. ChaCha enc, dec;
  14314. byte cipher[128];
  14315. byte plain[128];
  14316. const byte key[] =
  14317. {
  14318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  14321. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  14322. };
  14323. const char* input = "Everybody gets Friday off.";
  14324. word32 keySz = sizeof(key)/sizeof(byte);
  14325. unsigned long int inlen = XSTRLEN(input);
  14326. /*Initialize stack varialbes.*/
  14327. XMEMSET(cipher, 0, 128);
  14328. XMEMSET(plain, 0, 128);
  14329. printf(testingFmt, "wc_Chacha_Process()");
  14330. ret = wc_Chacha_SetKey(&enc, key, keySz);
  14331. AssertIntEQ(ret, 0);
  14332. ret = wc_Chacha_SetKey(&dec, key, keySz);
  14333. AssertIntEQ(ret, 0);
  14334. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14335. AssertIntEQ(ret, 0);
  14336. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14337. AssertIntEQ(ret, 0);
  14338. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
  14339. AssertIntEQ(ret, 0);
  14340. ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
  14341. AssertIntEQ(ret, 0);
  14342. ret = XMEMCMP(input, plain, (int)inlen);
  14343. AssertIntEQ(ret, 0);
  14344. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  14345. /* test checking and using leftovers, currently just in C code */
  14346. ret = wc_Chacha_SetIV(&enc, cipher, 0);
  14347. AssertIntEQ(ret, 0);
  14348. ret = wc_Chacha_SetIV(&dec, cipher, 0);
  14349. AssertIntEQ(ret, 0);
  14350. ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
  14351. AssertIntEQ(ret, 0);
  14352. ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
  14353. (byte*)input + (inlen - 2), 2);
  14354. AssertIntEQ(ret, 0);
  14355. ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
  14356. AssertIntEQ(ret, 0);
  14357. ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
  14358. (byte*)input + (inlen - 2), 2);
  14359. AssertIntEQ(ret, 0);
  14360. ret = XMEMCMP(input, plain, (int)inlen);
  14361. AssertIntEQ(ret, 0);
  14362. /* check edge cases with counter increment */
  14363. {
  14364. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  14365. const byte expected[] = {
  14366. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  14367. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  14368. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  14369. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  14370. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  14371. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  14372. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  14373. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  14374. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  14375. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  14376. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  14377. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  14378. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  14379. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  14380. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  14381. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  14382. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  14383. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  14384. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  14385. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  14386. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  14387. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  14388. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  14389. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  14390. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  14391. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  14392. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  14393. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  14394. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  14395. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  14396. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  14397. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  14398. };
  14399. const byte iv2[] = {
  14400. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  14401. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  14402. };
  14403. byte input2[256];
  14404. int i;
  14405. for (i = 0; i < 256; i++)
  14406. input2[i] = i;
  14407. ret = wc_Chacha_SetIV(&enc, iv2, 0);
  14408. AssertIntEQ(ret, 0);
  14409. ret = wc_Chacha_Process(&enc, cipher, input2, 64);
  14410. AssertIntEQ(ret, 0);
  14411. AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
  14412. ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
  14413. AssertIntEQ(ret, 0);
  14414. AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  14415. /* partial */
  14416. ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
  14417. AssertIntEQ(ret, 0);
  14418. AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  14419. ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
  14420. AssertIntEQ(ret, 0);
  14421. AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  14422. }
  14423. #endif
  14424. /* Test bad args. */
  14425. ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
  14426. AssertIntEQ(ret, BAD_FUNC_ARG);
  14427. if (ret == BAD_FUNC_ARG) {
  14428. ret = 0;
  14429. }
  14430. printf(resultFmt, ret == 0 ? passed : failed);
  14431. #endif
  14432. return ret;
  14433. } /* END test_wc_Chacha_Process */
  14434. /*
  14435. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  14436. */
  14437. static int test_wc_ChaCha20Poly1305_aead(void)
  14438. {
  14439. int ret = 0;
  14440. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  14441. const byte key[] = {
  14442. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  14443. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  14444. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  14445. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  14446. };
  14447. const byte plaintext[] = {
  14448. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  14449. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  14450. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  14451. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  14452. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  14453. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  14454. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  14455. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  14456. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  14457. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  14458. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  14459. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  14460. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  14461. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  14462. 0x74, 0x2e
  14463. };
  14464. const byte iv[] = {
  14465. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  14466. 0x44, 0x45, 0x46, 0x47
  14467. };
  14468. const byte aad[] = { /* additional data */
  14469. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  14470. 0xc4, 0xc5, 0xc6, 0xc7
  14471. };
  14472. const byte cipher[] = { /* expected output from operation */
  14473. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  14474. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  14475. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  14476. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  14477. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  14478. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  14479. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  14480. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  14481. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  14482. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  14483. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  14484. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  14485. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  14486. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  14487. 0x61, 0x16
  14488. };
  14489. const byte authTag[] = { /* expected output from operation */
  14490. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  14491. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  14492. };
  14493. byte generatedCiphertext[272];
  14494. byte generatedPlaintext[272];
  14495. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  14496. /* Initialize stack variables. */
  14497. XMEMSET(generatedCiphertext, 0, 272);
  14498. XMEMSET(generatedPlaintext, 0, 272);
  14499. /* Test Encrypt */
  14500. printf(testingFmt, "wc_ChaCha20Poly1305_Encrypt()");
  14501. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
  14502. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14503. AssertIntEQ(ret, 0);
  14504. ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
  14505. AssertIntEQ(ret, 0);
  14506. /* Test bad args. */
  14507. ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
  14508. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14509. AssertIntEQ(ret, BAD_FUNC_ARG);
  14510. ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  14511. plaintext, sizeof(plaintext),
  14512. generatedCiphertext, generatedAuthTag);
  14513. AssertIntEQ(ret, BAD_FUNC_ARG);
  14514. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  14515. sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14516. AssertIntEQ(ret, BAD_FUNC_ARG);
  14517. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14518. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
  14519. AssertIntEQ(ret, BAD_FUNC_ARG);
  14520. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14521. plaintext, sizeof(plaintext), NULL, generatedAuthTag);
  14522. AssertIntEQ(ret, BAD_FUNC_ARG);
  14523. ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  14524. plaintext, sizeof(plaintext), generatedCiphertext, NULL);
  14525. if (ret == BAD_FUNC_ARG) {
  14526. ret = 0;
  14527. }
  14528. printf(resultFmt, ret == 0 ? passed : failed);
  14529. if (ret != 0) {
  14530. return ret;
  14531. }
  14532. printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
  14533. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14534. sizeof(cipher), authTag, generatedPlaintext);
  14535. AssertIntEQ(ret, 0);
  14536. ret = XMEMCMP(generatedPlaintext, plaintext,
  14537. sizeof(plaintext)/sizeof(byte));
  14538. AssertIntEQ(ret, 0);
  14539. /* Test bad args. */
  14540. ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  14541. sizeof(cipher), authTag, generatedPlaintext);
  14542. AssertIntEQ(ret, BAD_FUNC_ARG);
  14543. ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  14544. cipher, sizeof(cipher), authTag, generatedPlaintext);
  14545. AssertIntEQ(ret, BAD_FUNC_ARG);
  14546. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14547. sizeof(cipher), authTag, generatedPlaintext);
  14548. AssertIntEQ(ret, BAD_FUNC_ARG);
  14549. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14550. sizeof(cipher), NULL, generatedPlaintext);
  14551. AssertIntEQ(ret, BAD_FUNC_ARG);
  14552. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  14553. sizeof(cipher), authTag, NULL);
  14554. AssertIntEQ(ret, BAD_FUNC_ARG);
  14555. ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  14556. sizeof(cipher), authTag, generatedPlaintext);
  14557. AssertIntEQ(ret, BAD_FUNC_ARG);
  14558. if (ret == BAD_FUNC_ARG) {
  14559. ret = 0;
  14560. }
  14561. printf(resultFmt, ret == 0 ? passed : failed);
  14562. #endif
  14563. return ret;
  14564. } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
  14565. /*
  14566. * Testing function for wc_Rc2SetKey().
  14567. */
  14568. static int test_wc_Rc2SetKey(void)
  14569. {
  14570. int ret = 0;
  14571. #ifdef WC_RC2
  14572. Rc2 rc2;
  14573. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  14574. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14575. printf(testingFmt, "wc_Rc2SetKey()");
  14576. /* valid key and IV */
  14577. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14578. iv, 40);
  14579. if (ret == 0) {
  14580. /* valid key, no IV */
  14581. ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  14582. NULL, 40);
  14583. }
  14584. /* bad arguments */
  14585. if (ret == 0) {
  14586. /* null Rc2 struct */
  14587. ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  14588. iv, 40);
  14589. if (ret == BAD_FUNC_ARG) {
  14590. ret = 0;
  14591. }
  14592. }
  14593. if (ret == 0) {
  14594. /* null key */
  14595. ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  14596. iv, 40);
  14597. if (ret == BAD_FUNC_ARG) {
  14598. ret = 0;
  14599. }
  14600. }
  14601. if (ret == 0) {
  14602. /* key size == 0 */
  14603. ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
  14604. if (ret == WC_KEY_SIZE_E) {
  14605. ret = 0;
  14606. }
  14607. }
  14608. if (ret == 0) {
  14609. /* key size > 128 */
  14610. ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
  14611. if (ret == WC_KEY_SIZE_E) {
  14612. ret = 0;
  14613. }
  14614. }
  14615. if (ret == 0) {
  14616. /* effective bits == 0 */
  14617. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14618. iv, 0);
  14619. if (ret == WC_KEY_SIZE_E) {
  14620. ret = 0;
  14621. }
  14622. }
  14623. if (ret == 0) {
  14624. /* effective bits > 1024 */
  14625. ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  14626. iv, 1025);
  14627. if (ret == WC_KEY_SIZE_E) {
  14628. ret = 0;
  14629. }
  14630. }
  14631. printf(resultFmt, ret == 0 ? passed : failed);
  14632. #endif
  14633. return ret;
  14634. } /* END test_wc_Rc2SetKey */
  14635. /*
  14636. * Testing function for wc_Rc2SetIV().
  14637. */
  14638. static int test_wc_Rc2SetIV(void)
  14639. {
  14640. int ret = 0;
  14641. #ifdef WC_RC2
  14642. Rc2 rc2;
  14643. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  14644. printf(testingFmt, "wc_Rc2SetIV()");
  14645. /* valid IV */
  14646. ret = wc_Rc2SetIV(&rc2, iv);
  14647. if (ret == 0) {
  14648. /* valid NULL IV */
  14649. ret = wc_Rc2SetIV(&rc2, NULL);
  14650. }
  14651. /* bad arguments */
  14652. if (ret == 0) {
  14653. ret = wc_Rc2SetIV(NULL, iv);
  14654. if (ret == BAD_FUNC_ARG) {
  14655. ret = 0;
  14656. }
  14657. }
  14658. printf(resultFmt, ret == 0 ? passed : failed);
  14659. #endif
  14660. return ret;
  14661. } /* END test_wc_Rc2SetKey */
  14662. /*
  14663. * Testing function for wc_Rc2EcbEncrypt().
  14664. */
  14665. static int test_wc_Rc2EcbEncryptDecrypt(void)
  14666. {
  14667. int ret = 0;
  14668. #ifdef WC_RC2
  14669. Rc2 rc2;
  14670. int effectiveKeyBits = 63;
  14671. byte cipher[RC2_BLOCK_SIZE];
  14672. byte plain[RC2_BLOCK_SIZE];
  14673. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14674. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  14675. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  14676. printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
  14677. XMEMSET(cipher, 0, sizeof(cipher));
  14678. XMEMSET(plain, 0, sizeof(plain));
  14679. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14680. NULL, effectiveKeyBits);
  14681. if (ret == 0) {
  14682. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
  14683. if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
  14684. ret = WOLFSSL_FATAL_ERROR;
  14685. }
  14686. if (ret == 0) {
  14687. ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
  14688. if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
  14689. ret = WOLFSSL_FATAL_ERROR;
  14690. }
  14691. }
  14692. }
  14693. /* Rc2EcbEncrypt bad arguments */
  14694. if (ret == 0) {
  14695. /* null Rc2 struct */
  14696. ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
  14697. if (ret == BAD_FUNC_ARG) {
  14698. ret = 0;
  14699. }
  14700. }
  14701. if (ret == 0) {
  14702. /* null out buffer */
  14703. ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
  14704. if (ret == BAD_FUNC_ARG) {
  14705. ret = 0;
  14706. }
  14707. }
  14708. if (ret == 0) {
  14709. /* null input buffer */
  14710. ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
  14711. if (ret == BAD_FUNC_ARG) {
  14712. ret = 0;
  14713. }
  14714. }
  14715. if (ret == 0) {
  14716. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14717. ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
  14718. if (ret == BUFFER_E) {
  14719. ret = 0;
  14720. }
  14721. }
  14722. /* Rc2EcbDecrypt bad arguments */
  14723. if (ret == 0) {
  14724. /* null Rc2 struct */
  14725. ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
  14726. if (ret == BAD_FUNC_ARG) {
  14727. ret = 0;
  14728. }
  14729. }
  14730. if (ret == 0) {
  14731. /* null out buffer */
  14732. ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
  14733. if (ret == BAD_FUNC_ARG) {
  14734. ret = 0;
  14735. }
  14736. }
  14737. if (ret == 0) {
  14738. /* null input buffer */
  14739. ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
  14740. if (ret == BAD_FUNC_ARG) {
  14741. ret = 0;
  14742. }
  14743. }
  14744. if (ret == 0) {
  14745. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  14746. ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
  14747. if (ret == BUFFER_E) {
  14748. ret = 0;
  14749. }
  14750. }
  14751. printf(resultFmt, ret == 0 ? passed : failed);
  14752. #endif
  14753. return ret;
  14754. } /* END test_wc_Rc2SetKey */
  14755. /*
  14756. * Testing function for wc_Rc2CbcEncrypt().
  14757. */
  14758. static int test_wc_Rc2CbcEncryptDecrypt(void)
  14759. {
  14760. int ret = 0;
  14761. #ifdef WC_RC2
  14762. Rc2 rc2;
  14763. int effectiveKeyBits = 63;
  14764. byte cipher[RC2_BLOCK_SIZE*2];
  14765. byte plain[RC2_BLOCK_SIZE*2];
  14766. /* vector taken from test.c */
  14767. byte key[] = {
  14768. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14769. };
  14770. byte iv[] = {
  14771. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14772. };
  14773. byte input[] = {
  14774. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  14775. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  14776. };
  14777. byte output[] = {
  14778. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  14779. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  14780. };
  14781. printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
  14782. XMEMSET(cipher, 0, sizeof(cipher));
  14783. XMEMSET(plain, 0, sizeof(plain));
  14784. ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  14785. iv, effectiveKeyBits);
  14786. if (ret == 0) {
  14787. ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
  14788. if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
  14789. ret = WOLFSSL_FATAL_ERROR;
  14790. } else {
  14791. /* reset IV for decrypt */
  14792. ret = wc_Rc2SetIV(&rc2, iv);
  14793. }
  14794. if (ret == 0) {
  14795. ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
  14796. if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
  14797. ret = WOLFSSL_FATAL_ERROR;
  14798. }
  14799. }
  14800. }
  14801. /* Rc2CbcEncrypt bad arguments */
  14802. if (ret == 0) {
  14803. /* null Rc2 struct */
  14804. ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
  14805. if (ret == BAD_FUNC_ARG) {
  14806. ret = 0;
  14807. }
  14808. }
  14809. if (ret == 0) {
  14810. /* null out buffer */
  14811. ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
  14812. if (ret == BAD_FUNC_ARG) {
  14813. ret = 0;
  14814. }
  14815. }
  14816. if (ret == 0) {
  14817. /* null input buffer */
  14818. ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
  14819. if (ret == BAD_FUNC_ARG) {
  14820. ret = 0;
  14821. }
  14822. }
  14823. /* Rc2CbcDecrypt bad arguments */
  14824. if (ret == 0) {
  14825. /* in size is 0 */
  14826. ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
  14827. if (ret != 0) {
  14828. ret = WOLFSSL_FATAL_ERROR;
  14829. }
  14830. }
  14831. if (ret == 0) {
  14832. /* null Rc2 struct */
  14833. ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
  14834. if (ret == BAD_FUNC_ARG) {
  14835. ret = 0;
  14836. }
  14837. }
  14838. if (ret == 0) {
  14839. /* null out buffer */
  14840. ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
  14841. if (ret == BAD_FUNC_ARG) {
  14842. ret = 0;
  14843. }
  14844. }
  14845. if (ret == 0) {
  14846. /* null input buffer */
  14847. ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
  14848. if (ret == BAD_FUNC_ARG) {
  14849. ret = 0;
  14850. }
  14851. }
  14852. printf(resultFmt, ret == 0 ? passed : failed);
  14853. #endif
  14854. return ret;
  14855. } /* END test_wc_Rc2SetKey */
  14856. /*
  14857. * Testing function for wc_AesSetIV
  14858. */
  14859. static int test_wc_AesSetIV(void)
  14860. {
  14861. int ret = 0;
  14862. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14863. Aes aes;
  14864. byte key16[] =
  14865. {
  14866. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14867. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14868. };
  14869. byte iv1[] = "1234567890abcdef";
  14870. byte iv2[] = "0987654321fedcba";
  14871. printf(testingFmt, "wc_AesSetIV()");
  14872. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14873. if (ret != 0)
  14874. return ret;
  14875. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14876. iv1, AES_ENCRYPTION);
  14877. if(ret == 0) {
  14878. ret = wc_AesSetIV(&aes, iv2);
  14879. }
  14880. /* Test bad args. */
  14881. if(ret == 0) {
  14882. ret = wc_AesSetIV(NULL, iv1);
  14883. if(ret == BAD_FUNC_ARG) {
  14884. /* NULL iv should return 0. */
  14885. ret = wc_AesSetIV(&aes, NULL);
  14886. } else {
  14887. ret = WOLFSSL_FATAL_ERROR;
  14888. }
  14889. }
  14890. wc_AesFree(&aes);
  14891. printf(resultFmt, ret == 0 ? passed : failed);
  14892. #endif
  14893. return ret;
  14894. } /* test_wc_AesSetIV */
  14895. /*
  14896. * Testing function for wc_AesSetKey().
  14897. */
  14898. static int test_wc_AesSetKey(void)
  14899. {
  14900. int ret = 0;
  14901. #ifndef NO_AES
  14902. Aes aes;
  14903. byte key16[] =
  14904. {
  14905. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14906. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14907. };
  14908. #ifdef WOLFSSL_AES_192
  14909. byte key24[] =
  14910. {
  14911. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14912. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14913. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  14914. };
  14915. #endif
  14916. #ifdef WOLFSSL_AES_256
  14917. byte key32[] =
  14918. {
  14919. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14920. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14921. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14922. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14923. };
  14924. #endif
  14925. byte badKey16[] =
  14926. {
  14927. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14928. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  14929. };
  14930. byte iv[] = "1234567890abcdef";
  14931. printf(testingFmt, "wc_AesSetKey()");
  14932. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  14933. if (ret != 0)
  14934. return ret;
  14935. #ifdef WOLFSSL_AES_128
  14936. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  14937. iv, AES_ENCRYPTION);
  14938. #endif
  14939. #ifdef WOLFSSL_AES_192
  14940. if (ret == 0) {
  14941. ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
  14942. iv, AES_ENCRYPTION);
  14943. }
  14944. #endif
  14945. #ifdef WOLFSSL_AES_256
  14946. if (ret == 0) {
  14947. ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
  14948. iv, AES_ENCRYPTION);
  14949. }
  14950. #endif
  14951. /* Pass in bad args. */
  14952. if (ret == 0) {
  14953. ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
  14954. iv, AES_ENCRYPTION);
  14955. if (ret == BAD_FUNC_ARG) {
  14956. ret = wc_AesSetKey(&aes, badKey16,
  14957. (word32) sizeof(badKey16) / sizeof(byte),
  14958. iv, AES_ENCRYPTION);
  14959. }
  14960. if (ret == BAD_FUNC_ARG) {
  14961. ret = 0;
  14962. } else {
  14963. ret = WOLFSSL_FATAL_ERROR;
  14964. }
  14965. }
  14966. wc_AesFree(&aes);
  14967. printf(resultFmt, ret == 0 ? passed : failed);
  14968. #endif
  14969. return ret;
  14970. } /* END test_wc_AesSetKey */
  14971. /*
  14972. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  14973. * and wc_AesCbcDecryptWithKey()
  14974. */
  14975. static int test_wc_AesCbcEncryptDecrypt(void)
  14976. {
  14977. int ret = 0;
  14978. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  14979. defined(WOLFSSL_AES_256)
  14980. Aes aes;
  14981. byte key32[] =
  14982. {
  14983. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14984. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  14985. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  14986. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  14987. };
  14988. byte vector[] = /* Now is the time for all good men w/o trailing 0 */
  14989. {
  14990. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  14991. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  14992. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
  14993. 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
  14994. };
  14995. byte iv[] = "1234567890abcdef";
  14996. byte enc[sizeof(vector)];
  14997. byte dec[sizeof(vector)];
  14998. int cbcE = WOLFSSL_FATAL_ERROR;
  14999. int cbcD = WOLFSSL_FATAL_ERROR;
  15000. int cbcDWK = WOLFSSL_FATAL_ERROR;
  15001. byte dec2[sizeof(vector)];
  15002. /* Init stack variables. */
  15003. XMEMSET(enc, 0, sizeof(enc));
  15004. XMEMSET(dec, 0, sizeof(vector));
  15005. XMEMSET(dec2, 0, sizeof(vector));
  15006. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15007. if (ret != 0)
  15008. return ret;
  15009. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
  15010. if (ret == 0) {
  15011. ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
  15012. if (ret == 0) {
  15013. /* Re init for decrypt and set flag. */
  15014. cbcE = 0;
  15015. wc_AesFree(&aes);
  15016. ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
  15017. iv, AES_DECRYPTION);
  15018. }
  15019. if (ret == 0) {
  15020. ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
  15021. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
  15022. ret = WOLFSSL_FATAL_ERROR;
  15023. } else {
  15024. /* Set flag. */
  15025. cbcD = 0;
  15026. }
  15027. }
  15028. }
  15029. /* If encrypt succeeds but cbc decrypt fails, we can still test. */
  15030. if (ret == 0 || cbcE == 0) {
  15031. ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15032. key32, sizeof(key32)/sizeof(byte), iv);
  15033. if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
  15034. cbcDWK = 0;
  15035. }
  15036. }
  15037. printf(testingFmt, "wc_AesCbcEncrypt()");
  15038. /* Pass in bad args */
  15039. if (cbcE == 0) {
  15040. cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
  15041. if (cbcE == BAD_FUNC_ARG) {
  15042. cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
  15043. }
  15044. if (cbcE == BAD_FUNC_ARG) {
  15045. cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
  15046. }
  15047. if (cbcE == BAD_FUNC_ARG) {
  15048. cbcE = 0;
  15049. } else {
  15050. cbcE = WOLFSSL_FATAL_ERROR;
  15051. }
  15052. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15053. if (cbcE == 0) {
  15054. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
  15055. }
  15056. if (cbcE == BAD_LENGTH_E) {
  15057. cbcE = 0;
  15058. } else {
  15059. cbcE = WOLFSSL_FATAL_ERROR;
  15060. }
  15061. #endif
  15062. }
  15063. if (cbcE == 0) {
  15064. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15065. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  15066. printf("Zero length inputs not supported with AESNI in FIPS mode (v2),"
  15067. " skip test");
  15068. #else
  15069. /* Test passing in size of 0 */
  15070. XMEMSET(enc, 0, sizeof(enc));
  15071. cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
  15072. if (cbcE == 0) {
  15073. /* Check enc was not modified */
  15074. int i;
  15075. for (i = 0; i < (int)sizeof(enc); i++)
  15076. cbcE |= enc[i];
  15077. }
  15078. #endif
  15079. }
  15080. printf(resultFmt, cbcE == 0 ? passed : failed);
  15081. if (cbcE != 0) {
  15082. wc_AesFree(&aes);
  15083. return cbcE;
  15084. }
  15085. printf(testingFmt, "wc_AesCbcDecrypt()");
  15086. if (cbcD == 0) {
  15087. cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
  15088. if (cbcD == BAD_FUNC_ARG) {
  15089. cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
  15090. }
  15091. if (cbcD == BAD_FUNC_ARG) {
  15092. cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
  15093. }
  15094. if (cbcD == BAD_FUNC_ARG) {
  15095. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
  15096. }
  15097. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  15098. if (cbcD == BAD_LENGTH_E) {
  15099. cbcD = 0;
  15100. } else {
  15101. cbcD = WOLFSSL_FATAL_ERROR;
  15102. }
  15103. #else
  15104. if (cbcD == BAD_FUNC_ARG) {
  15105. cbcD = 0;
  15106. } else {
  15107. cbcD = WOLFSSL_FATAL_ERROR;
  15108. }
  15109. #endif
  15110. }
  15111. if (cbcD == 0) {
  15112. /* Test passing in size of 0 */
  15113. XMEMSET(dec, 0, sizeof(dec));
  15114. cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
  15115. if (cbcD == 0) {
  15116. /* Check dec was not modified */
  15117. int i;
  15118. for (i = 0; i < (int)sizeof(dec); i++)
  15119. cbcD |= dec[i];
  15120. }
  15121. }
  15122. printf(resultFmt, cbcD == 0 ? passed : failed);
  15123. if (cbcD != 0) {
  15124. wc_AesFree(&aes);
  15125. return cbcD;
  15126. }
  15127. printf(testingFmt, "wc_AesCbcDecryptWithKey()");
  15128. if (cbcDWK == 0) {
  15129. cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  15130. key32, sizeof(key32)/sizeof(byte), iv);
  15131. if (cbcDWK == BAD_FUNC_ARG) {
  15132. cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  15133. key32, sizeof(key32)/sizeof(byte), iv);
  15134. }
  15135. if (cbcDWK == BAD_FUNC_ARG) {
  15136. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15137. NULL, sizeof(key32)/sizeof(byte), iv);
  15138. }
  15139. if (cbcDWK == BAD_FUNC_ARG) {
  15140. cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  15141. key32, sizeof(key32)/sizeof(byte), NULL);
  15142. }
  15143. if (cbcDWK == BAD_FUNC_ARG) {
  15144. cbcDWK = 0;
  15145. } else {
  15146. cbcDWK = WOLFSSL_FATAL_ERROR;
  15147. }
  15148. }
  15149. wc_AesFree(&aes);
  15150. printf(resultFmt, cbcDWK == 0 ? passed : failed);
  15151. if (cbcDWK != 0) {
  15152. return cbcDWK;
  15153. }
  15154. #endif
  15155. return ret;
  15156. } /* END test_wc_AesCbcEncryptDecrypt */
  15157. /*
  15158. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  15159. */
  15160. static int test_wc_AesCtrEncryptDecrypt(void)
  15161. {
  15162. int ret = 0;
  15163. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  15164. Aes aesEnc, aesDec;
  15165. byte key32[] =
  15166. {
  15167. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15168. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15169. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15170. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15171. };
  15172. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15173. {
  15174. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15175. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15176. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15177. };
  15178. byte iv[] = "1234567890abcdef";
  15179. byte enc[AES_BLOCK_SIZE * 2];
  15180. byte dec[AES_BLOCK_SIZE * 2];
  15181. /* Init stack variables. */
  15182. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  15183. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  15184. printf(testingFmt, "wc_AesCtrEncrypt()");
  15185. ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
  15186. if (ret != 0)
  15187. return ret;
  15188. ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
  15189. if (ret != 0) {
  15190. wc_AesFree(&aesEnc);
  15191. return ret;
  15192. }
  15193. ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
  15194. iv, AES_ENCRYPTION);
  15195. if (ret == 0) {
  15196. ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
  15197. sizeof(vector)/sizeof(byte));
  15198. if (ret == 0) {
  15199. /* Decrypt with wc_AesCtrEncrypt() */
  15200. ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
  15201. iv, AES_ENCRYPTION);
  15202. }
  15203. if (ret == 0) {
  15204. ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
  15205. if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
  15206. ret = WOLFSSL_FATAL_ERROR;
  15207. }
  15208. }
  15209. }
  15210. /* Test bad args. */
  15211. if (ret == 0) {
  15212. ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
  15213. if (ret == BAD_FUNC_ARG) {
  15214. ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
  15215. }
  15216. if (ret == BAD_FUNC_ARG) {
  15217. ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
  15218. }
  15219. if (ret == BAD_FUNC_ARG) {
  15220. ret = 0;
  15221. } else {
  15222. ret = WOLFSSL_FATAL_ERROR;
  15223. }
  15224. }
  15225. wc_AesFree(&aesEnc);
  15226. wc_AesFree(&aesDec);
  15227. printf(resultFmt, ret == 0 ? passed : failed);
  15228. #endif
  15229. return ret;
  15230. } /* END test_wc_AesCtrEncryptDecrypt */
  15231. /*
  15232. * test function for wc_AesGcmSetKey()
  15233. */
  15234. static int test_wc_AesGcmSetKey(void)
  15235. {
  15236. int ret = 0;
  15237. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15238. Aes aes;
  15239. #ifdef WOLFSSL_AES_128
  15240. byte key16[] =
  15241. {
  15242. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15243. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15244. };
  15245. #endif
  15246. #ifdef WOLFSSL_AES_192
  15247. byte key24[] =
  15248. {
  15249. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15250. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15251. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15252. };
  15253. #endif
  15254. #ifdef WOLFSSL_AES_256
  15255. byte key32[] =
  15256. {
  15257. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15258. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15259. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15260. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15261. };
  15262. #endif
  15263. byte badKey16[] =
  15264. {
  15265. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15267. };
  15268. byte badKey24[] =
  15269. {
  15270. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15271. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15272. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  15273. };
  15274. byte badKey32[] =
  15275. {
  15276. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  15277. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15278. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15279. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  15280. };
  15281. printf(testingFmt, "wc_AesGcmSetKey()");
  15282. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15283. if (ret != 0)
  15284. return ret;
  15285. #ifdef WOLFSSL_AES_128
  15286. ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
  15287. #endif
  15288. #ifdef WOLFSSL_AES_192
  15289. if (ret == 0) {
  15290. ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
  15291. }
  15292. #endif
  15293. #ifdef WOLFSSL_AES_256
  15294. if (ret == 0) {
  15295. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15296. }
  15297. #endif
  15298. /* Pass in bad args. */
  15299. if (ret == 0) {
  15300. ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
  15301. if (ret == BAD_FUNC_ARG) {
  15302. ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
  15303. }
  15304. if (ret == BAD_FUNC_ARG) {
  15305. ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
  15306. }
  15307. if (ret == BAD_FUNC_ARG) {
  15308. ret = 0;
  15309. } else {
  15310. ret = WOLFSSL_FATAL_ERROR;
  15311. }
  15312. }
  15313. wc_AesFree(&aes);
  15314. printf(resultFmt, ret == 0 ? passed : failed);
  15315. #endif
  15316. return ret;
  15317. } /* END test_wc_AesGcmSetKey */
  15318. /*
  15319. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  15320. */
  15321. static int test_wc_AesGcmEncryptDecrypt(void)
  15322. {
  15323. int ret = 0;
  15324. /* WOLFSSL_AFALG requires 12 byte IV */
  15325. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  15326. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  15327. Aes aes;
  15328. byte key32[] =
  15329. {
  15330. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15331. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15332. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15333. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15334. };
  15335. byte vector[] = /* Now is the time for all w/o trailing 0 */
  15336. {
  15337. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15338. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15339. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15340. };
  15341. const byte a[] =
  15342. {
  15343. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15344. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  15345. 0xab, 0xad, 0xda, 0xd2
  15346. };
  15347. byte iv[] = "1234567890a";
  15348. byte longIV[] = "1234567890abcdefghij";
  15349. byte enc[sizeof(vector)];
  15350. byte resultT[AES_BLOCK_SIZE];
  15351. byte dec[sizeof(vector)];
  15352. int gcmD = WOLFSSL_FATAL_ERROR;
  15353. int gcmE = WOLFSSL_FATAL_ERROR;
  15354. /* Init stack variables. */
  15355. XMEMSET(enc, 0, sizeof(vector));
  15356. XMEMSET(dec, 0, sizeof(vector));
  15357. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  15358. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  15359. if (ret != 0)
  15360. return ret;
  15361. ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
  15362. if (ret == 0) {
  15363. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
  15364. iv, sizeof(iv)/sizeof(byte), resultT,
  15365. sizeof(resultT), a, sizeof(a));
  15366. }
  15367. if (gcmE == 0) { /* If encrypt fails, no decrypt. */
  15368. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
  15369. iv, sizeof(iv)/sizeof(byte), resultT,
  15370. sizeof(resultT), a, sizeof(a));
  15371. if(gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
  15372. gcmD = WOLFSSL_FATAL_ERROR;
  15373. }
  15374. }
  15375. printf(testingFmt, "wc_AesGcmEncrypt()");
  15376. /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  15377. if (gcmE == 0) {
  15378. gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
  15379. iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
  15380. a, sizeof(a));
  15381. if (gcmE == BAD_FUNC_ARG) {
  15382. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15383. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15384. resultT, sizeof(resultT) + 1, a, sizeof(a));
  15385. }
  15386. if (gcmE == BAD_FUNC_ARG) {
  15387. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15388. sizeof(vector), iv, sizeof(iv)/sizeof(byte),
  15389. resultT, sizeof(resultT) - 5, a, sizeof(a));
  15390. }
  15391. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15392. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  15393. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15394. /* FIPS does not check the lower bound of ivSz */
  15395. #else
  15396. if (gcmE == BAD_FUNC_ARG) {
  15397. gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
  15398. sizeof(vector), iv, 0,
  15399. resultT, sizeof(resultT), a, sizeof(a));
  15400. }
  15401. #endif
  15402. if (gcmE == BAD_FUNC_ARG) {
  15403. gcmE = 0;
  15404. } else {
  15405. gcmE = WOLFSSL_FATAL_ERROR;
  15406. }
  15407. }
  15408. /* This case is now considered good. Long IVs are now allowed.
  15409. * Except for the original FIPS release, it still has an upper
  15410. * bound on the IV length. */
  15411. #if (!defined(HAVE_FIPS) || \
  15412. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  15413. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15414. if (gcmE == 0) {
  15415. gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  15416. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
  15417. a, sizeof(a));
  15418. }
  15419. #else
  15420. (void)longIV;
  15421. #endif /* Old FIPS */
  15422. /* END wc_AesGcmEncrypt */
  15423. printf(resultFmt, gcmE == 0 ? passed : failed);
  15424. if (gcmE != 0) {
  15425. wc_AesFree(&aes);
  15426. return gcmE;
  15427. }
  15428. #ifdef HAVE_AES_DECRYPT
  15429. printf(testingFmt, "wc_AesGcmDecrypt()");
  15430. if (gcmD == 0) {
  15431. gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
  15432. iv, sizeof(iv)/sizeof(byte), resultT,
  15433. sizeof(resultT), a, sizeof(a));
  15434. if (gcmD == BAD_FUNC_ARG) {
  15435. gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
  15436. iv, sizeof(iv)/sizeof(byte), resultT,
  15437. sizeof(resultT), a, sizeof(a));
  15438. }
  15439. if (gcmD == BAD_FUNC_ARG) {
  15440. gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
  15441. iv, sizeof(iv)/sizeof(byte), resultT,
  15442. sizeof(resultT), a, sizeof(a));
  15443. }
  15444. if (gcmD == BAD_FUNC_ARG) {
  15445. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15446. NULL, sizeof(iv)/sizeof(byte), resultT,
  15447. sizeof(resultT), a, sizeof(a));
  15448. }
  15449. if (gcmD == BAD_FUNC_ARG) {
  15450. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15451. iv, sizeof(iv)/sizeof(byte), NULL,
  15452. sizeof(resultT), a, sizeof(a));
  15453. }
  15454. if (gcmD == BAD_FUNC_ARG) {
  15455. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15456. iv, sizeof(iv)/sizeof(byte), resultT,
  15457. sizeof(resultT) + 1, a, sizeof(a));
  15458. }
  15459. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  15460. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  15461. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  15462. /* FIPS does not check the lower bound of ivSz */
  15463. #else
  15464. if (gcmD == BAD_FUNC_ARG) {
  15465. gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  15466. iv, 0, resultT,
  15467. sizeof(resultT), a, sizeof(a));
  15468. }
  15469. #endif
  15470. if (gcmD == BAD_FUNC_ARG) {
  15471. gcmD = 0;
  15472. } else {
  15473. gcmD = WOLFSSL_FATAL_ERROR;
  15474. }
  15475. } /* END wc_AesGcmDecrypt */
  15476. printf(resultFmt, gcmD == 0 ? passed : failed);
  15477. #endif /* HAVE_AES_DECRYPT */
  15478. wc_AesFree(&aes);
  15479. #endif
  15480. return ret;
  15481. } /* END test_wc_AesGcmEncryptDecrypt */
  15482. /*
  15483. * unit test for wc_GmacSetKey()
  15484. */
  15485. static int test_wc_GmacSetKey(void)
  15486. {
  15487. int ret = 0;
  15488. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15489. Gmac gmac;
  15490. byte key16[] =
  15491. {
  15492. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15493. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15494. };
  15495. #ifdef WOLFSSL_AES_192
  15496. byte key24[] =
  15497. {
  15498. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15499. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15500. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15501. };
  15502. #endif
  15503. #ifdef WOLFSSL_AES_256
  15504. byte key32[] =
  15505. {
  15506. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15507. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15508. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15509. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15510. };
  15511. #endif
  15512. byte badKey16[] =
  15513. {
  15514. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15515. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  15516. };
  15517. byte badKey24[] =
  15518. {
  15519. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  15520. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  15521. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  15522. };
  15523. byte badKey32[] =
  15524. {
  15525. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15526. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  15527. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  15528. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  15529. };
  15530. printf(testingFmt, "wc_GmacSetKey()");
  15531. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15532. if (ret != 0)
  15533. return ret;
  15534. #ifdef WOLFSSL_AES_128
  15535. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
  15536. #endif
  15537. #ifdef WOLFSSL_AES_192
  15538. if (ret == 0) {
  15539. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15540. }
  15541. #endif
  15542. #ifdef WOLFSSL_AES_256
  15543. if (ret == 0) {
  15544. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15545. }
  15546. #endif
  15547. /* Pass in bad args. */
  15548. if (ret == 0) {
  15549. ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
  15550. if (ret == BAD_FUNC_ARG) {
  15551. ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
  15552. }
  15553. if (ret == BAD_FUNC_ARG) {
  15554. ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
  15555. }
  15556. if (ret == BAD_FUNC_ARG) {
  15557. ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
  15558. }
  15559. if (ret == BAD_FUNC_ARG) {
  15560. ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
  15561. }
  15562. if (ret == BAD_FUNC_ARG) {
  15563. ret = 0;
  15564. } else {
  15565. ret = WOLFSSL_FATAL_ERROR;
  15566. }
  15567. }
  15568. wc_AesFree(&gmac.aes);
  15569. printf(resultFmt, ret == 0 ? passed : failed);
  15570. #endif
  15571. return ret;
  15572. } /* END test_wc_GmacSetKey */
  15573. /*
  15574. * unit test for wc_GmacUpdate
  15575. */
  15576. static int test_wc_GmacUpdate(void)
  15577. {
  15578. int ret = 0;
  15579. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  15580. Gmac gmac;
  15581. #ifdef WOLFSSL_AES_128
  15582. const byte key16[] =
  15583. {
  15584. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  15585. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  15586. };
  15587. #endif
  15588. #ifdef WOLFSSL_AES_192
  15589. byte key24[] =
  15590. {
  15591. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  15592. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  15593. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  15594. };
  15595. #endif
  15596. #ifdef WOLFSSL_AES_256
  15597. byte key32[] =
  15598. {
  15599. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  15600. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  15601. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  15602. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  15603. };
  15604. #endif
  15605. #ifdef WOLFSSL_AES_128
  15606. const byte authIn[] =
  15607. {
  15608. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  15609. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  15610. };
  15611. #endif
  15612. #ifdef WOLFSSL_AES_192
  15613. const byte authIn2[] =
  15614. {
  15615. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  15616. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  15617. };
  15618. #endif
  15619. const byte authIn3[] =
  15620. {
  15621. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  15622. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  15623. };
  15624. #ifdef WOLFSSL_AES_128
  15625. const byte tag1[] = /* Known. */
  15626. {
  15627. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  15628. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  15629. };
  15630. #endif
  15631. #ifdef WOLFSSL_AES_192
  15632. const byte tag2[] = /* Known */
  15633. {
  15634. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  15635. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  15636. };
  15637. #endif
  15638. const byte tag3[] = /* Known */
  15639. {
  15640. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  15641. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  15642. };
  15643. #ifdef WOLFSSL_AES_128
  15644. const byte iv[] =
  15645. {
  15646. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  15647. 0xe2, 0x8c, 0x8f, 0x16
  15648. };
  15649. #endif
  15650. #ifdef WOLFSSL_AES_192
  15651. const byte iv2[] =
  15652. {
  15653. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  15654. 0x7e, 0x1a, 0x6f, 0xbc
  15655. };
  15656. #endif
  15657. const byte iv3[] =
  15658. {
  15659. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  15660. 0xc3, 0xfb, 0x6c, 0x8a
  15661. };
  15662. byte tagOut[16];
  15663. byte tagOut2[24];
  15664. byte tagOut3[32];
  15665. /* Init stack variables. */
  15666. XMEMSET(tagOut, 0, sizeof(tagOut));
  15667. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  15668. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  15669. printf(testingFmt, "wc_GmacUpdate()");
  15670. ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
  15671. if (ret != 0)
  15672. return ret;
  15673. #ifdef WOLFSSL_AES_128
  15674. ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
  15675. if (ret == 0) {
  15676. ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  15677. tagOut, sizeof(tag1));
  15678. if (ret == 0) {
  15679. ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
  15680. }
  15681. wc_AesFree(&gmac.aes);
  15682. }
  15683. #endif
  15684. #ifdef WOLFSSL_AES_192
  15685. if (ret == 0) {
  15686. XMEMSET(&gmac, 0, sizeof(Gmac));
  15687. ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
  15688. }
  15689. if (ret == 0) {
  15690. ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
  15691. sizeof(authIn2), tagOut2, sizeof(tag2));
  15692. }
  15693. if (ret == 0) {
  15694. ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
  15695. wc_AesFree(&gmac.aes);
  15696. }
  15697. #endif
  15698. #ifdef WOLFSSL_AES_256
  15699. if (ret == 0) {
  15700. XMEMSET(&gmac, 0, sizeof(Gmac));
  15701. ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
  15702. }
  15703. if (ret == 0) {
  15704. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15705. sizeof(authIn3), tagOut3, sizeof(tag3));
  15706. }
  15707. if (ret == 0) {
  15708. ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
  15709. }
  15710. #endif
  15711. /*Pass bad args. */
  15712. if (ret == 0) {
  15713. ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
  15714. sizeof(authIn3), tagOut3, sizeof(tag3));
  15715. if (ret == BAD_FUNC_ARG) {
  15716. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15717. sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
  15718. }
  15719. if (ret == BAD_FUNC_ARG) {
  15720. ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
  15721. sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
  15722. }
  15723. if (ret == BAD_FUNC_ARG) {
  15724. ret = 0;
  15725. } else {
  15726. ret = WOLFSSL_FATAL_ERROR;
  15727. }
  15728. }
  15729. wc_AesFree(&gmac.aes);
  15730. printf(resultFmt, ret == 0 ? passed : failed);
  15731. #endif
  15732. return ret;
  15733. } /* END test_wc_GmacUpdate */
  15734. /*
  15735. * testing wc_CamelliaSetKey
  15736. */
  15737. static int test_wc_CamelliaSetKey(void)
  15738. {
  15739. int ret = 0;
  15740. #ifdef HAVE_CAMELLIA
  15741. Camellia camellia;
  15742. /*128-bit key*/
  15743. static const byte key16[] =
  15744. {
  15745. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15746. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  15747. };
  15748. /* 192-bit key */
  15749. static const byte key24[] =
  15750. {
  15751. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15752. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15753. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15754. };
  15755. /* 256-bit key */
  15756. static const byte key32[] =
  15757. {
  15758. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15759. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15760. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  15761. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  15762. };
  15763. static const byte iv[] =
  15764. {
  15765. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15766. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15767. };
  15768. printf(testingFmt, "wc_CamelliaSetKey()");
  15769. ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
  15770. if (ret == 0) {
  15771. ret = wc_CamelliaSetKey(&camellia, key16,
  15772. (word32)sizeof(key16), NULL);
  15773. if (ret == 0) {
  15774. ret = wc_CamelliaSetKey(&camellia, key24,
  15775. (word32)sizeof(key24), iv);
  15776. }
  15777. if (ret == 0) {
  15778. ret = wc_CamelliaSetKey(&camellia, key24,
  15779. (word32)sizeof(key24), NULL);
  15780. }
  15781. if (ret == 0) {
  15782. ret = wc_CamelliaSetKey(&camellia, key32,
  15783. (word32)sizeof(key32), iv);
  15784. }
  15785. if (ret == 0) {
  15786. ret = wc_CamelliaSetKey(&camellia, key32,
  15787. (word32)sizeof(key32), NULL);
  15788. }
  15789. }
  15790. /* Bad args. */
  15791. if (ret == 0) {
  15792. ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
  15793. if (ret != BAD_FUNC_ARG) {
  15794. ret = WOLFSSL_FATAL_ERROR;
  15795. } else {
  15796. ret = 0;
  15797. }
  15798. } /* END bad args. */
  15799. #endif
  15800. return ret;
  15801. } /* END test_wc_CammeliaSetKey */
  15802. /*
  15803. * Testing wc_CamelliaSetIV()
  15804. */
  15805. static int test_wc_CamelliaSetIV(void)
  15806. {
  15807. int ret = 0;
  15808. #ifdef HAVE_CAMELLIA
  15809. Camellia camellia;
  15810. static const byte iv[] =
  15811. {
  15812. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15813. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15814. };
  15815. printf(testingFmt, "wc_CamelliaSetIV()");
  15816. ret = wc_CamelliaSetIV(&camellia, iv);
  15817. if (ret == 0) {
  15818. ret = wc_CamelliaSetIV(&camellia, NULL);
  15819. }
  15820. /* Bad args. */
  15821. if (ret == 0) {
  15822. ret = wc_CamelliaSetIV(NULL, NULL);
  15823. if (ret != BAD_FUNC_ARG) {
  15824. ret = WOLFSSL_FATAL_ERROR;
  15825. } else {
  15826. ret = 0;
  15827. }
  15828. }
  15829. printf(resultFmt, ret == 0 ? passed : failed);
  15830. #endif
  15831. return ret;
  15832. } /*END test_wc_CamelliaSetIV*/
  15833. /*
  15834. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  15835. */
  15836. static int test_wc_CamelliaEncryptDecryptDirect(void)
  15837. {
  15838. int ret = 0;
  15839. #ifdef HAVE_CAMELLIA
  15840. Camellia camellia;
  15841. static const byte key24[] =
  15842. {
  15843. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15844. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15845. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15846. };
  15847. static const byte iv[] =
  15848. {
  15849. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  15850. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  15851. };
  15852. static const byte plainT[] =
  15853. {
  15854. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15855. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15856. };
  15857. byte enc[sizeof(plainT)];
  15858. byte dec[sizeof(enc)];
  15859. int camE = WOLFSSL_FATAL_ERROR;
  15860. int camD = WOLFSSL_FATAL_ERROR;
  15861. /*Init stack variables.*/
  15862. XMEMSET(enc, 0, 16);
  15863. XMEMSET(enc, 0, 16);
  15864. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
  15865. if (ret == 0) {
  15866. ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
  15867. if (ret == 0) {
  15868. ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
  15869. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15870. ret = WOLFSSL_FATAL_ERROR;
  15871. }
  15872. }
  15873. }
  15874. printf(testingFmt, "wc_CamelliaEncryptDirect()");
  15875. /* Pass bad args. */
  15876. if (ret == 0) {
  15877. camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
  15878. if (camE == BAD_FUNC_ARG) {
  15879. camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
  15880. }
  15881. if (camE == BAD_FUNC_ARG) {
  15882. camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
  15883. }
  15884. if (camE == BAD_FUNC_ARG) {
  15885. camE = 0;
  15886. } else {
  15887. camE = WOLFSSL_FATAL_ERROR;
  15888. }
  15889. }
  15890. printf(resultFmt, camE == 0 ? passed : failed);
  15891. if (camE != 0) {
  15892. return camE;
  15893. }
  15894. printf(testingFmt, "wc_CamelliaDecryptDirect()");
  15895. if (ret == 0) {
  15896. camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
  15897. if (camD == BAD_FUNC_ARG) {
  15898. camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
  15899. }
  15900. if (camD == BAD_FUNC_ARG) {
  15901. camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
  15902. }
  15903. if (camD == BAD_FUNC_ARG) {
  15904. camD = 0;
  15905. } else {
  15906. camD = WOLFSSL_FATAL_ERROR;
  15907. }
  15908. }
  15909. printf(resultFmt, camD == 0 ? passed : failed);
  15910. if (camD != 0) {
  15911. return camD;
  15912. }
  15913. #endif
  15914. return ret;
  15915. } /* END test-wc_CamelliaEncryptDecryptDirect */
  15916. /*
  15917. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  15918. */
  15919. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  15920. {
  15921. int ret = 0;
  15922. #ifdef HAVE_CAMELLIA
  15923. Camellia camellia;
  15924. static const byte key24[] =
  15925. {
  15926. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  15927. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  15928. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  15929. };
  15930. static const byte plainT[] =
  15931. {
  15932. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  15933. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  15934. };
  15935. byte enc[CAMELLIA_BLOCK_SIZE];
  15936. byte dec[CAMELLIA_BLOCK_SIZE];
  15937. int camCbcE = WOLFSSL_FATAL_ERROR;
  15938. int camCbcD = WOLFSSL_FATAL_ERROR;
  15939. /* Init stack variables. */
  15940. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15941. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  15942. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15943. if (ret == 0) {
  15944. ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15945. if (ret != 0) {
  15946. ret = WOLFSSL_FATAL_ERROR;
  15947. }
  15948. }
  15949. if (ret == 0) {
  15950. ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
  15951. if (ret == 0) {
  15952. ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
  15953. if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
  15954. ret = WOLFSSL_FATAL_ERROR;
  15955. }
  15956. }
  15957. }
  15958. printf(testingFmt, "wc_CamelliaCbcEncrypt");
  15959. /* Pass in bad args. */
  15960. if (ret == 0) {
  15961. camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
  15962. if (camCbcE == BAD_FUNC_ARG) {
  15963. camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  15964. CAMELLIA_BLOCK_SIZE);
  15965. }
  15966. if (camCbcE == BAD_FUNC_ARG) {
  15967. camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  15968. CAMELLIA_BLOCK_SIZE);
  15969. }
  15970. if (camCbcE == BAD_FUNC_ARG) {
  15971. camCbcE = 0;
  15972. } else {
  15973. camCbcE = WOLFSSL_FATAL_ERROR;
  15974. }
  15975. }
  15976. printf(resultFmt, camCbcE == 0 ? passed : failed);
  15977. if (camCbcE != 0) {
  15978. return camCbcE;
  15979. }
  15980. printf(testingFmt, "wc_CamelliaCbcDecrypt()");
  15981. if (ret == 0) {
  15982. camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
  15983. if (camCbcD == BAD_FUNC_ARG) {
  15984. camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  15985. CAMELLIA_BLOCK_SIZE);
  15986. }
  15987. if (camCbcD == BAD_FUNC_ARG) {
  15988. camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  15989. CAMELLIA_BLOCK_SIZE);
  15990. }
  15991. if (camCbcD == BAD_FUNC_ARG) {
  15992. camCbcD = 0;
  15993. } else {
  15994. camCbcD = WOLFSSL_FATAL_ERROR;
  15995. }
  15996. } /* END bad args. */
  15997. printf(resultFmt, camCbcD == 0 ? passed : failed);
  15998. if (camCbcD != 0) {
  15999. return camCbcD;
  16000. }
  16001. #endif
  16002. return ret;
  16003. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16004. /*
  16005. * Testing wc_Arc4SetKey()
  16006. */
  16007. static int test_wc_Arc4SetKey(void)
  16008. {
  16009. int ret = 0;
  16010. #ifndef NO_RC4
  16011. Arc4 arc;
  16012. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16013. int keyLen = 8;
  16014. printf(testingFmt, "wc_Arch4SetKey()");
  16015. ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
  16016. /* Test bad args. */
  16017. if (ret == 0) {
  16018. ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
  16019. if (ret == BAD_FUNC_ARG)
  16020. ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
  16021. if (ret == BAD_FUNC_ARG)
  16022. ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
  16023. if (ret == BAD_FUNC_ARG)
  16024. ret = WOLFSSL_ERROR_NONE;
  16025. else
  16026. ret = WOLFSSL_FATAL_ERROR;
  16027. } /* END test bad args. */
  16028. printf(resultFmt, ret == 0 ? passed : failed);
  16029. #endif
  16030. return ret;
  16031. } /* END test_wc_Arc4SetKey */
  16032. /*
  16033. * Testing wc_Arc4Process for ENC/DEC.
  16034. */
  16035. static int test_wc_Arc4Process(void)
  16036. {
  16037. int ret = 0;
  16038. #ifndef NO_RC4
  16039. Arc4 enc, dec;
  16040. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16041. int keyLen = 8;
  16042. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16043. byte cipher[8];
  16044. byte plain[8];
  16045. /* Init stack variables */
  16046. XMEMSET(cipher, 0, sizeof(cipher));
  16047. XMEMSET(plain, 0, sizeof(plain));
  16048. /* Use for async. */
  16049. ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
  16050. if (ret == 0) {
  16051. ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
  16052. }
  16053. printf(testingFmt, "wc_Arc4Process()");
  16054. if (ret == 0) {
  16055. ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
  16056. }
  16057. if (ret == 0) {
  16058. ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
  16059. }
  16060. if (ret == 0) {
  16061. ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
  16062. }
  16063. if (ret == 0) {
  16064. ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
  16065. if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
  16066. ret = WOLFSSL_FATAL_ERROR;
  16067. } else {
  16068. ret = 0;
  16069. }
  16070. }
  16071. /* Bad args. */
  16072. if (ret == 0) {
  16073. ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
  16074. if (ret == BAD_FUNC_ARG) {
  16075. ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
  16076. }
  16077. if (ret == BAD_FUNC_ARG) {
  16078. ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
  16079. }
  16080. if (ret == BAD_FUNC_ARG) {
  16081. ret = 0;
  16082. } else {
  16083. ret = WOLFSSL_FATAL_ERROR;
  16084. }
  16085. }
  16086. printf(resultFmt, ret == 0 ? passed : failed);
  16087. wc_Arc4Free(&enc);
  16088. wc_Arc4Free(&dec);
  16089. #endif
  16090. return ret;
  16091. }/* END test_wc_Arc4Process */
  16092. /*
  16093. * Testing wc_Init RsaKey()
  16094. */
  16095. static int test_wc_InitRsaKey(void)
  16096. {
  16097. int ret = 0;
  16098. #ifndef NO_RSA
  16099. RsaKey key;
  16100. printf(testingFmt, "wc_InitRsaKey()");
  16101. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16102. /* Test bad args. */
  16103. if (ret == 0) {
  16104. ret = wc_InitRsaKey(NULL, HEAP_HINT);
  16105. #ifndef HAVE_USER_RSA
  16106. if (ret == BAD_FUNC_ARG) {
  16107. ret = 0;
  16108. } else {
  16109. #else
  16110. if (ret == USER_CRYPTO_ERROR) {
  16111. ret = 0;
  16112. } else {
  16113. #endif
  16114. ret = WOLFSSL_FATAL_ERROR;
  16115. }
  16116. } /* end if */
  16117. if (wc_FreeRsaKey(&key) || ret != 0) {
  16118. ret = WOLFSSL_FATAL_ERROR;
  16119. }
  16120. printf(resultFmt, ret == 0 ? passed : failed);
  16121. #endif
  16122. return ret;
  16123. } /* END test_wc_InitRsaKey */
  16124. /*
  16125. * Testing wc_RsaPrivateKeyDecode()
  16126. */
  16127. static int test_wc_RsaPrivateKeyDecode(void)
  16128. {
  16129. int ret = 0;
  16130. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16131. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16132. RsaKey key;
  16133. byte* tmp;
  16134. word32 idx = 0;
  16135. int bytes = 0;
  16136. printf(testingFmt, "wc_RsaPrivateKeyDecode()");
  16137. tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16138. if (tmp == NULL) {
  16139. ret = WOLFSSL_FATAL_ERROR;
  16140. }
  16141. if (ret == 0) {
  16142. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16143. }
  16144. if (ret == 0) {
  16145. #ifdef USE_CERT_BUFFERS_1024
  16146. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16147. bytes = sizeof_client_key_der_1024;
  16148. #else
  16149. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16150. bytes = sizeof_client_key_der_2048;
  16151. #endif /* Use cert buffers. */
  16152. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
  16153. }
  16154. #ifndef HAVE_USER_RSA
  16155. /* Test bad args. */
  16156. if (ret == 0) {
  16157. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16158. if (ret == BAD_FUNC_ARG) {
  16159. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16160. }
  16161. if (ret == BAD_FUNC_ARG) {
  16162. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16163. }
  16164. if (ret == BAD_FUNC_ARG) {
  16165. ret = 0;
  16166. } else {
  16167. ret = WOLFSSL_FATAL_ERROR;
  16168. }
  16169. }
  16170. #else
  16171. /* Test bad args. User RSA. */
  16172. if (ret == 0) {
  16173. ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
  16174. if (ret == USER_CRYPTO_ERROR) {
  16175. ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
  16176. }
  16177. if (ret == USER_CRYPTO_ERROR) {
  16178. ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16179. }
  16180. if (ret == USER_CRYPTO_ERROR) {
  16181. ret = 0;
  16182. } else {
  16183. ret = WOLFSSL_FATAL_ERROR;
  16184. }
  16185. }
  16186. #endif
  16187. if (tmp != NULL) {
  16188. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16189. }
  16190. if (wc_FreeRsaKey(&key) || ret != 0) {
  16191. ret = WOLFSSL_FATAL_ERROR;
  16192. }
  16193. printf(resultFmt, ret == 0 ? passed : failed);
  16194. #endif
  16195. return ret;
  16196. } /* END test_wc_RsaPrivateKeyDecode */
  16197. /*
  16198. * Testing wc_RsaPublicKeyDecode()
  16199. */
  16200. static int test_wc_RsaPublicKeyDecode(void)
  16201. {
  16202. int ret = 0;
  16203. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16204. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16205. RsaKey keyPub;
  16206. byte* tmp;
  16207. word32 idx = 0;
  16208. int bytes = 0;
  16209. word32 keySz = 0;
  16210. word32 tstKeySz = 0;
  16211. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16212. XFILE f;
  16213. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16214. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16215. byte buf[4096];
  16216. #endif
  16217. tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16218. if (tmp == NULL) {
  16219. ret = WOLFSSL_FATAL_ERROR;
  16220. }
  16221. if (ret == 0) {
  16222. ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
  16223. }
  16224. if (ret == 0) {
  16225. #ifdef USE_CERT_BUFFERS_1024
  16226. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16227. bytes = sizeof_client_keypub_der_1024;
  16228. keySz = 1024;
  16229. #else
  16230. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16231. bytes = sizeof_client_keypub_der_2048;
  16232. keySz = 2048;
  16233. #endif
  16234. printf(testingFmt, "wc_RsaPublicKeyDecode()");
  16235. ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
  16236. }
  16237. #ifndef HAVE_USER_RSA
  16238. /* Pass in bad args. */
  16239. if (ret == 0) {
  16240. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16241. if (ret == BAD_FUNC_ARG) {
  16242. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16243. }
  16244. if (ret == BAD_FUNC_ARG) {
  16245. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16246. }
  16247. if (ret == BAD_FUNC_ARG) {
  16248. ret = 0;
  16249. } else {
  16250. ret = WOLFSSL_FATAL_ERROR;
  16251. }
  16252. }
  16253. #else
  16254. /* Pass in bad args. */
  16255. if (ret == 0) {
  16256. ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
  16257. if (ret == USER_CRYPTO_ERROR) {
  16258. ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
  16259. }
  16260. if (ret == USER_CRYPTO_ERROR) {
  16261. ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
  16262. }
  16263. if (ret == USER_CRYPTO_ERROR) {
  16264. ret = 0;
  16265. } else {
  16266. ret = WOLFSSL_FATAL_ERROR;
  16267. }
  16268. }
  16269. #endif
  16270. if (wc_FreeRsaKey(&keyPub) || ret != 0) {
  16271. ret = WOLFSSL_FATAL_ERROR;
  16272. }
  16273. if (ret == 0) {
  16274. /* Test for getting modulus key size */
  16275. idx = 0;
  16276. ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16277. &tstKeySz, NULL, NULL);
  16278. ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
  16279. }
  16280. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16281. f = XFOPEN(rsaPssPubKey, "rb");
  16282. AssertTrue((f != XBADFILE));
  16283. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16284. XFCLOSE(f);
  16285. idx = 0;
  16286. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16287. NULL), 0);
  16288. f = XFOPEN(rsaPssPubKeyNoParams, "rb");
  16289. AssertTrue((f != XBADFILE));
  16290. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  16291. XFCLOSE(f);
  16292. idx = 0;
  16293. AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  16294. NULL), 0);
  16295. #endif
  16296. if (tmp != NULL) {
  16297. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16298. }
  16299. printf(resultFmt, ret == 0 ? passed : failed);
  16300. #endif
  16301. return ret;
  16302. } /* END test_wc_RsaPublicKeyDecode */
  16303. /*
  16304. * Testing wc_RsaPublicKeyDecodeRaw()
  16305. */
  16306. static int test_wc_RsaPublicKeyDecodeRaw(void)
  16307. {
  16308. int ret = 0;
  16309. #if !defined(NO_RSA)
  16310. RsaKey key;
  16311. const byte n = 0x23;
  16312. const byte e = 0x03;
  16313. int nSz = sizeof(n);
  16314. int eSz = sizeof(e);
  16315. printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
  16316. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16317. if (ret == 0) {
  16318. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
  16319. }
  16320. #ifndef HAVE_USER_RSA
  16321. /* Pass in bad args. */
  16322. if (ret == 0) {
  16323. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16324. if (ret == BAD_FUNC_ARG) {
  16325. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16326. }
  16327. if (ret == BAD_FUNC_ARG) {
  16328. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16329. }
  16330. if (ret == BAD_FUNC_ARG) {
  16331. ret = 0;
  16332. } else {
  16333. ret = WOLFSSL_FATAL_ERROR;
  16334. }
  16335. }
  16336. #else
  16337. /* Pass in bad args. User RSA. */
  16338. if (ret == 0) {
  16339. ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
  16340. if (ret == USER_CRYPTO_ERROR) {
  16341. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
  16342. }
  16343. if (ret == USER_CRYPTO_ERROR) {
  16344. ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
  16345. }
  16346. if (ret == USER_CRYPTO_ERROR) {
  16347. ret = 0;
  16348. } else {
  16349. ret = WOLFSSL_FATAL_ERROR;
  16350. }
  16351. }
  16352. #endif
  16353. if (wc_FreeRsaKey(&key) || ret != 0) {
  16354. ret = WOLFSSL_FATAL_ERROR;
  16355. }
  16356. printf(resultFmt, ret == 0 ? passed : failed);
  16357. #endif
  16358. return ret;
  16359. } /* END test_wc_RsaPublicKeyDecodeRaw */
  16360. #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
  16361. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  16362. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  16363. * trying until it gets a probable prime. */
  16364. #ifdef HAVE_FIPS
  16365. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  16366. {
  16367. int ret;
  16368. for (;;) {
  16369. ret = wc_MakeRsaKey(key, size, e, rng);
  16370. if (ret != PRIME_GEN_E) break;
  16371. printf("MakeRsaKey couldn't find prime; trying again.\n");
  16372. }
  16373. return ret;
  16374. }
  16375. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  16376. #else
  16377. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  16378. #endif
  16379. #endif
  16380. /*
  16381. * Testing wc_MakeRsaKey()
  16382. */
  16383. static int test_wc_MakeRsaKey(void)
  16384. {
  16385. int ret = 0;
  16386. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16387. RsaKey genKey;
  16388. WC_RNG rng;
  16389. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16390. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16391. int bits = 1024;
  16392. #else
  16393. int bits = 2048;
  16394. #endif
  16395. printf(testingFmt, "wc_MakeRsaKey()");
  16396. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  16397. if (ret == 0) {
  16398. ret = wc_InitRng(&rng);
  16399. if (ret == 0) {
  16400. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  16401. if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
  16402. ret = WOLFSSL_FATAL_ERROR;
  16403. }
  16404. }
  16405. }
  16406. #ifndef HAVE_USER_RSA
  16407. /* Test bad args. */
  16408. if (ret == 0) {
  16409. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16410. if (ret == BAD_FUNC_ARG) {
  16411. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16412. }
  16413. if (ret == BAD_FUNC_ARG) {
  16414. /* e < 3 */
  16415. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16416. }
  16417. if (ret == BAD_FUNC_ARG) {
  16418. /* e & 1 == 0 */
  16419. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16420. }
  16421. if (ret == BAD_FUNC_ARG) {
  16422. ret = 0;
  16423. } else {
  16424. ret = WOLFSSL_FATAL_ERROR;
  16425. }
  16426. }
  16427. #else
  16428. /* Test bad args. */
  16429. if (ret == 0) {
  16430. ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
  16431. if (ret == USER_CRYPTO_ERROR) {
  16432. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
  16433. }
  16434. if (ret == USER_CRYPTO_ERROR) {
  16435. /* e < 3 */
  16436. ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
  16437. }
  16438. if (ret == USER_CRYPTO_ERROR) {
  16439. /* e & 1 == 0 */
  16440. ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
  16441. }
  16442. if (ret == USER_CRYPTO_ERROR) {
  16443. ret = 0;
  16444. } else {
  16445. ret = WOLFSSL_FATAL_ERROR;
  16446. }
  16447. }
  16448. #endif
  16449. if (wc_FreeRng(&rng) || ret != 0) {
  16450. ret = WOLFSSL_FATAL_ERROR;
  16451. }
  16452. printf(resultFmt, ret == 0 ? passed : failed);
  16453. #endif
  16454. return ret;
  16455. } /* END test_wc_MakeRsaKey */
  16456. /*
  16457. * Test the bounds checking on the cipher text versus the key modulus.
  16458. * 1. Make a new RSA key.
  16459. * 2. Set c to 1.
  16460. * 3. Decrypt c into k. (error)
  16461. * 4. Copy the key modulus to c and sub 1 from the copy.
  16462. * 5. Decrypt c into k. (error)
  16463. * Valid bounds test cases are covered by all the other RSA tests.
  16464. */
  16465. static int test_RsaDecryptBoundsCheck(void)
  16466. {
  16467. int ret = 0;
  16468. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  16469. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  16470. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  16471. RsaKey key;
  16472. byte flatC[256];
  16473. word32 flatCSz;
  16474. byte out[256];
  16475. word32 outSz = sizeof(out);
  16476. WC_RNG rng;
  16477. printf(testingFmt, "RSA decrypt bounds check");
  16478. XMEMSET(&rng, 0, sizeof(rng));
  16479. ret = wc_InitRng(&rng);
  16480. if (ret == 0)
  16481. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16482. if (ret == 0) {
  16483. const byte* derKey;
  16484. word32 derKeySz;
  16485. word32 idx = 0;
  16486. #ifdef USE_CERT_BUFFERS_1024
  16487. derKey = server_key_der_1024;
  16488. derKeySz = (word32)sizeof_server_key_der_1024;
  16489. flatCSz = 128;
  16490. #else
  16491. derKey = server_key_der_2048;
  16492. derKeySz = (word32)sizeof_server_key_der_2048;
  16493. flatCSz = 256;
  16494. #endif
  16495. ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
  16496. }
  16497. if (ret == 0) {
  16498. XMEMSET(flatC, 0, flatCSz);
  16499. flatC[flatCSz-1] = 1;
  16500. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16501. RSA_PRIVATE_DECRYPT, &rng);
  16502. if (ret == RSA_OUT_OF_RANGE_E) {
  16503. mp_int c;
  16504. mp_init_copy(&c, &key.n);
  16505. mp_sub_d(&c, 1, &c);
  16506. mp_to_unsigned_bin(&c, flatC);
  16507. ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  16508. RSA_PRIVATE_DECRYPT, NULL);
  16509. mp_clear(&c);
  16510. }
  16511. if (ret == RSA_OUT_OF_RANGE_E)
  16512. ret = 0;
  16513. else
  16514. ret = WOLFSSL_FATAL_ERROR;
  16515. }
  16516. if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
  16517. ret = WOLFSSL_FATAL_ERROR;
  16518. printf(resultFmt, ret == 0 ? passed : failed);
  16519. #endif
  16520. return ret;
  16521. } /* END test_wc_RsaDecryptBoundsCheck */
  16522. /*
  16523. * Testing wc_SetKeyUsage()
  16524. */
  16525. static int test_wc_SetKeyUsage(void)
  16526. {
  16527. int ret = 0;
  16528. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  16529. Cert myCert;
  16530. ret = wc_InitCert(&myCert);
  16531. printf(testingFmt, "wc_SetKeyUsage()");
  16532. if (ret == 0) {
  16533. ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
  16534. if (ret == 0) {
  16535. ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
  16536. }
  16537. if (ret == 0) {
  16538. ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
  16539. }
  16540. if (ret == 0) {
  16541. ret = wc_SetKeyUsage(&myCert, "decipherOnly");
  16542. }
  16543. if (ret == 0) {
  16544. ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
  16545. }
  16546. }
  16547. /* Test bad args. */
  16548. if (ret == 0) {
  16549. ret = wc_SetKeyUsage(NULL, "decipherOnly");
  16550. if (ret == BAD_FUNC_ARG) {
  16551. ret = wc_SetKeyUsage(&myCert, NULL);
  16552. }
  16553. if (ret == BAD_FUNC_ARG) {
  16554. ret = wc_SetKeyUsage(&myCert, "");
  16555. }
  16556. if (ret == KEYUSAGE_E) {
  16557. ret = wc_SetKeyUsage(&myCert, ",");
  16558. }
  16559. if (ret == KEYUSAGE_E) {
  16560. ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
  16561. }
  16562. if (ret == KEYUSAGE_E) {
  16563. ret = 0;
  16564. } else {
  16565. ret = WOLFSSL_FATAL_ERROR;
  16566. }
  16567. }
  16568. printf(resultFmt, ret == 0 ? passed : failed);
  16569. #endif
  16570. return ret;
  16571. } /* END test_wc_SetKeyUsage */
  16572. /*
  16573. * Testing wc_CheckProbablePrime()
  16574. */
  16575. static int test_wc_CheckProbablePrime(void)
  16576. {
  16577. int ret = 0;
  16578. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16579. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  16580. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  16581. RsaKey key;
  16582. WC_RNG rng;
  16583. byte e[3];
  16584. word32 eSz = (word32)sizeof(e);
  16585. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16586. word32 nSz = (word32)sizeof(n);
  16587. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  16588. word32 dSz = (word32)sizeof(d);
  16589. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16590. word32 pSz = (word32)sizeof(p);
  16591. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  16592. word32 qSz = (word32)sizeof(q);
  16593. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  16594. int* isPrime;
  16595. int test[5];
  16596. isPrime = test;
  16597. printf(testingFmt, "wc_CheckProbablePrime()");
  16598. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16599. if (ret == 0) {
  16600. ret = wc_InitRng(&rng);
  16601. }
  16602. if (ret == 0) {
  16603. ret = wc_RsaSetRNG(&key, &rng);
  16604. }
  16605. if (ret == 0) {
  16606. ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
  16607. }
  16608. if (ret == 0) {
  16609. PRIVATE_KEY_UNLOCK();
  16610. ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
  16611. p, &pSz, q, &qSz);
  16612. PRIVATE_KEY_LOCK();
  16613. }
  16614. /* Bad cases */
  16615. if (ret == 0) {
  16616. ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
  16617. nlen, isPrime);
  16618. if (ret == BAD_FUNC_ARG) {
  16619. ret = 0;
  16620. }
  16621. }
  16622. if (ret == 0) {
  16623. ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
  16624. nlen, isPrime);
  16625. if (ret == BAD_FUNC_ARG) {
  16626. ret = 0;
  16627. }
  16628. }
  16629. if (ret == 0) {
  16630. ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
  16631. nlen, isPrime);
  16632. if (ret == BAD_FUNC_ARG) {
  16633. ret = 0;
  16634. }
  16635. }
  16636. if (ret == 0) {
  16637. ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
  16638. nlen, isPrime);
  16639. if (ret == BAD_FUNC_ARG) {
  16640. ret = 0;
  16641. }
  16642. }
  16643. if (ret == 0) {
  16644. ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
  16645. nlen, isPrime);
  16646. if (ret == BAD_FUNC_ARG) {
  16647. ret = 0;
  16648. }
  16649. }
  16650. if (ret == 0) {
  16651. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
  16652. nlen, isPrime);
  16653. if (ret == BAD_FUNC_ARG) {
  16654. ret = 0;
  16655. }
  16656. }
  16657. if (ret == 0) {
  16658. ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
  16659. nlen, isPrime);
  16660. if (ret == BAD_FUNC_ARG) {
  16661. ret = 0;
  16662. }
  16663. }
  16664. /* Good case */
  16665. if (ret == 0) {
  16666. ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
  16667. nlen, isPrime);
  16668. }
  16669. wc_FreeRsaKey(&key);
  16670. wc_FreeRng(&rng);
  16671. printf(resultFmt, ret == 0 ? passed : failed);
  16672. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  16673. #endif
  16674. return ret;
  16675. } /* END test_wc_CheckProbablePrime */
  16676. /*
  16677. * Testing wc_RsaPSS_Verify()
  16678. */
  16679. static int test_wc_RsaPSS_Verify(void)
  16680. {
  16681. int ret = 0;
  16682. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16683. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16684. RsaKey key;
  16685. WC_RNG rng;
  16686. int sz = 256;
  16687. byte* pt;
  16688. const char* szMessage = "This is the string to be signed";
  16689. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16690. unsigned char pDecrypted[2048/8];
  16691. word32 outLen = sizeof(pDecrypted);
  16692. pt = pDecrypted;
  16693. printf(testingFmt, "wc_RsaPSS_Verify()");
  16694. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16695. if (ret == 0) {
  16696. ret = wc_InitRng(&rng);
  16697. }
  16698. if (ret == 0) {
  16699. ret = wc_RsaSetRNG(&key, &rng);
  16700. }
  16701. if (ret == 0) {
  16702. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16703. }
  16704. if (ret == 0) {
  16705. ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
  16706. pSignature, sizeof(pSignature),
  16707. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16708. if (ret > 0 ){
  16709. sz = ret;
  16710. ret = 0;
  16711. }
  16712. }
  16713. /* Bad cases */
  16714. if (ret == 0) {
  16715. ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  16716. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16717. if (ret == BAD_FUNC_ARG) {
  16718. ret = 0;
  16719. }
  16720. }
  16721. if (ret == 0) {
  16722. ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  16723. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16724. if (ret == BAD_FUNC_ARG) {
  16725. ret = 0;
  16726. }
  16727. }
  16728. if (ret == 0) {
  16729. ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  16730. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16731. if (ret == BAD_FUNC_ARG) {
  16732. ret = 0;
  16733. }
  16734. }
  16735. if (ret == 0) {
  16736. ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  16737. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16738. if (ret == BAD_FUNC_ARG) {
  16739. ret = 0;
  16740. }
  16741. }
  16742. /* Good case */
  16743. if (ret == 0) {
  16744. ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  16745. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16746. if (ret > 0) {
  16747. ret = 0;
  16748. }
  16749. }
  16750. wc_FreeRsaKey(&key);
  16751. wc_FreeRng(&rng);
  16752. printf(resultFmt, ret == 0 ? passed : failed);
  16753. #endif
  16754. return ret;
  16755. } /* END test_wc_RsaPSS_Verify */
  16756. /*
  16757. * Testing wc_RsaPSS_VerifyCheck()
  16758. */
  16759. static int test_wc_RsaPSS_VerifyCheck(void)
  16760. {
  16761. int ret = 0;
  16762. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16763. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16764. RsaKey key;
  16765. WC_RNG rng;
  16766. int sz = 256; /* 2048/8 */
  16767. byte* pt;
  16768. byte digest[32];
  16769. word32 digestSz = sizeof(digest);
  16770. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16771. word32 pSignatureSz = sizeof(pSignature);
  16772. unsigned char pDecrypted[2048/8];
  16773. word32 outLen = sizeof(pDecrypted);
  16774. pt = pDecrypted;
  16775. printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
  16776. XMEMSET(digest, 0, sizeof(digest));
  16777. XMEMSET(pSignature, 0, sizeof(pSignature));
  16778. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16779. if (ret == 0) {
  16780. ret = wc_InitRng(&rng);
  16781. }
  16782. if (ret == 0) {
  16783. ret = wc_RsaSetRNG(&key, &rng);
  16784. }
  16785. if (ret == 0) {
  16786. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16787. }
  16788. if (ret == 0) {
  16789. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16790. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16791. }
  16792. if (ret == 0) {
  16793. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  16794. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16795. if (ret > 0 ){
  16796. sz = ret;
  16797. ret = 0;
  16798. }
  16799. }
  16800. /* Bad cases */
  16801. if (ret == 0) {
  16802. ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
  16803. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16804. if (ret == BAD_FUNC_ARG) {
  16805. ret = 0;
  16806. }
  16807. }
  16808. if (ret == 0) {
  16809. ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
  16810. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16811. if (ret == BAD_FUNC_ARG) {
  16812. ret = 0;
  16813. }
  16814. }
  16815. if (ret == 0) {
  16816. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
  16817. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16818. if (ret == BAD_FUNC_ARG) {
  16819. ret = 0;
  16820. }
  16821. }
  16822. if (ret == 0) {
  16823. ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
  16824. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16825. if (ret == BAD_FUNC_ARG) {
  16826. ret = 0;
  16827. }
  16828. }
  16829. /* Good case */
  16830. if (ret == 0) {
  16831. ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
  16832. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16833. if (ret > 0) {
  16834. ret = 0;
  16835. }
  16836. }
  16837. wc_FreeRsaKey(&key);
  16838. wc_FreeRng(&rng);
  16839. printf(resultFmt, ret == 0 ? passed : failed);
  16840. #endif
  16841. return ret;
  16842. } /* END test_wc_RsaPSS_VerifyCheck */
  16843. /*
  16844. * Testing wc_RsaPSS_VerifyCheckInline()
  16845. */
  16846. static int test_wc_RsaPSS_VerifyCheckInline(void)
  16847. {
  16848. int ret = 0;
  16849. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  16850. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  16851. RsaKey key;
  16852. WC_RNG rng;
  16853. int sz = 256;
  16854. byte* pt;
  16855. byte digest[32];
  16856. word32 digestSz = sizeof(digest);
  16857. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  16858. unsigned char pDecrypted[2048/8];
  16859. pt = pDecrypted;
  16860. printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
  16861. ret = wc_InitRsaKey(&key, HEAP_HINT);
  16862. XMEMSET(digest, 0, sizeof(digest));
  16863. XMEMSET(pSignature, 0, sizeof(pSignature));
  16864. if (ret == 0) {
  16865. ret = wc_InitRng(&rng);
  16866. }
  16867. if (ret == 0) {
  16868. ret = wc_RsaSetRNG(&key, &rng);
  16869. }
  16870. if (ret == 0) {
  16871. ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
  16872. }
  16873. if (ret == 0) {
  16874. digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
  16875. ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
  16876. }
  16877. if (ret == 0) {
  16878. ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
  16879. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
  16880. if (ret > 0 ){
  16881. sz = ret;
  16882. ret = 0;
  16883. }
  16884. }
  16885. /* Bad Cases */
  16886. if (ret == 0) {
  16887. ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
  16888. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16889. if (ret == BAD_FUNC_ARG) {
  16890. ret = 0;
  16891. }
  16892. }
  16893. if (ret == 0) {
  16894. ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
  16895. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16896. if (ret == BAD_FUNC_ARG) {
  16897. ret = 0;
  16898. }
  16899. }
  16900. if (ret == 0) {
  16901. ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
  16902. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16903. if (ret == BAD_FUNC_ARG) {
  16904. ret = 0;
  16905. }
  16906. }
  16907. if (ret == 0) {
  16908. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16909. digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
  16910. if (ret == BAD_FUNC_ARG) {
  16911. ret = 0;
  16912. }
  16913. }
  16914. /* Good case */
  16915. if (ret == 0) {
  16916. ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
  16917. digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
  16918. if (ret > 0) {
  16919. ret = 0;
  16920. }
  16921. }
  16922. wc_FreeRsaKey(&key);
  16923. wc_FreeRng(&rng);
  16924. printf(resultFmt, ret == 0 ? passed : failed);
  16925. #endif
  16926. return ret;
  16927. } /* END test_wc_RsaPSS_VerifyCheckInline */
  16928. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16929. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  16930. {
  16931. (void)flag;
  16932. (void)type;
  16933. (void)file;
  16934. (void)line;
  16935. }
  16936. #endif
  16937. /*
  16938. * Testing wc_LockMutex_ex
  16939. */
  16940. static int test_wc_LockMutex_ex(void)
  16941. {
  16942. int ret = 0;
  16943. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16944. int flag = CRYPTO_LOCK;
  16945. int type = 0;
  16946. const char* file = "./test-LockMutex_ex.txt";
  16947. int line = 0;
  16948. printf(testingFmt, "wc_LockMutex_ex()");
  16949. /*without SetMutexCb*/
  16950. ret = wc_LockMutex_ex(flag, type, file, line);
  16951. if (ret == BAD_STATE_E) {
  16952. ret = 0;
  16953. }
  16954. /*with SetMutexCb*/
  16955. if (ret == 0) {
  16956. ret = wc_SetMutexCb(sample_mutex_cb);
  16957. if (ret == 0) {
  16958. ret = wc_LockMutex_ex(flag, type, file, line);
  16959. }
  16960. }
  16961. printf(resultFmt, ret == 0 ? passed : failed);
  16962. #endif
  16963. return ret;
  16964. }/*End test_wc_LockMutex_ex*/
  16965. /*
  16966. * Testing wc_SetMutexCb
  16967. */
  16968. static int test_wc_SetMutexCb(void)
  16969. {
  16970. int ret = 0;
  16971. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  16972. printf(testingFmt, "wc_SetMutexCb()");
  16973. ret = wc_SetMutexCb(sample_mutex_cb);
  16974. printf(resultFmt, ret == 0 ? passed : failed);
  16975. #endif
  16976. return ret;
  16977. }/*End test_wc_SetMutexCb*/
  16978. /*
  16979. * Testing wc_RsaKeyToDer()
  16980. */
  16981. static int test_wc_RsaKeyToDer(void)
  16982. {
  16983. int ret = 0;
  16984. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  16985. RsaKey genKey;
  16986. WC_RNG rng;
  16987. byte* der;
  16988. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  16989. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  16990. int bits = 1024;
  16991. word32 derSz = 611;
  16992. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  16993. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  16994. #else
  16995. int bits = 2048;
  16996. word32 derSz = 1196;
  16997. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  16998. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  16999. #endif
  17000. XMEMSET(&rng, 0, sizeof(rng));
  17001. XMEMSET(&genKey, 0, sizeof(genKey));
  17002. der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17003. if (der == NULL) {
  17004. ret = WOLFSSL_FATAL_ERROR;
  17005. }
  17006. /* Init structures. */
  17007. if (ret == 0) {
  17008. ret = wc_InitRsaKey(&genKey, HEAP_HINT);
  17009. }
  17010. if (ret == 0) {
  17011. ret = wc_InitRng(&rng);
  17012. }
  17013. /* Make key. */
  17014. if (ret == 0) {
  17015. ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
  17016. if (ret != 0) {
  17017. ret = WOLFSSL_FATAL_ERROR;
  17018. }
  17019. }
  17020. printf(testingFmt, "wc_RsaKeyToDer()");
  17021. if (ret == 0) {
  17022. ret = wc_RsaKeyToDer(&genKey, der, derSz);
  17023. if (ret > 0) {
  17024. ret = 0;
  17025. } else {
  17026. ret = WOLFSSL_FATAL_ERROR;
  17027. }
  17028. }
  17029. #ifndef HAVE_USER_RSA
  17030. /* Pass good/bad args. */
  17031. if (ret == 0) {
  17032. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17033. if (ret == BAD_FUNC_ARG) {
  17034. /* Get just the output length */
  17035. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17036. }
  17037. if (ret > 0) {
  17038. /* Try Public Key. */
  17039. genKey.type = 0;
  17040. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17041. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17042. /* Put back to Private Key */
  17043. genKey.type = 1;
  17044. #endif
  17045. }
  17046. if (ret == BAD_FUNC_ARG) {
  17047. ret = 0;
  17048. } else {
  17049. ret = WOLFSSL_FATAL_ERROR;
  17050. }
  17051. }
  17052. #else
  17053. /* Pass good/bad args. */
  17054. if (ret == 0) {
  17055. ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
  17056. if (ret == USER_CRYPTO_ERROR) {
  17057. /* Get just the output length */
  17058. ret = wc_RsaKeyToDer(&genKey, NULL, 0);
  17059. }
  17060. if (ret > 0) {
  17061. /* Try Public Key. */
  17062. genKey.type = 0;
  17063. ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
  17064. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17065. /* Put back to Private Key */
  17066. genKey.type = 1;
  17067. #endif
  17068. }
  17069. if (ret == USER_CRYPTO_ERROR) {
  17070. ret = 0;
  17071. } else {
  17072. ret = WOLFSSL_FATAL_ERROR;
  17073. }
  17074. }
  17075. #endif
  17076. if (der != NULL) {
  17077. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17078. }
  17079. if (wc_FreeRsaKey(&genKey) || 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_RsaKeyToDer */
  17089. /*
  17090. * Testing wc_RsaKeyToPublicDer()
  17091. */
  17092. static int test_wc_RsaKeyToPublicDer(void)
  17093. {
  17094. int ret = 0;
  17095. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17096. RsaKey key;
  17097. WC_RNG rng;
  17098. byte* der;
  17099. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17100. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17101. int bits = 1024;
  17102. word32 derLen = 162;
  17103. #else
  17104. int bits = 2048;
  17105. word32 derLen = 294;
  17106. #endif
  17107. XMEMSET(&rng, 0, sizeof(rng));
  17108. XMEMSET(&key, 0, sizeof(key));
  17109. der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17110. if (der == NULL) {
  17111. ret = WOLFSSL_FATAL_ERROR;
  17112. }
  17113. if (ret == 0) {
  17114. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17115. }
  17116. if (ret == 0) {
  17117. ret = wc_InitRng(&rng);
  17118. }
  17119. if (ret == 0) {
  17120. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17121. }
  17122. printf(testingFmt, "wc_RsaKeyToPublicDer()");
  17123. if (ret == 0) {
  17124. /* test getting size only */
  17125. ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
  17126. if (ret >= 0)
  17127. ret = 0;
  17128. }
  17129. if (ret == 0) {
  17130. ret = wc_RsaKeyToPublicDer(&key, der, derLen);
  17131. if (ret >= 0) {
  17132. ret = 0;
  17133. } else {
  17134. ret = WOLFSSL_FATAL_ERROR;
  17135. }
  17136. }
  17137. if (ret == 0) {
  17138. /* test getting size only */
  17139. ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
  17140. if (ret >= 0)
  17141. ret = 0;
  17142. }
  17143. if (ret == 0) {
  17144. ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
  17145. if (ret >= 0) {
  17146. ret = 0;
  17147. } else {
  17148. ret = WOLFSSL_FATAL_ERROR;
  17149. }
  17150. }
  17151. #ifndef HAVE_USER_RSA
  17152. /* Pass in bad args. */
  17153. if (ret == 0) {
  17154. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17155. if (ret == BAD_FUNC_ARG) {
  17156. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17157. }
  17158. if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
  17159. ret = 0;
  17160. } else {
  17161. ret = WOLFSSL_FATAL_ERROR;
  17162. }
  17163. }
  17164. #else
  17165. /* Pass in bad args. */
  17166. if (ret == 0) {
  17167. ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
  17168. if (ret == USER_CRYPTO_ERROR) {
  17169. ret = wc_RsaKeyToPublicDer(&key, der, -1);
  17170. }
  17171. if (ret == USER_CRYPTO_ERROR) {
  17172. ret = 0;
  17173. } else {
  17174. ret = WOLFSSL_FATAL_ERROR;
  17175. }
  17176. }
  17177. #endif
  17178. if (der != NULL) {
  17179. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17180. }
  17181. if (wc_FreeRsaKey(&key) || ret != 0) {
  17182. ret = WOLFSSL_FATAL_ERROR;
  17183. }
  17184. if (wc_FreeRng(&rng) || ret != 0) {
  17185. ret = WOLFSSL_FATAL_ERROR;
  17186. }
  17187. printf(resultFmt, ret == 0 ? passed : failed);
  17188. #endif
  17189. return ret;
  17190. } /* END test_wc_RsaKeyToPublicDer */
  17191. /*
  17192. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17193. */
  17194. static int test_wc_RsaPublicEncryptDecrypt(void)
  17195. {
  17196. int ret = 0;
  17197. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17198. RsaKey key;
  17199. WC_RNG rng;
  17200. const char inStr[] = TEST_STRING;
  17201. const word32 plainLen = (word32)TEST_STRING_SZ;
  17202. const word32 inLen = (word32)TEST_STRING_SZ;
  17203. int bits = TEST_RSA_BITS;
  17204. const word32 cipherLen = TEST_RSA_BYTES;
  17205. word32 cipherLenResult = cipherLen;
  17206. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17207. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17208. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17209. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17210. if (in == NULL || plain == NULL || cipher == NULL) {
  17211. printf("test_wc_RsaPublicEncryptDecrypt malloc failed\n");
  17212. return MEMORY_E;
  17213. }
  17214. #endif
  17215. XMEMCPY(in, inStr, inLen);
  17216. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17217. if (ret == 0) {
  17218. ret = wc_InitRng(&rng);
  17219. }
  17220. if (ret == 0) {
  17221. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17222. }
  17223. /* Encrypt. */
  17224. printf(testingFmt, "wc_RsaPublicEncrypt()");
  17225. if (ret == 0) {
  17226. ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
  17227. if (ret >= 0) {
  17228. cipherLenResult = ret;
  17229. ret = 0;
  17230. } else {
  17231. ret = WOLFSSL_FATAL_ERROR;
  17232. }
  17233. }
  17234. /* Pass bad args. */
  17235. /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
  17236. printf(resultFmt, ret == 0 ? passed : failed);
  17237. if (ret != 0) {
  17238. return ret;
  17239. }
  17240. /* Decrypt */
  17241. printf(testingFmt, "wc_RsaPrivateDecrypt()");
  17242. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17243. /* Bind rng */
  17244. if (ret == 0) {
  17245. ret = wc_RsaSetRNG(&key, &rng);
  17246. }
  17247. #endif
  17248. if (ret == 0) {
  17249. ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
  17250. }
  17251. if (ret >= 0) {
  17252. ret = XMEMCMP(plain, inStr, plainLen);
  17253. }
  17254. /* Pass in bad args. */
  17255. /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
  17256. WC_FREE_VAR(in, NULL);
  17257. WC_FREE_VAR(plain, NULL);
  17258. WC_FREE_VAR(cipher, NULL);
  17259. if (wc_FreeRsaKey(&key) || ret != 0) {
  17260. ret = WOLFSSL_FATAL_ERROR;
  17261. }
  17262. if (wc_FreeRng(&rng) || ret != 0) {
  17263. ret = WOLFSSL_FATAL_ERROR;
  17264. }
  17265. printf(resultFmt, ret == 0 ? passed : failed);
  17266. #endif
  17267. return ret;
  17268. } /* END test_wc_RsaPublicEncryptDecrypt */
  17269. /*
  17270. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17271. */
  17272. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17273. {
  17274. int ret = 0;
  17275. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17276. && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
  17277. && !defined(NO_SHA)
  17278. RsaKey key;
  17279. WC_RNG rng;
  17280. const char inStr[] = TEST_STRING;
  17281. const word32 inLen = (word32)TEST_STRING_SZ;
  17282. const word32 plainSz = (word32)TEST_STRING_SZ;
  17283. byte* res = NULL;
  17284. int idx = 0;
  17285. int bits = TEST_RSA_BITS;
  17286. const word32 cipherSz = TEST_RSA_BYTES;
  17287. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17288. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17289. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17290. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17291. if (in == NULL || plain == NULL || cipher == NULL) {
  17292. printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
  17293. return MEMORY_E;
  17294. }
  17295. #endif
  17296. XMEMCPY(in, inStr, inLen);
  17297. /* Initialize stack structures. */
  17298. XMEMSET(&rng, 0, sizeof(rng));
  17299. XMEMSET(&key, 0, sizeof(key));
  17300. ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
  17301. if (ret == 0) {
  17302. ret = wc_InitRng(&rng);
  17303. }
  17304. if (ret == 0) {
  17305. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17306. }
  17307. /* Encrypt */
  17308. printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
  17309. if (ret == 0) {
  17310. ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
  17311. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
  17312. if (ret >= 0) {
  17313. idx = ret;
  17314. ret = 0;
  17315. } else {
  17316. ret = WOLFSSL_FATAL_ERROR;
  17317. }
  17318. }
  17319. /*Pass bad args.*/
  17320. /* Tests RsaPublicEncryptEx again. No need duplicate. */
  17321. printf(resultFmt, ret == 0 ? passed : failed);
  17322. if (ret != 0) {
  17323. return ret;
  17324. }
  17325. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17326. /* Decrypt */
  17327. printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
  17328. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17329. if (ret == 0) {
  17330. ret = wc_RsaSetRNG(&key, &rng);
  17331. }
  17332. #endif
  17333. if (ret == 0) {
  17334. ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
  17335. plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  17336. WC_MGF1SHA1, NULL, 0);
  17337. }
  17338. if (ret >= 0) {
  17339. if (!XMEMCMP(plain, inStr, plainSz)) {
  17340. ret = 0;
  17341. } else {
  17342. ret = WOLFSSL_FATAL_ERROR;
  17343. }
  17344. }
  17345. /*Pass bad args.*/
  17346. /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
  17347. printf(resultFmt, ret == 0 ? passed : failed);
  17348. if (ret != 0) {
  17349. return ret;
  17350. }
  17351. printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
  17352. if (ret == 0) {
  17353. ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
  17354. &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
  17355. WC_MGF1SHA1, NULL, 0);
  17356. if (ret >= 0) {
  17357. if (!XMEMCMP(inStr, res, plainSz)) {
  17358. ret = 0;
  17359. } else {
  17360. ret = WOLFSSL_FATAL_ERROR;
  17361. }
  17362. }
  17363. }
  17364. #endif
  17365. WC_FREE_VAR(in, NULL);
  17366. WC_FREE_VAR(plain, NULL);
  17367. WC_FREE_VAR(cipher, NULL);
  17368. if (wc_FreeRsaKey(&key) || ret != 0) {
  17369. ret = WOLFSSL_FATAL_ERROR;
  17370. }
  17371. if (wc_FreeRng(&rng) || ret != 0) {
  17372. ret = WOLFSSL_FATAL_ERROR;
  17373. }
  17374. printf(resultFmt, ret == 0 ? passed : failed);
  17375. #endif
  17376. return ret;
  17377. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17378. /*
  17379. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17380. */
  17381. static int test_wc_RsaSSL_SignVerify(void)
  17382. {
  17383. int ret = 0;
  17384. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17385. RsaKey key;
  17386. WC_RNG rng;
  17387. const char inStr[] = TEST_STRING;
  17388. const word32 plainSz = (word32)TEST_STRING_SZ;
  17389. const word32 inLen = (word32)TEST_STRING_SZ;
  17390. word32 idx = 0;
  17391. int bits = TEST_RSA_BITS;
  17392. const word32 outSz = TEST_RSA_BYTES;
  17393. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17394. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17395. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17396. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17397. if (in == NULL || out == NULL || plain == NULL) {
  17398. printf("test_wc_RsaSSL_SignVerify failed\n");
  17399. return MEMORY_E;
  17400. }
  17401. #endif
  17402. XMEMCPY(in, inStr, inLen);
  17403. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17404. if (ret == 0) {
  17405. ret = wc_InitRng(&rng);
  17406. }
  17407. if (ret == 0) {
  17408. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17409. }
  17410. /* Sign. */
  17411. printf(testingFmt, "wc_RsaSSL_Sign()");
  17412. if (ret == 0) {
  17413. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
  17414. if (ret == (int)outSz) {
  17415. idx = ret;
  17416. ret = 0;
  17417. } else {
  17418. ret = WOLFSSL_FATAL_ERROR;
  17419. }
  17420. }
  17421. #ifndef HAVE_USER_RSA
  17422. /* Test bad args. */
  17423. if (ret == 0) {
  17424. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17425. if (ret == BAD_FUNC_ARG) {
  17426. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17427. }
  17428. if (ret == BAD_FUNC_ARG) {
  17429. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17430. }
  17431. if (ret == BAD_FUNC_ARG) {
  17432. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17433. }
  17434. if (ret == BAD_FUNC_ARG) {
  17435. ret = 0;
  17436. } else {
  17437. ret = WOLFSSL_FATAL_ERROR;
  17438. }
  17439. }
  17440. #else
  17441. /* Test bad args. */
  17442. if (ret == 0) {
  17443. ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
  17444. if (ret == USER_CRYPTO_ERROR) {
  17445. ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
  17446. }
  17447. if (ret == USER_CRYPTO_ERROR) {
  17448. ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
  17449. }
  17450. if (ret == USER_CRYPTO_ERROR) {
  17451. ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
  17452. }
  17453. if (ret == USER_CRYPTO_ERROR) {
  17454. ret = 0;
  17455. } else {
  17456. ret = WOLFSSL_FATAL_ERROR;
  17457. }
  17458. }
  17459. #endif
  17460. printf(resultFmt, ret == 0 ? passed : failed);
  17461. if (ret != 0) {
  17462. return ret;
  17463. }
  17464. /* Verify. */
  17465. printf(testingFmt, "wc_RsaSSL_Verify()");
  17466. if (ret == 0) {
  17467. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
  17468. if (ret == (int)inLen) {
  17469. ret = 0;
  17470. } else {
  17471. ret = WOLFSSL_FATAL_ERROR;
  17472. }
  17473. }
  17474. #ifndef HAVE_USER_RSA
  17475. /* Pass bad args. */
  17476. if (ret == 0) {
  17477. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17478. if (ret == BAD_FUNC_ARG) {
  17479. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17480. }
  17481. if (ret == BAD_FUNC_ARG) {
  17482. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17483. }
  17484. if (ret == BAD_FUNC_ARG) {
  17485. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17486. }
  17487. if (ret == BAD_FUNC_ARG) {
  17488. ret = 0;
  17489. } else {
  17490. ret = WOLFSSL_FATAL_ERROR;
  17491. }
  17492. }
  17493. #else
  17494. /* Pass bad args. */
  17495. if (ret == 0) {
  17496. ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
  17497. if (ret == USER_CRYPTO_ERROR) {
  17498. ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
  17499. }
  17500. if (ret == USER_CRYPTO_ERROR) {
  17501. ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
  17502. }
  17503. if (ret == USER_CRYPTO_ERROR) {
  17504. ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
  17505. }
  17506. if (ret == USER_CRYPTO_ERROR) {
  17507. ret = 0;
  17508. } else {
  17509. ret = WOLFSSL_FATAL_ERROR;
  17510. }
  17511. }
  17512. #endif
  17513. WC_FREE_VAR(in, NULL);
  17514. WC_FREE_VAR(out, NULL);
  17515. WC_FREE_VAR(plain, NULL);
  17516. if (wc_FreeRsaKey(&key) || ret != 0) {
  17517. ret = WOLFSSL_FATAL_ERROR;
  17518. }
  17519. if (wc_FreeRng(&rng) || ret != 0) {
  17520. ret = WOLFSSL_FATAL_ERROR;
  17521. }
  17522. printf(resultFmt, ret == 0 ? passed : failed);
  17523. #endif
  17524. return ret;
  17525. } /* END test_wc_RsaSSL_SignVerify */
  17526. /*
  17527. * Testing wc_RsaEncryptSize()
  17528. */
  17529. static int test_wc_RsaEncryptSize(void)
  17530. {
  17531. int ret = 0;
  17532. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17533. RsaKey key;
  17534. WC_RNG rng;
  17535. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17536. if (ret == 0) {
  17537. ret = wc_InitRng(&rng);
  17538. }
  17539. printf(testingFmt, "wc_RsaEncryptSize()");
  17540. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17541. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17542. if (ret == 0) {
  17543. ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
  17544. if (ret == 0) {
  17545. ret = wc_RsaEncryptSize(&key);
  17546. }
  17547. if (ret == 128) {
  17548. ret = 0;
  17549. } else {
  17550. ret = WOLFSSL_FATAL_ERROR;
  17551. }
  17552. }
  17553. if (wc_FreeRsaKey(&key) || ret != 0) {
  17554. ret = WOLFSSL_FATAL_ERROR;
  17555. } else {
  17556. ret = 0;
  17557. }
  17558. #endif
  17559. if (ret == 0) {
  17560. ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
  17561. if (ret == 0) {
  17562. ret = wc_RsaEncryptSize(&key);
  17563. }
  17564. if (ret == 256) {
  17565. ret = 0;
  17566. } else {
  17567. ret = WOLFSSL_FATAL_ERROR;
  17568. }
  17569. }
  17570. /* Pass in bad arg. */
  17571. if (ret == 0) {
  17572. ret = wc_RsaEncryptSize(NULL);
  17573. #ifndef HAVE_USER_RSA
  17574. if (ret == BAD_FUNC_ARG) {
  17575. ret = 0;
  17576. } else {
  17577. ret = WOLFSSL_FATAL_ERROR;
  17578. }
  17579. #endif
  17580. }
  17581. if (wc_FreeRsaKey(&key) || ret != 0) {
  17582. ret = WOLFSSL_FATAL_ERROR;
  17583. }
  17584. if (wc_FreeRng(&rng) || ret != 0) {
  17585. ret = WOLFSSL_FATAL_ERROR;
  17586. }
  17587. printf(resultFmt, ret == 0 ? passed : failed);
  17588. #endif
  17589. return ret;
  17590. } /* END test_wc_RsaEncryptSize*/
  17591. /*
  17592. * Testing wc_RsaFlattenPublicKey()
  17593. */
  17594. static int test_wc_RsaFlattenPublicKey(void)
  17595. {
  17596. int ret = 0;
  17597. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17598. RsaKey key;
  17599. WC_RNG rng;
  17600. byte e[256];
  17601. byte n[256];
  17602. word32 eSz = sizeof(e);
  17603. word32 nSz = sizeof(n);
  17604. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17605. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17606. int bits = 1024;
  17607. #else
  17608. int bits = 2048;
  17609. #endif
  17610. ret = wc_InitRsaKey(&key, HEAP_HINT);
  17611. if (ret == 0) {
  17612. ret = wc_InitRng(&rng);
  17613. }
  17614. if (ret == 0) {
  17615. ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
  17616. if (ret >= 0) {
  17617. ret = 0;
  17618. } else {
  17619. ret = WOLFSSL_FATAL_ERROR;
  17620. }
  17621. }
  17622. printf(testingFmt, "wc_RsaFlattenPublicKey()");
  17623. if (ret == 0) {
  17624. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
  17625. }
  17626. #ifndef HAVE_USER_RSA
  17627. /* Pass bad args. */
  17628. if (ret == 0) {
  17629. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17630. if (ret == BAD_FUNC_ARG) {
  17631. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17632. }
  17633. if (ret == BAD_FUNC_ARG) {
  17634. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17635. }
  17636. if (ret == BAD_FUNC_ARG) {
  17637. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17638. }
  17639. if (ret == BAD_FUNC_ARG) {
  17640. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17641. }
  17642. if (ret == BAD_FUNC_ARG) {
  17643. ret = 0;
  17644. } else {
  17645. ret = WOLFSSL_FATAL_ERROR;
  17646. }
  17647. }
  17648. #else
  17649. /* Pass bad args. */
  17650. if (ret == 0) {
  17651. ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
  17652. if (ret == USER_CRYPTO_ERROR) {
  17653. ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
  17654. }
  17655. if (ret == USER_CRYPTO_ERROR) {
  17656. ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
  17657. }
  17658. if (ret == USER_CRYPTO_ERROR) {
  17659. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
  17660. }
  17661. if (ret == USER_CRYPTO_ERROR) {
  17662. ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
  17663. }
  17664. if (ret == USER_CRYPTO_ERROR) {
  17665. ret = 0;
  17666. } else {
  17667. ret = WOLFSSL_FATAL_ERROR;
  17668. }
  17669. }
  17670. #endif
  17671. if (wc_FreeRsaKey(&key) || ret != 0) {
  17672. ret = WOLFSSL_FATAL_ERROR;
  17673. }
  17674. if (wc_FreeRng(&rng) || ret != 0) {
  17675. ret = WOLFSSL_FATAL_ERROR;
  17676. }
  17677. printf(resultFmt, ret == 0 ? passed : failed);
  17678. #endif
  17679. return ret;
  17680. } /* END test_wc_RsaFlattenPublicKey */
  17681. /*
  17682. * unit test for wc_AesCcmSetKey
  17683. */
  17684. static int test_wc_AesCcmSetKey(void)
  17685. {
  17686. int ret = 0;
  17687. #ifdef HAVE_AESCCM
  17688. Aes aes;
  17689. const byte key16[] =
  17690. {
  17691. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17692. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17693. };
  17694. const byte key24[] =
  17695. {
  17696. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17697. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17698. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17699. };
  17700. const byte key32[] =
  17701. {
  17702. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17703. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17704. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17705. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17706. };
  17707. printf(testingFmt, "wc_AesCcmSetKey()");
  17708. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17709. if (ret != 0)
  17710. return ret;
  17711. #ifdef WOLFSSL_AES_128
  17712. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17713. #endif
  17714. #ifdef WOLFSSL_AES_192
  17715. if (ret == 0) {
  17716. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
  17717. }
  17718. #endif
  17719. #ifdef WOLFSSL_AES_256
  17720. if (ret == 0) {
  17721. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
  17722. }
  17723. #endif
  17724. /* Test bad args. */
  17725. if (ret == 0) {
  17726. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
  17727. if (ret == BAD_FUNC_ARG) {
  17728. ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
  17729. }
  17730. if (ret == BAD_FUNC_ARG) {
  17731. ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
  17732. }
  17733. if (ret != BAD_FUNC_ARG) {
  17734. ret = WOLFSSL_FATAL_ERROR;
  17735. } else {
  17736. ret = 0;
  17737. }
  17738. }
  17739. wc_AesFree(&aes);
  17740. printf(resultFmt, ret == 0 ? passed : failed);
  17741. #endif
  17742. return ret;
  17743. } /* END test_wc_AesCcmSetKey */
  17744. /*
  17745. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17746. */
  17747. static int test_wc_AesCcmEncryptDecrypt(void)
  17748. {
  17749. int ret = 0;
  17750. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17751. Aes aes;
  17752. const byte key16[] =
  17753. {
  17754. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17755. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17756. };
  17757. /* plaintext */
  17758. const byte plainT[] =
  17759. {
  17760. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17761. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17762. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17763. };
  17764. /* nonce */
  17765. const byte iv[] =
  17766. {
  17767. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17768. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17769. };
  17770. const byte c[] = /* cipher text. */
  17771. {
  17772. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17773. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17774. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17775. };
  17776. const byte t[] = /* Auth tag */
  17777. {
  17778. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17779. };
  17780. const byte authIn[] =
  17781. {
  17782. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17783. };
  17784. byte cipherOut[sizeof(plainT)];
  17785. byte authTag[sizeof(t)];
  17786. int ccmE = WOLFSSL_FATAL_ERROR;
  17787. #ifdef HAVE_AES_DECRYPT
  17788. int ccmD = WOLFSSL_FATAL_ERROR;
  17789. byte plainOut[sizeof(cipherOut)];
  17790. #endif
  17791. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  17792. if (ret != 0)
  17793. return ret;
  17794. ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
  17795. if (ret == 0) {
  17796. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17797. iv, sizeof(iv), authTag, sizeof(authTag),
  17798. authIn , sizeof(authIn));
  17799. if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
  17800. XMEMCMP(t, authTag, sizeof(t))) {
  17801. ccmE = WOLFSSL_FATAL_ERROR;
  17802. ret = WOLFSSL_FATAL_ERROR;
  17803. }
  17804. #ifdef HAVE_AES_DECRYPT
  17805. if (ret == 0) {
  17806. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17807. sizeof(plainOut), iv, sizeof(iv),
  17808. authTag, sizeof(authTag),
  17809. authIn, sizeof(authIn));
  17810. if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
  17811. ccmD = WOLFSSL_FATAL_ERROR;
  17812. }
  17813. }
  17814. #endif
  17815. }
  17816. printf(testingFmt, "wc_AesCcmEncrypt()");
  17817. /* Pass in bad args. Encrypt*/
  17818. if (ret == 0 && ccmE == 0) {
  17819. ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17820. iv, sizeof(iv), authTag, sizeof(authTag),
  17821. authIn , sizeof(authIn));
  17822. if (ccmE == BAD_FUNC_ARG) {
  17823. ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17824. iv, sizeof(iv), authTag, sizeof(authTag),
  17825. authIn , sizeof(authIn));
  17826. }
  17827. if (ccmE == BAD_FUNC_ARG) {
  17828. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17829. iv, sizeof(iv), authTag, sizeof(authTag),
  17830. authIn , sizeof(authIn));
  17831. }
  17832. if (ccmE == BAD_FUNC_ARG) {
  17833. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17834. NULL, sizeof(iv), authTag, sizeof(authTag),
  17835. authIn , sizeof(authIn));
  17836. }
  17837. if (ccmE == BAD_FUNC_ARG) {
  17838. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17839. iv, sizeof(iv), NULL, sizeof(authTag),
  17840. authIn , sizeof(authIn));
  17841. }
  17842. if (ccmE == BAD_FUNC_ARG) {
  17843. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17844. iv, sizeof(iv) + 1, authTag, sizeof(authTag),
  17845. authIn , sizeof(authIn));
  17846. }
  17847. if (ccmE == BAD_FUNC_ARG) {
  17848. ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17849. iv, sizeof(iv) - 7, authTag, sizeof(authTag),
  17850. authIn , sizeof(authIn));
  17851. }
  17852. if (ccmE != BAD_FUNC_ARG) {
  17853. ccmE = WOLFSSL_FATAL_ERROR;
  17854. } else {
  17855. ccmE = 0;
  17856. }
  17857. } /* End Encrypt */
  17858. printf(resultFmt, ccmE == 0 ? passed : failed);
  17859. if (ccmE != 0) {
  17860. wc_AesFree(&aes);
  17861. return ccmE;
  17862. }
  17863. #ifdef HAVE_AES_DECRYPT
  17864. printf(testingFmt, "wc_AesCcmDecrypt()");
  17865. /* Pass in bad args. Decrypt*/
  17866. if (ret == 0 && ccmD == 0) {
  17867. ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17868. iv, sizeof(iv), authTag, sizeof(authTag),
  17869. authIn, sizeof(authIn));
  17870. if (ccmD == BAD_FUNC_ARG) {
  17871. ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17872. iv, sizeof(iv), authTag, sizeof(authTag),
  17873. authIn, sizeof(authIn));
  17874. }
  17875. if (ccmD == BAD_FUNC_ARG) {
  17876. ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17877. iv, sizeof(iv), authTag, sizeof(authTag),
  17878. authIn, sizeof(authIn));
  17879. }
  17880. if (ccmD == BAD_FUNC_ARG) {
  17881. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17882. sizeof(plainOut), NULL, sizeof(iv),
  17883. authTag, sizeof(authTag),
  17884. authIn, sizeof(authIn));
  17885. }
  17886. if (ccmD == BAD_FUNC_ARG) {
  17887. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17888. sizeof(plainOut), iv, sizeof(iv), NULL,
  17889. sizeof(authTag), authIn, sizeof(authIn));
  17890. }
  17891. if (ccmD == BAD_FUNC_ARG) {
  17892. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17893. sizeof(plainOut), iv, sizeof(iv) + 1,
  17894. authTag, sizeof(authTag),
  17895. authIn, sizeof(authIn));
  17896. }
  17897. if (ccmD == BAD_FUNC_ARG) {
  17898. ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
  17899. sizeof(plainOut), iv, sizeof(iv) - 7,
  17900. authTag, sizeof(authTag),
  17901. authIn, sizeof(authIn));
  17902. }
  17903. if (ccmD != BAD_FUNC_ARG) {
  17904. ccmD = WOLFSSL_FATAL_ERROR;
  17905. } else {
  17906. ccmD = 0;
  17907. }
  17908. } /* END Decrypt */
  17909. printf(resultFmt, ccmD == 0 ? passed : failed);
  17910. if (ccmD != 0) {
  17911. return ccmD;
  17912. }
  17913. #endif
  17914. wc_AesFree(&aes);
  17915. #endif /* HAVE_AESCCM */
  17916. return ret;
  17917. } /* END test_wc_AesCcmEncryptDecrypt */
  17918. /*
  17919. * Testing wc_InitDsaKey()
  17920. */
  17921. static int test_wc_InitDsaKey(void)
  17922. {
  17923. int ret = 0;
  17924. #ifndef NO_DSA
  17925. DsaKey key;
  17926. printf(testingFmt, "wc_InitDsaKey()");
  17927. ret = wc_InitDsaKey(&key);
  17928. /* Pass in bad args. */
  17929. if (ret == 0) {
  17930. ret = wc_InitDsaKey(NULL);
  17931. if (ret == BAD_FUNC_ARG) {
  17932. ret = 0;
  17933. } else {
  17934. ret = WOLFSSL_FATAL_ERROR;
  17935. }
  17936. }
  17937. printf(resultFmt, ret == 0 ? passed : failed);
  17938. wc_FreeDsaKey(&key);
  17939. #endif
  17940. return ret;
  17941. } /* END test_wc_InitDsaKey */
  17942. /*
  17943. * Testing wc_DsaSign() and wc_DsaVerify()
  17944. */
  17945. static int test_wc_DsaSignVerify(void)
  17946. {
  17947. int ret = 0;
  17948. #if !defined(NO_DSA)
  17949. DsaKey key;
  17950. WC_RNG rng;
  17951. wc_Sha sha;
  17952. byte signature[DSA_SIG_SIZE];
  17953. byte hash[WC_SHA_DIGEST_SIZE];
  17954. word32 idx = 0;
  17955. word32 bytes;
  17956. int answer;
  17957. #ifdef USE_CERT_BUFFERS_1024
  17958. byte tmp[ONEK_BUF];
  17959. XMEMSET(tmp, 0, sizeof(tmp));
  17960. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  17961. bytes = sizeof_dsa_key_der_1024;
  17962. #elif defined(USE_CERT_BUFFERS_2048)
  17963. byte tmp[TWOK_BUF];
  17964. XMEMSET(tmp, 0, sizeof(tmp));
  17965. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  17966. bytes = sizeof_dsa_key_der_2048;
  17967. #else
  17968. byte tmp[TWOK_BUF];
  17969. XMEMSET(tmp, 0, sizeof(tmp));
  17970. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  17971. if (fp == XBADFILE) {
  17972. return WOLFSSL_BAD_FILE;
  17973. }
  17974. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  17975. XFCLOSE(fp);
  17976. #endif /* END USE_CERT_BUFFERS_1024 */
  17977. ret = wc_InitSha(&sha);
  17978. if (ret == 0) {
  17979. ret = wc_ShaUpdate(&sha, tmp, bytes);
  17980. if (ret == 0) {
  17981. ret = wc_ShaFinal(&sha, hash);
  17982. }
  17983. if (ret == 0) {
  17984. ret = wc_InitDsaKey(&key);
  17985. }
  17986. if (ret == 0) {
  17987. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  17988. }
  17989. if (ret == 0) {
  17990. ret = wc_InitRng(&rng);
  17991. }
  17992. }
  17993. printf(testingFmt, "wc_DsaSign()");
  17994. /* Sign. */
  17995. if (ret == 0) {
  17996. ret = wc_DsaSign(hash, signature, &key, &rng);
  17997. }
  17998. /* Test bad args. */
  17999. if (ret == 0) {
  18000. ret = wc_DsaSign(NULL, signature, &key, &rng);
  18001. if (ret == BAD_FUNC_ARG) {
  18002. ret = wc_DsaSign(hash, NULL, &key, &rng);
  18003. }
  18004. if (ret == BAD_FUNC_ARG) {
  18005. ret = wc_DsaSign(hash, signature, NULL, &rng);
  18006. }
  18007. if (ret == BAD_FUNC_ARG) {
  18008. ret = wc_DsaSign(hash, signature, &key, NULL);
  18009. }
  18010. if (ret == BAD_FUNC_ARG) {
  18011. ret = 0;
  18012. } else {
  18013. ret = WOLFSSL_FATAL_ERROR;
  18014. }
  18015. }
  18016. printf(resultFmt, ret == 0 ? passed : failed);
  18017. if (ret != 0) {
  18018. return ret;
  18019. }
  18020. /* Verify. */
  18021. printf(testingFmt, "wc_DsaVerify()");
  18022. ret = wc_DsaVerify(hash, signature, &key, &answer);
  18023. if (ret != 0 || answer != 1) {
  18024. ret = WOLFSSL_FATAL_ERROR;
  18025. } else {
  18026. ret = 0;
  18027. }
  18028. /* Pass in bad args. */
  18029. if (ret == 0) {
  18030. ret = wc_DsaVerify(NULL, signature, &key, &answer);
  18031. if (ret == BAD_FUNC_ARG) {
  18032. ret = wc_DsaVerify(hash, NULL, &key, &answer);
  18033. }
  18034. if (ret == BAD_FUNC_ARG) {
  18035. ret = wc_DsaVerify(hash, signature, NULL, &answer);
  18036. }
  18037. if (ret == BAD_FUNC_ARG) {
  18038. ret = wc_DsaVerify(hash, signature, &key, NULL);
  18039. }
  18040. if (ret == BAD_FUNC_ARG) {
  18041. ret = 0;
  18042. } else {
  18043. ret = WOLFSSL_FATAL_ERROR;
  18044. }
  18045. }
  18046. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  18047. /* hard set q to 0 and test fail case */
  18048. mp_free(&key.q);
  18049. mp_init(&key.q);
  18050. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18051. mp_set(&key.q, 1);
  18052. AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  18053. #endif
  18054. if (wc_FreeRng(&rng) && ret == 0) {
  18055. ret = WOLFSSL_FATAL_ERROR;
  18056. }
  18057. printf(resultFmt, ret == 0 ? passed : failed);
  18058. wc_FreeDsaKey(&key);
  18059. wc_ShaFree(&sha);
  18060. #endif
  18061. return ret;
  18062. } /* END test_wc_DsaSign */
  18063. /*
  18064. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  18065. */
  18066. static int test_wc_DsaPublicPrivateKeyDecode(void)
  18067. {
  18068. int ret = 0;
  18069. #if !defined(NO_DSA)
  18070. DsaKey key;
  18071. word32 bytes;
  18072. word32 idx = 0;
  18073. int priv = WOLFSSL_FATAL_ERROR;
  18074. int pub = WOLFSSL_FATAL_ERROR;
  18075. #ifdef USE_CERT_BUFFERS_1024
  18076. byte tmp[ONEK_BUF];
  18077. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18078. bytes = sizeof_dsa_key_der_1024;
  18079. #elif defined(USE_CERT_BUFFERS_2048)
  18080. byte tmp[TWOK_BUF];
  18081. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18082. bytes = sizeof_dsa_key_der_2048;
  18083. #else
  18084. byte tmp[TWOK_BUF];
  18085. XMEMSET(tmp, 0, sizeof(tmp));
  18086. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18087. if (fp == XBADFILE)
  18088. {
  18089. return WOLFSSL_BAD_FILE;
  18090. }
  18091. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18092. XFCLOSE(fp);
  18093. #endif /* END USE_CERT_BUFFERS_1024 */
  18094. ret = wc_InitDsaKey(&key);
  18095. printf(testingFmt, "wc_DsaPrivateKeyDecode()");
  18096. if (ret == 0) {
  18097. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18098. /* Test bad args. */
  18099. if (priv == 0) {
  18100. priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
  18101. if (priv == BAD_FUNC_ARG) {
  18102. priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
  18103. }
  18104. if (priv == BAD_FUNC_ARG) {
  18105. priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
  18106. }
  18107. if (priv == BAD_FUNC_ARG) {
  18108. priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
  18109. }
  18110. if (priv == ASN_PARSE_E) {
  18111. priv = 0;
  18112. } else {
  18113. priv = WOLFSSL_FATAL_ERROR;
  18114. }
  18115. }
  18116. wc_FreeDsaKey(&key);
  18117. ret = wc_InitDsaKey(&key);
  18118. }
  18119. printf(resultFmt, priv == 0 ? passed : failed);
  18120. printf(testingFmt, "wc_DsaPublicKeyDecode()");
  18121. if (ret == 0) {
  18122. idx = 0; /* Reset */
  18123. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18124. /* Test bad args. */
  18125. if (pub == 0) {
  18126. pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
  18127. if (pub == BAD_FUNC_ARG) {
  18128. pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
  18129. }
  18130. if (pub == BAD_FUNC_ARG) {
  18131. pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
  18132. }
  18133. if (pub == BAD_FUNC_ARG) {
  18134. pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
  18135. }
  18136. if (pub == ASN_PARSE_E) {
  18137. pub = 0;
  18138. } else {
  18139. pub = WOLFSSL_FATAL_ERROR;
  18140. }
  18141. }
  18142. } /* END Public Key */
  18143. printf(resultFmt, pub == 0 ? passed : failed);
  18144. wc_FreeDsaKey(&key);
  18145. #endif
  18146. return ret;
  18147. } /* END test_wc_DsaPublicPrivateKeyDecode */
  18148. /*
  18149. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  18150. */
  18151. static int test_wc_MakeDsaKey(void)
  18152. {
  18153. int ret = 0;
  18154. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18155. DsaKey genKey;
  18156. WC_RNG rng;
  18157. XMEMSET(&rng, 0, sizeof(rng));
  18158. XMEMSET(&genKey, 0, sizeof(genKey));
  18159. ret = wc_InitRng(&rng);
  18160. if (ret == 0) {
  18161. ret = wc_InitDsaKey(&genKey);
  18162. }
  18163. printf(testingFmt, "wc_MakeDsaParameters()");
  18164. if (ret == 0) {
  18165. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18166. }
  18167. /* Test bad args. */
  18168. if (ret == 0) {
  18169. ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
  18170. if (ret == BAD_FUNC_ARG) {
  18171. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
  18172. }
  18173. if (ret == BAD_FUNC_ARG) {
  18174. ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
  18175. }
  18176. if (ret == BAD_FUNC_ARG) {
  18177. ret = 0;
  18178. } else {
  18179. ret = WOLFSSL_FATAL_ERROR;
  18180. }
  18181. }
  18182. printf(resultFmt, ret == 0 ? passed : failed);
  18183. printf(testingFmt, "wc_MakeDsaKey()");
  18184. if (ret == 0) {
  18185. ret = wc_MakeDsaKey(&rng, &genKey);
  18186. }
  18187. /* Test bad args. */
  18188. if (ret == 0) {
  18189. ret = wc_MakeDsaKey(NULL, &genKey);
  18190. if (ret == BAD_FUNC_ARG) {
  18191. ret = wc_MakeDsaKey(&rng, NULL);
  18192. }
  18193. if (ret == BAD_FUNC_ARG) {
  18194. ret = 0;
  18195. } else {
  18196. ret = WOLFSSL_FATAL_ERROR;
  18197. }
  18198. }
  18199. if (wc_FreeRng(&rng) && ret == 0) {
  18200. ret = WOLFSSL_FAILURE;
  18201. }
  18202. printf(resultFmt, ret == 0 ? passed : failed);
  18203. wc_FreeDsaKey(&genKey);
  18204. #endif
  18205. return ret;
  18206. } /* END test_wc_MakeDsaKey */
  18207. /*
  18208. * Testing wc_DsaKeyToDer()
  18209. */
  18210. static int test_wc_DsaKeyToDer(void)
  18211. {
  18212. int ret = 0;
  18213. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18214. DsaKey genKey;
  18215. WC_RNG rng;
  18216. word32 bytes;
  18217. word32 idx = 0;
  18218. #ifdef USE_CERT_BUFFERS_1024
  18219. byte tmp[ONEK_BUF];
  18220. byte der[ONEK_BUF];
  18221. XMEMSET(tmp, 0, sizeof(tmp));
  18222. XMEMSET(der, 0, sizeof(der));
  18223. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  18224. bytes = sizeof_dsa_key_der_1024;
  18225. #elif defined(USE_CERT_BUFFERS_2048)
  18226. byte tmp[TWOK_BUF];
  18227. byte der[TWOK_BUF];
  18228. XMEMSET(tmp, 0, sizeof(tmp));
  18229. XMEMSET(der, 0, sizeof(der));
  18230. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  18231. bytes = sizeof_dsa_key_der_2048;
  18232. #else
  18233. byte tmp[TWOK_BUF];
  18234. byte der[TWOK_BUF];
  18235. XMEMSET(tmp, 0, sizeof(tmp));
  18236. XMEMSET(der, 0, sizeof(der));
  18237. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  18238. if (fp == XBADFILE) {
  18239. return WOLFSSL_BAD_FILE;
  18240. }
  18241. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  18242. XFCLOSE(fp);
  18243. #endif /* END USE_CERT_BUFFERS_1024 */
  18244. XMEMSET(&rng, 0, sizeof(rng));
  18245. XMEMSET(&genKey, 0, sizeof(genKey));
  18246. ret = wc_InitRng(&rng);
  18247. if (ret == 0) {
  18248. ret = wc_InitDsaKey(&genKey);
  18249. }
  18250. if (ret == 0) {
  18251. ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
  18252. if (ret == 0) {
  18253. wc_FreeDsaKey(&genKey);
  18254. ret = wc_InitDsaKey(&genKey);
  18255. }
  18256. }
  18257. if (ret == 0) {
  18258. ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
  18259. }
  18260. printf(testingFmt, "wc_DsaKeyToDer()");
  18261. if (ret == 0) {
  18262. ret = wc_DsaKeyToDer(&genKey, der, bytes);
  18263. if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
  18264. ret = 0;
  18265. }
  18266. }
  18267. /* Test bad args. */
  18268. if (ret == 0) {
  18269. ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
  18270. if (ret == BAD_FUNC_ARG) {
  18271. ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
  18272. }
  18273. if (ret == BAD_FUNC_ARG) {
  18274. ret = 0;
  18275. } else {
  18276. ret = WOLFSSL_FATAL_ERROR;
  18277. }
  18278. }
  18279. if (wc_FreeRng(&rng) && ret == 0) {
  18280. ret = WOLFSSL_FATAL_ERROR;
  18281. }
  18282. printf(resultFmt, ret == 0 ? passed : failed);
  18283. wc_FreeDsaKey(&genKey);
  18284. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  18285. return ret;
  18286. } /* END test_wc_DsaKeyToDer */
  18287. /*
  18288. * Testing wc_DsaKeyToPublicDer()
  18289. * (indirectly testing setDsaPublicKey())
  18290. */
  18291. static int test_wc_DsaKeyToPublicDer(void)
  18292. {
  18293. int ret = 0;
  18294. #ifndef HAVE_SELFTEST
  18295. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18296. DsaKey genKey;
  18297. WC_RNG rng;
  18298. byte* der;
  18299. word32 sz;
  18300. printf(testingFmt, "wc_DsaKeyToPublicDer()");
  18301. der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  18302. if (der == NULL) {
  18303. ret = WOLFSSL_FATAL_ERROR;
  18304. }
  18305. if (ret == 0) {
  18306. ret = wc_InitDsaKey(&genKey);
  18307. }
  18308. if (ret == 0) {
  18309. ret = wc_InitRng(&rng);
  18310. }
  18311. if (ret == 0) {
  18312. ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
  18313. }
  18314. if (ret == 0) {
  18315. ret = wc_MakeDsaKey(&rng, &genKey);
  18316. }
  18317. if (ret == 0) {
  18318. ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
  18319. if (ret >= 0) {
  18320. sz = ret;
  18321. ret = 0;
  18322. } else {
  18323. ret = WOLFSSL_FATAL_ERROR;
  18324. }
  18325. }
  18326. if (ret == 0) {
  18327. word32 idx = 0;
  18328. wc_FreeDsaKey(&genKey);
  18329. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18330. }
  18331. /* Test without the SubjectPublicKeyInfo header */
  18332. if (ret == 0) {
  18333. ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
  18334. if (ret >= 0) {
  18335. sz = ret;
  18336. ret = 0;
  18337. } else {
  18338. ret = WOLFSSL_FATAL_ERROR;
  18339. }
  18340. }
  18341. if (ret == 0) {
  18342. word32 idx = 0;
  18343. wc_FreeDsaKey(&genKey);
  18344. ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
  18345. }
  18346. /* Test bad args. */
  18347. if (ret == 0) {
  18348. ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
  18349. if (ret == BAD_FUNC_ARG) {
  18350. ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
  18351. }
  18352. if (ret == BAD_FUNC_ARG) {
  18353. ret = 0;
  18354. } else {
  18355. ret = WOLFSSL_FATAL_ERROR;
  18356. }
  18357. }
  18358. if (wc_FreeRng(&rng) && ret == 0) {
  18359. ret = WOLFSSL_FATAL_ERROR;
  18360. }
  18361. printf(resultFmt, ret == 0 ? passed : failed);
  18362. XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
  18363. wc_FreeDsaKey(&genKey);
  18364. #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
  18365. #endif /* HAVE_SELFTEST */
  18366. return ret;
  18367. } /* END test_wc_DsaKeyToPublicDer */
  18368. /*
  18369. * Testing wc_DsaImportParamsRaw()
  18370. */
  18371. static int test_wc_DsaImportParamsRaw(void)
  18372. {
  18373. int ret = 0;
  18374. #if !defined(NO_DSA)
  18375. DsaKey key;
  18376. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18377. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18378. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18379. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18380. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18381. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18382. "80a0093113a8bd73";
  18383. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18384. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18385. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18386. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18387. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18388. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18389. "76341a7e7d9";
  18390. /* invalid p and q parameters */
  18391. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18392. const char* invalidQ = "96c5390a";
  18393. printf(testingFmt, "wc_DsaImportParamsRaw()");
  18394. ret = wc_InitDsaKey(&key);
  18395. if (ret == 0) {
  18396. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18397. }
  18398. /* test bad args */
  18399. if (ret == 0) {
  18400. /* null key struct */
  18401. ret = wc_DsaImportParamsRaw(NULL, p, q, g);
  18402. if (ret == BAD_FUNC_ARG) {
  18403. /* null param pointers */
  18404. ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
  18405. }
  18406. if (ret == BAD_FUNC_ARG) {
  18407. /* illegal p length */
  18408. ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
  18409. }
  18410. if (ret == BAD_FUNC_ARG) {
  18411. /* illegal q length */
  18412. ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
  18413. if (ret == BAD_FUNC_ARG)
  18414. ret = 0;
  18415. }
  18416. }
  18417. printf(resultFmt, ret == 0 ? passed : failed);
  18418. wc_FreeDsaKey(&key);
  18419. #endif
  18420. return ret;
  18421. } /* END test_wc_DsaImportParamsRaw */
  18422. /*
  18423. * Testing wc_DsaImportParamsRawCheck()
  18424. */
  18425. static int test_wc_DsaImportParamsRawCheck(void)
  18426. {
  18427. int ret = 0;
  18428. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  18429. DsaKey key;
  18430. int trusted = 0;
  18431. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18432. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18433. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18434. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18435. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18436. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18437. "80a0093113a8bd73";
  18438. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18439. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18440. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18441. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18442. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18443. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18444. "76341a7e7d9";
  18445. /* invalid p and q parameters */
  18446. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  18447. const char* invalidQ = "96c5390a";
  18448. printf(testingFmt, "wc_DsaImportParamsRawCheck()");
  18449. ret = wc_InitDsaKey(&key);
  18450. if (ret == 0) {
  18451. ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
  18452. }
  18453. /* test bad args */
  18454. if (ret == 0) {
  18455. /* null key struct */
  18456. ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
  18457. if (ret == BAD_FUNC_ARG) {
  18458. /* null param pointers */
  18459. ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
  18460. }
  18461. if (ret == BAD_FUNC_ARG) {
  18462. /* illegal p length */
  18463. ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
  18464. }
  18465. if (ret == BAD_FUNC_ARG) {
  18466. /* illegal q length */
  18467. ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
  18468. if (ret == BAD_FUNC_ARG)
  18469. ret = 0;
  18470. }
  18471. }
  18472. printf(resultFmt, ret == 0 ? passed : failed);
  18473. wc_FreeDsaKey(&key);
  18474. #endif
  18475. return ret;
  18476. } /* END test_wc_DsaImportParamsRawCheck */
  18477. /*
  18478. * Testing wc_DsaExportParamsRaw()
  18479. */
  18480. static int test_wc_DsaExportParamsRaw(void)
  18481. {
  18482. int ret = 0;
  18483. #if !defined(NO_DSA)
  18484. DsaKey key;
  18485. /* [mod = L=1024, N=160], from CAVP KeyPair */
  18486. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  18487. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  18488. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  18489. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  18490. "47123188f8dc551054ee162b634d60f097f719076640e209"
  18491. "80a0093113a8bd73";
  18492. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  18493. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  18494. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  18495. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  18496. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  18497. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  18498. "76341a7e7d9";
  18499. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  18500. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  18501. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  18502. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  18503. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  18504. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  18505. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  18506. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  18507. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  18508. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  18509. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  18510. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  18511. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  18512. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  18513. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  18514. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  18515. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  18516. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  18517. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  18518. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  18519. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  18520. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  18521. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  18522. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  18523. byte pOut[MAX_DSA_PARAM_SIZE];
  18524. byte qOut[MAX_DSA_PARAM_SIZE];
  18525. byte gOut[MAX_DSA_PARAM_SIZE];
  18526. word32 pOutSz, qOutSz, gOutSz;
  18527. printf(testingFmt, "wc_DsaExportParamsRaw()");
  18528. ret = wc_InitDsaKey(&key);
  18529. if (ret == 0) {
  18530. /* first test using imported raw parameters, for expected */
  18531. ret = wc_DsaImportParamsRaw(&key, p, q, g);
  18532. }
  18533. if (ret == 0) {
  18534. pOutSz = sizeof(pOut);
  18535. qOutSz = sizeof(qOut);
  18536. gOutSz = sizeof(gOut);
  18537. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18538. gOut, &gOutSz);
  18539. }
  18540. if (ret == 0) {
  18541. /* validate exported parameters are correct */
  18542. if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
  18543. (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
  18544. (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
  18545. ret = -1;
  18546. }
  18547. }
  18548. /* test bad args */
  18549. if (ret == 0) {
  18550. /* null key struct */
  18551. ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
  18552. gOut, &gOutSz);
  18553. if (ret == BAD_FUNC_ARG) {
  18554. /* null output pointers */
  18555. ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
  18556. NULL, &gOutSz);
  18557. }
  18558. if (ret == LENGTH_ONLY_E) {
  18559. /* null output size pointers */
  18560. ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
  18561. gOut, NULL);
  18562. }
  18563. if (ret == BAD_FUNC_ARG) {
  18564. /* p output buffer size too small */
  18565. pOutSz = 1;
  18566. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18567. gOut, &gOutSz);
  18568. pOutSz = sizeof(pOut);
  18569. }
  18570. if (ret == BUFFER_E) {
  18571. /* q output buffer size too small */
  18572. qOutSz = 1;
  18573. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18574. gOut, &gOutSz);
  18575. qOutSz = sizeof(qOut);
  18576. }
  18577. if (ret == BUFFER_E) {
  18578. /* g output buffer size too small */
  18579. gOutSz = 1;
  18580. ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
  18581. gOut, &gOutSz);
  18582. if (ret == BUFFER_E)
  18583. ret = 0;
  18584. }
  18585. }
  18586. printf(resultFmt, ret == 0 ? passed : failed);
  18587. wc_FreeDsaKey(&key);
  18588. #endif
  18589. return ret;
  18590. } /* END test_wc_DsaExportParamsRaw */
  18591. /*
  18592. * Testing wc_DsaExportKeyRaw()
  18593. */
  18594. static int test_wc_DsaExportKeyRaw(void)
  18595. {
  18596. int ret = 0;
  18597. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  18598. DsaKey key;
  18599. WC_RNG rng;
  18600. byte xOut[MAX_DSA_PARAM_SIZE];
  18601. byte yOut[MAX_DSA_PARAM_SIZE];
  18602. word32 xOutSz, yOutSz;
  18603. printf(testingFmt, "wc_DsaExportKeyRaw()");
  18604. XMEMSET(&rng, 0, sizeof(rng));
  18605. XMEMSET(&key, 0, sizeof(key));
  18606. ret = wc_InitRng(&rng);
  18607. if (ret == 0) {
  18608. ret = wc_InitDsaKey(&key);
  18609. }
  18610. if (ret == 0) {
  18611. ret = wc_MakeDsaParameters(&rng, 1024, &key);
  18612. if (ret == 0) {
  18613. ret = wc_MakeDsaKey(&rng, &key);
  18614. }
  18615. }
  18616. /* try successful export */
  18617. if (ret == 0) {
  18618. xOutSz = sizeof(xOut);
  18619. yOutSz = sizeof(yOut);
  18620. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18621. }
  18622. /* test bad args */
  18623. if (ret == 0) {
  18624. /* null key struct */
  18625. ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
  18626. if (ret == BAD_FUNC_ARG) {
  18627. /* null output pointers */
  18628. ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
  18629. }
  18630. if (ret == LENGTH_ONLY_E) {
  18631. /* null output size pointers */
  18632. ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
  18633. }
  18634. if (ret == BAD_FUNC_ARG) {
  18635. /* x output buffer size too small */
  18636. xOutSz = 1;
  18637. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18638. xOutSz = sizeof(xOut);
  18639. }
  18640. if (ret == BUFFER_E) {
  18641. /* y output buffer size too small */
  18642. yOutSz = 1;
  18643. ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
  18644. if (ret == BUFFER_E)
  18645. ret = 0;
  18646. }
  18647. }
  18648. printf(resultFmt, ret == 0 ? passed : failed);
  18649. wc_FreeDsaKey(&key);
  18650. wc_FreeRng(&rng);
  18651. #endif
  18652. return ret;
  18653. } /* END test_wc_DsaExportParamsRaw */
  18654. /*
  18655. * Testing wc_ed25519_make_key().
  18656. */
  18657. static int test_wc_ed25519_make_key(void)
  18658. {
  18659. int ret = 0;
  18660. #if defined(HAVE_ED25519)
  18661. ed25519_key key;
  18662. WC_RNG rng;
  18663. unsigned char pubkey[ED25519_PUB_KEY_SIZE];
  18664. ret = wc_InitRng(&rng);
  18665. if (ret == 0) {
  18666. ret = wc_ed25519_init(&key);
  18667. }
  18668. if (ret == 0) {
  18669. ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
  18670. if (ret == ECC_PRIV_KEY_E) {
  18671. ret = 0;
  18672. }
  18673. else if (ret == 0) {
  18674. ret = -1;
  18675. }
  18676. }
  18677. printf(testingFmt, "wc_ed25519_make_key()");
  18678. if (ret == 0) {
  18679. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18680. }
  18681. /* Test bad args. */
  18682. if (ret == 0) {
  18683. ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
  18684. if (ret == BAD_FUNC_ARG) {
  18685. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
  18686. }
  18687. if (ret == BAD_FUNC_ARG) {
  18688. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
  18689. }
  18690. if (ret == BAD_FUNC_ARG) {
  18691. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
  18692. }
  18693. if (ret == BAD_FUNC_ARG) {
  18694. ret = 0;
  18695. } else if (ret == 0) {
  18696. ret = WOLFSSL_FATAL_ERROR;
  18697. }
  18698. }
  18699. printf(resultFmt, ret == 0 ? passed : failed);
  18700. if (wc_FreeRng(&rng) && ret == 0) {
  18701. ret = WOLFSSL_FATAL_ERROR;
  18702. }
  18703. wc_ed25519_free(&key);
  18704. #endif
  18705. return ret;
  18706. } /* END test_wc_ed25519_make_key */
  18707. /*
  18708. * Testing wc_ed25519_init()
  18709. */
  18710. static int test_wc_ed25519_init(void)
  18711. {
  18712. int ret = 0;
  18713. #if defined(HAVE_ED25519)
  18714. ed25519_key key;
  18715. printf(testingFmt, "wc_ed25519_init()");
  18716. ret = wc_ed25519_init(&key);
  18717. /* Test bad args. */
  18718. if (ret == 0) {
  18719. ret = wc_ed25519_init(NULL);
  18720. if (ret == BAD_FUNC_ARG) {
  18721. ret = 0;
  18722. } else if (ret == 0) {
  18723. ret = WOLFSSL_FATAL_ERROR;
  18724. }
  18725. }
  18726. printf(resultFmt, ret == 0 ? passed : failed);
  18727. wc_ed25519_free(&key);
  18728. #endif
  18729. return ret;
  18730. } /* END test_wc_ed25519_init */
  18731. /*
  18732. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  18733. */
  18734. static int test_wc_ed25519_sign_msg(void)
  18735. {
  18736. int ret = 0;
  18737. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  18738. WC_RNG rng;
  18739. ed25519_key key;
  18740. byte msg[] = "Everybody gets Friday off.\n";
  18741. byte sig[ED25519_SIG_SIZE];
  18742. word32 msglen = sizeof(msg);
  18743. word32 siglen = sizeof(sig);
  18744. word32 badSigLen = sizeof(sig) - 1;
  18745. #ifdef HAVE_ED25519_VERIFY
  18746. int verify_ok = 0; /*1 = Verify success.*/
  18747. #endif
  18748. /* Initialize stack variables. */
  18749. XMEMSET(sig, 0, siglen);
  18750. /* Initialize key. */
  18751. ret = wc_InitRng(&rng);
  18752. if (ret == 0) {
  18753. ret = wc_ed25519_init(&key);
  18754. if (ret == 0) {
  18755. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18756. }
  18757. }
  18758. printf(testingFmt, "wc_ed25519_sign_msg()");
  18759. if (ret == 0) {
  18760. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
  18761. }
  18762. /* Test bad args. */
  18763. if (ret == 0 && siglen == ED25519_SIG_SIZE) {
  18764. ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
  18765. if (ret == BAD_FUNC_ARG) {
  18766. ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
  18767. }
  18768. if (ret == BAD_FUNC_ARG) {
  18769. ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
  18770. }
  18771. if (ret == BAD_FUNC_ARG) {
  18772. ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
  18773. }
  18774. if (ret == BAD_FUNC_ARG) {
  18775. ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
  18776. }
  18777. if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
  18778. badSigLen -= 1;
  18779. ret = 0;
  18780. } else if (ret == 0) {
  18781. ret = WOLFSSL_FATAL_ERROR;
  18782. }
  18783. } /* END sign */
  18784. printf(resultFmt, ret == 0 ? passed : failed);
  18785. #ifdef HAVE_ED25519_VERIFY
  18786. printf(testingFmt, "wc_ed25519_verify_msg()");
  18787. if (ret == 0) {
  18788. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
  18789. if (ret == 0 && verify_ok == 1) {
  18790. ret = 0;
  18791. } else if (ret == 0) {
  18792. ret = WOLFSSL_FATAL_ERROR;
  18793. }
  18794. /* Test bad args. */
  18795. if (ret == 0) {
  18796. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
  18797. msglen, &verify_ok, &key),
  18798. BAD_FUNC_ARG);
  18799. AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
  18800. msglen, &verify_ok, &key),
  18801. BAD_FUNC_ARG);
  18802. ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  18803. &key);
  18804. if (ret == BAD_FUNC_ARG) {
  18805. ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
  18806. &verify_ok, &key);
  18807. }
  18808. if (ret == BAD_FUNC_ARG) {
  18809. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18810. NULL, &key);
  18811. }
  18812. if (ret == BAD_FUNC_ARG) {
  18813. ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
  18814. &verify_ok, NULL);
  18815. }
  18816. if (ret == BAD_FUNC_ARG) {
  18817. ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
  18818. &verify_ok, &key);
  18819. }
  18820. if (ret == BAD_FUNC_ARG) {
  18821. ret = 0;
  18822. } else if (ret == 0) {
  18823. ret = WOLFSSL_FATAL_ERROR;
  18824. }
  18825. }
  18826. } /* END verify. */
  18827. printf(resultFmt, ret == 0 ? passed : failed);
  18828. #endif /* Verify. */
  18829. if (wc_FreeRng(&rng) && ret == 0) {
  18830. ret = WOLFSSL_FATAL_ERROR;
  18831. }
  18832. wc_ed25519_free(&key);
  18833. #endif
  18834. return ret;
  18835. } /* END test_wc_ed25519_sign_msg */
  18836. /*
  18837. * Testing wc_ed25519_import_public()
  18838. */
  18839. static int test_wc_ed25519_import_public(void)
  18840. {
  18841. int ret = 0;
  18842. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18843. WC_RNG rng;
  18844. ed25519_key pubKey;
  18845. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  18846. word32 inlen = sizeof(in);
  18847. ret = wc_InitRng(&rng);
  18848. if (ret == 0) {
  18849. ret = wc_ed25519_init(&pubKey);
  18850. if (ret == 0) {
  18851. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
  18852. }
  18853. }
  18854. printf(testingFmt, "wc_ed25519_import_public()");
  18855. if (ret == 0) {
  18856. ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
  18857. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  18858. ret = 0;
  18859. } else {
  18860. ret = WOLFSSL_FATAL_ERROR;
  18861. }
  18862. /* Test bad args. */
  18863. if (ret == 0) {
  18864. ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
  18865. if (ret == BAD_FUNC_ARG) {
  18866. ret = wc_ed25519_import_public(in, inlen, NULL);
  18867. }
  18868. if (ret == BAD_FUNC_ARG) {
  18869. ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
  18870. }
  18871. if (ret == BAD_FUNC_ARG) {
  18872. ret = 0;
  18873. } else if (ret == 0) {
  18874. ret = WOLFSSL_FATAL_ERROR;
  18875. }
  18876. }
  18877. }
  18878. printf(resultFmt, ret == 0 ? passed : failed);
  18879. if (wc_FreeRng(&rng) && ret == 0) {
  18880. ret = WOLFSSL_FATAL_ERROR;
  18881. }
  18882. wc_ed25519_free(&pubKey);
  18883. #endif
  18884. return ret;
  18885. } /* END wc_ed25519_import_public */
  18886. /*
  18887. * Testing wc_ed25519_import_private_key()
  18888. */
  18889. static int test_wc_ed25519_import_private_key(void)
  18890. {
  18891. int ret = 0;
  18892. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  18893. WC_RNG rng;
  18894. ed25519_key key;
  18895. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  18896. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  18897. word32 privKeySz = sizeof(privKey);
  18898. word32 pubKeySz = sizeof(pubKey);
  18899. #ifdef HAVE_ED25519_KEY_EXPORT
  18900. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  18901. word32 bothKeysSz = sizeof(bothKeys);
  18902. #endif
  18903. ret = wc_InitRng(&rng);
  18904. if (ret != 0) {
  18905. return ret;
  18906. }
  18907. ret = wc_ed25519_init(&key);
  18908. if (ret != 0) {
  18909. wc_FreeRng(&rng);
  18910. return ret;
  18911. }
  18912. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18913. printf(testingFmt, "wc_ed25519_import_private_key()");
  18914. if (ret == 0) {
  18915. ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  18916. pubKeySz, &key, 1);
  18917. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18918. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18919. ret = WOLFSSL_FATAL_ERROR;
  18920. }
  18921. }
  18922. #ifdef HAVE_ED25519_KEY_EXPORT
  18923. if (ret == 0)
  18924. ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
  18925. if (ret == 0) {
  18926. ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  18927. &key, 1);
  18928. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
  18929. || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  18930. ret = WOLFSSL_FATAL_ERROR;
  18931. }
  18932. }
  18933. #endif
  18934. /* Test bad args. */
  18935. if (ret == 0) {
  18936. ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  18937. &key);
  18938. if (ret == BAD_FUNC_ARG) {
  18939. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18940. pubKeySz, &key);
  18941. }
  18942. if (ret == BAD_FUNC_ARG) {
  18943. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18944. pubKeySz, NULL);
  18945. }
  18946. if (ret == BAD_FUNC_ARG) {
  18947. ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  18948. pubKeySz, &key);
  18949. }
  18950. if (ret == BAD_FUNC_ARG) {
  18951. ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  18952. pubKeySz - 1, &key);
  18953. }
  18954. if (ret == BAD_FUNC_ARG) {
  18955. ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  18956. 0, &key);
  18957. }
  18958. if (ret == BAD_FUNC_ARG) {
  18959. ret = 0;
  18960. } else if (ret == 0) {
  18961. ret = WOLFSSL_FATAL_ERROR;
  18962. }
  18963. }
  18964. printf(resultFmt, ret == 0 ? passed : failed);
  18965. if (wc_FreeRng(&rng) && ret == 0) {
  18966. ret = WOLFSSL_FATAL_ERROR;
  18967. }
  18968. wc_ed25519_free(&key);
  18969. #endif
  18970. return ret;
  18971. } /* END test_wc_ed25519_import_private_key */
  18972. /*
  18973. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  18974. */
  18975. static int test_wc_ed25519_export(void)
  18976. {
  18977. int ret = 0;
  18978. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  18979. WC_RNG rng;
  18980. ed25519_key key;
  18981. byte priv[ED25519_PRV_KEY_SIZE];
  18982. byte pub[ED25519_PUB_KEY_SIZE];
  18983. word32 privSz = sizeof(priv);
  18984. word32 pubSz = sizeof(pub);
  18985. ret = wc_InitRng(&rng);
  18986. if (ret != 0) {
  18987. return ret;
  18988. }
  18989. ret = wc_ed25519_init(&key);
  18990. if (ret != 0) {
  18991. wc_FreeRng(&rng);
  18992. return ret;
  18993. }
  18994. if (ret == 0) {
  18995. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  18996. }
  18997. printf(testingFmt, "wc_ed25519_export_public()");
  18998. if (ret == 0) {
  18999. ret = wc_ed25519_export_public(&key, pub, &pubSz);
  19000. if (ret == 0 && (pubSz != ED25519_KEY_SIZE
  19001. || XMEMCMP(key.p, pub, pubSz) != 0)) {
  19002. ret = WOLFSSL_FATAL_ERROR;
  19003. }
  19004. if (ret == 0) {
  19005. ret = wc_ed25519_export_public(NULL, pub, &pubSz);
  19006. if (ret == BAD_FUNC_ARG) {
  19007. ret = wc_ed25519_export_public(&key, NULL, &pubSz);
  19008. }
  19009. if (ret == BAD_FUNC_ARG) {
  19010. ret = wc_ed25519_export_public(&key, pub, NULL);
  19011. }
  19012. if (ret == BAD_FUNC_ARG) {
  19013. ret = 0;
  19014. } else if (ret == 0) {
  19015. ret = WOLFSSL_FATAL_ERROR;
  19016. }
  19017. }
  19018. }
  19019. printf(resultFmt, ret == 0 ? passed : failed);
  19020. printf(testingFmt, "wc_ed25519_export_private_only()");
  19021. if (ret == 0) {
  19022. ret = wc_ed25519_export_private_only(&key, priv, &privSz);
  19023. if (ret == 0 && (privSz != ED25519_KEY_SIZE
  19024. || XMEMCMP(key.k, priv, privSz) != 0)) {
  19025. ret = WOLFSSL_FATAL_ERROR;
  19026. }
  19027. if (ret == 0) {
  19028. ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
  19029. if (ret == BAD_FUNC_ARG) {
  19030. ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
  19031. }
  19032. if (ret == BAD_FUNC_ARG) {
  19033. ret = wc_ed25519_export_private_only(&key, priv, NULL);
  19034. }
  19035. if (ret == BAD_FUNC_ARG) {
  19036. ret = 0;
  19037. } else if (ret == 0) {
  19038. ret = WOLFSSL_FATAL_ERROR;
  19039. }
  19040. }
  19041. }
  19042. printf(resultFmt, ret == 0 ? passed : failed);
  19043. if (wc_FreeRng(&rng) && ret == 0) {
  19044. ret = WOLFSSL_FATAL_ERROR;
  19045. }
  19046. wc_ed25519_free(&key);
  19047. #endif
  19048. return ret;
  19049. } /* END test_wc_ed25519_export */
  19050. /*
  19051. * Testing wc_ed25519_size()
  19052. */
  19053. static int test_wc_ed25519_size(void)
  19054. {
  19055. int ret = 0;
  19056. #if defined(HAVE_ED25519)
  19057. WC_RNG rng;
  19058. ed25519_key key;
  19059. ret = wc_InitRng(&rng);
  19060. if (ret != 0) {
  19061. return ret;
  19062. }
  19063. ret = wc_ed25519_init(&key);
  19064. if (ret != 0) {
  19065. wc_FreeRng(&rng);
  19066. return ret;
  19067. }
  19068. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19069. if (ret != 0) {
  19070. wc_FreeRng(&rng);
  19071. wc_ed25519_free(&key);
  19072. return ret;
  19073. }
  19074. printf(testingFmt, "wc_ed25519_size()");
  19075. ret = wc_ed25519_size(&key);
  19076. /* Test bad args. */
  19077. if (ret == ED25519_KEY_SIZE) {
  19078. ret = wc_ed25519_size(NULL);
  19079. if (ret == BAD_FUNC_ARG) {
  19080. ret = 0;
  19081. }
  19082. }
  19083. printf(resultFmt, ret == 0 ? passed : failed);
  19084. if (ret == 0) {
  19085. printf(testingFmt, "wc_ed25519_sig_size()");
  19086. ret = wc_ed25519_sig_size(&key);
  19087. if (ret == ED25519_SIG_SIZE) {
  19088. ret = 0;
  19089. }
  19090. /* Test bad args. */
  19091. if (ret == 0) {
  19092. ret = wc_ed25519_sig_size(NULL);
  19093. if (ret == BAD_FUNC_ARG) {
  19094. ret = 0;
  19095. }
  19096. }
  19097. printf(resultFmt, ret == 0 ? passed : failed);
  19098. } /* END wc_ed25519_sig_size() */
  19099. if (ret == 0) {
  19100. printf(testingFmt, "wc_ed25519_pub_size");
  19101. ret = wc_ed25519_pub_size(&key);
  19102. if (ret == ED25519_PUB_KEY_SIZE) {
  19103. ret = 0;
  19104. }
  19105. if (ret == 0) {
  19106. ret = wc_ed25519_pub_size(NULL);
  19107. if (ret == BAD_FUNC_ARG) {
  19108. ret = 0;
  19109. }
  19110. }
  19111. printf(resultFmt, ret == 0 ? passed : failed);
  19112. } /* END wc_ed25519_pub_size */
  19113. if (ret == 0) {
  19114. printf(testingFmt, "wc_ed25519_priv_size");
  19115. ret = wc_ed25519_priv_size(&key);
  19116. if (ret == ED25519_PRV_KEY_SIZE) {
  19117. ret = 0;
  19118. }
  19119. if (ret == 0) {
  19120. ret = wc_ed25519_priv_size(NULL);
  19121. if (ret == BAD_FUNC_ARG) {
  19122. ret = 0;
  19123. }
  19124. }
  19125. printf(resultFmt, ret == 0 ? passed : failed);
  19126. } /* END wc_ed25519_pub_size */
  19127. if (wc_FreeRng(&rng) && ret == 0) {
  19128. ret = WOLFSSL_FATAL_ERROR;
  19129. }
  19130. wc_ed25519_free(&key);
  19131. #endif
  19132. return ret;
  19133. } /* END test_wc_ed25519_size */
  19134. /*
  19135. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  19136. */
  19137. static int test_wc_ed25519_exportKey(void)
  19138. {
  19139. int ret = 0;
  19140. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  19141. WC_RNG rng;
  19142. ed25519_key key;
  19143. byte priv[ED25519_PRV_KEY_SIZE];
  19144. byte pub[ED25519_PUB_KEY_SIZE];
  19145. byte privOnly[ED25519_PRV_KEY_SIZE];
  19146. word32 privSz = sizeof(priv);
  19147. word32 pubSz = sizeof(pub);
  19148. word32 privOnlySz = sizeof(privOnly);
  19149. ret = wc_InitRng(&rng);
  19150. if (ret != 0) {
  19151. return ret;
  19152. }
  19153. ret = wc_ed25519_init(&key);
  19154. if (ret != 0) {
  19155. wc_FreeRng(&rng);
  19156. return ret;
  19157. }
  19158. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19159. if (ret != 0) {
  19160. wc_FreeRng(&rng);
  19161. wc_ed25519_free(&key);
  19162. return ret;
  19163. }
  19164. printf(testingFmt, "wc_ed25519_export_private()");
  19165. ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
  19166. if (ret == 0) {
  19167. ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
  19168. if (ret == BAD_FUNC_ARG) {
  19169. ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
  19170. }
  19171. if (ret == BAD_FUNC_ARG) {
  19172. ret = wc_ed25519_export_private(&key, privOnly, NULL);
  19173. }
  19174. if (ret == BAD_FUNC_ARG) {
  19175. ret = 0;
  19176. } else if (ret == 0) {
  19177. ret = WOLFSSL_FATAL_ERROR;
  19178. }
  19179. }
  19180. printf(resultFmt, ret == 0 ? passed : failed);
  19181. if (ret == 0) {
  19182. printf(testingFmt, "wc_ed25519_export_key()");
  19183. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
  19184. if (ret == 0) {
  19185. ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
  19186. if (ret == BAD_FUNC_ARG) {
  19187. ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
  19188. }
  19189. if (ret == BAD_FUNC_ARG) {
  19190. ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
  19191. }
  19192. if (ret == BAD_FUNC_ARG) {
  19193. ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
  19194. }
  19195. if (ret == BAD_FUNC_ARG) {
  19196. ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
  19197. }
  19198. if (ret == BAD_FUNC_ARG) {
  19199. ret = 0;
  19200. } else if (ret == 0) {
  19201. ret = WOLFSSL_FATAL_ERROR;
  19202. }
  19203. }
  19204. printf(resultFmt, ret == 0 ? passed : failed);
  19205. } /* END wc_ed25519_export_key() */
  19206. /* Cross check output. */
  19207. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  19208. ret = WOLFSSL_FATAL_ERROR;
  19209. }
  19210. if (wc_FreeRng(&rng) && ret == 0) {
  19211. ret = WOLFSSL_FATAL_ERROR;
  19212. }
  19213. wc_ed25519_free(&key);
  19214. #endif
  19215. return ret;
  19216. } /* END test_wc_ed25519_exportKey */
  19217. /*
  19218. * Testing wc_Ed25519PublicKeyToDer
  19219. */
  19220. static int test_wc_Ed25519PublicKeyToDer(void)
  19221. {
  19222. int ret = 0;
  19223. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  19224. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  19225. int tmp;
  19226. ed25519_key key;
  19227. byte derBuf[1024];
  19228. printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
  19229. /* Test bad args */
  19230. tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
  19231. if (tmp != BAD_FUNC_ARG) {
  19232. ret = WOLFSSL_FATAL_ERROR;
  19233. }
  19234. if (ret == 0) {
  19235. wc_ed25519_init(&key);
  19236. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
  19237. if (tmp != BUFFER_E) {
  19238. ret = WOLFSSL_FATAL_ERROR;
  19239. }
  19240. wc_ed25519_free(&key);
  19241. }
  19242. /* Test good args */
  19243. if (ret == 0) {
  19244. WC_RNG rng;
  19245. ret = wc_InitRng(&rng);
  19246. if (ret != 0) {
  19247. return ret;
  19248. }
  19249. ret = wc_ed25519_init(&key);
  19250. if (ret != 0) {
  19251. wc_FreeRng(&rng);
  19252. return ret;
  19253. }
  19254. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
  19255. if (ret != 0) {
  19256. wc_FreeRng(&rng);
  19257. wc_ed25519_free(&key);
  19258. return ret;
  19259. }
  19260. tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
  19261. if (tmp <= 0) {
  19262. ret = WOLFSSL_FATAL_ERROR;
  19263. }
  19264. wc_FreeRng(&rng);
  19265. wc_ed25519_free(&key);
  19266. }
  19267. printf(resultFmt, ret == 0 ? passed : failed);
  19268. #endif
  19269. return ret;
  19270. } /* END testing wc_Ed25519PublicKeyToDer */
  19271. /*
  19272. * Testing wc_curve25519_init and wc_curve25519_free.
  19273. */
  19274. static int test_wc_curve25519_init(void)
  19275. {
  19276. int ret = 0;
  19277. #if defined(HAVE_CURVE25519)
  19278. curve25519_key key;
  19279. printf(testingFmt, "wc_curve25519_init()");
  19280. ret = wc_curve25519_init(&key);
  19281. /* Test bad args for wc_curve25519_init */
  19282. if (ret == 0) {
  19283. ret = wc_curve25519_init(NULL);
  19284. if (ret == BAD_FUNC_ARG) {
  19285. ret = 0;
  19286. } else if (ret == 0) {
  19287. ret = WOLFSSL_FATAL_ERROR;
  19288. }
  19289. }
  19290. printf(resultFmt, ret == 0 ? passed : failed);
  19291. /* Test good args for wc_curve_25519_free */
  19292. wc_curve25519_free(&key);
  19293. wc_curve25519_free(NULL);
  19294. #endif
  19295. return ret;
  19296. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  19297. /*
  19298. * Testing test_wc_curve25519_size.
  19299. */
  19300. static int test_wc_curve25519_size(void)
  19301. {
  19302. int ret = 0;
  19303. #if defined(HAVE_CURVE25519)
  19304. curve25519_key key;
  19305. printf(testingFmt, "wc_curve25519_size()");
  19306. ret = wc_curve25519_init(&key);
  19307. /* Test good args for wc_curve25519_size */
  19308. if (ret == 0) {
  19309. ret = wc_curve25519_size(&key);
  19310. }
  19311. /* Test bad args for wc_curve25519_size */
  19312. if (ret != 0) {
  19313. ret = wc_curve25519_size(NULL);
  19314. }
  19315. printf(resultFmt, ret == 0 ? passed : failed);
  19316. wc_curve25519_free(&key);
  19317. #endif
  19318. return ret;
  19319. } /* END test_wc_curve25519_size*/
  19320. /*
  19321. * Testing test_wc_curve25519_export_key_raw().
  19322. */
  19323. static int test_wc_curve25519_export_key_raw(void)
  19324. {
  19325. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19326. curve25519_key key;
  19327. WC_RNG rng;
  19328. byte privateKey[CURVE25519_KEYSIZE];
  19329. byte publicKey[CURVE25519_KEYSIZE];
  19330. word32 prvkSz;
  19331. word32 pubkSz;
  19332. byte prik[CURVE25519_KEYSIZE];
  19333. byte pubk[CURVE25519_KEYSIZE];
  19334. word32 prksz;
  19335. word32 pbksz;
  19336. printf(testingFmt, "wc_curve25519_export_key_raw()");
  19337. if(0 != wc_InitRng(&rng)){
  19338. printf(testingFmt, "failed due to wc_InitRng");
  19339. fflush(stdout);
  19340. return 1;
  19341. }
  19342. if(0 != wc_curve25519_init(&key)){
  19343. printf(testingFmt, "failed due to wc_curve25519_init");
  19344. fflush(stdout);
  19345. wc_FreeRng(&rng);
  19346. return 1;
  19347. }
  19348. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  19349. printf(testingFmt, "failed due to wc_curve25519_make_key");
  19350. fflush(stdout);
  19351. wc_curve25519_free(&key);
  19352. wc_FreeRng(&rng);
  19353. return 1;
  19354. }
  19355. /*
  19356. bad-argument-test cases
  19357. target function sould return BAD_FUNC_ARG
  19358. */
  19359. prvkSz = CURVE25519_KEYSIZE;
  19360. pubkSz = CURVE25519_KEYSIZE;
  19361. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19362. NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
  19363. printf(testingFmt,"failed at bad-arg-case-1.");
  19364. fflush(stdout);
  19365. wc_curve25519_free(&key);
  19366. wc_FreeRng(&rng);
  19367. return 1;
  19368. }
  19369. prvkSz = CURVE25519_KEYSIZE;
  19370. pubkSz = CURVE25519_KEYSIZE;
  19371. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19372. &key , NULL, &prvkSz, publicKey, &pubkSz)){
  19373. printf(testingFmt,"failed at bad-arg-case-2.");
  19374. fflush(stdout);
  19375. wc_curve25519_free(&key);
  19376. wc_FreeRng(&rng);
  19377. return 1;
  19378. }
  19379. prvkSz = CURVE25519_KEYSIZE;
  19380. pubkSz = CURVE25519_KEYSIZE;
  19381. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19382. &key , privateKey, NULL, publicKey, &pubkSz)){
  19383. printf(testingFmt,"failed at bad-arg-case-3.");
  19384. fflush(stdout);
  19385. wc_curve25519_free(&key);
  19386. wc_FreeRng(&rng);
  19387. return 1;
  19388. }
  19389. /* prvkSz = CURVE25519_KEYSIZE; */
  19390. pubkSz = CURVE25519_KEYSIZE;
  19391. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19392. &key , privateKey, &prvkSz, NULL, &pubkSz)){
  19393. printf(testingFmt,"failed at bad-arg-case-4.");
  19394. fflush(stdout);
  19395. wc_curve25519_free(&key);
  19396. wc_FreeRng(&rng);
  19397. return 1;
  19398. }
  19399. prvkSz = CURVE25519_KEYSIZE;
  19400. pubkSz = CURVE25519_KEYSIZE;
  19401. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
  19402. &key , privateKey, &prvkSz, publicKey, NULL )){
  19403. printf(testingFmt,"failed at bad-arg-case-5.");
  19404. fflush(stdout);
  19405. wc_curve25519_free(&key);
  19406. wc_FreeRng(&rng);
  19407. return 1;
  19408. }
  19409. /*
  19410. cross-testing
  19411. */
  19412. prksz = CURVE25519_KEYSIZE;
  19413. if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
  19414. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  19415. fflush(stdout);
  19416. wc_curve25519_free(&key);
  19417. wc_FreeRng(&rng);
  19418. return 1;
  19419. }
  19420. pbksz = CURVE25519_KEYSIZE;
  19421. if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
  19422. printf(testingFmt,"failed due to wc_curve25519_export_public");
  19423. fflush(stdout);
  19424. wc_curve25519_free(&key);
  19425. wc_FreeRng(&rng);
  19426. return 1;
  19427. }
  19428. prvkSz = CURVE25519_KEYSIZE;
  19429. /* pubkSz = CURVE25519_KEYSIZE; */
  19430. if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  19431. publicKey, &pubkSz)){
  19432. printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
  19433. fflush(stdout);
  19434. wc_curve25519_free(&key);
  19435. wc_FreeRng(&rng);
  19436. return 1;
  19437. }
  19438. if((prksz == CURVE25519_KEYSIZE) &&
  19439. (pbksz == CURVE25519_KEYSIZE) &&
  19440. (prvkSz == CURVE25519_KEYSIZE) &&
  19441. (pubkSz == CURVE25519_KEYSIZE)){
  19442. if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) &&
  19443. 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){
  19444. printf(resultFmt,passed);
  19445. fflush(stdout);
  19446. wc_curve25519_free(&key);
  19447. wc_FreeRng(&rng);
  19448. return 0;
  19449. }
  19450. else{
  19451. printf(testingFmt,"failed due to key-contents-inconsistency.");
  19452. fflush(stdout);
  19453. wc_curve25519_free(&key);
  19454. wc_FreeRng(&rng);
  19455. return 1;
  19456. }
  19457. }
  19458. else{
  19459. printf(testingFmt,"failed due to bad-key-size.");
  19460. fflush(stdout);
  19461. wc_curve25519_free(&key);
  19462. wc_FreeRng(&rng);
  19463. return 1;
  19464. }
  19465. #endif
  19466. fflush(stdout);
  19467. return 0;
  19468. } /* end of test_wc_curve25519_export_key_raw */
  19469. /*
  19470. * Testing test_wc_curve25519_export_key_raw_ex().
  19471. */
  19472. static int test_wc_curve25519_export_key_raw_ex(void)
  19473. {
  19474. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  19475. curve25519_key key;
  19476. WC_RNG rng;
  19477. byte privateKey[CURVE25519_KEYSIZE];
  19478. byte publicKey[CURVE25519_KEYSIZE];
  19479. word32 prvkSz;
  19480. word32 pubkSz;
  19481. byte prik[CURVE25519_KEYSIZE];
  19482. byte pubk[CURVE25519_KEYSIZE];
  19483. word32 prksz;
  19484. word32 pbksz;
  19485. printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
  19486. if(0 != wc_InitRng(&rng)){
  19487. printf(testingFmt, "failed due to wc_InitRng");
  19488. fflush(stdout);
  19489. return 1;
  19490. }
  19491. if(0 != wc_curve25519_init(&key)){
  19492. printf(testingFmt, "failed due to wc_curve25519_init");
  19493. fflush(stdout);
  19494. wc_FreeRng(&rng);
  19495. return 1;
  19496. }
  19497. if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
  19498. printf(testingFmt, "failed due to wc_curve25519_make_key");
  19499. fflush(stdout);
  19500. wc_curve25519_free(&key);
  19501. wc_FreeRng(&rng);
  19502. return 1;
  19503. }
  19504. /*
  19505. bad-argument-test cases
  19506. target function sould return BAD_FUNC_ARG
  19507. */
  19508. prvkSz = CURVE25519_KEYSIZE;
  19509. pubkSz = CURVE25519_KEYSIZE;
  19510. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
  19511. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19512. printf(testingFmt,"failed at bad-arg-case-1.");
  19513. fflush(stdout);
  19514. wc_curve25519_free(&key);
  19515. wc_FreeRng(&rng);
  19516. return 1;
  19517. }
  19518. prvkSz = CURVE25519_KEYSIZE;
  19519. pubkSz = CURVE25519_KEYSIZE;
  19520. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
  19521. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19522. printf(testingFmt,"failed at bad-arg-case-2.");
  19523. fflush(stdout);
  19524. wc_curve25519_free(&key);
  19525. wc_FreeRng(&rng);
  19526. return 1;
  19527. }
  19528. prvkSz = CURVE25519_KEYSIZE;
  19529. pubkSz = CURVE25519_KEYSIZE;
  19530. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
  19531. NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
  19532. printf(testingFmt,"failed at bad-arg-case-3.");
  19533. fflush(stdout);
  19534. wc_curve25519_free(&key);
  19535. wc_FreeRng(&rng);
  19536. return 1;
  19537. }
  19538. /* prvkSz = CURVE25519_KEYSIZE; */
  19539. pubkSz = CURVE25519_KEYSIZE;
  19540. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19541. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19542. printf(testingFmt,"failed at bad-arg-case-4.");
  19543. fflush(stdout);
  19544. wc_curve25519_free(&key);
  19545. wc_FreeRng(&rng);
  19546. return 1;
  19547. }
  19548. prvkSz = CURVE25519_KEYSIZE;
  19549. pubkSz = CURVE25519_KEYSIZE;
  19550. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19551. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
  19552. printf(testingFmt,"failed at bad-arg-case-5.");
  19553. fflush(stdout);
  19554. wc_curve25519_free(&key);
  19555. wc_FreeRng(&rng);
  19556. return 1;
  19557. }
  19558. prvkSz = CURVE25519_KEYSIZE;
  19559. /* pubkSz = CURVE25519_KEYSIZE; */
  19560. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
  19561. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19562. printf(testingFmt,"failed at bad-arg-case-6.");
  19563. fflush(stdout);
  19564. wc_curve25519_free(&key);
  19565. wc_FreeRng(&rng);
  19566. return 1;
  19567. }
  19568. prvkSz = CURVE25519_KEYSIZE;
  19569. pubkSz = CURVE25519_KEYSIZE;
  19570. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
  19571. publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19572. printf(testingFmt,"failed at bad-arg-case-7.");
  19573. fflush(stdout);
  19574. wc_curve25519_free(&key);
  19575. wc_FreeRng(&rng);
  19576. return 1;
  19577. }
  19578. prvkSz = CURVE25519_KEYSIZE;
  19579. pubkSz = CURVE25519_KEYSIZE;
  19580. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19581. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19582. printf(testingFmt,"failed at bad-arg-case-8.");
  19583. fflush(stdout);
  19584. wc_curve25519_free(&key);
  19585. wc_FreeRng(&rng);
  19586. return 1;
  19587. }
  19588. /* prvkSz = CURVE25519_KEYSIZE; */
  19589. pubkSz = CURVE25519_KEYSIZE;
  19590. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19591. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
  19592. printf(testingFmt,"failed at bad-arg-case-9.");
  19593. fflush(stdout);
  19594. wc_curve25519_free(&key);
  19595. wc_FreeRng(&rng);
  19596. return 1;
  19597. }
  19598. prvkSz = CURVE25519_KEYSIZE;
  19599. pubkSz = CURVE25519_KEYSIZE;
  19600. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19601. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
  19602. printf(testingFmt,"failed at bad-arg-case-10.");
  19603. fflush(stdout);
  19604. wc_curve25519_free(&key);
  19605. wc_FreeRng(&rng);
  19606. return 1;
  19607. }
  19608. /* illegal value for endien */
  19609. prvkSz = CURVE25519_KEYSIZE;
  19610. /* pubkSz = CURVE25519_KEYSIZE; */
  19611. if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
  19612. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){
  19613. printf(testingFmt,"failed at bad-arg-case-11.");
  19614. fflush(stdout);
  19615. wc_curve25519_free(&key);
  19616. wc_FreeRng(&rng);
  19617. return 1;
  19618. }
  19619. /*
  19620. cross-testing
  19621. */
  19622. prksz = CURVE25519_KEYSIZE;
  19623. if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
  19624. printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
  19625. fflush(stdout);
  19626. wc_curve25519_free(&key);
  19627. wc_FreeRng(&rng);
  19628. return 1;
  19629. }
  19630. pbksz = CURVE25519_KEYSIZE;
  19631. if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
  19632. printf(testingFmt,"failed due to wc_curve25519_export_public");
  19633. fflush(stdout);
  19634. wc_curve25519_free(&key);
  19635. wc_FreeRng(&rng);
  19636. return 1;
  19637. }
  19638. prvkSz = CURVE25519_KEYSIZE;
  19639. /* pubkSz = CURVE25519_KEYSIZE; */
  19640. if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  19641. publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
  19642. printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
  19643. fflush(stdout);
  19644. wc_curve25519_free(&key);
  19645. wc_FreeRng(&rng);
  19646. return 1;
  19647. }
  19648. if( prksz == CURVE25519_KEYSIZE &&
  19649. pbksz == CURVE25519_KEYSIZE &&
  19650. prvkSz == CURVE25519_KEYSIZE &&
  19651. pubkSz == CURVE25519_KEYSIZE ){
  19652. if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) &&
  19653. 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){
  19654. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  19655. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
  19656. if( prvkSz == CURVE25519_KEYSIZE &&
  19657. pubkSz == CURVE25519_KEYSIZE ){
  19658. ; /* proceed to the next test */
  19659. }
  19660. else{
  19661. printf(testingFmt,"failed due to key-size-inconsistency");
  19662. fflush(stdout);
  19663. wc_curve25519_free(&key);
  19664. wc_FreeRng(&rng);
  19665. return 1;
  19666. }
  19667. }
  19668. else{
  19669. printf(testingFmt,
  19670. "failed due to wc_curve25519_export_key_raw_ex");
  19671. fflush(stdout);
  19672. wc_curve25519_free(&key);
  19673. wc_FreeRng(&rng);
  19674. return 1;
  19675. }
  19676. }
  19677. else{
  19678. printf(testingFmt,"failed due to key-contents-inconsistency");
  19679. fflush(stdout);
  19680. wc_curve25519_free(&key);
  19681. wc_FreeRng(&rng);
  19682. return 1;
  19683. }
  19684. }
  19685. else{
  19686. printf(testingFmt,"failed due to bad-key-size");
  19687. fflush(stdout);
  19688. wc_curve25519_free(&key);
  19689. wc_FreeRng(&rng);
  19690. return 1;
  19691. }
  19692. /*
  19693. try once with another endian
  19694. */
  19695. prvkSz = CURVE25519_KEYSIZE;
  19696. pubkSz = CURVE25519_KEYSIZE;
  19697. if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
  19698. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
  19699. if( prvkSz == CURVE25519_KEYSIZE &&
  19700. pubkSz == CURVE25519_KEYSIZE ){
  19701. /* no more test*/
  19702. printf(resultFmt, passed );
  19703. fflush(stdout);
  19704. wc_curve25519_free(&key);
  19705. wc_FreeRng(&rng);
  19706. return 0;
  19707. }
  19708. else{
  19709. printf(testingFmt,"failed due to key-size-inconsistency");
  19710. fflush(stdout);
  19711. wc_curve25519_free(&key);
  19712. wc_FreeRng(&rng);
  19713. return 1;
  19714. }
  19715. }
  19716. else{
  19717. printf(testingFmt,
  19718. "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
  19719. fflush(stdout);
  19720. wc_curve25519_free(&key);
  19721. wc_FreeRng(&rng);
  19722. return 1;
  19723. }
  19724. #else
  19725. return 0;
  19726. #endif
  19727. } /* end of test_wc_curve25519_export_key_raw_ex */
  19728. /*
  19729. * Testing wc_curve25519_make_key
  19730. */
  19731. static int test_wc_curve25519_make_key(void)
  19732. {
  19733. int ret = 0;
  19734. #if defined(HAVE_CURVE25519)
  19735. WC_RNG rng;
  19736. curve25519_key key;
  19737. int keysize;
  19738. printf(testingFmt, "wc_curve25519_make_key()");
  19739. ret = wc_curve25519_init(&key);
  19740. if (ret == 0) {
  19741. ret = wc_InitRng(&rng);
  19742. }
  19743. if (ret == 0) {
  19744. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19745. if (ret == 0) {
  19746. keysize = wc_curve25519_size(&key);
  19747. if (keysize != CURVE25519_KEYSIZE) {
  19748. ret = WOLFSSL_FATAL_ERROR;
  19749. }
  19750. }
  19751. if (ret == 0) {
  19752. ret = wc_curve25519_make_key(&rng, keysize, &key);
  19753. }
  19754. }
  19755. /*test bad cases*/
  19756. if (ret == 0) {
  19757. ret = wc_curve25519_make_key(NULL, 0, NULL);
  19758. if (ret == BAD_FUNC_ARG) {
  19759. ret = 0;
  19760. }
  19761. }
  19762. if (ret == 0) {
  19763. ret = wc_curve25519_make_key(&rng, keysize, NULL);
  19764. if (ret == BAD_FUNC_ARG) {
  19765. ret = 0;
  19766. }
  19767. }
  19768. if (ret == 0) {
  19769. ret = wc_curve25519_make_key(NULL, keysize, &key);
  19770. if (ret == BAD_FUNC_ARG) {
  19771. ret = 0;
  19772. }
  19773. }
  19774. if (ret == 0) {
  19775. ret = wc_curve25519_make_key(&rng, 0, &key);
  19776. if (ret == ECC_BAD_ARG_E) {
  19777. ret = 0;
  19778. }
  19779. }
  19780. printf(resultFmt, ret == 0 ? passed : failed);
  19781. wc_curve25519_free(&key);
  19782. wc_FreeRng(&rng);
  19783. #endif
  19784. return ret;
  19785. } /*END test_wc_curve25519_make_key*/
  19786. /*
  19787. * Testing wc_curve25519_shared_secret_ex
  19788. */
  19789. static int test_wc_curve25519_shared_secret_ex(void)
  19790. {
  19791. int ret = 0;
  19792. #if defined(HAVE_CURVE25519)
  19793. WC_RNG rng;
  19794. curve25519_key private_key, public_key;
  19795. byte out[CURVE25519_KEYSIZE];
  19796. word32 outLen = sizeof(out);
  19797. int endian = EC25519_BIG_ENDIAN;
  19798. printf(testingFmt, "wc_curve25519_shared_secret_ex()");
  19799. ret = wc_curve25519_init(&private_key);
  19800. if (ret == 0) {
  19801. ret = wc_curve25519_init(&public_key);
  19802. }
  19803. if (ret == 0) {
  19804. ret = wc_InitRng(&rng);
  19805. }
  19806. if (ret == 0) {
  19807. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
  19808. }
  19809. if (ret == 0) {
  19810. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
  19811. }
  19812. if (ret == 0) {
  19813. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19814. &outLen, endian);
  19815. }
  19816. /*test bad cases*/
  19817. if (ret == 0) {
  19818. ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
  19819. 0, endian);
  19820. if (ret == 0) {
  19821. ret = -1;
  19822. }
  19823. if (ret == BAD_FUNC_ARG) {
  19824. ret = 0;
  19825. }
  19826. }
  19827. if (ret == 0) {
  19828. ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
  19829. &outLen, endian);
  19830. if (ret == 0) {
  19831. ret = -1;
  19832. }
  19833. else if (ret == BAD_FUNC_ARG) {
  19834. ret = 0;
  19835. }
  19836. }
  19837. if (ret == 0) {
  19838. ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
  19839. &outLen, endian);
  19840. if (ret == 0) {
  19841. ret = -1;
  19842. }
  19843. else if (ret == BAD_FUNC_ARG) {
  19844. ret = 0;
  19845. }
  19846. }
  19847. if (ret == 0) {
  19848. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  19849. &outLen, endian);
  19850. if (ret == 0) {
  19851. ret = -1;
  19852. }
  19853. else if (ret == BAD_FUNC_ARG) {
  19854. ret = 0;
  19855. }
  19856. }
  19857. if (ret == 0) {
  19858. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19859. NULL, endian);
  19860. if (ret == 0) {
  19861. ret = -1;
  19862. }
  19863. else if (ret == BAD_FUNC_ARG) {
  19864. ret = 0;
  19865. }
  19866. }
  19867. if (ret == 0) {
  19868. /*curve25519.c is checking for public_key size less than or equal to 0x7f,
  19869. *increasing to 0x8f checks for error being returned*/
  19870. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  19871. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19872. &outLen, endian);
  19873. if (ret == 0) {
  19874. ret = -1;
  19875. }
  19876. else if (ret == ECC_BAD_ARG_E) {
  19877. ret = 0;
  19878. }
  19879. }
  19880. outLen = outLen - 2;
  19881. if (ret == 0) {
  19882. ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  19883. &outLen, endian);
  19884. if (ret == 0) {
  19885. ret = -1;
  19886. }
  19887. else if (ret == BAD_FUNC_ARG) {
  19888. ret = 0;
  19889. }
  19890. }
  19891. printf(resultFmt, ret == 0 ? passed : failed);
  19892. fflush(stdout);
  19893. wc_curve25519_free(&private_key);
  19894. wc_curve25519_free(&public_key);
  19895. wc_FreeRng(&rng);
  19896. #endif
  19897. return ret;
  19898. } /*END test_wc_curve25519_shared_secret_ex*/
  19899. /*
  19900. * Testing wc_curve25519_make_pub
  19901. */
  19902. static int test_wc_curve25519_make_pub(void)
  19903. {
  19904. int ret = 0;
  19905. #ifdef HAVE_CURVE25519
  19906. WC_RNG rng;
  19907. curve25519_key key;
  19908. byte out[CURVE25519_KEYSIZE];
  19909. printf(testingFmt, "wc_curve25519_make_pub()");
  19910. ret = wc_curve25519_init(&key);
  19911. if (ret == 0) {
  19912. ret = wc_InitRng(&rng);
  19913. if (ret == 0) {
  19914. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19915. }
  19916. }
  19917. if (ret == 0) {
  19918. ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
  19919. }
  19920. /*test bad cases*/
  19921. if (ret == 0) {
  19922. ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
  19923. if (ret == ECC_BAD_ARG_E) {
  19924. ret = 0;
  19925. }
  19926. }
  19927. if (ret == 0) {
  19928. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
  19929. if (ret == ECC_BAD_ARG_E) {
  19930. ret = 0;
  19931. }
  19932. }
  19933. if (ret == 0) {
  19934. ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
  19935. if (ret == ECC_BAD_ARG_E) {
  19936. ret = 0;
  19937. }
  19938. }
  19939. if (ret == 0) {
  19940. ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
  19941. if (ret == ECC_BAD_ARG_E) {
  19942. ret = 0;
  19943. }
  19944. }
  19945. if (ret == 0) {
  19946. /* verify clamping test */
  19947. key.k[0] |= ~248;
  19948. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19949. if (ret == ECC_BAD_ARG_E) {
  19950. ret = 0;
  19951. }
  19952. key.k[0] &= 248;
  19953. }
  19954. /* repeat the expected-to-succeed test. */
  19955. if (ret == 0) {
  19956. ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
  19957. }
  19958. printf(resultFmt, ret == 0 ? passed : failed);
  19959. fflush(stdout);
  19960. wc_curve25519_free(&key);
  19961. wc_FreeRng(&rng);
  19962. #endif
  19963. return ret;
  19964. } /*END test_wc_curve25519_make_pub */
  19965. /*
  19966. * Testing test_wc_curve25519_export_public_ex
  19967. */
  19968. static int test_wc_curve25519_export_public_ex(void)
  19969. {
  19970. int ret = 0;
  19971. #if defined(HAVE_CURVE25519)
  19972. WC_RNG rng;
  19973. curve25519_key key;
  19974. byte out[CURVE25519_KEYSIZE];
  19975. word32 outLen = sizeof(out);
  19976. int endian = EC25519_BIG_ENDIAN;
  19977. printf(testingFmt, "wc_curve25519_export_public_ex()");
  19978. ret = wc_curve25519_init(&key);
  19979. if (ret == 0) {
  19980. ret = wc_InitRng(&rng);
  19981. }
  19982. if (ret == 0) {
  19983. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  19984. if (ret == 0) {
  19985. ret = wc_curve25519_export_public(&key, out, &outLen);
  19986. }
  19987. if (ret == 0) {
  19988. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  19989. }
  19990. }
  19991. /*test bad cases*/
  19992. if (ret == 0) {
  19993. ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
  19994. if (ret == BAD_FUNC_ARG) {
  19995. ret = 0;
  19996. }
  19997. }
  19998. if (ret == 0) {
  19999. ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
  20000. if (ret == BAD_FUNC_ARG) {
  20001. ret = 0;
  20002. }
  20003. }
  20004. if (ret == 0) {
  20005. ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
  20006. if (ret == BAD_FUNC_ARG) {
  20007. ret = 0;
  20008. }
  20009. }
  20010. if (ret == 0) {
  20011. ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
  20012. if (ret == BAD_FUNC_ARG) {
  20013. ret = 0;
  20014. }
  20015. }
  20016. outLen = outLen - 2;
  20017. if (ret == 0) {
  20018. ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
  20019. if (ret == ECC_BAD_ARG_E) {
  20020. ret = 0;
  20021. }
  20022. }
  20023. printf(resultFmt, ret == 0 ? passed : failed);
  20024. fflush(stdout);
  20025. wc_curve25519_free(&key);
  20026. wc_FreeRng(&rng);
  20027. #endif
  20028. return ret;
  20029. } /*END test_wc_curve25519_export_public_ex*/
  20030. /*
  20031. * Testing test_wc_curve25519_import_private_raw_ex
  20032. */
  20033. static int test_wc_curve25519_import_private_raw_ex(void)
  20034. {
  20035. int ret = 0;
  20036. #if defined(HAVE_CURVE25519)
  20037. WC_RNG rng;
  20038. curve25519_key key;
  20039. byte priv[CURVE25519_KEYSIZE];
  20040. byte pub[CURVE25519_KEYSIZE];
  20041. word32 privSz = sizeof(priv);
  20042. word32 pubSz = sizeof(pub);
  20043. int endian = EC25519_BIG_ENDIAN;
  20044. printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
  20045. ret = wc_curve25519_init(&key);
  20046. if (ret == 0) {
  20047. ret = wc_InitRng(&rng);
  20048. }
  20049. if (ret == 0) {
  20050. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20051. if (ret == 0) {
  20052. ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
  20053. }
  20054. if (ret == 0) {
  20055. ret = wc_curve25519_export_public(&key, pub, &pubSz);
  20056. }
  20057. if (ret == 0) {
  20058. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20059. &key, endian);
  20060. }
  20061. }
  20062. /*test bad cases*/
  20063. if (ret == 0) {
  20064. ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20065. endian);
  20066. if (ret == BAD_FUNC_ARG) {
  20067. ret = 0;
  20068. }
  20069. }
  20070. if (ret == 0) {
  20071. ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20072. &key, endian);
  20073. if (ret == BAD_FUNC_ARG) {
  20074. ret = 0;
  20075. }
  20076. }
  20077. if (ret == 0) {
  20078. ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20079. &key, endian);
  20080. if (ret == BAD_FUNC_ARG) {
  20081. ret = 0;
  20082. }
  20083. }
  20084. if (ret == 0) {
  20085. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20086. NULL, endian);
  20087. if (ret == BAD_FUNC_ARG) {
  20088. ret = 0;
  20089. }
  20090. }
  20091. if (ret == 0) {
  20092. ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20093. &key, endian);
  20094. if (ret == ECC_BAD_ARG_E) {
  20095. ret = 0;
  20096. }
  20097. }
  20098. if (ret == 0) {
  20099. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20100. &key, endian);
  20101. if (ret == ECC_BAD_ARG_E) {
  20102. ret = 0;
  20103. }
  20104. }
  20105. if (ret == 0) {
  20106. ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20107. &key, EC25519_LITTLE_ENDIAN);
  20108. }
  20109. printf(resultFmt, ret == 0 ? passed : failed);
  20110. fflush(stdout);
  20111. wc_curve25519_free(&key);
  20112. wc_FreeRng(&rng);
  20113. #endif
  20114. return ret;
  20115. } /*END test_wc_curve25519_import_private_raw_ex*/
  20116. /*
  20117. * Testing test_wc_curve25519_import_private
  20118. */
  20119. static int test_wc_curve25519_import_private(void)
  20120. {
  20121. int ret = 0;
  20122. #if defined(HAVE_CURVE25519)
  20123. curve25519_key key;
  20124. WC_RNG rng;
  20125. byte priv[CURVE25519_KEYSIZE];
  20126. word32 privSz = sizeof(priv);
  20127. printf(testingFmt, "wc_curve25519_import_private()");
  20128. ret = wc_curve25519_init(&key);
  20129. if (ret == 0) {
  20130. ret = wc_InitRng(&rng);
  20131. }
  20132. if (ret == 0) {
  20133. ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
  20134. if (ret == 0) {
  20135. ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
  20136. }
  20137. }
  20138. if (ret == 0) {
  20139. ret = wc_curve25519_import_private(priv, privSz, &key);
  20140. }
  20141. printf(resultFmt, ret == 0 ? passed : failed);
  20142. fflush(stdout);
  20143. wc_curve25519_free(&key);
  20144. wc_FreeRng(&rng);
  20145. #endif
  20146. return ret;
  20147. } /*END test_wc_curve25519_import*/
  20148. /*
  20149. * Testing test_wc_curve25519_export_private_raw_ex
  20150. */
  20151. static int test_wc_curve25519_export_private_raw_ex(void)
  20152. {
  20153. int ret = 0;
  20154. #if defined(HAVE_CURVE25519)
  20155. curve25519_key key;
  20156. byte out[CURVE25519_KEYSIZE];
  20157. word32 outLen = sizeof(out);
  20158. int endian = EC25519_BIG_ENDIAN;
  20159. printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
  20160. ret = wc_curve25519_init(&key);
  20161. if (ret == 0) {
  20162. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20163. }
  20164. /*test bad cases*/
  20165. if (ret == 0) {
  20166. ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
  20167. if (ret == BAD_FUNC_ARG) {
  20168. ret = 0;
  20169. }
  20170. }
  20171. if (ret == 0) {
  20172. ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
  20173. if (ret == BAD_FUNC_ARG) {
  20174. ret = 0;
  20175. }
  20176. }
  20177. if (ret == 0) {
  20178. ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
  20179. if (ret == BAD_FUNC_ARG) {
  20180. ret = 0;
  20181. }
  20182. }
  20183. if (ret == 0) {
  20184. ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
  20185. if (ret == BAD_FUNC_ARG) {
  20186. ret = 0;
  20187. }
  20188. }
  20189. if (ret == 0) {
  20190. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20191. EC25519_LITTLE_ENDIAN);
  20192. }
  20193. outLen = outLen - 2;
  20194. if (ret == 0) {
  20195. ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
  20196. if (ret == ECC_BAD_ARG_E) {
  20197. ret = 0;
  20198. }
  20199. }
  20200. printf(resultFmt, ret == 0 ? passed : failed);
  20201. fflush(stdout);
  20202. wc_curve25519_free(&key);
  20203. #endif
  20204. return ret;
  20205. }/*END test_wc_curve25519_export_private_raw_ex*/
  20206. /*
  20207. * Testing wc_ed448_make_key().
  20208. */
  20209. static int test_wc_ed448_make_key(void)
  20210. {
  20211. int ret = 0;
  20212. #if defined(HAVE_ED448)
  20213. ed448_key key;
  20214. WC_RNG rng;
  20215. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20216. ret = wc_InitRng(&rng);
  20217. if (ret == 0) {
  20218. ret = wc_ed448_init(&key);
  20219. }
  20220. if (ret == 0) {
  20221. ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
  20222. if (ret == ECC_PRIV_KEY_E) {
  20223. ret = 0;
  20224. }
  20225. else if (ret == 0) {
  20226. ret = -1;
  20227. }
  20228. }
  20229. printf(testingFmt, "wc_ed448_make_key()");
  20230. if (ret == 0) {
  20231. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20232. }
  20233. /* Test bad args. */
  20234. if (ret == 0) {
  20235. ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
  20236. if (ret == BAD_FUNC_ARG) {
  20237. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
  20238. }
  20239. if (ret == BAD_FUNC_ARG) {
  20240. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
  20241. }
  20242. if (ret == BAD_FUNC_ARG) {
  20243. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
  20244. }
  20245. if (ret == BAD_FUNC_ARG) {
  20246. ret = 0;
  20247. } else if (ret == 0) {
  20248. ret = WOLFSSL_FATAL_ERROR;
  20249. }
  20250. }
  20251. printf(resultFmt, ret == 0 ? passed : failed);
  20252. fflush(stdout);
  20253. if (wc_FreeRng(&rng) && ret == 0) {
  20254. ret = WOLFSSL_FATAL_ERROR;
  20255. }
  20256. wc_ed448_free(&key);
  20257. #endif
  20258. return ret;
  20259. } /* END test_wc_ed448_make_key */
  20260. /*
  20261. * Testing wc_ed448_init()
  20262. */
  20263. static int test_wc_ed448_init(void)
  20264. {
  20265. int ret = 0;
  20266. #if defined(HAVE_ED448)
  20267. ed448_key key;
  20268. printf(testingFmt, "wc_ed448_init()");
  20269. ret = wc_ed448_init(&key);
  20270. /* Test bad args. */
  20271. if (ret == 0) {
  20272. ret = wc_ed448_init(NULL);
  20273. if (ret == BAD_FUNC_ARG) {
  20274. ret = 0;
  20275. } else if (ret == 0) {
  20276. ret = WOLFSSL_FATAL_ERROR;
  20277. }
  20278. }
  20279. printf(resultFmt, ret == 0 ? passed : failed);
  20280. fflush(stdout);
  20281. wc_ed448_free(&key);
  20282. #endif
  20283. return ret;
  20284. } /* END test_wc_ed448_init */
  20285. /*
  20286. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20287. */
  20288. static int test_wc_ed448_sign_msg(void)
  20289. {
  20290. int ret = 0;
  20291. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20292. WC_RNG rng;
  20293. ed448_key key;
  20294. byte msg[] = "Everybody gets Friday off.\n";
  20295. byte sig[ED448_SIG_SIZE];
  20296. word32 msglen = sizeof(msg);
  20297. word32 siglen = sizeof(sig);
  20298. word32 badSigLen = sizeof(sig) - 1;
  20299. #ifdef HAVE_ED448_VERIFY
  20300. int verify_ok = 0; /*1 = Verify success.*/
  20301. #endif
  20302. /* Initialize stack variables. */
  20303. XMEMSET(sig, 0, siglen);
  20304. /* Initialize key. */
  20305. ret = wc_InitRng(&rng);
  20306. if (ret == 0) {
  20307. ret = wc_ed448_init(&key);
  20308. if (ret == 0) {
  20309. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20310. }
  20311. }
  20312. printf(testingFmt, "wc_ed448_sign_msg()");
  20313. if (ret == 0) {
  20314. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
  20315. }
  20316. /* Test bad args. */
  20317. if (ret == 0 && siglen == ED448_SIG_SIZE) {
  20318. ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
  20319. if (ret == BAD_FUNC_ARG) {
  20320. ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
  20321. }
  20322. if (ret == BAD_FUNC_ARG) {
  20323. ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
  20324. }
  20325. if (ret == BAD_FUNC_ARG) {
  20326. ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
  20327. }
  20328. if (ret == BAD_FUNC_ARG) {
  20329. ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
  20330. NULL, 0);
  20331. }
  20332. if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
  20333. badSigLen -= 1;
  20334. ret = 0;
  20335. } else if (ret == 0) {
  20336. ret = WOLFSSL_FATAL_ERROR;
  20337. }
  20338. } /* END sign */
  20339. printf(resultFmt, ret == 0 ? passed : failed);
  20340. fflush(stdout);
  20341. #ifdef HAVE_ED448_VERIFY
  20342. printf(testingFmt, "wc_ed448_verify_msg()");
  20343. if (ret == 0) {
  20344. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20345. &key, NULL, 0);
  20346. if (ret == 0 && verify_ok == 1) {
  20347. ret = 0;
  20348. } else if (ret == 0) {
  20349. ret = WOLFSSL_FATAL_ERROR;
  20350. }
  20351. /* Test bad args. */
  20352. if (ret == 0) {
  20353. AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
  20354. msglen, &verify_ok, &key,
  20355. NULL, 0),
  20356. BAD_FUNC_ARG);
  20357. AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
  20358. msglen, &verify_ok, &key,
  20359. NULL, 0),
  20360. BAD_FUNC_ARG);
  20361. ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20362. &key, NULL, 0);
  20363. if (ret == BAD_FUNC_ARG) {
  20364. ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
  20365. &verify_ok, &key, NULL, 0);
  20366. }
  20367. if (ret == BAD_FUNC_ARG) {
  20368. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20369. NULL, &key, NULL, 0);
  20370. }
  20371. if (ret == BAD_FUNC_ARG) {
  20372. ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
  20373. &verify_ok, NULL, NULL, 0);
  20374. }
  20375. if (ret == BAD_FUNC_ARG) {
  20376. ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
  20377. &verify_ok, &key, NULL, 0);
  20378. }
  20379. if (ret == BAD_FUNC_ARG) {
  20380. ret = 0;
  20381. } else if (ret == 0) {
  20382. ret = WOLFSSL_FATAL_ERROR;
  20383. }
  20384. }
  20385. } /* END verify. */
  20386. printf(resultFmt, ret == 0 ? passed : failed);
  20387. fflush(stdout);
  20388. #endif /* Verify. */
  20389. if (wc_FreeRng(&rng) && ret == 0) {
  20390. ret = WOLFSSL_FATAL_ERROR;
  20391. }
  20392. wc_ed448_free(&key);
  20393. #endif
  20394. return ret;
  20395. } /* END test_wc_ed448_sign_msg */
  20396. /*
  20397. * Testing wc_ed448_import_public()
  20398. */
  20399. static int test_wc_ed448_import_public(void)
  20400. {
  20401. int ret = 0;
  20402. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20403. WC_RNG rng;
  20404. ed448_key pubKey;
  20405. const byte in[] =
  20406. "Ed448PublicKeyUnitTest.................................\n";
  20407. word32 inlen = sizeof(in);
  20408. ret = wc_InitRng(&rng);
  20409. if (ret == 0) {
  20410. ret = wc_ed448_init(&pubKey);
  20411. if (ret == 0) {
  20412. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
  20413. }
  20414. }
  20415. printf(testingFmt, "wc_ed448_import_public()");
  20416. if (ret == 0) {
  20417. ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
  20418. if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
  20419. ret = 0;
  20420. } else {
  20421. ret = WOLFSSL_FATAL_ERROR;
  20422. }
  20423. /* Test bad args. */
  20424. if (ret == 0) {
  20425. ret = wc_ed448_import_public(NULL, inlen, &pubKey);
  20426. if (ret == BAD_FUNC_ARG) {
  20427. ret = wc_ed448_import_public(in, inlen, NULL);
  20428. }
  20429. if (ret == BAD_FUNC_ARG) {
  20430. ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
  20431. }
  20432. if (ret == BAD_FUNC_ARG) {
  20433. ret = 0;
  20434. } else if (ret == 0) {
  20435. ret = WOLFSSL_FATAL_ERROR;
  20436. }
  20437. }
  20438. }
  20439. printf(resultFmt, ret == 0 ? passed : failed);
  20440. fflush(stdout);
  20441. if (wc_FreeRng(&rng) && ret == 0) {
  20442. ret = WOLFSSL_FATAL_ERROR;
  20443. }
  20444. wc_ed448_free(&pubKey);
  20445. #endif
  20446. return ret;
  20447. } /* END wc_ed448_import_public */
  20448. /*
  20449. * Testing wc_ed448_import_private_key()
  20450. */
  20451. static int test_wc_ed448_import_private_key(void)
  20452. {
  20453. int ret = 0;
  20454. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20455. WC_RNG rng;
  20456. ed448_key key;
  20457. const byte privKey[] =
  20458. "Ed448PrivateKeyUnitTest................................\n";
  20459. const byte pubKey[] =
  20460. "Ed448PublicKeyUnitTest.................................\n";
  20461. word32 privKeySz = sizeof(privKey);
  20462. word32 pubKeySz = sizeof(pubKey);
  20463. #ifdef HAVE_ED448_KEY_EXPORT
  20464. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20465. word32 bothKeysSz = sizeof(bothKeys);
  20466. #endif
  20467. ret = wc_InitRng(&rng);
  20468. if (ret != 0) {
  20469. return ret;
  20470. }
  20471. ret = wc_ed448_init(&key);
  20472. if (ret != 0) {
  20473. wc_FreeRng(&rng);
  20474. return ret;
  20475. }
  20476. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20477. printf(testingFmt, "wc_ed448_import_private_key()");
  20478. if (ret == 0) {
  20479. ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20480. pubKeySz, &key, 1);
  20481. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20482. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20483. ret = WOLFSSL_FATAL_ERROR;
  20484. }
  20485. }
  20486. #ifdef HAVE_ED448_KEY_EXPORT
  20487. if (ret == 0)
  20488. ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
  20489. if (ret == 0) {
  20490. ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20491. &key, 1);
  20492. if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
  20493. XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
  20494. ret = WOLFSSL_FATAL_ERROR;
  20495. }
  20496. }
  20497. #endif
  20498. /* Test bad args. */
  20499. if (ret == 0) {
  20500. ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20501. &key);
  20502. if (ret == BAD_FUNC_ARG) {
  20503. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20504. pubKeySz, &key);
  20505. }
  20506. if (ret == BAD_FUNC_ARG) {
  20507. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20508. pubKeySz, NULL);
  20509. }
  20510. if (ret == BAD_FUNC_ARG) {
  20511. ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20512. pubKeySz, &key);
  20513. }
  20514. if (ret == BAD_FUNC_ARG) {
  20515. ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20516. pubKeySz - 1, &key);
  20517. }
  20518. if (ret == BAD_FUNC_ARG) {
  20519. ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
  20520. 0, &key);
  20521. }
  20522. if (ret == BAD_FUNC_ARG) {
  20523. ret = 0;
  20524. } else if (ret == 0) {
  20525. ret = WOLFSSL_FATAL_ERROR;
  20526. }
  20527. }
  20528. printf(resultFmt, ret == 0 ? passed : failed);
  20529. fflush(stdout);
  20530. if (wc_FreeRng(&rng) && ret == 0) {
  20531. ret = WOLFSSL_FATAL_ERROR;
  20532. }
  20533. wc_ed448_free(&key);
  20534. #endif
  20535. return ret;
  20536. } /* END test_wc_ed448_import_private_key */
  20537. /*
  20538. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20539. */
  20540. static int test_wc_ed448_export(void)
  20541. {
  20542. int ret = 0;
  20543. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20544. WC_RNG rng;
  20545. ed448_key key;
  20546. byte priv[ED448_PRV_KEY_SIZE];
  20547. byte pub[ED448_PUB_KEY_SIZE];
  20548. word32 privSz = sizeof(priv);
  20549. word32 pubSz = sizeof(pub);
  20550. ret = wc_InitRng(&rng);
  20551. if (ret != 0) {
  20552. return ret;
  20553. }
  20554. ret = wc_ed448_init(&key);
  20555. if (ret != 0) {
  20556. wc_FreeRng(&rng);
  20557. return ret;
  20558. }
  20559. if (ret == 0) {
  20560. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20561. }
  20562. printf(testingFmt, "wc_ed448_export_public()");
  20563. if (ret == 0) {
  20564. ret = wc_ed448_export_public(&key, pub, &pubSz);
  20565. if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
  20566. XMEMCMP(key.p, pub, pubSz) != 0)) {
  20567. ret = WOLFSSL_FATAL_ERROR;
  20568. }
  20569. if (ret == 0) {
  20570. ret = wc_ed448_export_public(NULL, pub, &pubSz);
  20571. if (ret == BAD_FUNC_ARG) {
  20572. ret = wc_ed448_export_public(&key, NULL, &pubSz);
  20573. }
  20574. if (ret == BAD_FUNC_ARG) {
  20575. ret = wc_ed448_export_public(&key, pub, NULL);
  20576. }
  20577. if (ret == BAD_FUNC_ARG) {
  20578. ret = 0;
  20579. } else if (ret == 0) {
  20580. ret = WOLFSSL_FATAL_ERROR;
  20581. }
  20582. }
  20583. }
  20584. printf(resultFmt, ret == 0 ? passed : failed);
  20585. fflush(stdout);
  20586. printf(testingFmt, "wc_ed448_export_private_only()");
  20587. if (ret == 0) {
  20588. ret = wc_ed448_export_private_only(&key, priv, &privSz);
  20589. if (ret == 0 && (privSz != ED448_KEY_SIZE ||
  20590. XMEMCMP(key.k, priv, privSz) != 0)) {
  20591. ret = WOLFSSL_FATAL_ERROR;
  20592. }
  20593. if (ret == 0) {
  20594. ret = wc_ed448_export_private_only(NULL, priv, &privSz);
  20595. if (ret == BAD_FUNC_ARG) {
  20596. ret = wc_ed448_export_private_only(&key, NULL, &privSz);
  20597. }
  20598. if (ret == BAD_FUNC_ARG) {
  20599. ret = wc_ed448_export_private_only(&key, priv, NULL);
  20600. }
  20601. if (ret == BAD_FUNC_ARG) {
  20602. ret = 0;
  20603. } else if (ret == 0) {
  20604. ret = WOLFSSL_FATAL_ERROR;
  20605. }
  20606. }
  20607. }
  20608. printf(resultFmt, ret == 0 ? passed : failed);
  20609. fflush(stdout);
  20610. if (wc_FreeRng(&rng) && ret == 0) {
  20611. ret = WOLFSSL_FATAL_ERROR;
  20612. }
  20613. wc_ed448_free(&key);
  20614. #endif
  20615. return ret;
  20616. } /* END test_wc_ed448_export */
  20617. /*
  20618. * Testing wc_ed448_size()
  20619. */
  20620. static int test_wc_ed448_size(void)
  20621. {
  20622. int ret = 0;
  20623. #if defined(HAVE_ED448)
  20624. WC_RNG rng;
  20625. ed448_key key;
  20626. ret = wc_InitRng(&rng);
  20627. if (ret != 0) {
  20628. return ret;
  20629. }
  20630. ret = wc_ed448_init(&key);
  20631. if (ret != 0) {
  20632. wc_FreeRng(&rng);
  20633. return ret;
  20634. }
  20635. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20636. if (ret != 0) {
  20637. wc_FreeRng(&rng);
  20638. wc_ed448_free(&key);
  20639. return ret;
  20640. }
  20641. printf(testingFmt, "wc_ed448_size()");
  20642. ret = wc_ed448_size(&key);
  20643. /* Test bad args. */
  20644. if (ret == ED448_KEY_SIZE) {
  20645. ret = wc_ed448_size(NULL);
  20646. if (ret == BAD_FUNC_ARG) {
  20647. ret = 0;
  20648. }
  20649. }
  20650. printf(resultFmt, ret == 0 ? passed : failed);
  20651. fflush(stdout);
  20652. if (ret == 0) {
  20653. printf(testingFmt, "wc_ed448_sig_size()");
  20654. ret = wc_ed448_sig_size(&key);
  20655. if (ret == ED448_SIG_SIZE) {
  20656. ret = 0;
  20657. }
  20658. /* Test bad args. */
  20659. if (ret == 0) {
  20660. ret = wc_ed448_sig_size(NULL);
  20661. if (ret == BAD_FUNC_ARG) {
  20662. ret = 0;
  20663. }
  20664. }
  20665. printf(resultFmt, ret == 0 ? passed : failed);
  20666. fflush(stdout);
  20667. } /* END wc_ed448_sig_size() */
  20668. if (ret == 0) {
  20669. printf(testingFmt, "wc_ed448_pub_size");
  20670. ret = wc_ed448_pub_size(&key);
  20671. if (ret == ED448_PUB_KEY_SIZE) {
  20672. ret = 0;
  20673. }
  20674. if (ret == 0) {
  20675. ret = wc_ed448_pub_size(NULL);
  20676. if (ret == BAD_FUNC_ARG) {
  20677. ret = 0;
  20678. }
  20679. }
  20680. printf(resultFmt, ret == 0 ? passed : failed);
  20681. fflush(stdout);
  20682. } /* END wc_ed448_pub_size */
  20683. if (ret == 0) {
  20684. printf(testingFmt, "wc_ed448_priv_size");
  20685. ret = wc_ed448_priv_size(&key);
  20686. if (ret == ED448_PRV_KEY_SIZE) {
  20687. ret = 0;
  20688. }
  20689. if (ret == 0) {
  20690. ret = wc_ed448_priv_size(NULL);
  20691. if (ret == BAD_FUNC_ARG) {
  20692. ret = 0;
  20693. }
  20694. }
  20695. printf(resultFmt, ret == 0 ? passed : failed);
  20696. fflush(stdout);
  20697. } /* END wc_ed448_pub_size */
  20698. if (wc_FreeRng(&rng) && ret == 0) {
  20699. ret = WOLFSSL_FATAL_ERROR;
  20700. }
  20701. wc_ed448_free(&key);
  20702. #endif
  20703. return ret;
  20704. } /* END test_wc_ed448_size */
  20705. /*
  20706. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20707. */
  20708. static int test_wc_ed448_exportKey(void)
  20709. {
  20710. int ret = 0;
  20711. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20712. WC_RNG rng;
  20713. ed448_key key;
  20714. byte priv[ED448_PRV_KEY_SIZE];
  20715. byte pub[ED448_PUB_KEY_SIZE];
  20716. byte privOnly[ED448_PRV_KEY_SIZE];
  20717. word32 privSz = sizeof(priv);
  20718. word32 pubSz = sizeof(pub);
  20719. word32 privOnlySz = sizeof(privOnly);
  20720. ret = wc_InitRng(&rng);
  20721. if (ret != 0) {
  20722. return ret;
  20723. }
  20724. ret = wc_ed448_init(&key);
  20725. if (ret != 0) {
  20726. wc_FreeRng(&rng);
  20727. return ret;
  20728. }
  20729. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20730. if (ret != 0) {
  20731. wc_FreeRng(&rng);
  20732. wc_ed448_free(&key);
  20733. return ret;
  20734. }
  20735. printf(testingFmt, "wc_ed448_export_private()");
  20736. ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
  20737. if (ret == 0) {
  20738. ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
  20739. if (ret == BAD_FUNC_ARG) {
  20740. ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
  20741. }
  20742. if (ret == BAD_FUNC_ARG) {
  20743. ret = wc_ed448_export_private(&key, privOnly, NULL);
  20744. }
  20745. if (ret == BAD_FUNC_ARG) {
  20746. ret = 0;
  20747. } else if (ret == 0) {
  20748. ret = WOLFSSL_FATAL_ERROR;
  20749. }
  20750. }
  20751. printf(resultFmt, ret == 0 ? passed : failed);
  20752. fflush(stdout);
  20753. if (ret == 0) {
  20754. printf(testingFmt, "wc_ed448_export_key()");
  20755. ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
  20756. if (ret == 0) {
  20757. ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
  20758. if (ret == BAD_FUNC_ARG) {
  20759. ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
  20760. }
  20761. if (ret == BAD_FUNC_ARG) {
  20762. ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
  20763. }
  20764. if (ret == BAD_FUNC_ARG) {
  20765. ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
  20766. }
  20767. if (ret == BAD_FUNC_ARG) {
  20768. ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
  20769. }
  20770. if (ret == BAD_FUNC_ARG) {
  20771. ret = 0;
  20772. } else if (ret == 0) {
  20773. ret = WOLFSSL_FATAL_ERROR;
  20774. }
  20775. }
  20776. printf(resultFmt, ret == 0 ? passed : failed);
  20777. fflush(stdout);
  20778. } /* END wc_ed448_export_key() */
  20779. /* Cross check output. */
  20780. if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
  20781. ret = WOLFSSL_FATAL_ERROR;
  20782. }
  20783. if (wc_FreeRng(&rng) && ret == 0) {
  20784. ret = WOLFSSL_FATAL_ERROR;
  20785. }
  20786. wc_ed448_free(&key);
  20787. #endif
  20788. return ret;
  20789. } /* END test_wc_ed448_exportKey */
  20790. /*
  20791. * Testing wc_Ed448PublicKeyToDer
  20792. */
  20793. static int test_wc_Ed448PublicKeyToDer(void)
  20794. {
  20795. int ret = 0;
  20796. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20797. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20798. int tmp;
  20799. ed448_key key;
  20800. byte derBuf[1024];
  20801. printf(testingFmt, "wc_Ed448PublicKeyToDer()");
  20802. /* Test bad args */
  20803. tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
  20804. if (tmp != BAD_FUNC_ARG) {
  20805. ret = WOLFSSL_FATAL_ERROR;
  20806. }
  20807. if (ret == 0) {
  20808. wc_ed448_init(&key);
  20809. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
  20810. if (tmp != BUFFER_E) {
  20811. ret = WOLFSSL_FATAL_ERROR;
  20812. }
  20813. wc_ed448_free(&key);
  20814. }
  20815. /* Test good args */
  20816. if (ret == 0) {
  20817. WC_RNG rng;
  20818. ret = wc_InitRng(&rng);
  20819. if (ret != 0) {
  20820. return ret;
  20821. }
  20822. ret = wc_ed448_init(&key);
  20823. if (ret != 0) {
  20824. wc_FreeRng(&rng);
  20825. return ret;
  20826. }
  20827. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
  20828. if (ret != 0) {
  20829. wc_FreeRng(&rng);
  20830. wc_ed448_free(&key);
  20831. return ret;
  20832. }
  20833. tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
  20834. if (tmp <= 0) {
  20835. ret = WOLFSSL_FATAL_ERROR;
  20836. }
  20837. wc_FreeRng(&rng);
  20838. wc_ed448_free(&key);
  20839. }
  20840. printf(resultFmt, ret == 0 ? passed : failed);
  20841. fflush(stdout);
  20842. #endif
  20843. return ret;
  20844. } /* END testing wc_Ed448PublicKeyToDer */
  20845. /*
  20846. * Testing wc_curve448_init and wc_curve448_free.
  20847. */
  20848. static int test_wc_curve448_init(void)
  20849. {
  20850. int ret = 0;
  20851. #if defined(HAVE_CURVE448)
  20852. curve448_key key;
  20853. printf(testingFmt, "wc_curve448_init()");
  20854. ret = wc_curve448_init(&key);
  20855. /* Test bad args for wc_curve448_init */
  20856. if (ret == 0) {
  20857. ret = wc_curve448_init(NULL);
  20858. if (ret == BAD_FUNC_ARG) {
  20859. ret = 0;
  20860. } else if (ret == 0) {
  20861. ret = WOLFSSL_FATAL_ERROR;
  20862. }
  20863. }
  20864. printf(resultFmt, ret == 0 ? passed : failed);
  20865. fflush(stdout);
  20866. /* Test good args for wc_curve_448_free */
  20867. wc_curve448_free(&key);
  20868. wc_curve448_free(NULL);
  20869. #endif
  20870. return ret;
  20871. } /* END test_wc_curve448_init and wc_curve_448_free*/
  20872. /*
  20873. * Testing wc_curve448_make_key
  20874. */
  20875. static int test_wc_curve448_make_key(void)
  20876. {
  20877. int ret = 0;
  20878. #if defined(HAVE_CURVE448)
  20879. WC_RNG rng;
  20880. curve448_key key;
  20881. int keysize;
  20882. printf(testingFmt, "wc_curve448_make_key()");
  20883. ret = wc_curve448_init(&key);
  20884. if (ret == 0) {
  20885. ret = wc_InitRng(&rng);
  20886. }
  20887. if (ret == 0) {
  20888. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  20889. if (ret == 0) {
  20890. keysize = wc_curve448_size(&key);
  20891. if (keysize != CURVE448_KEY_SIZE) {
  20892. ret = WOLFSSL_FATAL_ERROR;
  20893. }
  20894. }
  20895. if (ret == 0) {
  20896. ret = wc_curve448_make_key(&rng, keysize, &key);
  20897. }
  20898. }
  20899. /*test bad cases*/
  20900. if (ret == 0) {
  20901. ret = wc_curve448_make_key(NULL, 0, NULL);
  20902. if (ret == BAD_FUNC_ARG) {
  20903. ret = 0;
  20904. }
  20905. }
  20906. if (ret == 0) {
  20907. ret = wc_curve448_make_key(&rng, keysize, NULL);
  20908. if (ret == BAD_FUNC_ARG) {
  20909. ret = 0;
  20910. }
  20911. }
  20912. if (ret == 0) {
  20913. ret = wc_curve448_make_key(NULL, keysize, &key);
  20914. if (ret == BAD_FUNC_ARG) {
  20915. ret = 0;
  20916. }
  20917. }
  20918. if (ret == 0) {
  20919. ret = wc_curve448_make_key(&rng, 0, &key);
  20920. if (ret == ECC_BAD_ARG_E) {
  20921. ret = 0;
  20922. }
  20923. }
  20924. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  20925. ret = WOLFSSL_FATAL_ERROR;
  20926. }
  20927. printf(resultFmt, ret == 0 ? passed : failed);
  20928. fflush(stdout);
  20929. wc_curve448_free(&key);
  20930. #endif
  20931. return ret;
  20932. } /*END test_wc_curve448_make_key*/
  20933. /*
  20934. * Testing test_wc_curve448_shared_secret_ex
  20935. */
  20936. static int test_wc_curve448_shared_secret_ex(void)
  20937. {
  20938. int ret = 0;
  20939. #if defined(HAVE_CURVE448)
  20940. WC_RNG rng;
  20941. curve448_key private_key, public_key;
  20942. byte out[CURVE448_KEY_SIZE];
  20943. word32 outLen = sizeof(out);
  20944. int endian = EC448_BIG_ENDIAN;
  20945. printf(testingFmt, "wc_curve448_shared_secret_ex()");
  20946. ret = wc_curve448_init(&private_key);
  20947. if (ret == 0) {
  20948. ret = wc_InitRng(&rng);
  20949. if (ret == 0) {
  20950. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
  20951. }
  20952. }
  20953. if (ret == 0) {
  20954. ret = wc_curve448_init(&public_key);
  20955. }
  20956. if (ret == 0) {
  20957. if (ret == 0) {
  20958. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
  20959. }
  20960. }
  20961. if (ret == 0) {
  20962. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20963. &outLen, endian);
  20964. }
  20965. /*test bad cases*/
  20966. if (ret == 0) {
  20967. ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL,
  20968. 0, endian);
  20969. if (ret == BAD_FUNC_ARG) {
  20970. ret = 0;
  20971. }
  20972. }
  20973. if (ret == 0) {
  20974. ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
  20975. &outLen, endian);
  20976. if (ret == BAD_FUNC_ARG) {
  20977. ret = 0;
  20978. }
  20979. }
  20980. if (ret == 0) {
  20981. ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
  20982. &outLen, endian);
  20983. if (ret == BAD_FUNC_ARG) {
  20984. ret = 0;
  20985. }
  20986. }
  20987. if (ret == 0) {
  20988. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  20989. &outLen, endian);
  20990. if (ret == BAD_FUNC_ARG) {
  20991. ret = 0;
  20992. }
  20993. }
  20994. if (ret == 0) {
  20995. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  20996. NULL, endian);
  20997. if (ret == BAD_FUNC_ARG) {
  20998. ret = 0;
  20999. }
  21000. }
  21001. outLen = outLen - 2;
  21002. if (ret == 0) {
  21003. ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21004. &outLen, endian);
  21005. if (ret == BAD_FUNC_ARG) {
  21006. ret = 0;
  21007. }
  21008. }
  21009. printf(resultFmt, ret == 0 ? passed : failed);
  21010. fflush(stdout);
  21011. wc_curve448_free(&private_key);
  21012. wc_curve448_free(&public_key);
  21013. wc_FreeRng(&rng);
  21014. #endif
  21015. return ret;
  21016. } /*END test_wc_curve448_shared_secret_ex*/
  21017. /*
  21018. * Testing test_wc_curve448_export_public_ex
  21019. */
  21020. static int test_wc_curve448_export_public_ex(void)
  21021. {
  21022. int ret = 0;
  21023. #if defined(HAVE_CURVE448)
  21024. WC_RNG rng;
  21025. curve448_key key;
  21026. byte out[CURVE448_KEY_SIZE];
  21027. word32 outLen = sizeof(out);
  21028. int endian = EC448_BIG_ENDIAN;
  21029. printf(testingFmt, "wc_curve448_export_public_ex()");
  21030. ret = wc_curve448_init(&key);
  21031. if (ret == 0) {
  21032. ret = wc_InitRng(&rng);
  21033. }
  21034. if (ret == 0) {
  21035. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21036. if (ret == 0){
  21037. ret = wc_curve448_export_public(&key, out, &outLen);
  21038. }
  21039. if (ret == 0) {
  21040. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21041. }
  21042. }
  21043. /*test bad cases*/
  21044. if (ret == 0) {
  21045. ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
  21046. if (ret == BAD_FUNC_ARG) {
  21047. ret = 0;
  21048. }
  21049. }
  21050. if (ret == 0) {
  21051. ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
  21052. if (ret == BAD_FUNC_ARG) {
  21053. ret = 0;
  21054. }
  21055. }
  21056. if (ret == 0) {
  21057. ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
  21058. if (ret == BAD_FUNC_ARG) {
  21059. ret = 0;
  21060. }
  21061. }
  21062. if (ret == 0) {
  21063. ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
  21064. if (ret == BAD_FUNC_ARG) {
  21065. ret = 0;
  21066. }
  21067. }
  21068. outLen = outLen - 2;
  21069. if (ret == 0) {
  21070. ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
  21071. if (ret == ECC_BAD_ARG_E) {
  21072. ret = 0;
  21073. }
  21074. }
  21075. printf(resultFmt, ret == 0 ? passed : failed);
  21076. fflush(stdout);
  21077. wc_curve448_free(&key);
  21078. wc_FreeRng(&rng);
  21079. #endif
  21080. return ret;
  21081. } /*END test_wc_curve448_export_public_ex*/
  21082. /*
  21083. * Testing test_wc_curve448_export_private_raw_ex
  21084. */
  21085. static int test_wc_curve448_export_private_raw_ex(void)
  21086. {
  21087. int ret = 0;
  21088. #if defined(HAVE_CURVE448)
  21089. curve448_key key;
  21090. byte out[CURVE448_KEY_SIZE];
  21091. word32 outLen = sizeof(out);
  21092. int endian = EC448_BIG_ENDIAN;
  21093. printf(testingFmt, "wc_curve448_export_private_raw_ex()");
  21094. ret = wc_curve448_init(&key);
  21095. if (ret == 0) {
  21096. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21097. }
  21098. /*test bad cases*/
  21099. if (ret == 0) {
  21100. ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
  21101. if (ret == BAD_FUNC_ARG) {
  21102. ret = 0;
  21103. }
  21104. }
  21105. if (ret == 0) {
  21106. ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
  21107. if (ret == BAD_FUNC_ARG) {
  21108. ret = 0;
  21109. }
  21110. }
  21111. if (ret == 0) {
  21112. ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
  21113. if (ret == BAD_FUNC_ARG) {
  21114. ret = 0;
  21115. }
  21116. }
  21117. if (ret == 0) {
  21118. ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
  21119. if (ret == BAD_FUNC_ARG) {
  21120. ret = 0;
  21121. }
  21122. }
  21123. if (ret == 0) {
  21124. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21125. EC448_LITTLE_ENDIAN);
  21126. }
  21127. outLen = outLen - 2;
  21128. if (ret == 0) {
  21129. ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
  21130. if (ret == ECC_BAD_ARG_E) {
  21131. ret = 0;
  21132. }
  21133. }
  21134. printf(resultFmt, ret == 0 ? passed : failed);
  21135. fflush(stdout);
  21136. wc_curve448_free(&key);
  21137. #endif
  21138. return ret;
  21139. }/*END test_wc_curve448_export_private_raw_ex*/
  21140. /*
  21141. * Testing test_wc_curve448_import_private_raw_ex
  21142. */
  21143. static int test_wc_curve448_import_private_raw_ex(void)
  21144. {
  21145. int ret = 0;
  21146. #if defined(HAVE_CURVE448)
  21147. WC_RNG rng;
  21148. curve448_key key;
  21149. byte priv[CURVE448_KEY_SIZE];
  21150. byte pub[CURVE448_KEY_SIZE];
  21151. word32 privSz = sizeof(priv);
  21152. word32 pubSz = sizeof(pub);
  21153. int endian = EC448_BIG_ENDIAN;
  21154. printf(testingFmt, "wc_curve448_import_private_raw_ex()");
  21155. ret = wc_curve448_init(&key);
  21156. if (ret == 0) {
  21157. ret = wc_InitRng(&rng);
  21158. }
  21159. if (ret == 0) {
  21160. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21161. if (ret == 0){
  21162. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21163. }
  21164. if (ret == 0){
  21165. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21166. }
  21167. if (ret == 0) {
  21168. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21169. &key, endian);
  21170. }
  21171. }
  21172. /*test bad cases*/
  21173. if (ret == 0) {
  21174. ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
  21175. if (ret == BAD_FUNC_ARG) {
  21176. ret = 0;
  21177. }
  21178. }
  21179. if (ret == 0) {
  21180. ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21181. &key, endian);
  21182. if (ret == BAD_FUNC_ARG) {
  21183. ret = 0;
  21184. }
  21185. }
  21186. if (ret == 0) {
  21187. ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21188. &key, endian);
  21189. if (ret == BAD_FUNC_ARG) {
  21190. ret = 0;
  21191. }
  21192. }
  21193. if (ret == 0) {
  21194. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21195. NULL, endian);
  21196. if (ret == BAD_FUNC_ARG) {
  21197. ret = 0;
  21198. }
  21199. }
  21200. if (ret == 0) {
  21201. ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21202. &key, endian);
  21203. if (ret == ECC_BAD_ARG_E) {
  21204. ret = 0;
  21205. }
  21206. }
  21207. if (ret == 0) {
  21208. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21209. &key, endian);
  21210. if (ret == ECC_BAD_ARG_E) {
  21211. ret = 0;
  21212. }
  21213. }
  21214. if (ret == 0) {
  21215. ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21216. &key, EC448_LITTLE_ENDIAN);
  21217. }
  21218. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21219. ret = WOLFSSL_FATAL_ERROR;
  21220. }
  21221. printf(resultFmt, ret == 0 ? passed : failed);
  21222. fflush(stdout);
  21223. wc_curve448_free(&key);
  21224. #endif
  21225. return ret;
  21226. } /*END test_wc_curve448_import_private_raw_ex*/
  21227. /*
  21228. * Testing test_curve448_export_key_raw
  21229. */
  21230. static int test_wc_curve448_export_key_raw(void)
  21231. {
  21232. int ret = 0;
  21233. #if defined(HAVE_CURVE448)
  21234. WC_RNG rng;
  21235. curve448_key key;
  21236. byte priv[CURVE448_KEY_SIZE];
  21237. byte pub[CURVE448_KEY_SIZE];
  21238. word32 privSz = sizeof(priv);
  21239. word32 pubSz = sizeof(pub);
  21240. printf(testingFmt, "wc_curve448_export_key_raw()");
  21241. ret = wc_curve448_init(&key);
  21242. if (ret == 0) {
  21243. ret = wc_InitRng(&rng);
  21244. }
  21245. if (ret == 0) {
  21246. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21247. if (ret == 0) {
  21248. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21249. }
  21250. if (ret == 0) {
  21251. ret = wc_curve448_export_public(&key, pub, &pubSz);
  21252. }
  21253. if (ret == 0) {
  21254. ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
  21255. }
  21256. }
  21257. printf(resultFmt, ret == 0 ? passed : failed);
  21258. fflush(stdout);
  21259. wc_curve448_free(&key);
  21260. wc_FreeRng(&rng);
  21261. #endif
  21262. return ret;
  21263. }/*END test_wc_curve448_import_private_raw_ex*/
  21264. /*
  21265. * Testing test_wc_curve448_import_private
  21266. */
  21267. static int test_wc_curve448_import_private(void)
  21268. {
  21269. int ret = 0;
  21270. #if defined(HAVE_CURVE448)
  21271. curve448_key key;
  21272. WC_RNG rng;
  21273. byte priv[CURVE448_KEY_SIZE];
  21274. word32 privSz = sizeof(priv);
  21275. printf(testingFmt, "wc_curve448_import_private()");
  21276. ret = wc_curve448_init(&key);
  21277. if (ret == 0) {
  21278. ret = wc_InitRng(&rng);
  21279. }
  21280. if (ret == 0) {
  21281. ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
  21282. if (ret == 0) {
  21283. ret = wc_curve448_export_private_raw(&key, priv, &privSz);
  21284. }
  21285. }
  21286. if (ret == 0) {
  21287. ret = wc_curve448_import_private(priv, privSz, &key);
  21288. }
  21289. printf(resultFmt, ret == 0 ? passed : failed);
  21290. fflush(stdout);
  21291. wc_curve448_free(&key);
  21292. wc_FreeRng(&rng);
  21293. #endif
  21294. return ret;
  21295. } /*END test_wc_curve448_import*/
  21296. /*
  21297. * Testing test_wc_curve448_size.
  21298. */
  21299. static int test_wc_curve448_size(void)
  21300. {
  21301. int ret = 0;
  21302. #if defined(HAVE_CURVE448)
  21303. curve448_key key;
  21304. printf(testingFmt, "wc_curve448_size()");
  21305. ret = wc_curve448_init(&key);
  21306. /* Test good args for wc_curve448_size */
  21307. if (ret == 0) {
  21308. ret = wc_curve448_size(&key);
  21309. }
  21310. /* Test bad args for wc_curve448_size */
  21311. if (ret != 0) {
  21312. ret = wc_curve448_size(NULL);
  21313. }
  21314. printf(resultFmt, ret == 0 ? passed : failed);
  21315. fflush(stdout);
  21316. wc_curve448_free(&key);
  21317. #endif
  21318. return ret;
  21319. } /* END test_wc_curve448_size*/
  21320. /*
  21321. * Testing wc_ecc_make_key.
  21322. */
  21323. static int test_wc_ecc_make_key(void)
  21324. {
  21325. int ret = 0;
  21326. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21327. WC_RNG rng;
  21328. ecc_key key;
  21329. printf(testingFmt, "wc_ecc_make_key()");
  21330. ret = wc_InitRng(&rng);
  21331. if (ret != 0)
  21332. return ret;
  21333. ret = wc_ecc_init(&key);
  21334. if (ret == 0) {
  21335. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21336. #if defined(WOLFSSL_ASYNC_CRYPT)
  21337. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21338. #endif
  21339. /* Pass in bad args. */
  21340. if (ret == 0) {
  21341. ret = wc_ecc_make_key(NULL, KEY14, &key);
  21342. if (ret == BAD_FUNC_ARG) {
  21343. ret = wc_ecc_make_key(&rng, KEY14, NULL);
  21344. }
  21345. if (ret == BAD_FUNC_ARG) {
  21346. ret = 0;
  21347. } else if (ret == 0) {
  21348. ret = WOLFSSL_FATAL_ERROR;
  21349. }
  21350. }
  21351. wc_ecc_free(&key);
  21352. }
  21353. if (wc_FreeRng(&rng) != 0 && ret == 0) {
  21354. ret = WOLFSSL_FATAL_ERROR;
  21355. }
  21356. #ifdef FP_ECC
  21357. wc_ecc_fp_free();
  21358. #endif
  21359. printf(resultFmt, ret == 0 ? passed : failed);
  21360. fflush(stdout);
  21361. #endif
  21362. return ret;
  21363. } /* END test_wc_ecc_make_key */
  21364. /*
  21365. * Testing wc_ecc_init()
  21366. */
  21367. static int test_wc_ecc_init(void)
  21368. {
  21369. int ret = 0;
  21370. #ifdef HAVE_ECC
  21371. ecc_key key;
  21372. printf(testingFmt, "wc_ecc_init()");
  21373. ret = wc_ecc_init(&key);
  21374. /* Pass in bad args. */
  21375. if (ret == 0) {
  21376. ret = wc_ecc_init(NULL);
  21377. if (ret == BAD_FUNC_ARG) {
  21378. ret = 0;
  21379. } else if (ret == 0) {
  21380. ret = WOLFSSL_FATAL_ERROR;
  21381. }
  21382. }
  21383. printf(resultFmt, ret == 0 ? passed : failed);
  21384. fflush(stdout);
  21385. wc_ecc_free(&key);
  21386. #endif
  21387. return ret;
  21388. } /* END test_wc_ecc_init */
  21389. /*
  21390. * Testing wc_ecc_check_key()
  21391. */
  21392. static int test_wc_ecc_check_key(void)
  21393. {
  21394. int ret = 0;
  21395. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21396. WC_RNG rng;
  21397. ecc_key key;
  21398. XMEMSET(&rng, 0, sizeof(rng));
  21399. XMEMSET(&key, 0, sizeof(key));
  21400. ret = wc_InitRng(&rng);
  21401. if (ret == 0) {
  21402. ret = wc_ecc_init(&key);
  21403. if (ret == 0) {
  21404. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21405. #if defined(WOLFSSL_ASYNC_CRYPT)
  21406. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21407. #endif
  21408. }
  21409. }
  21410. printf(testingFmt, "wc_ecc_check_key()");
  21411. if (ret == 0) {
  21412. ret = wc_ecc_check_key(&key);
  21413. }
  21414. /* Pass in bad args. */
  21415. if (ret == 0) {
  21416. ret = wc_ecc_check_key(NULL);
  21417. if (ret == BAD_FUNC_ARG) {
  21418. ret = 0;
  21419. } else if (ret == 0) {
  21420. ret = WOLFSSL_FATAL_ERROR;
  21421. }
  21422. }
  21423. printf(resultFmt, ret == 0 ? passed : failed);
  21424. fflush(stdout);
  21425. if (wc_FreeRng(&rng) && ret == 0) {
  21426. ret = WOLFSSL_FATAL_ERROR;
  21427. }
  21428. wc_ecc_free(&key);
  21429. #ifdef FP_ECC
  21430. wc_ecc_fp_free();
  21431. #endif
  21432. #endif
  21433. return ret;
  21434. } /* END test_wc_ecc_check_key */
  21435. /*
  21436. * Testing wc_ecc_get_generator()
  21437. */
  21438. static int test_wc_ecc_get_generator(void)
  21439. {
  21440. int ret = 0;
  21441. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21442. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21443. ecc_point* pt;
  21444. printf(testingFmt, "wc_ecc_new_point()");
  21445. pt = wc_ecc_new_point();
  21446. if (!pt) {
  21447. ret = WOLFSSL_FATAL_ERROR;
  21448. }
  21449. printf(testingFmt, "wc_ecc_get_generator()");
  21450. if (ret == 0) {
  21451. ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21452. }
  21453. /* Test bad args. */
  21454. if (ret == MP_OKAY) {
  21455. /* Returns Zero for bad arg. */
  21456. ret = wc_ecc_get_generator(pt, -1);
  21457. if (ret != MP_OKAY)
  21458. wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
  21459. if (ret != MP_OKAY)
  21460. wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
  21461. * increase this number */
  21462. if (ret != MP_OKAY)
  21463. wc_ecc_get_generator(NULL, -1);
  21464. ret = ret == MP_OKAY ? WOLFSSL_FATAL_ERROR : 0;
  21465. }
  21466. printf(resultFmt, ret == 0 ? passed : failed);
  21467. fflush(stdout);
  21468. wc_ecc_del_point(pt);
  21469. #endif
  21470. return ret;
  21471. } /* END test_wc_ecc_get_generator */
  21472. /*
  21473. * Testing wc_ecc_size()
  21474. */
  21475. static int test_wc_ecc_size(void)
  21476. {
  21477. int ret = 0;
  21478. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21479. WC_RNG rng;
  21480. ecc_key key;
  21481. XMEMSET(&rng, 0, sizeof(rng));
  21482. XMEMSET(&key, 0, sizeof(key));
  21483. ret = wc_InitRng(&rng);
  21484. if (ret == 0) {
  21485. ret = wc_ecc_init(&key);
  21486. if (ret == 0) {
  21487. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21488. #if defined(WOLFSSL_ASYNC_CRYPT)
  21489. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21490. #endif
  21491. }
  21492. }
  21493. printf(testingFmt, "wc_ecc_size()");
  21494. if (ret == 0) {
  21495. ret = wc_ecc_size(&key);
  21496. if (ret == KEY14) {
  21497. ret = 0;
  21498. } else if (ret == 0){
  21499. ret = WOLFSSL_FATAL_ERROR;
  21500. }
  21501. }
  21502. /* Test bad args. */
  21503. if (ret == 0) {
  21504. /* Returns Zero for bad arg. */
  21505. ret = wc_ecc_size(NULL);
  21506. }
  21507. printf(resultFmt, ret == 0 ? passed : failed);
  21508. fflush(stdout);
  21509. if (wc_FreeRng(&rng) && ret == 0) {
  21510. ret = WOLFSSL_FATAL_ERROR;
  21511. }
  21512. wc_ecc_free(&key);
  21513. #endif
  21514. return ret;
  21515. } /* END test_wc_ecc_size */
  21516. static int test_wc_ecc_params(void)
  21517. {
  21518. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21519. It was added after certifications */
  21520. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21521. const ecc_set_type* ecc_set;
  21522. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21523. /* Test for SECP256R1 curve */
  21524. int curve_id = ECC_SECP256R1;
  21525. int curve_idx = wc_ecc_get_curve_idx(curve_id);
  21526. AssertIntNE(curve_idx, ECC_CURVE_INVALID);
  21527. ecc_set = wc_ecc_get_curve_params(curve_idx);
  21528. AssertNotNull(ecc_set);
  21529. AssertIntEQ(ecc_set->id, curve_id);
  21530. #endif
  21531. /* Test case when SECP256R1 is not enabled */
  21532. /* Test that we get curve params for index 0 */
  21533. ecc_set = wc_ecc_get_curve_params(0);
  21534. AssertNotNull(ecc_set);
  21535. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21536. return 0;
  21537. }
  21538. /*
  21539. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21540. */
  21541. static int test_wc_ecc_signVerify_hash(void)
  21542. {
  21543. int ret = 0;
  21544. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21545. WC_RNG rng;
  21546. ecc_key key;
  21547. int signH = WOLFSSL_FATAL_ERROR;
  21548. #ifdef HAVE_ECC_VERIFY
  21549. int verifyH = WOLFSSL_FATAL_ERROR;
  21550. int verify = 0;
  21551. #endif
  21552. word32 siglen = ECC_BUFSIZE;
  21553. byte sig[ECC_BUFSIZE];
  21554. byte adjustedSig[ECC_BUFSIZE+1];
  21555. byte digest[] = TEST_STRING;
  21556. word32 digestlen = (word32)TEST_STRING_SZ;
  21557. /* Init stack var */
  21558. XMEMSET(sig, 0, siglen);
  21559. XMEMSET(&key, 0, sizeof(key));
  21560. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21561. /* Init structs. */
  21562. ret = wc_InitRng(&rng);
  21563. if (ret == 0) {
  21564. ret = wc_ecc_init(&key);
  21565. if (ret == 0) {
  21566. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21567. #if defined(WOLFSSL_ASYNC_CRYPT)
  21568. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21569. #endif
  21570. }
  21571. }
  21572. printf(testingFmt, "wc_ecc_sign_hash()");
  21573. if (ret == 0) {
  21574. ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
  21575. }
  21576. /* Check bad args. */
  21577. if (ret == 0) {
  21578. signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
  21579. if (signH == ECC_BAD_ARG_E) {
  21580. signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
  21581. &rng, &key);
  21582. }
  21583. if (signH == ECC_BAD_ARG_E) {
  21584. signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
  21585. &rng, &key);
  21586. }
  21587. if (signH == ECC_BAD_ARG_E) {
  21588. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21589. NULL, &key);
  21590. }
  21591. if (signH == ECC_BAD_ARG_E) {
  21592. signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
  21593. &rng, NULL);
  21594. }
  21595. if (signH == ECC_BAD_ARG_E) {
  21596. signH = 0;
  21597. } else if (ret == 0) {
  21598. signH = WOLFSSL_FATAL_ERROR;
  21599. }
  21600. }
  21601. printf(resultFmt, signH == 0 ? passed : failed);
  21602. #ifdef HAVE_ECC_VERIFY
  21603. printf(testingFmt, "wc_ecc_verify_hash()");
  21604. ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
  21605. if (verify != 1 && ret == 0) {
  21606. ret = WOLFSSL_FATAL_ERROR;
  21607. }
  21608. /* test check on length of signature passed in */
  21609. XMEMCPY(adjustedSig, sig, siglen);
  21610. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21611. #ifndef NO_STRICT_ECDSA_LEN
  21612. AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21613. &verify, &key), 0);
  21614. #else
  21615. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21616. * is allowed */
  21617. AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21618. &verify, &key), 0);
  21619. #endif
  21620. /* Test bad args. */
  21621. if (ret == 0) {
  21622. verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
  21623. &verify, &key);
  21624. if (verifyH == ECC_BAD_ARG_E) {
  21625. verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
  21626. &verify, &key);
  21627. }
  21628. if (verifyH == ECC_BAD_ARG_E) {
  21629. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21630. NULL, &key);
  21631. }
  21632. if (verifyH == ECC_BAD_ARG_E) {
  21633. verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
  21634. &verify, NULL);
  21635. }
  21636. if (verifyH == ECC_BAD_ARG_E) {
  21637. verifyH = 0;
  21638. } else if (ret == 0) {
  21639. verifyH = WOLFSSL_FATAL_ERROR;
  21640. }
  21641. }
  21642. printf(resultFmt, verifyH == 0 ? passed : failed);
  21643. #endif /* HAVE_ECC_VERIFY */
  21644. if (wc_FreeRng(&rng) && ret == 0) {
  21645. ret = WOLFSSL_FATAL_ERROR;
  21646. }
  21647. wc_ecc_free(&key);
  21648. #ifdef FP_ECC
  21649. wc_ecc_fp_free();
  21650. #endif
  21651. #endif
  21652. return ret;
  21653. } /* END test_wc_ecc_sign_hash */
  21654. /*
  21655. * Testing wc_ecc_shared_secret()
  21656. */
  21657. static int test_wc_ecc_shared_secret(void)
  21658. {
  21659. int ret = 0;
  21660. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21661. ecc_key key, pubKey;
  21662. WC_RNG rng;
  21663. byte out[KEY32];
  21664. int keySz = sizeof(out);
  21665. word32 outlen = (word32)sizeof(out);
  21666. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21667. const char* qx =
  21668. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21669. const char* qy =
  21670. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21671. const char* d =
  21672. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21673. const char* curveName = "SECP256R1";
  21674. const byte expected_shared_secret[] =
  21675. {
  21676. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21677. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21678. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21679. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21680. };
  21681. #endif
  21682. PRIVATE_KEY_UNLOCK();
  21683. /* Initialize variables. */
  21684. XMEMSET(out, 0, keySz);
  21685. XMEMSET(&rng, 0, sizeof(rng));
  21686. XMEMSET(&key, 0, sizeof(key));
  21687. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21688. ret = wc_InitRng(&rng);
  21689. if (ret == 0) {
  21690. ret = wc_ecc_init(&key);
  21691. if (ret == 0) {
  21692. ret = wc_ecc_init(&pubKey);
  21693. }
  21694. }
  21695. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21696. if (ret == 0) {
  21697. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  21698. }
  21699. if (ret == 0) {
  21700. ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
  21701. }
  21702. #else
  21703. if (ret == 0) {
  21704. ret = wc_ecc_make_key(&rng, keySz, &key);
  21705. #if defined(WOLFSSL_ASYNC_CRYPT)
  21706. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21707. #endif
  21708. }
  21709. if (ret == 0) {
  21710. ret = wc_ecc_make_key(&rng, keySz, &pubKey);
  21711. #if defined(WOLFSSL_ASYNC_CRYPT)
  21712. ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
  21713. #endif
  21714. }
  21715. #endif
  21716. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21717. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21718. !defined(HAVE_SELFTEST)
  21719. if (ret == 0) {
  21720. ret = wc_ecc_set_rng(&key, &rng);
  21721. }
  21722. #endif
  21723. printf(testingFmt, "wc_ecc_shared_secret()");
  21724. if (ret == 0) {
  21725. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21726. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21727. if (ret == 0) {
  21728. if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
  21729. ret = WOLFSSL_FATAL_ERROR;
  21730. }
  21731. }
  21732. #endif
  21733. /* Test bad args. */
  21734. if (ret == 0) {
  21735. ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
  21736. if (ret == BAD_FUNC_ARG) {
  21737. ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
  21738. }
  21739. if (ret == BAD_FUNC_ARG) {
  21740. ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
  21741. }
  21742. if (ret == BAD_FUNC_ARG) {
  21743. ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
  21744. }
  21745. if (ret == BAD_FUNC_ARG) {
  21746. /* Invalid length */
  21747. outlen = 1;
  21748. ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
  21749. }
  21750. if (ret == BUFFER_E) {
  21751. ret = 0;
  21752. } else if (ret == 0) {
  21753. ret = WOLFSSL_FATAL_ERROR;
  21754. }
  21755. }
  21756. }
  21757. printf(resultFmt, ret == 0 ? passed : failed);
  21758. fflush(stdout);
  21759. if (wc_FreeRng(&rng) && ret == 0) {
  21760. ret = WOLFSSL_FATAL_ERROR;
  21761. }
  21762. wc_ecc_free(&key);
  21763. wc_ecc_free(&pubKey);
  21764. #ifdef FP_ECC
  21765. wc_ecc_fp_free();
  21766. #endif
  21767. #endif
  21768. PRIVATE_KEY_LOCK();
  21769. return ret;
  21770. } /* END tests_wc_ecc_shared_secret */
  21771. /*
  21772. * testint wc_ecc_export_x963()
  21773. */
  21774. static int test_wc_ecc_export_x963(void)
  21775. {
  21776. int ret = 0;
  21777. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21778. ecc_key key;
  21779. WC_RNG rng;
  21780. byte out[ECC_ASN963_MAX_BUF_SZ];
  21781. word32 outlen = sizeof(out);
  21782. PRIVATE_KEY_UNLOCK();
  21783. /* Initialize variables. */
  21784. XMEMSET(out, 0, outlen);
  21785. XMEMSET(&rng, 0, sizeof(rng));
  21786. XMEMSET(&key, 0, sizeof(key));
  21787. ret = wc_InitRng(&rng);
  21788. if (ret == 0) {
  21789. ret = wc_ecc_init(&key);
  21790. if (ret == 0) {
  21791. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21792. #if defined(WOLFSSL_ASYNC_CRYPT)
  21793. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21794. #endif
  21795. }
  21796. }
  21797. printf(testingFmt, "wc_ecc_export_x963()");
  21798. if (ret == 0) {
  21799. ret = wc_ecc_export_x963(&key, out, &outlen);
  21800. }
  21801. /* Test bad args. */
  21802. if (ret == 0) {
  21803. ret = wc_ecc_export_x963(NULL, out, &outlen);
  21804. if (ret == ECC_BAD_ARG_E) {
  21805. ret = wc_ecc_export_x963(&key, NULL, &outlen);
  21806. }
  21807. if (ret == LENGTH_ONLY_E) {
  21808. ret = wc_ecc_export_x963(&key, out, NULL);
  21809. }
  21810. if (ret == ECC_BAD_ARG_E) {
  21811. key.idx = -4;
  21812. ret = wc_ecc_export_x963(&key, out, &outlen);
  21813. }
  21814. if (ret == ECC_BAD_ARG_E) {
  21815. ret = 0;
  21816. } else {
  21817. ret = WOLFSSL_FATAL_ERROR;
  21818. }
  21819. }
  21820. printf(resultFmt, ret == 0 ? passed : failed);
  21821. fflush(stdout);
  21822. if (wc_FreeRng(&rng) && ret == 0) {
  21823. ret = WOLFSSL_FATAL_ERROR;
  21824. }
  21825. wc_ecc_free(&key);
  21826. #ifdef FP_ECC
  21827. wc_ecc_fp_free();
  21828. #endif
  21829. #endif
  21830. PRIVATE_KEY_LOCK();
  21831. return ret;
  21832. } /* END test_wc_ecc_export_x963 */
  21833. /*
  21834. * Testing wc_ecc_export_x963_ex()
  21835. * compile with --enable-compkey will use compression.
  21836. */
  21837. static int test_wc_ecc_export_x963_ex(void)
  21838. {
  21839. int ret = 0;
  21840. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21841. ecc_key key;
  21842. WC_RNG rng;
  21843. byte out[ECC_ASN963_MAX_BUF_SZ];
  21844. word32 outlen = sizeof(out);
  21845. #ifdef HAVE_COMP_KEY
  21846. word32 badOutLen = 5;
  21847. #endif
  21848. /* Init stack variables. */
  21849. XMEMSET(out, 0, outlen);
  21850. XMEMSET(&rng, 0, sizeof(rng));
  21851. XMEMSET(&key, 0, sizeof(key));
  21852. ret = wc_InitRng(&rng);
  21853. if (ret == 0) {
  21854. ret = wc_ecc_init(&key);
  21855. if (ret == 0) {
  21856. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21857. #if defined(WOLFSSL_ASYNC_CRYPT)
  21858. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21859. #endif
  21860. }
  21861. }
  21862. printf(testingFmt, "wc_ecc_export_x963_ex()");
  21863. #ifdef HAVE_COMP_KEY
  21864. if (ret == 0) {
  21865. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21866. }
  21867. #else
  21868. if (ret == 0) {
  21869. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21870. }
  21871. #endif
  21872. /* Test bad args. */
  21873. #ifdef HAVE_COMP_KEY
  21874. if (ret == 0) {
  21875. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
  21876. if (ret == BAD_FUNC_ARG) {
  21877. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
  21878. }
  21879. if (ret == BAD_FUNC_ARG) {
  21880. ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
  21881. }
  21882. if (ret == BAD_FUNC_ARG) {
  21883. ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
  21884. }
  21885. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21886. if (ret == BUFFER_E)
  21887. #else
  21888. if (ret == LENGTH_ONLY_E)
  21889. #endif
  21890. {
  21891. key.idx = -4;
  21892. ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
  21893. }
  21894. if (ret == ECC_BAD_ARG_E) {
  21895. ret = 0;
  21896. } else {
  21897. ret = WOLFSSL_FATAL_ERROR;
  21898. }
  21899. }
  21900. #else
  21901. if (ret == 0) {
  21902. ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
  21903. if (ret == BAD_FUNC_ARG) {
  21904. ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
  21905. }
  21906. if (ret == BAD_FUNC_ARG) {
  21907. ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
  21908. }
  21909. if (ret == NOT_COMPILED_IN) {
  21910. ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
  21911. }
  21912. if (ret == BAD_FUNC_ARG) {
  21913. key.idx = -4;
  21914. ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
  21915. }
  21916. if (ret == ECC_BAD_ARG_E) {
  21917. ret = 0;
  21918. } else if (ret == 0) {
  21919. ret = WOLFSSL_FATAL_ERROR;
  21920. }
  21921. }
  21922. #endif
  21923. printf(resultFmt, ret == 0 ? passed : failed);
  21924. fflush(stdout);
  21925. if (wc_FreeRng(&rng) && ret == 0) {
  21926. ret = WOLFSSL_FATAL_ERROR;
  21927. }
  21928. wc_ecc_free(&key);
  21929. #ifdef FP_ECC
  21930. wc_ecc_fp_free();
  21931. #endif
  21932. #endif
  21933. return ret;
  21934. } /* END test_wc_ecc_export_x963_ex */
  21935. /*
  21936. * testing wc_ecc_import_x963()
  21937. */
  21938. static int test_wc_ecc_import_x963(void)
  21939. {
  21940. int ret = 0;
  21941. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21942. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21943. ecc_key pubKey, key;
  21944. WC_RNG rng;
  21945. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21946. word32 x963Len = (word32)sizeof(x963);
  21947. /* Init stack variables. */
  21948. XMEMSET(x963, 0, x963Len);
  21949. XMEMSET(&rng, 0, sizeof(rng));
  21950. XMEMSET(&key, 0, sizeof(key));
  21951. XMEMSET(&pubKey, 0, sizeof(pubKey));
  21952. ret = wc_InitRng(&rng);
  21953. if (ret == 0) {
  21954. ret = wc_ecc_init(&pubKey);
  21955. if (ret == 0) {
  21956. ret = wc_ecc_init(&key);
  21957. }
  21958. if (ret == 0) {
  21959. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21960. #if defined(WOLFSSL_ASYNC_CRYPT)
  21961. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21962. #endif
  21963. }
  21964. if (ret == 0) {
  21965. PRIVATE_KEY_UNLOCK();
  21966. ret = wc_ecc_export_x963(&key, x963, &x963Len);
  21967. PRIVATE_KEY_LOCK();
  21968. }
  21969. }
  21970. printf(testingFmt, "wc_ecc_import_x963()");
  21971. if (ret == 0) {
  21972. ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
  21973. }
  21974. /* Test bad args. */
  21975. if (ret == 0) {
  21976. ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
  21977. if (ret == BAD_FUNC_ARG) {
  21978. ret = wc_ecc_import_x963(x963, x963Len, NULL);
  21979. }
  21980. if (ret == BAD_FUNC_ARG) {
  21981. ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
  21982. }
  21983. if (ret == ECC_BAD_ARG_E) {
  21984. ret = 0;
  21985. } else if (ret == 0) {
  21986. ret = WOLFSSL_FATAL_ERROR;
  21987. }
  21988. }
  21989. printf(resultFmt, ret == 0 ? passed : failed);
  21990. fflush(stdout);
  21991. if (wc_FreeRng(&rng) && ret == 0) {
  21992. ret = WOLFSSL_FATAL_ERROR;
  21993. }
  21994. wc_ecc_free(&key);
  21995. wc_ecc_free(&pubKey);
  21996. #ifdef FP_ECC
  21997. wc_ecc_fp_free();
  21998. #endif
  21999. #endif
  22000. return ret;
  22001. } /* END wc_ecc_import_x963 */
  22002. /*
  22003. * testing wc_ecc_import_private_key()
  22004. */
  22005. static int ecc_import_private_key (void)
  22006. {
  22007. int ret = 0;
  22008. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  22009. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22010. ecc_key key, keyImp;
  22011. WC_RNG rng;
  22012. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  22013. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  22014. word32 privKeySz = (word32)sizeof(privKey);
  22015. word32 x963KeySz = (word32)sizeof(x963Key);
  22016. /* Init stack variables. */
  22017. XMEMSET(privKey, 0, privKeySz);
  22018. XMEMSET(x963Key, 0, x963KeySz);
  22019. XMEMSET(&rng, 0, sizeof(rng));
  22020. XMEMSET(&key, 0, sizeof(key));
  22021. XMEMSET(&keyImp, 0, sizeof(keyImp));
  22022. ret = wc_InitRng(&rng);
  22023. if (ret == 0) {
  22024. ret = wc_ecc_init(&key);
  22025. if (ret == 0) {
  22026. ret = wc_ecc_init(&keyImp);
  22027. }
  22028. if (ret == 0) {
  22029. ret = wc_ecc_make_key(&rng, KEY48, &key);
  22030. #if defined(WOLFSSL_ASYNC_CRYPT)
  22031. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22032. #endif
  22033. }
  22034. if (ret == 0) {
  22035. PRIVATE_KEY_UNLOCK();
  22036. ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
  22037. PRIVATE_KEY_LOCK();
  22038. }
  22039. if (ret == 0) {
  22040. ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
  22041. }
  22042. }
  22043. printf(testingFmt, "wc_ecc_import_private_key()");
  22044. if (ret == 0) {
  22045. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22046. x963KeySz, &keyImp);
  22047. }
  22048. /* Pass in bad args. */
  22049. if (ret == 0) {
  22050. ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  22051. x963KeySz, NULL);
  22052. if (ret == BAD_FUNC_ARG) {
  22053. ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
  22054. x963KeySz, &keyImp);
  22055. }
  22056. if (ret == BAD_FUNC_ARG) {
  22057. ret = 0;
  22058. } else if (ret == 0) {
  22059. ret = WOLFSSL_FATAL_ERROR;
  22060. }
  22061. }
  22062. printf(resultFmt, ret == 0 ? passed : failed);
  22063. fflush(stdout);
  22064. if (wc_FreeRng(&rng) && ret == 0) {
  22065. ret = WOLFSSL_FATAL_ERROR;
  22066. }
  22067. wc_ecc_free(&key);
  22068. wc_ecc_free(&keyImp);
  22069. #ifdef FP_ECC
  22070. wc_ecc_fp_free();
  22071. #endif
  22072. #endif
  22073. return ret;
  22074. } /* END wc_ecc_import_private_key */
  22075. /*
  22076. * Testing wc_ecc_export_private_only()
  22077. */
  22078. static int test_wc_ecc_export_private_only(void)
  22079. {
  22080. int ret = 0;
  22081. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  22082. ecc_key key;
  22083. WC_RNG rng;
  22084. byte out[ECC_PRIV_KEY_BUF];
  22085. word32 outlen = sizeof(out);
  22086. /* Init stack variables. */
  22087. XMEMSET(out, 0, outlen);
  22088. XMEMSET(&rng, 0, sizeof(rng));
  22089. XMEMSET(&key, 0, sizeof(key));
  22090. ret = wc_InitRng(&rng);
  22091. if (ret == 0) {
  22092. ret = wc_ecc_init(&key);
  22093. if (ret == 0) {
  22094. ret = wc_ecc_make_key(&rng, KEY32, &key);
  22095. #if defined(WOLFSSL_ASYNC_CRYPT)
  22096. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22097. #endif
  22098. }
  22099. }
  22100. printf(testingFmt, "wc_ecc_export_private_only()");
  22101. if (ret == 0) {
  22102. ret = wc_ecc_export_private_only(&key, out, &outlen);
  22103. }
  22104. /* Pass in bad args. */
  22105. if (ret == 0) {
  22106. ret = wc_ecc_export_private_only(NULL, out, &outlen);
  22107. if (ret == BAD_FUNC_ARG) {
  22108. ret = wc_ecc_export_private_only(&key, NULL, &outlen);
  22109. }
  22110. if (ret == BAD_FUNC_ARG) {
  22111. ret = wc_ecc_export_private_only(&key, out, NULL);
  22112. }
  22113. if (ret == BAD_FUNC_ARG) {
  22114. ret = 0;
  22115. } else if (ret == 0) {
  22116. ret = WOLFSSL_FATAL_ERROR;
  22117. }
  22118. }
  22119. printf(resultFmt, ret == 0 ? passed : failed);
  22120. fflush(stdout);
  22121. if (wc_FreeRng(&rng) && ret == 0) {
  22122. ret = WOLFSSL_FATAL_ERROR;
  22123. }
  22124. wc_ecc_free(&key);
  22125. #ifdef FP_ECC
  22126. wc_ecc_fp_free();
  22127. #endif
  22128. #endif
  22129. return ret;
  22130. } /* END test_wc_ecc_export_private_only */
  22131. /*
  22132. * Testing wc_ecc_rs_to_sig()
  22133. */
  22134. static int test_wc_ecc_rs_to_sig(void)
  22135. {
  22136. int ret = 0;
  22137. #if defined(HAVE_ECC) && !defined(NO_ASN)
  22138. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  22139. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  22140. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  22141. const char* zeroStr = "0";
  22142. byte sig[ECC_MAX_SIG_SIZE];
  22143. word32 siglen = (word32)sizeof(sig);
  22144. /*R and S max size is the order of curve. 2^192.*/
  22145. int keySz = KEY24;
  22146. byte r[KEY24];
  22147. byte s[KEY24];
  22148. word32 rlen = (word32)sizeof(r);
  22149. word32 slen = (word32)sizeof(s);
  22150. /* Init stack variables. */
  22151. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  22152. XMEMSET(r, 0, keySz);
  22153. XMEMSET(s, 0, keySz);
  22154. printf(testingFmt, "wc_ecc_rs_to_sig()");
  22155. ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
  22156. /* Test bad args. */
  22157. if (ret == 0) {
  22158. ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
  22159. if (ret == ECC_BAD_ARG_E) {
  22160. ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
  22161. }
  22162. if (ret == ECC_BAD_ARG_E) {
  22163. ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
  22164. }
  22165. if (ret == ECC_BAD_ARG_E) {
  22166. ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
  22167. }
  22168. if (ret == ECC_BAD_ARG_E) {
  22169. ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
  22170. }
  22171. if (ret == MP_ZERO_E) {
  22172. ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
  22173. }
  22174. if (ret == MP_ZERO_E) {
  22175. ret = 0;
  22176. } else {
  22177. ret = WOLFSSL_FATAL_ERROR;
  22178. }
  22179. }
  22180. printf(resultFmt, ret == 0 ? passed : failed);
  22181. fflush(stdout);
  22182. printf(testingFmt, "wc_ecc_sig_to_rs()");
  22183. if (ret == 0) {
  22184. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
  22185. }
  22186. /* Test bad args. */
  22187. if (ret == 0) {
  22188. ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
  22189. if (ret == ECC_BAD_ARG_E) {
  22190. ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
  22191. }
  22192. if (ret == ECC_BAD_ARG_E) {
  22193. ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
  22194. }
  22195. if (ret == ECC_BAD_ARG_E) {
  22196. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
  22197. }
  22198. if (ret == ECC_BAD_ARG_E) {
  22199. ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
  22200. }
  22201. if (ret == ECC_BAD_ARG_E) {
  22202. ret = 0;
  22203. } else if (ret == 0) {
  22204. ret = WOLFSSL_FATAL_ERROR;
  22205. }
  22206. }
  22207. printf(resultFmt, ret == 0 ? passed : failed);
  22208. fflush(stdout);
  22209. #endif
  22210. return ret;
  22211. } /* END test_wc_ecc_rs_to_sig */
  22212. static int test_wc_ecc_import_raw(void)
  22213. {
  22214. int ret = 0;
  22215. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  22216. ecc_key key;
  22217. const char* qx =
  22218. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  22219. const char* qy =
  22220. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  22221. const char* d =
  22222. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  22223. const char* curveName = "SECP256R1";
  22224. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22225. const char* kNullStr = "";
  22226. #endif
  22227. ret = wc_ecc_init(&key);
  22228. printf(testingFmt, "wc_ecc_import_raw()");
  22229. if (ret == 0) {
  22230. ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
  22231. }
  22232. /* Test bad args. */
  22233. if (ret == 0) {
  22234. ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
  22235. if (ret == BAD_FUNC_ARG) {
  22236. ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
  22237. }
  22238. if (ret == BAD_FUNC_ARG) {
  22239. ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
  22240. }
  22241. if (ret == BAD_FUNC_ARG) {
  22242. ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
  22243. }
  22244. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22245. if (ret == BAD_FUNC_ARG) {
  22246. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22247. wc_ecc_free(&key);
  22248. #endif
  22249. ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
  22250. if (ret == ECC_INF_E)
  22251. ret = BAD_FUNC_ARG; /* This is expected by other tests */
  22252. }
  22253. #endif
  22254. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  22255. if (ret == BAD_FUNC_ARG) {
  22256. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22257. wc_ecc_free(&key);
  22258. #endif
  22259. ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
  22260. }
  22261. if (ret == BAD_FUNC_ARG) {
  22262. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22263. wc_ecc_free(&key);
  22264. #endif
  22265. ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
  22266. }
  22267. #endif
  22268. if (ret == BAD_FUNC_ARG) {
  22269. ret = 0;
  22270. }
  22271. }
  22272. printf(resultFmt, ret == 0 ? passed : failed);
  22273. fflush(stdout);
  22274. wc_ecc_free(&key);
  22275. #endif
  22276. return ret;
  22277. } /* END test_wc_ecc_import_raw */
  22278. static int test_wc_ecc_import_unsigned(void)
  22279. {
  22280. int ret = 0;
  22281. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22282. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  22283. ecc_key key;
  22284. const byte qx[] = {
  22285. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22286. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22287. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22288. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22289. };
  22290. const byte qy[] = {
  22291. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22292. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22293. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22294. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22295. };
  22296. const byte d[] = {
  22297. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22298. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22299. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22300. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22301. };
  22302. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22303. const byte nullBytes[32] = {0};
  22304. #endif
  22305. int curveId = ECC_SECP256R1;
  22306. ret = wc_ecc_init(&key);
  22307. printf(testingFmt, "wc_ecc_import_unsigned()");
  22308. if (ret == 0) {
  22309. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22310. curveId);
  22311. }
  22312. /* Test bad args. */
  22313. if (ret == 0) {
  22314. ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22315. curveId);
  22316. if (ret == BAD_FUNC_ARG) {
  22317. ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22318. curveId);
  22319. }
  22320. if (ret == BAD_FUNC_ARG) {
  22321. ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22322. curveId);
  22323. }
  22324. if (ret == BAD_FUNC_ARG) {
  22325. ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22326. ECC_CURVE_INVALID);
  22327. }
  22328. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22329. if (ret == BAD_FUNC_ARG) {
  22330. ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22331. (byte*)nullBytes, (byte*)nullBytes, curveId);
  22332. }
  22333. #endif
  22334. if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
  22335. ret = 0;
  22336. }
  22337. }
  22338. printf(resultFmt, ret == 0 ? passed : failed);
  22339. fflush(stdout);
  22340. wc_ecc_free(&key);
  22341. #endif
  22342. return ret;
  22343. } /* END test_wc_ecc_import_unsigned */
  22344. /*
  22345. * Testing wc_ecc_sig_size()
  22346. */
  22347. static int test_wc_ecc_sig_size(void)
  22348. {
  22349. int ret = 0;
  22350. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22351. ecc_key key;
  22352. WC_RNG rng;
  22353. int keySz = KEY16;
  22354. XMEMSET(&rng, 0, sizeof(rng));
  22355. XMEMSET(&key, 0, sizeof(key));
  22356. ret = wc_InitRng(&rng);
  22357. if (ret == 0) {
  22358. ret = wc_ecc_init(&key);
  22359. if (ret == 0) {
  22360. ret = wc_ecc_make_key(&rng, keySz, &key);
  22361. #if defined(WOLFSSL_ASYNC_CRYPT)
  22362. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22363. #endif
  22364. }
  22365. }
  22366. printf(testingFmt, "wc_ecc_sig_size()");
  22367. if (ret == 0) {
  22368. ret = wc_ecc_sig_size(&key);
  22369. if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
  22370. ret = 0;
  22371. }
  22372. }
  22373. printf(resultFmt, ret == 0 ? passed : failed);
  22374. fflush(stdout);
  22375. if (wc_FreeRng(&rng) && ret == 0) {
  22376. ret = WOLFSSL_FATAL_ERROR;
  22377. }
  22378. wc_ecc_free(&key);
  22379. #endif
  22380. return ret;
  22381. } /* END test_wc_ecc_sig_size */
  22382. /*
  22383. * Testing wc_ecc_ctx_new()
  22384. */
  22385. static int test_wc_ecc_ctx_new(void)
  22386. {
  22387. int ret = 0;
  22388. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22389. WC_RNG rng;
  22390. ecEncCtx* cli = NULL;
  22391. ecEncCtx* srv = NULL;
  22392. ret = wc_InitRng(&rng);
  22393. printf(testingFmt, "wc_ecc_ctx_new()");
  22394. if (ret == 0) {
  22395. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
  22396. srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
  22397. }
  22398. if (ret == 0 && (cli == NULL || srv == NULL)) {
  22399. ret = WOLFSSL_FATAL_ERROR;
  22400. }
  22401. wc_ecc_ctx_free(cli);
  22402. wc_ecc_ctx_free(srv);
  22403. /* Test bad args. */
  22404. if (ret == 0) {
  22405. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22406. cli = wc_ecc_ctx_new(0, &rng);
  22407. if (cli != NULL) {
  22408. ret = WOLFSSL_FATAL_ERROR;
  22409. }
  22410. cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
  22411. if (cli != NULL) {
  22412. ret = WOLFSSL_FATAL_ERROR;
  22413. }
  22414. }
  22415. printf(resultFmt, ret == 0 ? passed : failed);
  22416. fflush(stdout);
  22417. if (wc_FreeRng(&rng) && ret == 0) {
  22418. ret = WOLFSSL_FATAL_ERROR;
  22419. }
  22420. wc_ecc_ctx_free(cli);
  22421. #endif
  22422. return ret;
  22423. } /* END test_wc_ecc_ctx_new */
  22424. /*
  22425. * Tesing wc_ecc_reset()
  22426. */
  22427. static int test_wc_ecc_ctx_reset(void)
  22428. {
  22429. int ret = 0;
  22430. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22431. ecEncCtx* ctx = NULL;
  22432. WC_RNG rng;
  22433. ret = wc_InitRng(&rng);
  22434. if (ret == 0) {
  22435. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
  22436. ret = WOLFSSL_FATAL_ERROR;
  22437. }
  22438. }
  22439. printf(testingFmt, "wc_ecc_ctx_reset()");
  22440. if (ret == 0) {
  22441. ret = wc_ecc_ctx_reset(ctx, &rng);
  22442. }
  22443. /* Pass in bad args. */
  22444. if (ret == 0) {
  22445. ret = wc_ecc_ctx_reset(NULL, &rng);
  22446. if (ret == BAD_FUNC_ARG) {
  22447. ret = wc_ecc_ctx_reset(ctx, NULL);
  22448. }
  22449. if (ret == BAD_FUNC_ARG) {
  22450. ret = 0;
  22451. } else if (ret == 0) {
  22452. ret = WOLFSSL_FATAL_ERROR;
  22453. }
  22454. }
  22455. printf(resultFmt, ret == 0 ? passed : failed);
  22456. fflush(stdout);
  22457. if (wc_FreeRng(&rng) && ret == 0) {
  22458. ret = WOLFSSL_FATAL_ERROR;
  22459. }
  22460. wc_ecc_ctx_free(ctx);
  22461. #endif
  22462. return ret;
  22463. } /* END test_wc_ecc_ctx_reset */
  22464. /*
  22465. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22466. */
  22467. static int test_wc_ecc_ctx_set_peer_salt(void)
  22468. {
  22469. int ret = 0;
  22470. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22471. WC_RNG rng;
  22472. ecEncCtx* cliCtx = NULL;
  22473. ecEncCtx* servCtx = NULL;
  22474. const byte* cliSalt = NULL;
  22475. const byte* servSalt = NULL;
  22476. ret = wc_InitRng(&rng);
  22477. if (ret == 0) {
  22478. if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
  22479. ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
  22480. ret = WOLFSSL_FATAL_ERROR;
  22481. }
  22482. }
  22483. printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
  22484. /* Test bad args. */
  22485. if (ret == 0) {
  22486. cliSalt = wc_ecc_ctx_get_own_salt(NULL);
  22487. if (cliSalt != NULL) {
  22488. ret = WOLFSSL_FATAL_ERROR;
  22489. }
  22490. }
  22491. if (ret == 0) {
  22492. cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
  22493. servSalt = wc_ecc_ctx_get_own_salt(servCtx);
  22494. if (cliSalt == NULL || servSalt == NULL) {
  22495. ret = WOLFSSL_FATAL_ERROR;
  22496. }
  22497. }
  22498. printf(resultFmt, ret == 0 ? passed : failed);
  22499. fflush(stdout);
  22500. printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
  22501. if (ret == 0) {
  22502. ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
  22503. }
  22504. /* Test bad args. */
  22505. if (ret == 0) {
  22506. ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
  22507. if (ret == BAD_FUNC_ARG) {
  22508. ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
  22509. }
  22510. if (ret == BAD_FUNC_ARG) {
  22511. ret = 0;
  22512. } else if (ret == 0) {
  22513. ret = WOLFSSL_FATAL_ERROR;
  22514. }
  22515. }
  22516. printf(resultFmt, ret == 0 ? passed : failed);
  22517. fflush(stdout);
  22518. if (wc_FreeRng(&rng) && ret == 0) {
  22519. ret = WOLFSSL_FATAL_ERROR;
  22520. }
  22521. wc_ecc_ctx_free(cliCtx);
  22522. wc_ecc_ctx_free(servCtx);
  22523. #endif
  22524. return ret;
  22525. } /* END test_wc_ecc_ctx_set_peer_salt */
  22526. /*
  22527. * Testing wc_ecc_ctx_set_info()
  22528. */
  22529. static int test_wc_ecc_ctx_set_info(void)
  22530. {
  22531. int ret = 0;
  22532. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22533. ecEncCtx* ctx = NULL;
  22534. WC_RNG rng;
  22535. const char* optInfo = "Optional Test Info.";
  22536. int optInfoSz = (int)XSTRLEN(optInfo);
  22537. const char* badOptInfo = NULL;
  22538. ret = wc_InitRng(&rng);
  22539. if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
  22540. ret = WOLFSSL_FATAL_ERROR;
  22541. }
  22542. printf(testingFmt, "wc_ecc_ctx_set_info()");
  22543. if (ret == 0) {
  22544. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
  22545. }
  22546. /* Test bad args. */
  22547. if (ret == 0) {
  22548. ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
  22549. if (ret == BAD_FUNC_ARG) {
  22550. ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
  22551. }
  22552. if (ret == BAD_FUNC_ARG) {
  22553. ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
  22554. }
  22555. if (ret == BAD_FUNC_ARG) {
  22556. ret = 0;
  22557. } else if (ret == 0) {
  22558. ret = WOLFSSL_FATAL_ERROR;
  22559. }
  22560. }
  22561. printf(resultFmt, ret == 0 ? passed : failed);
  22562. fflush(stdout);
  22563. if (wc_FreeRng(&rng) && ret == 0) {
  22564. ret = WOLFSSL_FATAL_ERROR;
  22565. }
  22566. wc_ecc_ctx_free(ctx);
  22567. #endif
  22568. return ret;
  22569. } /* END test_wc_ecc_ctx_set_info */
  22570. /*
  22571. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22572. */
  22573. static int test_wc_ecc_encryptDecrypt(void)
  22574. {
  22575. int ret = 0;
  22576. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22577. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22578. ecc_key srvKey, cliKey, tmpKey;
  22579. WC_RNG rng;
  22580. const char* msg = "EccBlock Size 16";
  22581. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22582. #ifdef WOLFSSL_ECIES_OLD
  22583. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22584. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22585. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22586. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22587. #else
  22588. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22589. #endif
  22590. word32 outSz = (word32)sizeof(out);
  22591. byte plain[sizeof("EccBlock Size 16")];
  22592. word32 plainSz = (word32)sizeof(plain);
  22593. int keySz = KEY20;
  22594. /* Init stack variables. */
  22595. XMEMSET(out, 0, outSz);
  22596. XMEMSET(plain, 0, plainSz);
  22597. XMEMSET(&rng, 0, sizeof(rng));
  22598. XMEMSET(&srvKey, 0, sizeof(srvKey));
  22599. XMEMSET(&cliKey, 0, sizeof(cliKey));
  22600. ret = wc_InitRng(&rng);
  22601. if (ret == 0) {
  22602. ret = wc_ecc_init(&cliKey);
  22603. if (ret == 0) {
  22604. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22605. #if defined(WOLFSSL_ASYNC_CRYPT)
  22606. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22607. #endif
  22608. }
  22609. if (ret == 0) {
  22610. ret = wc_ecc_init(&srvKey);
  22611. }
  22612. if (ret == 0) {
  22613. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22614. #if defined(WOLFSSL_ASYNC_CRYPT)
  22615. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22616. #endif
  22617. }
  22618. if (ret == 0) {
  22619. ret = wc_ecc_init(&tmpKey);
  22620. }
  22621. }
  22622. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22623. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22624. !defined(HAVE_SELFTEST)
  22625. if (ret == 0) {
  22626. ret = wc_ecc_set_rng(&srvKey, &rng);
  22627. }
  22628. if (ret == 0) {
  22629. ret = wc_ecc_set_rng(&cliKey, &rng);
  22630. }
  22631. #endif
  22632. printf(testingFmt, "wc_ecc_encrypt()");
  22633. if (ret == 0) {
  22634. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22635. &outSz, NULL);
  22636. }
  22637. if (ret == 0) {
  22638. ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
  22639. &outSz, NULL);
  22640. if (ret == BAD_FUNC_ARG) {
  22641. ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
  22642. &outSz, NULL);
  22643. }
  22644. if (ret == BAD_FUNC_ARG) {
  22645. ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
  22646. &outSz, NULL);
  22647. }
  22648. if (ret == BAD_FUNC_ARG) {
  22649. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22650. &outSz, NULL);
  22651. }
  22652. if (ret == BAD_FUNC_ARG) {
  22653. ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22654. NULL, NULL);
  22655. }
  22656. if (ret == BAD_FUNC_ARG) {
  22657. ret = 0;
  22658. } else if (ret == 0) {
  22659. ret = WOLFSSL_FATAL_ERROR;
  22660. }
  22661. }
  22662. printf(resultFmt, ret == 0 ? passed : failed);
  22663. fflush(stdout);
  22664. printf(testingFmt, "wc_ecc_decrypt()");
  22665. #ifdef WOLFSSL_ECIES_OLD
  22666. if (ret == 0) {
  22667. tmpKey.dp = cliKey.dp;
  22668. ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
  22669. }
  22670. #endif
  22671. if (ret == 0) {
  22672. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
  22673. &plainSz, NULL);
  22674. }
  22675. if (ret == 0) {
  22676. ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
  22677. &plainSz, NULL);
  22678. #ifdef WOLFSSL_ECIES_OLD
  22679. /* NULL parameter allowed in new implementations - public key comes from
  22680. * the message. */
  22681. if (ret == BAD_FUNC_ARG) {
  22682. ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
  22683. &plainSz, NULL);
  22684. }
  22685. #endif
  22686. if (ret == BAD_FUNC_ARG) {
  22687. ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
  22688. &plainSz, NULL);
  22689. }
  22690. if (ret == BAD_FUNC_ARG) {
  22691. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
  22692. &plainSz, NULL);
  22693. }
  22694. if (ret == BAD_FUNC_ARG) {
  22695. ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
  22696. plain, NULL, NULL);
  22697. }
  22698. if (ret == BAD_FUNC_ARG) {
  22699. ret = 0;
  22700. } else if (ret == 0) {
  22701. ret = WOLFSSL_FATAL_ERROR;
  22702. }
  22703. }
  22704. if (XMEMCMP(msg, plain, msgSz) != 0) {
  22705. ret = WOLFSSL_FATAL_ERROR;
  22706. }
  22707. printf(resultFmt, ret == 0 ? passed : failed);
  22708. fflush(stdout);
  22709. if (wc_FreeRng(&rng) && ret == 0) {
  22710. ret = WOLFSSL_FATAL_ERROR;
  22711. }
  22712. wc_ecc_free(&tmpKey);
  22713. wc_ecc_free(&cliKey);
  22714. wc_ecc_free(&srvKey);
  22715. #endif
  22716. return ret;
  22717. } /* END test_wc_ecc_encryptDecrypt */
  22718. /*
  22719. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22720. */
  22721. static int test_wc_ecc_del_point(void)
  22722. {
  22723. int ret = 0;
  22724. #if defined(HAVE_ECC)
  22725. ecc_point* pt;
  22726. printf(testingFmt, "wc_ecc_new_point()");
  22727. pt = wc_ecc_new_point();
  22728. if (!pt) {
  22729. ret = WOLFSSL_FATAL_ERROR;
  22730. }
  22731. printf(resultFmt, ret == 0 ? passed : failed);
  22732. fflush(stdout);
  22733. wc_ecc_del_point(pt);
  22734. #endif
  22735. return ret;
  22736. } /* END test_wc_ecc_del_point */
  22737. /*
  22738. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22739. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22740. * and wc_ecc_cmp_point()
  22741. */
  22742. static int test_wc_ecc_pointFns(void)
  22743. {
  22744. int ret = 0;
  22745. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22746. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22747. !defined(WOLFSSL_ATECC608A)
  22748. ecc_key key;
  22749. WC_RNG rng;
  22750. ecc_point* point = NULL;
  22751. ecc_point* cpypt = NULL;
  22752. int idx = 0;
  22753. int keySz = KEY32;
  22754. byte der[DER_SZ(KEY32)];
  22755. word32 derlenChk = 0;
  22756. word32 derSz = DER_SZ(KEY32);
  22757. /* Init stack variables. */
  22758. XMEMSET(der, 0, derSz);
  22759. XMEMSET(&rng, 0, sizeof(rng));
  22760. XMEMSET(&key, 0, sizeof(key));
  22761. ret = wc_InitRng(&rng);
  22762. if (ret == 0) {
  22763. ret = wc_ecc_init(&key);
  22764. if (ret == 0) {
  22765. ret = wc_ecc_make_key(&rng, keySz, &key);
  22766. #if defined(WOLFSSL_ASYNC_CRYPT)
  22767. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22768. #endif
  22769. }
  22770. }
  22771. if (ret == 0) {
  22772. point = wc_ecc_new_point();
  22773. if (!point) {
  22774. ret = WOLFSSL_FATAL_ERROR;
  22775. }
  22776. }
  22777. if (ret == 0) {
  22778. cpypt = wc_ecc_new_point();
  22779. if (!cpypt) {
  22780. ret = WOLFSSL_FATAL_ERROR;
  22781. }
  22782. }
  22783. /* Export */
  22784. printf(testingFmt, "wc_ecc_export_point_der()");
  22785. if (ret == 0) {
  22786. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22787. NULL, &derlenChk);
  22788. /* Check length value. */
  22789. if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
  22790. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22791. der, &derSz);
  22792. }
  22793. }
  22794. /* Test bad args. */
  22795. if (ret == 0) {
  22796. ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
  22797. if (ret == ECC_BAD_ARG_E) {
  22798. ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
  22799. }
  22800. if (ret == ECC_BAD_ARG_E) {
  22801. ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
  22802. der, NULL);
  22803. }
  22804. if (ret == ECC_BAD_ARG_E) {
  22805. ret = 0;
  22806. } else if (ret == 0) {
  22807. ret = WOLFSSL_FATAL_ERROR;
  22808. }
  22809. }
  22810. printf(resultFmt, ret == 0 ? passed : failed);
  22811. fflush(stdout);
  22812. /* Import */
  22813. printf(testingFmt, "wc_ecc_import_point_der()");
  22814. if (ret == 0) {
  22815. ret = wc_ecc_import_point_der(der, derSz, idx, point);
  22816. /* Condition double checks wc_ecc_cmp_point(). */
  22817. if (ret == 0 &&
  22818. XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
  22819. ret = wc_ecc_cmp_point(&key.pubkey, point);
  22820. }
  22821. }
  22822. /* Test bad args. */
  22823. if (ret == 0) {
  22824. ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
  22825. if (ret == ECC_BAD_ARG_E) {
  22826. ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
  22827. }
  22828. if (ret == ECC_BAD_ARG_E) {
  22829. ret = wc_ecc_import_point_der(der, derSz, -1, point);
  22830. }
  22831. if (ret == ECC_BAD_ARG_E) {
  22832. ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
  22833. }
  22834. if (ret == ECC_BAD_ARG_E) {
  22835. ret = 0;
  22836. } else if (ret == 0) {
  22837. ret = WOLFSSL_FATAL_ERROR;
  22838. }
  22839. }
  22840. printf(resultFmt, ret == 0 ? passed : failed);
  22841. fflush(stdout);
  22842. /* Copy */
  22843. printf(testingFmt, "wc_ecc_copy_point()");
  22844. if (ret == 0) {
  22845. ret = wc_ecc_copy_point(point, cpypt);
  22846. }
  22847. /* Test bad args. */
  22848. if (ret == 0) {
  22849. ret = wc_ecc_copy_point(NULL, cpypt);
  22850. if (ret == ECC_BAD_ARG_E) {
  22851. ret = wc_ecc_copy_point(point, NULL);
  22852. }
  22853. if (ret == ECC_BAD_ARG_E) {
  22854. ret = 0;
  22855. } else if (ret == 0) {
  22856. ret = WOLFSSL_FATAL_ERROR;
  22857. }
  22858. }
  22859. printf(resultFmt, ret == 0 ? passed : failed);
  22860. fflush(stdout);
  22861. printf(testingFmt, "wc_ecc_cmp_point()");
  22862. /* Compare point */
  22863. if (ret == 0) {
  22864. ret = wc_ecc_cmp_point(point, cpypt);
  22865. }
  22866. /* Test bad args. */
  22867. if (ret == 0) {
  22868. ret = wc_ecc_cmp_point(NULL, cpypt);
  22869. if (ret == BAD_FUNC_ARG) {
  22870. ret = wc_ecc_cmp_point(point, NULL);
  22871. }
  22872. if (ret == BAD_FUNC_ARG) {
  22873. ret = 0;
  22874. } else if (ret == 0) {
  22875. ret = WOLFSSL_FATAL_ERROR;
  22876. }
  22877. }
  22878. printf(resultFmt, ret == 0 ? passed : failed);
  22879. fflush(stdout);
  22880. printf(testingFmt, "wc_ecc_point_is_at_infinity()");
  22881. /* At infinity if return == 1, otherwise return == 0. */
  22882. if (ret == 0) {
  22883. ret = wc_ecc_point_is_at_infinity(point);
  22884. }
  22885. /* Test bad args. */
  22886. if (ret == 0) {
  22887. ret = wc_ecc_point_is_at_infinity(NULL);
  22888. if (ret == BAD_FUNC_ARG) {
  22889. ret = 0;
  22890. } else if (ret == 0) {
  22891. ret = WOLFSSL_FATAL_ERROR;
  22892. }
  22893. }
  22894. printf(resultFmt, ret == 0 ? passed : failed);
  22895. fflush(stdout);
  22896. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  22897. #ifdef USE_ECC_B_PARAM
  22898. printf(testingFmt, "wc_ecc_point_is_on_curve()");
  22899. /* On curve if ret == 0 */
  22900. if (ret == 0) {
  22901. ret = wc_ecc_point_is_on_curve(point, idx);
  22902. }
  22903. /* Test bad args. */
  22904. if (ret == 0) {
  22905. ret = wc_ecc_point_is_on_curve(NULL, idx);
  22906. if (ret == BAD_FUNC_ARG) {
  22907. ret = wc_ecc_point_is_on_curve(point, 1000);
  22908. }
  22909. if (ret == ECC_BAD_ARG_E) {
  22910. ret = 0;
  22911. } else if (ret == 0) {
  22912. ret = WOLFSSL_FATAL_ERROR;
  22913. }
  22914. }
  22915. printf(resultFmt, ret == 0 ? passed : failed);
  22916. fflush(stdout);
  22917. #endif /* USE_ECC_B_PARAM */
  22918. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  22919. /* Free */
  22920. wc_ecc_del_point(point);
  22921. wc_ecc_del_point(cpypt);
  22922. wc_ecc_free(&key);
  22923. if (wc_FreeRng(&rng) && ret == 0) {
  22924. ret = WOLFSSL_FATAL_ERROR;
  22925. }
  22926. #endif
  22927. return ret;
  22928. } /* END test_wc_ecc_pointFns */
  22929. /*
  22930. * Testing wc_ecc_sahred_secret_ssh()
  22931. */
  22932. static int test_wc_ecc_shared_secret_ssh(void)
  22933. {
  22934. int ret = 0;
  22935. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22936. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22937. !defined(WOLFSSL_ATECC608A)
  22938. ecc_key key, key2;
  22939. WC_RNG rng;
  22940. int keySz = KEY32;
  22941. int key2Sz = KEY24;
  22942. byte secret[KEY32];
  22943. word32 secretLen = keySz;
  22944. /* Init stack variables. */
  22945. XMEMSET(secret, 0, secretLen);
  22946. XMEMSET(&rng, 0, sizeof(rng));
  22947. XMEMSET(&key, 0, sizeof(key));
  22948. XMEMSET(&key2, 0, sizeof(key2));
  22949. /* Make keys */
  22950. ret = wc_InitRng(&rng);
  22951. if (ret == 0) {
  22952. ret = wc_ecc_init(&key);
  22953. if (ret == 0) {
  22954. ret = wc_ecc_make_key(&rng, keySz, &key);
  22955. #if defined(WOLFSSL_ASYNC_CRYPT)
  22956. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22957. #endif
  22958. }
  22959. if (wc_FreeRng(&rng) && ret == 0) {
  22960. ret = WOLFSSL_FATAL_ERROR;
  22961. }
  22962. }
  22963. if (ret == 0) {
  22964. ret = wc_InitRng(&rng);
  22965. if (ret == 0) {
  22966. ret = wc_ecc_init(&key2);
  22967. }
  22968. if (ret == 0) {
  22969. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22970. #if defined(WOLFSSL_ASYNC_CRYPT)
  22971. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22972. #endif
  22973. }
  22974. }
  22975. printf(testingFmt, "ecc_shared_secret_ssh()");
  22976. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22977. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22978. !defined(HAVE_SELFTEST)
  22979. if (ret == 0) {
  22980. ret = wc_ecc_set_rng(&key, &rng);
  22981. }
  22982. #endif
  22983. if (ret == 0) {
  22984. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  22985. }
  22986. /* Pass in bad args. */
  22987. if (ret == 0) {
  22988. ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
  22989. if (ret == BAD_FUNC_ARG) {
  22990. ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
  22991. }
  22992. if (ret == BAD_FUNC_ARG) {
  22993. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
  22994. }
  22995. if (ret == BAD_FUNC_ARG) {
  22996. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
  22997. }
  22998. if (ret == BAD_FUNC_ARG) {
  22999. key.type = ECC_PUBLICKEY;
  23000. ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
  23001. if (ret == ECC_BAD_ARG_E) {
  23002. ret = 0;
  23003. } else if (ret == 0) {
  23004. ret = WOLFSSL_FATAL_ERROR;
  23005. }
  23006. } else if (ret == 0) {
  23007. ret = WOLFSSL_FATAL_ERROR;
  23008. }
  23009. }
  23010. printf(resultFmt, ret == 0 ? passed : failed);
  23011. fflush(stdout);
  23012. if (wc_FreeRng(&rng) && ret == 0) {
  23013. ret = WOLFSSL_FATAL_ERROR;
  23014. }
  23015. wc_ecc_free(&key);
  23016. wc_ecc_free(&key2);
  23017. #ifdef FP_ECC
  23018. wc_ecc_fp_free();
  23019. #endif
  23020. #endif
  23021. return ret;
  23022. } /* END test_wc_ecc_shared_secret_ssh */
  23023. /*
  23024. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  23025. */
  23026. static int test_wc_ecc_verify_hash_ex(void)
  23027. {
  23028. int ret = 0;
  23029. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  23030. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  23031. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  23032. ecc_key key;
  23033. WC_RNG rng;
  23034. mp_int r;
  23035. mp_int s;
  23036. mp_int z;
  23037. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  23038. unsigned char iHash[] = "Everyone gets Friday off.......";
  23039. unsigned char shortHash[] = TEST_STRING;
  23040. word32 hashlen = sizeof(hash);
  23041. word32 iHashLen = sizeof(iHash);
  23042. word32 shortHashLen = sizeof(shortHash);
  23043. int keySz = KEY32;
  23044. int sig = WOLFSSL_FATAL_ERROR;
  23045. int ver = WOLFSSL_FATAL_ERROR;
  23046. int verify_ok = 0;
  23047. /* Initialize r and s. */
  23048. ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
  23049. if (ret != MP_OKAY) {
  23050. return MP_INIT_E;
  23051. }
  23052. ret = wc_InitRng(&rng);
  23053. if (ret == 0) {
  23054. ret = wc_ecc_init(&key);
  23055. if (ret == 0) {
  23056. ret = wc_ecc_make_key(&rng, keySz, &key);
  23057. #if defined(WOLFSSL_ASYNC_CRYPT)
  23058. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23059. #endif
  23060. }
  23061. }
  23062. if (ret == 0) {
  23063. ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
  23064. if (ret == 0) {
  23065. /* verify_ok should be 1. */
  23066. ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
  23067. if (verify_ok != 1 && ret == 0) {
  23068. ret = WOLFSSL_FATAL_ERROR;
  23069. }
  23070. }
  23071. if (ret == 0) {
  23072. /* verify_ok should be 0 */
  23073. ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
  23074. &verify_ok, &key);
  23075. if (verify_ok != 0 && ret == 0) {
  23076. ret = WOLFSSL_FATAL_ERROR;
  23077. }
  23078. }
  23079. if (ret == 0) {
  23080. /* verify_ok should be 0. */
  23081. ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23082. &verify_ok, &key);
  23083. if (verify_ok != 0 && ret == 0) {
  23084. ret = WOLFSSL_FATAL_ERROR;
  23085. }
  23086. }
  23087. }
  23088. printf(testingFmt, "wc_ecc_sign_hash_ex()");
  23089. /* Test bad args. */
  23090. if (ret == 0) {
  23091. if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
  23092. == ECC_BAD_ARG_E) {
  23093. sig = 0;
  23094. }
  23095. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
  23096. != ECC_BAD_ARG_E) {
  23097. sig = WOLFSSL_FATAL_ERROR;
  23098. }
  23099. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
  23100. != ECC_BAD_ARG_E) {
  23101. sig = WOLFSSL_FATAL_ERROR;
  23102. }
  23103. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
  23104. != ECC_BAD_ARG_E) {
  23105. sig = WOLFSSL_FATAL_ERROR;
  23106. }
  23107. if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
  23108. != ECC_BAD_ARG_E) {
  23109. sig = WOLFSSL_FATAL_ERROR;
  23110. }
  23111. }
  23112. printf(resultFmt, sig == 0 ? passed : failed);
  23113. printf(testingFmt, "wc_ecc_verify_hash_ex()");
  23114. /* Test bad args. */
  23115. if (ret == 0) {
  23116. if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
  23117. == ECC_BAD_ARG_E) {
  23118. ver = 0;
  23119. }
  23120. if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  23121. &verify_ok, &key) != ECC_BAD_ARG_E) {
  23122. ver = WOLFSSL_FATAL_ERROR;
  23123. }
  23124. if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
  23125. != MP_ZERO_E) {
  23126. ver = WOLFSSL_FATAL_ERROR;
  23127. }
  23128. if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
  23129. != MP_ZERO_E) {
  23130. ver = WOLFSSL_FATAL_ERROR;
  23131. }
  23132. if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
  23133. != MP_ZERO_E) {
  23134. ver = WOLFSSL_FATAL_ERROR;
  23135. }
  23136. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  23137. &key) != ECC_BAD_ARG_E) {
  23138. ver = WOLFSSL_FATAL_ERROR;
  23139. }
  23140. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23141. NULL, &key) != ECC_BAD_ARG_E) {
  23142. ver = WOLFSSL_FATAL_ERROR;
  23143. }
  23144. if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  23145. &verify_ok, NULL) != ECC_BAD_ARG_E) {
  23146. ver = WOLFSSL_FATAL_ERROR;
  23147. }
  23148. }
  23149. printf(resultFmt, ver == 0 ? passed : failed);
  23150. wc_ecc_free(&key);
  23151. mp_free(&r);
  23152. mp_free(&s);
  23153. if (wc_FreeRng(&rng)) {
  23154. return WOLFSSL_FATAL_ERROR;
  23155. }
  23156. if (ret == 0 && (sig != 0 || ver != 0)) {
  23157. ret = WOLFSSL_FATAL_ERROR;
  23158. }
  23159. #endif
  23160. return ret;
  23161. } /* END test_wc_ecc_verify_hash_ex */
  23162. /*
  23163. * Testing wc_ecc_mulmod()
  23164. */
  23165. static int test_wc_ecc_mulmod(void)
  23166. {
  23167. int ret = 0;
  23168. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  23169. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  23170. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  23171. ecc_key key1, key2, key3;
  23172. WC_RNG rng;
  23173. ret = wc_InitRng(&rng);
  23174. if (ret == 0) {
  23175. ret = wc_ecc_init(&key1);
  23176. if (ret == 0) {
  23177. ret = wc_ecc_init(&key2);
  23178. }
  23179. if (ret == 0) {
  23180. ret = wc_ecc_init(&key3);
  23181. }
  23182. if (ret == 0) {
  23183. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  23184. #if defined(WOLFSSL_ASYNC_CRYPT)
  23185. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  23186. #endif
  23187. }
  23188. wc_FreeRng(&rng);
  23189. }
  23190. if (ret == 0) {
  23191. ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
  23192. ECC_SECP256R1);
  23193. if (ret == 0) {
  23194. ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  23195. key1.dp->prime, ECC_SECP256R1);
  23196. }
  23197. }
  23198. printf(testingFmt, "wc_ecc_mulmod()");
  23199. if (ret == 0) {
  23200. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
  23201. &key3.k, 1);
  23202. }
  23203. /* Test bad args. */
  23204. if (ret == 0) {
  23205. ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
  23206. &key3.k, 1);
  23207. if (ret == ECC_BAD_ARG_E) {
  23208. ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
  23209. &key3.k, 1);
  23210. }
  23211. if (ret == ECC_BAD_ARG_E) {
  23212. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
  23213. &key3.k, 1);
  23214. }
  23215. if (ret == ECC_BAD_ARG_E) {
  23216. ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
  23217. &key2.k, NULL, 1);
  23218. }
  23219. if (ret == ECC_BAD_ARG_E) {
  23220. ret = 0;
  23221. } else if (ret == 0) {
  23222. ret = WOLFSSL_FATAL_ERROR;
  23223. }
  23224. }
  23225. printf(resultFmt, ret == 0 ? passed : failed);
  23226. fflush(stdout);
  23227. wc_ecc_free(&key1);
  23228. wc_ecc_free(&key2);
  23229. wc_ecc_free(&key3);
  23230. #ifdef FP_ECC
  23231. wc_ecc_fp_free();
  23232. #endif
  23233. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  23234. return ret;
  23235. } /* END test_wc_ecc_mulmod */
  23236. /*
  23237. * Testing wc_ecc_is_valid_idx()
  23238. */
  23239. static int test_wc_ecc_is_valid_idx(void)
  23240. {
  23241. int ret = 0;
  23242. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  23243. ecc_key key;
  23244. WC_RNG rng;
  23245. int iVal = -2;
  23246. int iVal2 = 3000;
  23247. XMEMSET(&rng, 0, sizeof(rng));
  23248. XMEMSET(&key, 0, sizeof(key));
  23249. ret = wc_InitRng(&rng);
  23250. if (ret == 0) {
  23251. ret = wc_ecc_init(&key);
  23252. if (ret == 0) {
  23253. ret = wc_ecc_make_key(&rng, 32, &key);
  23254. #if defined(WOLFSSL_ASYNC_CRYPT)
  23255. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23256. #endif
  23257. }
  23258. }
  23259. printf(testingFmt, "wc_ecc_is_valid_idx()");
  23260. if (ret == 0) {
  23261. ret = wc_ecc_is_valid_idx(key.idx);
  23262. if (ret == 1) {
  23263. ret = 0;
  23264. } else {
  23265. ret = WOLFSSL_FATAL_ERROR;
  23266. }
  23267. }
  23268. /* Test bad args. */
  23269. if (ret == 0) {
  23270. ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
  23271. if (ret == 0) {
  23272. ret = wc_ecc_is_valid_idx(iVal2);
  23273. }
  23274. if (ret != 0) {
  23275. ret = WOLFSSL_FATAL_ERROR;
  23276. }
  23277. }
  23278. printf(resultFmt, ret == 0 ? passed : failed);
  23279. fflush(stdout);
  23280. if (wc_FreeRng(&rng) && ret == 0) {
  23281. ret = WOLFSSL_FATAL_ERROR;
  23282. }
  23283. wc_ecc_free(&key);
  23284. #ifdef FP_ECC
  23285. wc_ecc_fp_free();
  23286. #endif
  23287. #endif
  23288. return ret;
  23289. } /* END test_wc_ecc_is_valid_idx */
  23290. /*
  23291. * Testing wc_ecc_get_curve_id_from_oid()
  23292. */
  23293. static int test_wc_ecc_get_curve_id_from_oid(void)
  23294. {
  23295. int ret = 0;
  23296. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  23297. !defined(HAVE_FIPS)
  23298. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  23299. word32 len = sizeof(oid);
  23300. printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
  23301. /* Bad Cases */
  23302. ret = wc_ecc_get_curve_id_from_oid(NULL, len);
  23303. if (ret == BAD_FUNC_ARG) {
  23304. ret = 0;
  23305. }
  23306. if (ret == 0) {
  23307. ret = wc_ecc_get_curve_id_from_oid(oid, 0);
  23308. if (ret == ECC_CURVE_INVALID) {
  23309. ret = 0;
  23310. }
  23311. }
  23312. /* Good Case */
  23313. if (ret == 0) {
  23314. ret = wc_ecc_get_curve_id_from_oid(oid, len);
  23315. if (ret == ECC_SECP256R1) {
  23316. ret = 0;
  23317. }
  23318. }
  23319. printf(resultFmt, ret == 0 ? passed : failed);
  23320. fflush(stdout);
  23321. #endif
  23322. return ret;
  23323. }/* END test_wc_ecc_get_curve_id_from_oid */
  23324. /*
  23325. * Testing wc_ecc_sig_size_calc()
  23326. */
  23327. static int test_wc_ecc_sig_size_calc(void)
  23328. {
  23329. int ret = 0;
  23330. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  23331. ecc_key key;
  23332. WC_RNG rng;
  23333. int sz = 0;
  23334. printf(testingFmt, "wc_ecc_sig_size_calc()");
  23335. ret = wc_InitRng(&rng);
  23336. if (ret == 0) {
  23337. ret = wc_ecc_init(&key);
  23338. if (ret == 0) {
  23339. ret = wc_ecc_make_key(&rng, 16, &key);
  23340. #if defined(WOLFSSL_ASYNC_CRYPT)
  23341. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  23342. #endif
  23343. }
  23344. sz = key.dp->size;
  23345. }
  23346. if (ret == 0) {
  23347. ret = wc_ecc_sig_size_calc(sz);
  23348. if (ret > 0) {
  23349. ret = 0;
  23350. }
  23351. }
  23352. printf(resultFmt, ret == 0 ? passed : failed);
  23353. fflush(stdout);
  23354. wc_ecc_free(&key);
  23355. wc_FreeRng(&rng);
  23356. #endif
  23357. return ret;
  23358. } /* END test_wc_ecc_sig_size_calc */
  23359. /*
  23360. * Testing ToTraditional
  23361. */
  23362. static int test_ToTraditional(void)
  23363. {
  23364. int ret = 0;
  23365. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23366. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23367. defined(OPENSSL_EXTRA_X509_SMALL))
  23368. XFILE f;
  23369. byte input[TWOK_BUF];
  23370. word32 sz;
  23371. printf(testingFmt, "ToTraditional()");
  23372. f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
  23373. AssertTrue((f != XBADFILE));
  23374. sz = (word32)XFREAD(input, 1, sizeof(input), f);
  23375. XFCLOSE(f);
  23376. /* Good case */
  23377. ret = ToTraditional(input, sz);
  23378. if (ret > 0) {
  23379. ret = 0;
  23380. }
  23381. /* Bad cases */
  23382. if (ret == 0) {
  23383. ret = ToTraditional(NULL, 0);
  23384. if (ret == BAD_FUNC_ARG) {
  23385. ret = 0;
  23386. }
  23387. }
  23388. if (ret == 0) {
  23389. ret = ToTraditional(NULL, sz);
  23390. if (ret == BAD_FUNC_ARG) {
  23391. ret = 0;
  23392. }
  23393. }
  23394. if (ret == 0) {
  23395. ret = ToTraditional(input, 0);
  23396. if (ret == ASN_PARSE_E || ret == BUFFER_E) {
  23397. ret = 0;
  23398. }
  23399. }
  23400. printf(resultFmt, ret == 0 ? passed : failed);
  23401. fflush(stdout);
  23402. #endif
  23403. return ret;
  23404. }/* End test_ToTraditional*/
  23405. /*
  23406. * Testing wc_EccPrivateKeyToDer
  23407. */
  23408. static int test_wc_EccPrivateKeyToDer(void)
  23409. {
  23410. int ret = 0;
  23411. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23412. byte output[ONEK_BUF];
  23413. ecc_key eccKey;
  23414. WC_RNG rng;
  23415. word32 inLen;
  23416. printf(testingFmt, "wc_EccPrivateKeyToDer()");
  23417. ret = wc_InitRng(&rng);
  23418. if (ret == 0) {
  23419. ret = wc_ecc_init(&eccKey);
  23420. if (ret == 0) {
  23421. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23422. #if defined(WOLFSSL_ASYNC_CRYPT)
  23423. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23424. #endif
  23425. }
  23426. inLen = (word32)sizeof(output);
  23427. /* Bad Cases */
  23428. if (ret == 0) {
  23429. ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
  23430. if (ret == BAD_FUNC_ARG) {
  23431. ret = 0;
  23432. }
  23433. }
  23434. if (ret == 0) {
  23435. ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
  23436. if (ret == BAD_FUNC_ARG) {
  23437. ret = 0;
  23438. }
  23439. }
  23440. if (ret == 0) {
  23441. ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
  23442. if (ret == LENGTH_ONLY_E) {
  23443. ret = 0;
  23444. }
  23445. }
  23446. if (ret == 0) {
  23447. ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
  23448. if (ret == BAD_FUNC_ARG) {
  23449. ret = 0;
  23450. }
  23451. }
  23452. /*Good Case */
  23453. if (ret == 0) {
  23454. ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
  23455. if (ret > 0) {
  23456. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23457. /* test importing private only into a PKEY struct */
  23458. EC_KEY* ec;
  23459. EVP_PKEY* pkey;
  23460. const unsigned char* der = output;
  23461. pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
  23462. AssertNotNull(pkey);
  23463. der = output;
  23464. ec = d2i_ECPrivateKey(NULL, &der, ret);
  23465. AssertNotNull(ec);
  23466. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23467. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23468. #endif
  23469. ret = 0;
  23470. }
  23471. }
  23472. wc_ecc_free(&eccKey);
  23473. }
  23474. wc_FreeRng(&rng);
  23475. printf(resultFmt, ret == 0 ? passed : failed);
  23476. fflush(stdout);
  23477. #endif
  23478. return ret;
  23479. }/* End test_wc_EccPrivateKeyToDer*/
  23480. /*
  23481. * Testing wc_DhPublicKeyDecode
  23482. */
  23483. static int test_wc_DhPublicKeyDecode(void)
  23484. {
  23485. int ret = 0;
  23486. #ifndef NO_DH
  23487. word32 inOutIdx;
  23488. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23489. DhKey key;
  23490. AssertIntEQ(wc_InitDhKey(&key), 0);
  23491. printf(testingFmt, "wc_DhPublicKeyDecode()");
  23492. AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
  23493. BAD_FUNC_ARG);
  23494. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23495. BAD_FUNC_ARG);
  23496. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23497. BAD_FUNC_ARG);
  23498. inOutIdx = 0;
  23499. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23500. BAD_FUNC_ARG);
  23501. inOutIdx = 0;
  23502. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23503. BAD_FUNC_ARG);
  23504. inOutIdx = 0;
  23505. AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23506. sizeof_dh_pub_key_der_2048), 0);
  23507. AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
  23508. key.pub.used != 0 && key.priv.used == 0);
  23509. wc_FreeDhKey(&key);
  23510. printf(resultFmt, passed);
  23511. #endif
  23512. (void)inOutIdx;
  23513. #endif /* !NO_DH */
  23514. return ret;
  23515. }
  23516. /*
  23517. * Testing wc_Ed25519KeyToDer
  23518. */
  23519. static int test_wc_Ed25519KeyToDer(void)
  23520. {
  23521. int ret = 0;
  23522. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23523. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23524. byte output[ONEK_BUF];
  23525. ed25519_key ed25519Key;
  23526. WC_RNG rng;
  23527. word32 inLen;
  23528. printf(testingFmt, "wc_Ed25519KeyToDer()");
  23529. ret = wc_InitRng(&rng);
  23530. if (ret == 0) {
  23531. ret = wc_ed25519_init(&ed25519Key);
  23532. if (ret == 0) {
  23533. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23534. }
  23535. inLen = (word32)sizeof(output);
  23536. /* Bad Cases */
  23537. if (ret == 0) {
  23538. ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
  23539. if (ret == BAD_FUNC_ARG) {
  23540. ret = 0;
  23541. }
  23542. }
  23543. if (ret == 0) {
  23544. ret = wc_Ed25519KeyToDer(NULL, output, inLen);
  23545. if (ret == BAD_FUNC_ARG) {
  23546. ret = 0;
  23547. }
  23548. }
  23549. if (ret == 0) {
  23550. ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
  23551. if (ret == BAD_FUNC_ARG) {
  23552. ret = 0;
  23553. }
  23554. }
  23555. /* Good Cases */
  23556. if (ret == 0) {
  23557. /* length only */
  23558. ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
  23559. if (ret > 0) {
  23560. ret = 0;
  23561. }
  23562. }
  23563. if (ret == 0) {
  23564. ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
  23565. if (ret > 0) {
  23566. ret = 0;
  23567. }
  23568. }
  23569. wc_ed25519_free(&ed25519Key);
  23570. }
  23571. wc_FreeRng(&rng);
  23572. printf(resultFmt, ret == 0 ? passed : failed);
  23573. fflush(stdout);
  23574. #endif
  23575. return ret;
  23576. }/* End test_wc_Ed25519KeyToDer*/
  23577. /*
  23578. * Testing wc_Ed25519PrivateKeyToDer
  23579. */
  23580. static int test_wc_Ed25519PrivateKeyToDer(void)
  23581. {
  23582. int ret = 0;
  23583. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23584. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23585. byte output[ONEK_BUF];
  23586. ed25519_key ed25519PrivKey;
  23587. WC_RNG rng;
  23588. word32 inLen;
  23589. printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
  23590. ret = wc_InitRng(&rng);
  23591. if (ret == 0) {
  23592. ret = wc_ed25519_init(&ed25519PrivKey);
  23593. if (ret == 0) {
  23594. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
  23595. }
  23596. inLen = (word32)sizeof(output);
  23597. /* Bad Cases */
  23598. if (ret == 0) {
  23599. ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
  23600. if (ret == BAD_FUNC_ARG) {
  23601. ret = 0;
  23602. }
  23603. }
  23604. if (ret == 0) {
  23605. ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
  23606. if (ret == BAD_FUNC_ARG) {
  23607. ret = 0;
  23608. }
  23609. }
  23610. if (ret == 0) {
  23611. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
  23612. if (ret == BAD_FUNC_ARG) {
  23613. ret = 0;
  23614. }
  23615. }
  23616. /* Good Cases */
  23617. if (ret == 0) {
  23618. /* length only */
  23619. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
  23620. if (ret > 0) {
  23621. ret = 0;
  23622. }
  23623. }
  23624. if (ret == 0) {
  23625. ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
  23626. if (ret > 0) {
  23627. ret = 0;
  23628. }
  23629. }
  23630. wc_ed25519_free(&ed25519PrivKey);
  23631. }
  23632. wc_FreeRng(&rng);
  23633. printf(resultFmt, ret == 0 ? passed : failed);
  23634. fflush(stdout);
  23635. #endif
  23636. return ret;
  23637. }/* End test_wc_Ed25519PrivateKeyToDer*/
  23638. /*
  23639. * Testing wc_Ed448KeyToDer
  23640. */
  23641. static int test_wc_Ed448KeyToDer(void)
  23642. {
  23643. int ret = 0;
  23644. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23645. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23646. byte output[ONEK_BUF];
  23647. ed448_key ed448Key;
  23648. WC_RNG rng;
  23649. word32 inLen;
  23650. printf(testingFmt, "wc_Ed448KeyToDer()");
  23651. ret = wc_InitRng(&rng);
  23652. if (ret == 0) {
  23653. ret = wc_ed448_init(&ed448Key);
  23654. if (ret == 0) {
  23655. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23656. }
  23657. inLen = sizeof(output);
  23658. /* Bad Cases */
  23659. if (ret == 0) {
  23660. ret = wc_Ed448KeyToDer(NULL, NULL, 0);
  23661. if (ret == BAD_FUNC_ARG) {
  23662. ret = 0;
  23663. }
  23664. }
  23665. if (ret == 0) {
  23666. ret = wc_Ed448KeyToDer(NULL, output, inLen);
  23667. if (ret == BAD_FUNC_ARG) {
  23668. ret = 0;
  23669. }
  23670. }
  23671. if (ret == 0) {
  23672. ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
  23673. if (ret == BAD_FUNC_ARG) {
  23674. ret = 0;
  23675. }
  23676. }
  23677. /* Good Cases */
  23678. if (ret == 0) {
  23679. /* length only */
  23680. ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
  23681. if (ret > 0) {
  23682. ret = 0;
  23683. }
  23684. }
  23685. if (ret == 0) {
  23686. ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
  23687. if (ret > 0) {
  23688. ret = 0;
  23689. }
  23690. }
  23691. wc_ed448_free(&ed448Key);
  23692. }
  23693. wc_FreeRng(&rng);
  23694. printf(resultFmt, ret == 0 ? passed : failed);
  23695. fflush(stdout);
  23696. #endif
  23697. return ret;
  23698. }/* End test_wc_Ed448KeyToDer*/
  23699. /*
  23700. * Testing wc_Ed448PrivateKeyToDer
  23701. */
  23702. static int test_wc_Ed448PrivateKeyToDer(void)
  23703. {
  23704. int ret = 0;
  23705. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23706. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23707. byte output[ONEK_BUF];
  23708. ed448_key ed448PrivKey;
  23709. WC_RNG rng;
  23710. word32 inLen;
  23711. printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
  23712. ret = wc_InitRng(&rng);
  23713. if (ret == 0) {
  23714. ret = wc_ed448_init(&ed448PrivKey);
  23715. if (ret == 0) {
  23716. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
  23717. }
  23718. inLen = sizeof(output);
  23719. /* Bad Cases */
  23720. if (ret == 0) {
  23721. ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
  23722. if (ret == BAD_FUNC_ARG) {
  23723. ret = 0;
  23724. }
  23725. }
  23726. if (ret == 0) {
  23727. ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
  23728. if (ret == BAD_FUNC_ARG) {
  23729. ret = 0;
  23730. }
  23731. }
  23732. if (ret == 0) {
  23733. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
  23734. if (ret == BAD_FUNC_ARG) {
  23735. ret = 0;
  23736. }
  23737. }
  23738. /* Good cases */
  23739. if (ret == 0) {
  23740. /* length only */
  23741. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
  23742. if (ret > 0) {
  23743. ret = 0;
  23744. }
  23745. }
  23746. if (ret == 0) {
  23747. ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
  23748. if (ret > 0) {
  23749. ret = 0;
  23750. }
  23751. }
  23752. wc_ed448_free(&ed448PrivKey);
  23753. }
  23754. wc_FreeRng(&rng);
  23755. printf(resultFmt, ret == 0 ? passed : failed);
  23756. fflush(stdout);
  23757. #endif
  23758. return ret;
  23759. }/* End test_wc_Ed448PrivateKeyToDer*/
  23760. /*
  23761. * Testing wc_SetSubjectBuffer
  23762. */
  23763. static int test_wc_SetSubjectBuffer(void)
  23764. {
  23765. int ret = 0;
  23766. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  23767. Cert cert;
  23768. FILE* file;
  23769. byte* der;
  23770. word32 derSz;
  23771. printf(testingFmt, "wc_SetSubjectBuffer()");
  23772. derSz = FOURK_BUF;
  23773. der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23774. if (der == NULL) {
  23775. ret = -1;
  23776. }
  23777. if (ret == 0) {
  23778. file = XFOPEN("./certs/ca-cert.der", "rb");
  23779. if (file != NULL) {
  23780. derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
  23781. XFCLOSE(file);
  23782. }
  23783. else {
  23784. ret = -1;
  23785. }
  23786. }
  23787. if (ret == 0) {
  23788. ret = wc_InitCert(&cert);
  23789. }
  23790. if (ret == 0) {
  23791. ret = wc_SetSubjectBuffer(&cert, der, derSz);
  23792. }
  23793. if (ret == 0) {
  23794. ret = wc_SetSubjectBuffer(NULL, der, derSz);
  23795. if (ret == BAD_FUNC_ARG) {
  23796. ret = 0;
  23797. }
  23798. }
  23799. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23800. printf(resultFmt, ret == 0 ? passed : failed);
  23801. fflush(stdout);
  23802. #endif
  23803. return ret;
  23804. }/* End test_wc_SetSubjectBuffer*/
  23805. /*
  23806. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23807. */
  23808. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23809. {
  23810. int ret = 0;
  23811. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23812. WC_RNG rng;
  23813. Cert cert;
  23814. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23815. ed25519_key ed25519Key;
  23816. #endif
  23817. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23818. RsaKey rsaKey;
  23819. int bits = 2048;
  23820. #endif
  23821. #if defined(HAVE_ECC)
  23822. ecc_key eccKey;
  23823. #endif
  23824. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23825. ed448_key ed448Key;
  23826. #endif
  23827. printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
  23828. #ifndef HAVE_FIPS
  23829. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23830. #else
  23831. ret = wc_InitRng(&rng);
  23832. #endif
  23833. wc_InitCert(&cert);
  23834. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23835. if (ret == 0) { /*ED25519*/
  23836. ret = wc_ed25519_init(&ed25519Key);
  23837. if (ret == 0) {
  23838. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23839. }
  23840. if (ret == 0) {
  23841. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23842. &ed25519Key);
  23843. }
  23844. wc_ed25519_free(&ed25519Key);
  23845. }
  23846. #endif
  23847. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23848. if (ret == 0) { /*RSA*/
  23849. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23850. if (ret == 0) {
  23851. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23852. }
  23853. if (ret == 0) {
  23854. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23855. }
  23856. wc_FreeRsaKey(&rsaKey);
  23857. }
  23858. #endif
  23859. #if defined(HAVE_ECC)
  23860. if (ret == 0) { /*ECC*/
  23861. ret = wc_ecc_init(&eccKey);
  23862. if (ret == 0) {
  23863. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23864. #if defined(WOLFSSL_ASYNC_CRYPT)
  23865. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23866. #endif
  23867. }
  23868. if (ret == 0) {
  23869. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23870. }
  23871. wc_ecc_free(&eccKey);
  23872. }
  23873. #endif
  23874. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23875. if (ret == 0) { /*ED448*/
  23876. ret = wc_ed448_init(&ed448Key);
  23877. if (ret == 0) {
  23878. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23879. }
  23880. if (ret == 0) {
  23881. ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23882. &ed448Key);
  23883. }
  23884. wc_ed448_free(&ed448Key);
  23885. }
  23886. #endif
  23887. printf(resultFmt, ret == 0 ? passed : failed);
  23888. fflush(stdout);
  23889. wc_FreeRng(&rng);
  23890. #endif
  23891. return ret;
  23892. }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23893. /*
  23894. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23895. */
  23896. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23897. {
  23898. int ret = 0;
  23899. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23900. WC_RNG rng;
  23901. Cert cert;
  23902. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23903. ed25519_key ed25519Key;
  23904. #endif
  23905. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23906. RsaKey rsaKey;
  23907. int bits = 2048;
  23908. #endif
  23909. #if defined(HAVE_ECC)
  23910. ecc_key eccKey;
  23911. #endif
  23912. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23913. ed448_key ed448Key;
  23914. #endif
  23915. printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
  23916. #ifndef HAVE_FIPS
  23917. ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
  23918. #else
  23919. ret = wc_InitRng(&rng);
  23920. #endif
  23921. wc_InitCert(&cert);
  23922. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23923. if (ret == 0) { /*ED25519*/
  23924. ret = wc_ed25519_init(&ed25519Key);
  23925. if (ret == 0) {
  23926. ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
  23927. }
  23928. if (ret == 0) {
  23929. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23930. &ed25519Key);
  23931. }
  23932. wc_ed25519_free(&ed25519Key);
  23933. }
  23934. #endif
  23935. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23936. if (ret == 0) { /*RSA*/
  23937. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  23938. if (ret == 0) {
  23939. MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
  23940. }
  23941. if (ret == 0) {
  23942. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
  23943. }
  23944. wc_FreeRsaKey(&rsaKey);
  23945. }
  23946. #endif
  23947. #if defined(HAVE_ECC)
  23948. if (ret == 0) { /*ECC*/
  23949. ret = wc_ecc_init(&eccKey);
  23950. if (ret == 0) {
  23951. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23952. #if defined(WOLFSSL_ASYNC_CRYPT)
  23953. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23954. #endif
  23955. }
  23956. if (ret == 0) {
  23957. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
  23958. }
  23959. wc_ecc_free(&eccKey);
  23960. }
  23961. #endif
  23962. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23963. if (ret == 0) { /*ED448*/
  23964. ret = wc_ed448_init(&ed448Key);
  23965. if (ret == 0) {
  23966. ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
  23967. }
  23968. if (ret == 0) {
  23969. ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23970. &ed448Key);
  23971. }
  23972. wc_ed448_free(&ed448Key);
  23973. }
  23974. #endif
  23975. printf(resultFmt, ret == 0 ? passed : failed);
  23976. fflush(stdout);
  23977. wc_FreeRng(&rng);
  23978. #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23979. return ret;
  23980. }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23981. /*
  23982. * Testing wc_PKCS7_New()
  23983. */
  23984. static int test_wc_PKCS7_New (void)
  23985. {
  23986. #if defined(HAVE_PKCS7)
  23987. PKCS7* pkcs7;
  23988. void* heap = NULL;
  23989. printf(testingFmt, "wc_PKCS7_New()");
  23990. pkcs7 = wc_PKCS7_New(heap, testDevId);
  23991. AssertNotNull(pkcs7);
  23992. printf(resultFmt, passed);
  23993. wc_PKCS7_Free(pkcs7);
  23994. #endif
  23995. return 0;
  23996. } /* END test-wc_PKCS7_New */
  23997. /*
  23998. * Testing wc_PKCS7_Init()
  23999. */
  24000. static int test_wc_PKCS7_Init (void)
  24001. {
  24002. #if defined(HAVE_PKCS7)
  24003. PKCS7* pkcs7;
  24004. void* heap = NULL;
  24005. printf(testingFmt, "wc_PKCS7_Init()");
  24006. pkcs7 = wc_PKCS7_New(heap, testDevId);
  24007. AssertNotNull(pkcs7);
  24008. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  24009. /* Pass in bad args. */
  24010. AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  24011. printf(resultFmt, passed);
  24012. wc_PKCS7_Free(pkcs7);
  24013. #endif
  24014. return 0;
  24015. } /* END test-wc_PKCS7_Init */
  24016. /*
  24017. * Testing wc_PKCS7_InitWithCert()
  24018. */
  24019. static int test_wc_PKCS7_InitWithCert (void)
  24020. {
  24021. #if defined(HAVE_PKCS7)
  24022. PKCS7* pkcs7;
  24023. #ifndef NO_RSA
  24024. #if defined(USE_CERT_BUFFERS_2048)
  24025. unsigned char cert[sizeof(client_cert_der_2048)];
  24026. int certSz = (int)sizeof(cert);
  24027. XMEMSET(cert, 0, certSz);
  24028. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  24029. #elif defined(USE_CERT_BUFFERS_1024)
  24030. unsigned char cert[sizeof(client_cert_der_1024)];
  24031. int certSz = (int)sizeof(cert);
  24032. XMEMSET(cert, 0, certSz);
  24033. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  24034. #else
  24035. unsigned char cert[ONEK_BUF];
  24036. XFILE fp;
  24037. int certSz;
  24038. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24039. AssertTrue(fp != XBADFILE);
  24040. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24041. XFCLOSE(fp);
  24042. #endif
  24043. #elif defined(HAVE_ECC)
  24044. #if defined(USE_CERT_BUFFERS_256)
  24045. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24046. int certSz = (int)sizeof(cert);
  24047. XMEMSET(cert, 0, certSz);
  24048. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  24049. #else
  24050. unsigned char cert[ONEK_BUF];
  24051. XFILE fp;
  24052. int certSz;
  24053. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24054. AssertTrue(fp != XBADFILE);
  24055. certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
  24056. XFCLOSE(fp);
  24057. #endif
  24058. #else
  24059. #error PKCS7 requires ECC or RSA
  24060. #endif
  24061. #ifdef HAVE_ECC
  24062. {
  24063. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  24064. static unsigned char certWithInvalidEccKey[] = {
  24065. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  24066. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  24067. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  24068. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  24069. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24070. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  24071. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24072. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24073. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24074. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24075. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24076. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24077. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24078. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24079. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24080. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  24081. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  24082. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  24083. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  24084. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  24085. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  24086. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  24087. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  24088. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  24089. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  24090. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  24091. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  24092. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  24093. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  24094. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  24095. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  24096. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  24097. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  24098. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  24099. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  24100. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  24101. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  24102. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  24103. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  24104. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  24105. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24106. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  24107. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  24108. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  24109. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  24110. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  24111. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  24112. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  24113. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  24114. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  24115. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  24116. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  24117. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  24118. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  24119. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  24120. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  24121. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  24122. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  24123. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  24124. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  24125. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  24126. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  24127. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  24128. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  24129. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  24130. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  24131. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  24132. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  24133. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  24134. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  24135. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  24136. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  24137. 0x08, 0xA8, 0xB4};
  24138. #endif
  24139. printf(testingFmt, "wc_PKCS7_InitWithCert()");
  24140. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24141. /* If initialization is not successful, it's free'd in init func. */
  24142. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  24143. wc_PKCS7_Free(pkcs7);
  24144. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24145. /* Valid initialization usage. */
  24146. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24147. /* Pass in bad args. No need free for null checks, free at end.*/
  24148. AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  24149. BAD_FUNC_ARG);
  24150. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  24151. BAD_FUNC_ARG);
  24152. #ifdef HAVE_ECC
  24153. AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  24154. sizeof(certWithInvalidEccKey)), 0);
  24155. }
  24156. #endif
  24157. printf(resultFmt, passed);
  24158. wc_PKCS7_Free(pkcs7);
  24159. #endif
  24160. return 0;
  24161. } /* END test_wc_PKCS7_InitWithCert */
  24162. /*
  24163. * Testing wc_PKCS7_EncodeData()
  24164. */
  24165. static int test_wc_PKCS7_EncodeData (void)
  24166. {
  24167. #if defined(HAVE_PKCS7)
  24168. PKCS7* pkcs7;
  24169. byte output[FOURK_BUF];
  24170. byte data[] = "My encoded DER cert.";
  24171. #ifndef NO_RSA
  24172. #if defined(USE_CERT_BUFFERS_2048)
  24173. unsigned char cert[sizeof(client_cert_der_2048)];
  24174. unsigned char key[sizeof(client_key_der_2048)];
  24175. int certSz = (int)sizeof(cert);
  24176. int keySz = (int)sizeof(key);
  24177. XMEMSET(cert, 0, certSz);
  24178. XMEMSET(key, 0, keySz);
  24179. XMEMCPY(cert, client_cert_der_2048, certSz);
  24180. XMEMCPY(key, client_key_der_2048, keySz);
  24181. #elif defined(USE_CERT_BUFFERS_1024)
  24182. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  24183. unsigned char key[sizeof_client_key_der_1024];
  24184. int certSz = (int)sizeof(cert);
  24185. int keySz = (int)sizeof(key);
  24186. XMEMSET(cert, 0, certSz);
  24187. XMEMSET(key, 0, keySz);
  24188. XMEMCPY(cert, client_cert_der_1024, certSz);
  24189. XMEMCPY(key, client_key_der_1024, keySz);
  24190. #else
  24191. unsigned char cert[ONEK_BUF];
  24192. unsigned char key[ONEK_BUF];
  24193. XFILE fp;
  24194. int certSz;
  24195. int keySz;
  24196. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24197. AssertTrue(fp != XBADFILE);
  24198. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24199. XFCLOSE(fp);
  24200. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24201. AssertTrue(fp != XBADFILE);
  24202. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24203. XFCLOSE(fp);
  24204. #endif
  24205. #elif defined(HAVE_ECC)
  24206. #if defined(USE_CERT_BUFFERS_256)
  24207. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24208. unsigned char key[sizeof(ecc_clikey_der_256)];
  24209. int certSz = (int)sizeof(cert);
  24210. int keySz = (int)sizeof(key);
  24211. XMEMSET(cert, 0, certSz);
  24212. XMEMSET(key, 0, keySz);
  24213. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24214. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24215. #else
  24216. unsigned char cert[ONEK_BUF];
  24217. unsigned char key[ONEK_BUF];
  24218. XFILE fp;
  24219. int certSz, keySz;
  24220. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24221. AssertTrue(fp != XBADFILE);
  24222. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24223. XFCLOSE(fp);
  24224. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24225. AssertTrue(fp != XBADFILE);
  24226. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24227. XFCLOSE(fp);
  24228. #endif
  24229. #endif
  24230. XMEMSET(output, 0, sizeof(output));
  24231. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24232. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24233. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  24234. printf(testingFmt, "wc_PKCS7_EncodeData()");
  24235. pkcs7->content = data;
  24236. pkcs7->contentSz = sizeof(data);
  24237. pkcs7->privateKey = key;
  24238. pkcs7->privateKeySz = keySz;
  24239. AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  24240. /* Test bad args. */
  24241. AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  24242. BAD_FUNC_ARG);
  24243. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  24244. BAD_FUNC_ARG);
  24245. AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  24246. printf(resultFmt, passed);
  24247. wc_PKCS7_Free(pkcs7);
  24248. #endif
  24249. return 0;
  24250. } /* END test_wc_PKCS7_EncodeData */
  24251. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24252. !defined(NO_RSA) && !defined(NO_SHA256)
  24253. /* RSA sign raw digest callback */
  24254. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  24255. byte* out, word32 outSz, byte* privateKey,
  24256. word32 privateKeySz, int devid, int hashOID)
  24257. {
  24258. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  24259. byte digInfoEncoding[] = {
  24260. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  24261. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  24262. 0x00, 0x04, 0x20
  24263. };
  24264. int ret;
  24265. byte digestInfo[ONEK_BUF];
  24266. byte sig[FOURK_BUF];
  24267. word32 digestInfoSz = 0;
  24268. word32 idx = 0;
  24269. RsaKey rsa;
  24270. /* SHA-256 required only for this example callback due to above
  24271. * digInfoEncoding[] */
  24272. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24273. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24274. (hashOID != SHA256h)) {
  24275. return -1;
  24276. }
  24277. /* build DigestInfo */
  24278. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24279. digestInfoSz += sizeof(digInfoEncoding);
  24280. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24281. digestInfoSz += digestSz;
  24282. /* set up RSA key */
  24283. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24284. if (ret != 0) {
  24285. return ret;
  24286. }
  24287. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24288. /* sign DigestInfo */
  24289. if (ret == 0) {
  24290. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24291. &rsa, pkcs7->rng);
  24292. if (ret > 0) {
  24293. if (ret > (int)outSz) {
  24294. /* output buffer too small */
  24295. ret = -1;
  24296. } else {
  24297. /* success, ret holds sig size */
  24298. XMEMCPY(out, sig, ret);
  24299. }
  24300. }
  24301. }
  24302. wc_FreeRsaKey(&rsa);
  24303. return ret;
  24304. }
  24305. #endif
  24306. /*
  24307. * Testing wc_PKCS7_EncodeSignedData()
  24308. */
  24309. static int test_wc_PKCS7_EncodeSignedData(void)
  24310. {
  24311. #if defined(HAVE_PKCS7)
  24312. PKCS7* pkcs7;
  24313. WC_RNG rng;
  24314. byte output[FOURK_BUF];
  24315. byte badOut[1];
  24316. word32 outputSz = (word32)sizeof(output);
  24317. word32 badOutSz = 0;
  24318. byte data[] = "Test data to encode.";
  24319. #ifndef NO_RSA
  24320. #if defined(USE_CERT_BUFFERS_2048)
  24321. byte key[sizeof(client_key_der_2048)];
  24322. byte cert[sizeof(client_cert_der_2048)];
  24323. word32 keySz = (word32)sizeof(key);
  24324. word32 certSz = (word32)sizeof(cert);
  24325. XMEMSET(key, 0, keySz);
  24326. XMEMSET(cert, 0, certSz);
  24327. XMEMCPY(key, client_key_der_2048, keySz);
  24328. XMEMCPY(cert, client_cert_der_2048, certSz);
  24329. #elif defined(USE_CERT_BUFFERS_1024)
  24330. byte key[sizeof_client_key_der_1024];
  24331. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24332. word32 keySz = (word32)sizeof(key);
  24333. word32 certSz = (word32)sizeof(cert);
  24334. XMEMSET(key, 0, keySz);
  24335. XMEMSET(cert, 0, certSz);
  24336. XMEMCPY(key, client_key_der_1024, keySz);
  24337. XMEMCPY(cert, client_cert_der_1024, certSz);
  24338. #else
  24339. unsigned char cert[ONEK_BUF];
  24340. unsigned char key[ONEK_BUF];
  24341. XFILE fp;
  24342. int certSz;
  24343. int keySz;
  24344. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24345. AssertTrue(fp != XBADFILE);
  24346. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24347. XFCLOSE(fp);
  24348. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24349. AssertTrue(fp != XBADFILE);
  24350. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24351. XFCLOSE(fp);
  24352. #endif
  24353. #elif defined(HAVE_ECC)
  24354. #if defined(USE_CERT_BUFFERS_256)
  24355. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24356. unsigned char key[sizeof(ecc_clikey_der_256)];
  24357. int certSz = (int)sizeof(cert);
  24358. int keySz = (int)sizeof(key);
  24359. XMEMSET(cert, 0, certSz);
  24360. XMEMSET(key, 0, keySz);
  24361. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24362. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24363. #else
  24364. unsigned char cert[ONEK_BUF];
  24365. unsigned char key[ONEK_BUF];
  24366. XFILE fp;
  24367. int certSz, keySz;
  24368. fp = XOPEN("./certs/client-ecc-cert.der", "rb");
  24369. AssertTrue(fp != XBADFILE);
  24370. certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
  24371. XFCLOSE(fp);
  24372. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24373. AssertTrue(fp != XBADFILE);
  24374. keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
  24375. XFCLOSE(fp);
  24376. #endif
  24377. #endif
  24378. XMEMSET(output, 0, outputSz);
  24379. AssertIntEQ(wc_InitRng(&rng), 0);
  24380. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24381. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24382. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24383. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  24384. pkcs7->content = data;
  24385. pkcs7->contentSz = (word32)sizeof(data);
  24386. pkcs7->privateKey = key;
  24387. pkcs7->privateKeySz = (word32)sizeof(key);
  24388. pkcs7->encryptOID = RSAk;
  24389. pkcs7->hashOID = SHAh;
  24390. pkcs7->rng = &rng;
  24391. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24392. wc_PKCS7_Free(pkcs7);
  24393. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24394. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24395. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24396. /* Pass in bad args. */
  24397. AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
  24398. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24399. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24400. badOutSz), BAD_FUNC_ARG);
  24401. pkcs7->hashOID = 0; /* bad hashOID */
  24402. AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
  24403. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24404. !defined(NO_RSA) && !defined(NO_SHA256)
  24405. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24406. * Example callback assumes SHA-256, so only run test if compiled in. */
  24407. wc_PKCS7_Free(pkcs7);
  24408. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24409. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24410. pkcs7->content = data;
  24411. pkcs7->contentSz = (word32)sizeof(data);
  24412. pkcs7->privateKey = key;
  24413. pkcs7->privateKeySz = (word32)sizeof(key);
  24414. pkcs7->encryptOID = RSAk;
  24415. pkcs7->hashOID = SHA256h;
  24416. pkcs7->rng = &rng;
  24417. AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24418. AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24419. #endif
  24420. printf(resultFmt, passed);
  24421. wc_PKCS7_Free(pkcs7);
  24422. wc_FreeRng(&rng);
  24423. #endif
  24424. return 0;
  24425. } /* END test_wc_PKCS7_EncodeSignedData */
  24426. /*
  24427. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24428. */
  24429. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24430. {
  24431. #if defined(HAVE_PKCS7)
  24432. int ret, i;
  24433. PKCS7* pkcs7;
  24434. WC_RNG rng;
  24435. byte outputHead[FOURK_BUF/2];
  24436. byte outputFoot[FOURK_BUF/2];
  24437. word32 outputHeadSz = (word32)sizeof(outputHead);
  24438. word32 outputFootSz = (word32)sizeof(outputFoot);
  24439. byte data[FOURK_BUF];
  24440. wc_HashAlg hash;
  24441. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24442. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24443. word32 hashSz = wc_HashGetDigestSize(hashType);
  24444. #ifndef NO_RSA
  24445. #if defined(USE_CERT_BUFFERS_2048)
  24446. byte key[sizeof(client_key_der_2048)];
  24447. byte cert[sizeof(client_cert_der_2048)];
  24448. word32 keySz = (word32)sizeof(key);
  24449. word32 certSz = (word32)sizeof(cert);
  24450. XMEMSET(key, 0, keySz);
  24451. XMEMSET(cert, 0, certSz);
  24452. XMEMCPY(key, client_key_der_2048, keySz);
  24453. XMEMCPY(cert, client_cert_der_2048, certSz);
  24454. #elif defined(USE_CERT_BUFFERS_1024)
  24455. byte key[sizeof_client_key_der_1024];
  24456. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24457. word32 keySz = (word32)sizeof(key);
  24458. word32 certSz = (word32)sizeof(cert);
  24459. XMEMSET(key, 0, keySz);
  24460. XMEMSET(cert, 0, certSz);
  24461. XMEMCPY(key, client_key_der_1024, keySz);
  24462. XMEMCPY(cert, client_cert_der_1024, certSz);
  24463. #else
  24464. unsigned char cert[ONEK_BUF];
  24465. unsigned char key[ONEK_BUF];
  24466. XFILE fp;
  24467. int certSz;
  24468. int keySz;
  24469. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  24470. AssertTrue((fp != XBADFILE));
  24471. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  24472. XFCLOSE(fp);
  24473. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  24474. AssertTrue(fp != XBADFILE);
  24475. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  24476. XFCLOSE(fp);
  24477. #endif
  24478. #elif defined(HAVE_ECC)
  24479. #if defined(USE_CERT_BUFFERS_256)
  24480. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24481. unsigned char key[sizeof(ecc_clikey_der_256)];
  24482. int certSz = (int)sizeof(cert);
  24483. int keySz = (int)sizeof(key);
  24484. XMEMSET(cert, 0, certSz);
  24485. XMEMSET(key, 0, keySz);
  24486. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24487. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24488. #else
  24489. unsigned char cert[ONEK_BUF];
  24490. unsigned char key[ONEK_BUF];
  24491. XFILE fp;
  24492. int certSz, keySz;
  24493. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  24494. AssertTrue(fp != XBADFILE);
  24495. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  24496. XFCLOSE(fp);
  24497. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  24498. AssertTrue(fp != XBADFILE);
  24499. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  24500. XFCLOSE(fp);
  24501. #endif
  24502. #endif
  24503. /* initialize large data with sequence */
  24504. for (i=0; i<(int)sizeof(data); i++)
  24505. data[i] = i & 0xff;
  24506. XMEMSET(outputHead, 0, outputHeadSz);
  24507. XMEMSET(outputFoot, 0, outputFootSz);
  24508. AssertIntEQ(wc_InitRng(&rng), 0);
  24509. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24510. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24511. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24512. printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
  24513. pkcs7->content = NULL; /* not used for ex */
  24514. pkcs7->contentSz = (word32)sizeof(data);
  24515. pkcs7->privateKey = key;
  24516. pkcs7->privateKeySz = (word32)sizeof(key);
  24517. pkcs7->encryptOID = RSAk;
  24518. pkcs7->hashOID = SHAh;
  24519. pkcs7->rng = &rng;
  24520. /* calculate hash for content */
  24521. ret = wc_HashInit(&hash, hashType);
  24522. if (ret == 0) {
  24523. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24524. if (ret == 0) {
  24525. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24526. }
  24527. wc_HashFree(&hash, hashType);
  24528. }
  24529. AssertIntEQ(ret, 0);
  24530. /* Perform PKCS7 sign using hash directly */
  24531. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24532. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24533. AssertIntGT(outputHeadSz, 0);
  24534. AssertIntGT(outputFootSz, 0);
  24535. wc_PKCS7_Free(pkcs7);
  24536. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24537. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24538. /* required parameter even on verify when using _ex, if using outputHead
  24539. * and outputFoot */
  24540. pkcs7->contentSz = (word32)sizeof(data);
  24541. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24542. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24543. wc_PKCS7_Free(pkcs7);
  24544. /* assembly complete PKCS7 sign and use normal verify */
  24545. {
  24546. byte* output = (byte*)XMALLOC(
  24547. outputHeadSz + sizeof(data) + outputFootSz,
  24548. HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24549. word32 outputSz = 0;
  24550. AssertNotNull(output);
  24551. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24552. outputSz += outputHeadSz;
  24553. XMEMCPY(&output[outputSz], data, sizeof(data));
  24554. outputSz += sizeof(data);
  24555. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24556. outputSz += outputFootSz;
  24557. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24558. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24559. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24560. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24561. }
  24562. /* Pass in bad args. */
  24563. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24564. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24565. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24566. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24567. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24568. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24569. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24570. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24571. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24572. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24573. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24574. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24575. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24576. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24577. pkcs7->hashOID = 0; /* bad hashOID */
  24578. AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24579. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24580. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24581. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24582. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24583. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24584. #ifndef NO_PKCS7_STREAM
  24585. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24586. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24587. #else
  24588. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24589. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24590. #endif
  24591. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24592. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24593. #ifndef NO_PKCS7_STREAM
  24594. /* can pass in 0 buffer length with streaming API */
  24595. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24596. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24597. #else
  24598. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24599. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24600. #endif
  24601. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24602. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24603. #ifndef NO_PKCS7_STREAM
  24604. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24605. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24606. #else
  24607. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24608. outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
  24609. #endif
  24610. printf(resultFmt, passed);
  24611. wc_PKCS7_Free(pkcs7);
  24612. wc_FreeRng(&rng);
  24613. #endif
  24614. return 0;
  24615. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24616. #if defined(HAVE_PKCS7)
  24617. /**
  24618. * Loads certs/keys from files or buffers into the argument buffers,
  24619. * helper function called by CreatePKCS7SignedData().
  24620. *
  24621. * Returns 0 on success, negative on error.
  24622. */
  24623. static int LoadPKCS7SignedDataCerts(
  24624. int useIntermediateCertChain, int pkAlgoType,
  24625. byte* intCARoot, word32* intCARootSz,
  24626. byte* intCA1, word32* intCA1Sz,
  24627. byte* intCA2, word32* intCA2Sz,
  24628. byte* cert, word32* certSz,
  24629. byte* key, word32* keySz)
  24630. {
  24631. int ret = 0;
  24632. FILE* fp = NULL;
  24633. #ifndef NO_RSA
  24634. const char* intCARootRSA = "./certs/ca-cert.der";
  24635. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24636. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24637. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24638. const char* intServKeyRSA = "./certs/server-key.der";
  24639. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24640. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24641. const char* cli1024Key = "./certs/1024/client-key.der";
  24642. #endif
  24643. #endif
  24644. #ifdef HAVE_ECC
  24645. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24646. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24647. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24648. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24649. const char* intServKeyECC = "./certs/ecc-key.der";
  24650. #ifndef USE_CERT_BUFFERS_256
  24651. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24652. const char* cliEccKey = "./certs/client-ecc-key.der";
  24653. #endif
  24654. #endif
  24655. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24656. ((useIntermediateCertChain == 1) &&
  24657. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24658. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24659. return BAD_FUNC_ARG;
  24660. }
  24661. /* Read/load certs and keys to use for signing based on PK type and chain */
  24662. switch (pkAlgoType) {
  24663. #ifndef NO_RSA
  24664. case RSA_TYPE:
  24665. if (useIntermediateCertChain == 1) {
  24666. fp = XFOPEN(intCARootRSA, "rb");
  24667. AssertNotNull(fp);
  24668. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24669. XFCLOSE(fp);
  24670. AssertIntGT(*intCARootSz, 0);
  24671. fp = XFOPEN(intCA1RSA, "rb");
  24672. AssertNotNull(fp);
  24673. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24674. XFCLOSE(fp);
  24675. AssertIntGT(*intCA1Sz, 0);
  24676. fp = XFOPEN(intCA2RSA, "rb");
  24677. AssertNotNull(fp);
  24678. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24679. XFCLOSE(fp);
  24680. AssertIntGT(*intCA2Sz, 0);
  24681. fp = XFOPEN(intServCertRSA, "rb");
  24682. AssertNotNull(fp);
  24683. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24684. XFCLOSE(fp);
  24685. AssertIntGT(*certSz, 0);
  24686. fp = XFOPEN(intServKeyRSA, "rb");
  24687. AssertNotNull(fp);
  24688. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24689. XFCLOSE(fp);
  24690. AssertIntGT(*keySz, 0);
  24691. }
  24692. else {
  24693. #if defined(USE_CERT_BUFFERS_2048)
  24694. *keySz = sizeof_client_key_der_2048;
  24695. *certSz = sizeof_client_cert_der_2048;
  24696. XMEMCPY(key, client_key_der_2048, *keySz);
  24697. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24698. #elif defined(USE_CERT_BUFFERS_1024)
  24699. *keySz = sizeof_client_key_der_1024;
  24700. *certSz = sizeof_client_cert_der_1024;
  24701. XMEMCPY(key, client_key_der_1024, *keySz);
  24702. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24703. #else
  24704. fp = XFOPEN(cli1024Key, "rb");
  24705. AssertNotNull(fp);
  24706. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24707. XFCLOSE(fp);
  24708. AssertIntGT(*keySz, 0);
  24709. fp = XFOPEN(cli1024Cert, "rb");
  24710. AssertNotNull(fp);
  24711. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24712. XFCLOSE(fp);
  24713. AssertIntGT(*certSz, 0);
  24714. #endif /* USE_CERT_BUFFERS_2048 */
  24715. }
  24716. break;
  24717. #endif /* !NO_RSA */
  24718. #ifdef HAVE_ECC
  24719. case ECC_TYPE:
  24720. if (useIntermediateCertChain == 1) {
  24721. fp = XFOPEN(intCARootECC, "rb");
  24722. AssertNotNull(fp);
  24723. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24724. XFCLOSE(fp);
  24725. AssertIntGT(*intCARootSz, 0);
  24726. fp = XFOPEN(intCA1ECC, "rb");
  24727. AssertNotNull(fp);
  24728. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24729. XFCLOSE(fp);
  24730. AssertIntGT(*intCA1Sz, 0);
  24731. fp = XFOPEN(intCA2ECC, "rb");
  24732. AssertNotNull(fp);
  24733. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24734. XFCLOSE(fp);
  24735. AssertIntGT(*intCA2Sz, 0);
  24736. fp = XFOPEN(intServCertECC, "rb");
  24737. AssertNotNull(fp);
  24738. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24739. XFCLOSE(fp);
  24740. AssertIntGT(*certSz, 0);
  24741. fp = XFOPEN(intServKeyECC, "rb");
  24742. AssertNotNull(fp);
  24743. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24744. XFCLOSE(fp);
  24745. AssertIntGT(*keySz, 0);
  24746. }
  24747. else {
  24748. #if defined(USE_CERT_BUFFERS_256)
  24749. *keySz = sizeof_ecc_clikey_der_256;
  24750. *certSz = sizeof_cliecc_cert_der_256;
  24751. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24752. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24753. #else
  24754. fp = XFOPEN(cliEccKey, "rb");
  24755. AssertNotNull(fp);
  24756. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24757. XFCLOSE(fp);
  24758. AssertIntGT(*keySz, 0);
  24759. fp = XFOPEN(cliEccCert, "rb");
  24760. AssertNotNull(fp);
  24761. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24762. XFCLOSE(fp);
  24763. AssertIntGT(*certSz, 0);
  24764. #endif /* USE_CERT_BUFFERS_256 */
  24765. }
  24766. break;
  24767. #endif /* HAVE_ECC */
  24768. default:
  24769. WOLFSSL_MSG("Unsupported SignedData PK type");
  24770. ret = BAD_FUNC_ARG;
  24771. break;
  24772. }
  24773. return ret;
  24774. }
  24775. /**
  24776. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24777. *
  24778. * output output buffer to place SignedData
  24779. * outputSz size of output buffer
  24780. * data data buffer to be signed
  24781. * dataSz size of data buffer
  24782. * withAttribs [1/0] include attributes in SignedData message
  24783. * detachedSig [1/0] create detached signature, no content
  24784. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24785. * root CAs in bundle
  24786. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24787. *
  24788. * Return size of bundle created on success, negative on error */
  24789. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24790. byte* data, word32 dataSz,
  24791. int withAttribs, int detachedSig,
  24792. int useIntermediateCertChain,
  24793. int pkAlgoType)
  24794. {
  24795. int ret = 0;
  24796. WC_RNG rng;
  24797. PKCS7* pkcs7 = NULL;
  24798. static byte messageTypeOid[] =
  24799. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24800. 0x09, 0x02 };
  24801. static byte messageType[] = { 0x13, 2, '1', '9' };
  24802. PKCS7Attrib attribs[] =
  24803. {
  24804. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24805. sizeof(messageType) }
  24806. };
  24807. byte intCARoot[TWOK_BUF];
  24808. byte intCA1[TWOK_BUF];
  24809. byte intCA2[TWOK_BUF];
  24810. byte cert[TWOK_BUF];
  24811. byte key[TWOK_BUF];
  24812. word32 intCARootSz = sizeof(intCARoot);
  24813. word32 intCA1Sz = sizeof(intCA1);
  24814. word32 intCA2Sz = sizeof(intCA2);
  24815. word32 certSz = sizeof(cert);
  24816. word32 keySz = sizeof(key);
  24817. XMEMSET(intCARoot, 0, intCARootSz);
  24818. XMEMSET(intCA1, 0, intCA1Sz);
  24819. XMEMSET(intCA2, 0, intCA2Sz);
  24820. XMEMSET(cert, 0, certSz);
  24821. XMEMSET(key, 0, keySz);
  24822. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24823. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24824. cert, &certSz, key, &keySz);
  24825. AssertIntEQ(ret, 0);
  24826. XMEMSET(output, 0, outputSz);
  24827. AssertIntEQ(wc_InitRng(&rng), 0);
  24828. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24829. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24830. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24831. if (useIntermediateCertChain == 1) {
  24832. /* Add intermediate and root CA certs into SignedData Certs SET */
  24833. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24834. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24835. AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24836. }
  24837. pkcs7->content = data;
  24838. pkcs7->contentSz = dataSz;
  24839. pkcs7->privateKey = key;
  24840. pkcs7->privateKeySz = (word32)sizeof(key);
  24841. if (pkAlgoType == RSA_TYPE) {
  24842. pkcs7->encryptOID = RSAk;
  24843. }
  24844. else {
  24845. pkcs7->encryptOID = ECDSAk;
  24846. }
  24847. pkcs7->hashOID = SHAh;
  24848. pkcs7->rng = &rng;
  24849. if (withAttribs) {
  24850. /* include a signed attribute */
  24851. pkcs7->signedAttribs = attribs;
  24852. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24853. }
  24854. if (detachedSig) {
  24855. AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24856. }
  24857. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24858. AssertIntGT(outputSz, 0);
  24859. wc_PKCS7_Free(pkcs7);
  24860. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24861. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24862. if (detachedSig) {
  24863. pkcs7->content = data;
  24864. pkcs7->contentSz = dataSz;
  24865. }
  24866. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24867. wc_PKCS7_Free(pkcs7);
  24868. wc_FreeRng(&rng);
  24869. return outputSz;
  24870. }
  24871. #endif
  24872. /*
  24873. * Testing wc_PKCS_VerifySignedData()
  24874. */
  24875. static int test_wc_PKCS7_VerifySignedData(void)
  24876. {
  24877. #if defined(HAVE_PKCS7)
  24878. PKCS7* pkcs7;
  24879. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24880. word32 outputSz = sizeof(output);
  24881. byte data[] = "Test data to encode.";
  24882. byte badOut[1];
  24883. word32 badOutSz = 0;
  24884. byte badContent[] = "This is different content than was signed";
  24885. int ret;
  24886. wc_HashAlg hash;
  24887. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24888. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24889. word32 hashSz = wc_HashGetDigestSize(hashType);
  24890. printf(testingFmt, "wc_PKCS7_VerifySignedData()");
  24891. #ifndef NO_RSA
  24892. /* Success test with RSA certs/key */
  24893. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24894. (word32)sizeof(data),
  24895. 0, 0, 0, RSA_TYPE)), 0);
  24896. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24897. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24898. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24899. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24900. #endif
  24901. #ifdef HAVE_ECC
  24902. #ifndef NO_RSA
  24903. wc_PKCS7_Free(pkcs7);
  24904. #endif
  24905. /* Success test with ECC certs/key */
  24906. outputSz = sizeof(output);
  24907. XMEMSET(output, 0, outputSz);
  24908. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24909. (word32)sizeof(data),
  24910. 0, 0, 0, ECC_TYPE)), 0);
  24911. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24912. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24913. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24914. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24915. #endif
  24916. /* Test bad args. */
  24917. #if !defined(NO_RSA) || defined(HAVE_ECC)
  24918. AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24919. BAD_FUNC_ARG);
  24920. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24921. BAD_FUNC_ARG);
  24922. #ifndef NO_PKCS7_STREAM
  24923. /* can pass in 0 buffer length with streaming API */
  24924. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24925. badOutSz), WC_PKCS7_WANT_READ_E);
  24926. #else
  24927. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24928. badOutSz), BAD_FUNC_ARG);
  24929. #endif
  24930. wc_PKCS7_Free(pkcs7);
  24931. #endif /* !NO_RSA || HAVE_ECC */
  24932. /* Invalid content should error, use detached signature so we can
  24933. * easily change content */
  24934. #ifndef NO_RSA
  24935. /* Try RSA certs/key/sig first */
  24936. outputSz = sizeof(output);
  24937. XMEMSET(output, 0, outputSz);
  24938. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24939. (word32)sizeof(data),
  24940. 1, 1, 0, RSA_TYPE)), 0);
  24941. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24942. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24943. pkcs7->content = badContent;
  24944. pkcs7->contentSz = sizeof(badContent);
  24945. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24946. SIG_VERIFY_E);
  24947. wc_PKCS7_Free(pkcs7);
  24948. /* Test success case with detached signature and valid content */
  24949. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24950. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24951. pkcs7->content = data;
  24952. pkcs7->contentSz = sizeof(data);
  24953. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24954. wc_PKCS7_Free(pkcs7);
  24955. /* verify using pre-computed content digest only (no content) */
  24956. {
  24957. /* calculate hash for content */
  24958. ret = wc_HashInit(&hash, hashType);
  24959. if (ret == 0) {
  24960. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  24961. if (ret == 0) {
  24962. ret = wc_HashFinal(&hash, hashType, hashBuf);
  24963. }
  24964. wc_HashFree(&hash, hashType);
  24965. }
  24966. AssertIntEQ(ret, 0);
  24967. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24968. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24969. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24970. output, outputSz,
  24971. NULL, 0), 0);
  24972. wc_PKCS7_Free(pkcs7);
  24973. }
  24974. #endif /* !NO_RSA */
  24975. #ifdef HAVE_ECC
  24976. /* Try ECC certs/key/sig next */
  24977. outputSz = sizeof(output);
  24978. XMEMSET(output, 0, outputSz);
  24979. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24980. (word32)sizeof(data),
  24981. 1, 1, 0, ECC_TYPE)), 0);
  24982. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24983. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24984. pkcs7->content = badContent;
  24985. pkcs7->contentSz = sizeof(badContent);
  24986. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24987. SIG_VERIFY_E);
  24988. wc_PKCS7_Free(pkcs7);
  24989. /* Test success case with detached signature and valid content */
  24990. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24991. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24992. pkcs7->content = data;
  24993. pkcs7->contentSz = sizeof(data);
  24994. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24995. wc_PKCS7_Free(pkcs7);
  24996. /* verify using pre-computed content digest only (no content) */
  24997. {
  24998. /* calculate hash for content */
  24999. ret = wc_HashInit(&hash, hashType);
  25000. if (ret == 0) {
  25001. ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
  25002. if (ret == 0) {
  25003. ret = wc_HashFinal(&hash, hashType, hashBuf);
  25004. }
  25005. wc_HashFree(&hash, hashType);
  25006. }
  25007. AssertIntEQ(ret, 0);
  25008. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25009. AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25010. AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25011. output, outputSz,
  25012. NULL, 0), 0);
  25013. wc_PKCS7_Free(pkcs7);
  25014. }
  25015. #endif
  25016. /* Test verify on signedData containing intermediate/root CA certs */
  25017. #ifndef NO_RSA
  25018. outputSz = sizeof(output);
  25019. XMEMSET(output, 0, outputSz);
  25020. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25021. (word32)sizeof(data),
  25022. 0, 0, 1, RSA_TYPE)), 0);
  25023. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25024. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25025. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25026. wc_PKCS7_Free(pkcs7);
  25027. #endif /* !NO_RSA */
  25028. #ifdef HAVE_ECC
  25029. outputSz = sizeof(output);
  25030. XMEMSET(output, 0, outputSz);
  25031. AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25032. (word32)sizeof(data),
  25033. 0, 0, 1, ECC_TYPE)), 0);
  25034. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25035. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25036. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25037. wc_PKCS7_Free(pkcs7);
  25038. #endif /* HAVE_ECC */
  25039. printf(resultFmt, passed);
  25040. #endif
  25041. return 0;
  25042. } /* END test_wc_PKCS7_VerifySignedData() */
  25043. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25044. !defined(NO_AES_256)
  25045. static const byte defKey[] = {
  25046. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25047. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25048. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25049. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25050. };
  25051. static byte aesHandle[32]; /* simulated hardware key handle */
  25052. /* return 0 on success */
  25053. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25054. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25055. byte* in, int inSz, byte* out, void* usrCtx)
  25056. {
  25057. int ret;
  25058. Aes aes;
  25059. if (usrCtx == NULL) {
  25060. /* no simulated handle passed in */
  25061. return -1;
  25062. }
  25063. switch (encryptOID) {
  25064. case AES256CBCb:
  25065. if (ivSz != AES_BLOCK_SIZE)
  25066. return BAD_FUNC_ARG;
  25067. break;
  25068. default:
  25069. WOLFSSL_MSG("Unsupported content cipher type for test");
  25070. return ALGO_ID_E;
  25071. };
  25072. /* simulate using handle to get key */
  25073. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25074. if (ret == 0) {
  25075. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25076. if (ret == 0)
  25077. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25078. wc_AesFree(&aes);
  25079. }
  25080. (void)aad;
  25081. (void)aadSz;
  25082. (void)authTag;
  25083. (void)authTagSz;
  25084. (void)pkcs7;
  25085. return ret;
  25086. }
  25087. /* returns key size on success */
  25088. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25089. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25090. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25091. {
  25092. int ret = -1;
  25093. if (out == NULL)
  25094. return BAD_FUNC_ARG;
  25095. if (keyId[0] != 0x00) {
  25096. return -1;
  25097. }
  25098. if (type != (int)PKCS7_KEKRI) {
  25099. return -1;
  25100. }
  25101. switch (keyWrapAlgo) {
  25102. case AES256_WRAP:
  25103. /* simulate setting a handle for later decryption but use key
  25104. * as handle in the test case here */
  25105. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25106. aesHandle, sizeof(aesHandle), NULL);
  25107. if (ret < 0)
  25108. return ret;
  25109. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25110. if (ret < 0)
  25111. return ret;
  25112. /* return key size on success */
  25113. return sizeof(defKey);
  25114. default:
  25115. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25116. return BAD_KEYWRAP_ALG_E;
  25117. };
  25118. (void)cekSz;
  25119. (void)cek;
  25120. (void)outSz;
  25121. (void)keyIdSz;
  25122. (void)direction;
  25123. (void)orginKey; /* used with KAKRI */
  25124. (void)orginKeySz;
  25125. return ret;
  25126. }
  25127. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25128. /*
  25129. * Testing wc_PKCS7_EncodeEnvelopedData()
  25130. */
  25131. static int test_wc_PKCS7_EncodeDecodeEnvelopedData (void)
  25132. {
  25133. #if defined(HAVE_PKCS7)
  25134. PKCS7* pkcs7;
  25135. #ifdef ECC_TIMING_RESISTANT
  25136. WC_RNG rng;
  25137. #endif
  25138. word32 tempWrd32 = 0;
  25139. byte* tmpBytePtr = NULL;
  25140. const char input[] = "Test data to encode.";
  25141. int i;
  25142. int testSz = 0;
  25143. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25144. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25145. byte* rsaCert = NULL;
  25146. byte* rsaPrivKey = NULL;
  25147. word32 rsaCertSz;
  25148. word32 rsaPrivKeySz;
  25149. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25150. !defined(USE_CERT_BUFFERS_2048) )
  25151. static const char* rsaClientCert = "./certs/client-cert.der";
  25152. static const char* rsaClientKey = "./certs/client-key.der";
  25153. rsaCertSz = (word32)sizeof(rsaClientCert);
  25154. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25155. #endif
  25156. #endif
  25157. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25158. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25159. byte* eccCert = NULL;
  25160. byte* eccPrivKey = NULL;
  25161. word32 eccCertSz;
  25162. word32 eccPrivKeySz;
  25163. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25164. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25165. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25166. #endif
  25167. #endif
  25168. /* Generic buffer size. */
  25169. byte output[ONEK_BUF];
  25170. byte decoded[sizeof(input)/sizeof(char)];
  25171. int decodedSz = 0;
  25172. #ifndef NO_FILESYSTEM
  25173. XFILE certFile;
  25174. XFILE keyFile;
  25175. #endif
  25176. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25177. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25178. /* RSA certs and keys. */
  25179. #if defined(USE_CERT_BUFFERS_1024)
  25180. /* Allocate buffer space. */
  25181. AssertNotNull(rsaCert =
  25182. (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25183. /* Init buffer. */
  25184. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25185. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25186. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
  25187. DYNAMIC_TYPE_TMP_BUFFER));
  25188. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25189. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25190. #elif defined(USE_CERT_BUFFERS_2048)
  25191. /* Allocate buffer */
  25192. AssertNotNull(rsaCert =
  25193. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25194. /* Init buffer. */
  25195. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25196. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25197. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25198. DYNAMIC_TYPE_TMP_BUFFER));
  25199. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25200. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25201. #else
  25202. /* File system. */
  25203. certFile = XFOPEN(rsaClientCert, "rb");
  25204. AssertTrue(certFile != XBADFILE);
  25205. rsaCertSz = (word32)FOURK_BUF;
  25206. AssertNotNull(rsaCert =
  25207. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25208. rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
  25209. XFCLOSE(certFile);
  25210. keyFile = XFOPEN(rsaClientKey, "rb");
  25211. AssertTrue(keyFile != XBADFILE);
  25212. AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25213. DYNAMIC_TYPE_TMP_BUFFER));
  25214. rsaPrivKeySz = (word32)FOURK_BUF;
  25215. rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
  25216. XFCLOSE(keyFile);
  25217. #endif /* USE_CERT_BUFFERS */
  25218. #endif /* NO_RSA */
  25219. /* ECC */
  25220. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25221. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25222. #ifdef USE_CERT_BUFFERS_256
  25223. AssertNotNull(eccCert =
  25224. (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25225. /* Init buffer. */
  25226. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25227. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25228. AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25229. DYNAMIC_TYPE_TMP_BUFFER));
  25230. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25231. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25232. #else /* File system. */
  25233. certFile = XFOPEN(eccClientCert, "rb");
  25234. AssertTrue(certFile != XBADFILE);
  25235. eccCertSz = (word32)FOURK_BUF;
  25236. AssertNotNull(eccCert =
  25237. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25238. eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
  25239. XFCLOSE(certFile);
  25240. keyFile = XFOPEN(eccClientKey, "rb");
  25241. AssertTrue(keyFile != XBADFILE);
  25242. eccPrivKeySz = (word32)FOURK_BUF;
  25243. AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25244. DYNAMIC_TYPE_TMP_BUFFER));
  25245. eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
  25246. XFCLOSE(keyFile);
  25247. #endif /* USE_CERT_BUFFERS_256 */
  25248. #endif /* END HAVE_ECC */
  25249. /* Silence. */
  25250. (void)keyFile;
  25251. (void)certFile;
  25252. {
  25253. const pkcs7EnvelopedVector testVectors[] = {
  25254. /* DATA is a global variable defined in the makefile. */
  25255. #if !defined(NO_RSA)
  25256. #ifndef NO_DES3
  25257. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25258. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25259. #endif /* NO_DES3 */
  25260. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25261. #ifndef NO_AES_128
  25262. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25263. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25264. #endif
  25265. #ifndef NO_AES_192
  25266. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25267. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25268. #endif
  25269. #ifndef NO_AES_256
  25270. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25271. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25272. #endif
  25273. #endif /* NO_AES && HAVE_AES_CBC */
  25274. #endif /* NO_RSA */
  25275. #if defined(HAVE_ECC)
  25276. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25277. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25278. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25279. AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
  25280. eccCertSz, eccPrivKey, eccPrivKeySz},
  25281. #endif
  25282. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25283. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25284. AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
  25285. eccCertSz, eccPrivKey, eccPrivKeySz},
  25286. #endif
  25287. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25288. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25289. AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
  25290. eccCertSz, eccPrivKey, eccPrivKeySz},
  25291. #endif
  25292. #endif /* NO_AES && HAVE_AES_CBC*/
  25293. #endif /* END HAVE_ECC */
  25294. }; /* END pkcs7EnvelopedVector */
  25295. #ifdef ECC_TIMING_RESISTANT
  25296. AssertIntEQ(wc_InitRng(&rng), 0);
  25297. #endif
  25298. printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
  25299. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25300. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25301. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25302. for (i = 0; i < testSz; i++) {
  25303. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25304. (word32)(testVectors + i)->certSz), 0);
  25305. #ifdef ECC_TIMING_RESISTANT
  25306. pkcs7->rng = &rng;
  25307. #endif
  25308. pkcs7->content = (byte*)(testVectors + i)->content;
  25309. pkcs7->contentSz = (testVectors + i)->contentSz;
  25310. pkcs7->contentOID = (testVectors + i)->contentOID;
  25311. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25312. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25313. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25314. pkcs7->privateKey = (testVectors + i)->privateKey;
  25315. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25316. AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25317. (word32)sizeof(output)), 0);
  25318. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25319. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25320. AssertIntGE(decodedSz, 0);
  25321. /* Verify the size of each buffer. */
  25322. AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25323. /* Don't free the last time through the loop. */
  25324. if (i < testSz - 1 ){
  25325. wc_PKCS7_Free(pkcs7);
  25326. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25327. }
  25328. } /* END test loop. */
  25329. }
  25330. /* Test bad args. */
  25331. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25332. (word32)sizeof(output)), BAD_FUNC_ARG);
  25333. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25334. (word32)sizeof(output)), BAD_FUNC_ARG);
  25335. AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25336. printf(resultFmt, passed);
  25337. /* Decode. */
  25338. printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
  25339. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25340. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25341. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25342. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25343. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25344. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25345. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25346. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25347. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25348. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25349. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25350. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25351. /* only a failure for KARI test cases */
  25352. tempWrd32 = pkcs7->singleCertSz;
  25353. pkcs7->singleCertSz = 0;
  25354. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25355. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25356. pkcs7->singleCertSz = tempWrd32;
  25357. tmpBytePtr = pkcs7->singleCert;
  25358. pkcs7->singleCert = NULL;
  25359. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25360. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25361. pkcs7->singleCert = tmpBytePtr;
  25362. #endif
  25363. tempWrd32 = pkcs7->privateKeySz;
  25364. pkcs7->privateKeySz = 0;
  25365. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25366. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25367. pkcs7->privateKeySz = tempWrd32;
  25368. tmpBytePtr = pkcs7->privateKey;
  25369. pkcs7->privateKey = NULL;
  25370. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25371. (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25372. pkcs7->privateKey = tmpBytePtr;
  25373. wc_PKCS7_Free(pkcs7);
  25374. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25375. /* test of decrypt callback with KEKRI enveloped data */
  25376. {
  25377. int envelopedSz;
  25378. const byte keyId[] = { 0x00 };
  25379. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25380. pkcs7->content = (byte*)input;
  25381. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25382. pkcs7->contentOID = DATA;
  25383. pkcs7->encryptOID = AES256CBCb;
  25384. AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25385. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25386. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25387. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25388. AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25389. (word32)sizeof(output))), 0);
  25390. wc_PKCS7_Free(pkcs7);
  25391. /* decode envelopedData */
  25392. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25393. AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25394. AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25395. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25396. envelopedSz, decoded, sizeof(decoded))), 0);
  25397. wc_PKCS7_Free(pkcs7);
  25398. }
  25399. #endif /* !NO_AES && !NO_AES_256 */
  25400. printf(resultFmt, passed);
  25401. #ifndef NO_RSA
  25402. if (rsaCert) {
  25403. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25404. }
  25405. if (rsaPrivKey) {
  25406. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25407. }
  25408. #endif /*NO_RSA */
  25409. #ifdef HAVE_ECC
  25410. if (eccCert) {
  25411. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25412. }
  25413. if (eccPrivKey) {
  25414. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25415. }
  25416. #endif /* HAVE_ECC */
  25417. #ifdef ECC_TIMING_RESISTANT
  25418. wc_FreeRng(&rng);
  25419. #endif
  25420. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && !defined(NO_RSA)
  25421. {
  25422. byte out[7];
  25423. byte *cms;
  25424. word32 cmsSz;
  25425. XFILE cmsFile;
  25426. XMEMSET(out, 0, sizeof(out));
  25427. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25428. cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
  25429. AssertTrue(cmsFile != XBADFILE);
  25430. cmsSz = (word32)FOURK_BUF;
  25431. AssertNotNull(cms =
  25432. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  25433. cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
  25434. XFCLOSE(cmsFile);
  25435. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25436. sizeof_client_cert_der_2048), 0);
  25437. pkcs7->privateKey = (byte*)client_key_der_2048;
  25438. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25439. AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25440. 2), 0);
  25441. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25442. sizeof(out)), 0);
  25443. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25444. AssertIntEQ(XMEMCMP(out, "test", 4), 0);
  25445. wc_PKCS7_Free(pkcs7);
  25446. }
  25447. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 */
  25448. #endif /* HAVE_PKCS7 */
  25449. return 0;
  25450. } /* END test_wc_PKCS7_EncodeEnvelopedData() */
  25451. /*
  25452. * Testing wc_PKCS7_EncodeEncryptedData()
  25453. */
  25454. static int test_wc_PKCS7_EncodeEncryptedData (void)
  25455. {
  25456. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25457. PKCS7* pkcs7 = NULL;
  25458. byte* tmpBytePtr = NULL;
  25459. byte encrypted[TWOK_BUF];
  25460. byte decoded[TWOK_BUF];
  25461. word32 tmpWrd32 = 0;
  25462. int tmpInt = 0;
  25463. int decodedSz;
  25464. int encryptedSz;
  25465. int testSz;
  25466. int i;
  25467. const byte data[] = { /* Hello World */
  25468. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25469. 0x72,0x6c,0x64
  25470. };
  25471. #ifndef NO_DES3
  25472. byte desKey[] = {
  25473. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25474. };
  25475. byte des3Key[] = {
  25476. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25477. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25478. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25479. };
  25480. #endif
  25481. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25482. #ifndef NO_AES_128
  25483. byte aes128Key[] = {
  25484. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25485. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25486. };
  25487. #endif
  25488. #ifndef NO_AES_192
  25489. byte aes192Key[] = {
  25490. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25491. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25492. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25493. };
  25494. #endif
  25495. #ifndef NO_AES_256
  25496. byte aes256Key[] = {
  25497. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25498. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25499. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25500. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25501. };
  25502. #endif
  25503. #endif /* !NO_AES && HAVE_AES_CBC */
  25504. const pkcs7EncryptedVector testVectors[] =
  25505. {
  25506. #ifndef NO_DES3
  25507. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25508. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25509. #endif /* !NO_DES3 */
  25510. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25511. #ifndef NO_AES_128
  25512. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25513. sizeof(aes128Key)},
  25514. #endif
  25515. #ifndef NO_AES_192
  25516. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25517. sizeof(aes192Key)},
  25518. #endif
  25519. #ifndef NO_AES_256
  25520. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25521. sizeof(aes256Key)},
  25522. #endif
  25523. #endif /* !NO_AES && HAVE_AES_CBC */
  25524. };
  25525. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25526. for (i = 0; i < testSz; i++) {
  25527. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25528. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25529. pkcs7->content = (byte*)testVectors[i].content;
  25530. pkcs7->contentSz = testVectors[i].contentSz;
  25531. pkcs7->contentOID = testVectors[i].contentOID;
  25532. pkcs7->encryptOID = testVectors[i].encryptOID;
  25533. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25534. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25535. pkcs7->heap = HEAP_HINT;
  25536. /* encode encryptedData */
  25537. encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25538. sizeof(encrypted));
  25539. AssertIntGT(encryptedSz, 0);
  25540. /* Decode encryptedData */
  25541. decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25542. decoded, sizeof(decoded));
  25543. AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25544. /* Keep values for last itr. */
  25545. if (i < testSz - 1) {
  25546. wc_PKCS7_Free(pkcs7);
  25547. }
  25548. }
  25549. if (pkcs7 == NULL || testSz == 0) {
  25550. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25551. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25552. }
  25553. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  25554. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25555. sizeof(encrypted)),BAD_FUNC_ARG);
  25556. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25557. sizeof(encrypted)), BAD_FUNC_ARG);
  25558. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25559. 0), BAD_FUNC_ARG);
  25560. /* Testing the struct. */
  25561. tmpBytePtr = pkcs7->content;
  25562. pkcs7->content = NULL;
  25563. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25564. sizeof(encrypted)), BAD_FUNC_ARG);
  25565. pkcs7->content = tmpBytePtr;
  25566. tmpWrd32 = pkcs7->contentSz;
  25567. pkcs7->contentSz = 0;
  25568. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25569. sizeof(encrypted)), BAD_FUNC_ARG);
  25570. pkcs7->contentSz = tmpWrd32;
  25571. tmpInt = pkcs7->encryptOID;
  25572. pkcs7->encryptOID = 0;
  25573. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25574. sizeof(encrypted)), BAD_FUNC_ARG);
  25575. pkcs7->encryptOID = tmpInt;
  25576. tmpBytePtr = pkcs7->encryptionKey;
  25577. pkcs7->encryptionKey = NULL;
  25578. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25579. sizeof(encrypted)), BAD_FUNC_ARG);
  25580. pkcs7->encryptionKey = tmpBytePtr;
  25581. tmpWrd32 = pkcs7->encryptionKeySz;
  25582. pkcs7->encryptionKeySz = 0;
  25583. AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25584. sizeof(encrypted)), BAD_FUNC_ARG);
  25585. pkcs7->encryptionKeySz = tmpWrd32;
  25586. printf(resultFmt, passed);
  25587. printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
  25588. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25589. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25590. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25591. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25592. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25593. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25594. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25595. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25596. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25597. decoded, 0), BAD_FUNC_ARG);
  25598. /* Test struct fields */
  25599. tmpBytePtr = pkcs7->encryptionKey;
  25600. pkcs7->encryptionKey = NULL;
  25601. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25602. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25603. pkcs7->encryptionKey = tmpBytePtr;
  25604. pkcs7->encryptionKeySz = 0;
  25605. AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25606. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25607. printf(resultFmt, passed);
  25608. wc_PKCS7_Free(pkcs7);
  25609. #endif
  25610. return 0;
  25611. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25612. /*
  25613. * Testing wc_PKCS7_Degenerate()
  25614. */
  25615. static int test_wc_PKCS7_Degenerate(void)
  25616. {
  25617. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25618. PKCS7* pkcs7;
  25619. char fName[] = "./certs/test-degenerate.p7b";
  25620. XFILE f;
  25621. byte der[4096];
  25622. word32 derSz;
  25623. int ret;
  25624. printf(testingFmt, "wc_PKCS7_Degenerate()");
  25625. AssertNotNull(f = XFOPEN(fName, "rb"));
  25626. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25627. derSz = (word32)ret;
  25628. XFCLOSE(f);
  25629. /* test degenerate success */
  25630. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25631. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25632. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25633. #ifndef NO_RSA
  25634. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25635. #else
  25636. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25637. #endif
  25638. wc_PKCS7_Free(pkcs7);
  25639. /* test with turning off degenerate cases */
  25640. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25641. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25642. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25643. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25644. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
  25645. wc_PKCS7_Free(pkcs7);
  25646. printf(resultFmt, passed);
  25647. #endif
  25648. return 0;
  25649. } /* END test_wc_PKCS7_Degenerate() */
  25650. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25651. defined(ASN_BER_TO_DER) && !defined(NO_DES3)
  25652. static byte berContent[] = {
  25653. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25654. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  25655. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  25656. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  25657. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  25658. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  25659. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  25660. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  25661. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  25662. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  25663. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  25664. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  25665. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  25666. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  25667. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  25668. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  25669. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  25670. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  25671. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  25672. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  25673. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  25674. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  25675. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  25676. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  25677. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  25678. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  25679. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  25680. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  25681. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  25682. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  25683. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  25684. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  25685. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  25686. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  25687. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  25688. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  25689. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  25690. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  25691. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  25692. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  25693. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  25694. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  25695. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  25696. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  25697. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  25698. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  25699. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  25700. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  25701. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  25702. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  25703. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  25704. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  25705. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  25706. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  25707. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  25708. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  25709. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  25710. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  25711. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  25712. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  25713. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  25714. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  25715. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  25716. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  25717. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  25718. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  25719. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  25720. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  25721. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  25722. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  25723. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  25724. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  25725. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  25726. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  25727. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  25728. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  25729. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  25730. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  25731. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  25732. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  25733. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  25734. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  25735. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  25736. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  25737. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  25738. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  25739. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  25740. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  25741. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  25742. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  25743. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  25744. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  25745. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  25746. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  25747. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  25748. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  25749. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  25750. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  25751. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  25752. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  25753. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  25754. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  25755. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  25756. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  25757. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  25758. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  25759. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  25760. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  25761. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  25762. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  25763. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  25764. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  25765. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  25766. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  25767. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  25768. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  25769. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  25770. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  25771. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  25772. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  25773. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  25774. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  25775. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  25776. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  25777. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  25778. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  25779. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  25780. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  25781. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  25782. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  25783. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  25784. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  25785. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  25786. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  25787. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  25788. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  25789. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  25790. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  25791. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  25792. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  25793. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  25794. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  25795. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  25796. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  25797. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  25798. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  25799. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  25800. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  25801. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  25802. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  25803. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  25804. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  25805. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  25806. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  25807. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  25808. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  25809. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  25810. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  25811. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  25812. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  25813. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  25814. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  25815. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  25816. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  25817. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  25818. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  25819. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  25820. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  25821. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  25822. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  25823. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  25824. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  25825. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  25826. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  25827. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  25828. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  25829. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  25830. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  25831. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  25832. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  25833. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  25834. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  25835. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  25836. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  25837. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  25838. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  25839. 0x00, 0x00, 0x00, 0x00, 0x00
  25840. };
  25841. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER && !NO_DES3 */
  25842. /*
  25843. * Testing wc_PKCS7_BER()
  25844. */
  25845. static int test_wc_PKCS7_BER(void)
  25846. {
  25847. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  25848. defined(ASN_BER_TO_DER)
  25849. PKCS7* pkcs7;
  25850. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  25851. XFILE f;
  25852. byte der[4096];
  25853. #ifndef NO_DES3
  25854. byte decoded[2048];
  25855. #endif
  25856. word32 derSz;
  25857. int ret;
  25858. printf(testingFmt, "wc_PKCS7_BER()");
  25859. AssertNotNull(f = XFOPEN(fName, "rb"));
  25860. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25861. derSz = (word32)ret;
  25862. XFCLOSE(f);
  25863. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25864. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25865. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25866. #ifndef NO_RSA
  25867. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25868. #else
  25869. AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25870. #endif
  25871. wc_PKCS7_Free(pkcs7);
  25872. #ifndef NO_DES3
  25873. /* decode BER content */
  25874. AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
  25875. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25876. derSz = (word32)ret;
  25877. XFCLOSE(f);
  25878. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25879. #ifndef NO_RSA
  25880. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25881. #else
  25882. AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  25883. #endif
  25884. AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
  25885. AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
  25886. derSz = (word32)ret;
  25887. XFCLOSE(f);
  25888. pkcs7->privateKey = der;
  25889. pkcs7->privateKeySz = derSz;
  25890. #ifndef NO_RSA
  25891. #ifdef WOLFSSL_SP_MATH
  25892. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25893. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  25894. #else
  25895. AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25896. sizeof(berContent), decoded, sizeof(decoded)), 0);
  25897. #endif
  25898. #else
  25899. AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  25900. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  25901. #endif
  25902. wc_PKCS7_Free(pkcs7);
  25903. #endif /* !NO_DES3 */
  25904. printf(resultFmt, passed);
  25905. #endif
  25906. return 0;
  25907. } /* END test_wc_PKCS7_BER() */
  25908. static int test_PKCS7_signed_enveloped(void)
  25909. {
  25910. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  25911. !defined(NO_FILESYSTEM)
  25912. XFILE f;
  25913. PKCS7* pkcs7;
  25914. #ifdef HAVE_AES_CBC
  25915. PKCS7* inner;
  25916. #endif
  25917. void* pt;
  25918. WC_RNG rng;
  25919. unsigned char key[FOURK_BUF/2];
  25920. unsigned char cert[FOURK_BUF/2];
  25921. unsigned char env[FOURK_BUF];
  25922. int envSz = FOURK_BUF;
  25923. int keySz;
  25924. int certSz;
  25925. unsigned char sig[FOURK_BUF * 2];
  25926. int sigSz = FOURK_BUF * 2;
  25927. #ifdef HAVE_AES_CBC
  25928. unsigned char decoded[FOURK_BUF];
  25929. int decodedSz = FOURK_BUF;
  25930. #endif
  25931. printf(testingFmt, "PKCS7_signed_enveloped");
  25932. /* load cert */
  25933. AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
  25934. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  25935. XFCLOSE(f);
  25936. /* load key */
  25937. AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
  25938. AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  25939. XFCLOSE(f);
  25940. keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
  25941. /* sign cert for envelope */
  25942. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25943. AssertIntEQ(wc_InitRng(&rng), 0);
  25944. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25945. pkcs7->content = cert;
  25946. pkcs7->contentSz = certSz;
  25947. pkcs7->contentOID = DATA;
  25948. pkcs7->privateKey = key;
  25949. pkcs7->privateKeySz = keySz;
  25950. pkcs7->encryptOID = RSAk;
  25951. pkcs7->hashOID = SHA256h;
  25952. pkcs7->rng = &rng;
  25953. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25954. wc_PKCS7_Free(pkcs7);
  25955. wc_FreeRng(&rng);
  25956. #ifdef HAVE_AES_CBC
  25957. /* create envelope */
  25958. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25959. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25960. pkcs7->content = sig;
  25961. pkcs7->contentSz = sigSz;
  25962. pkcs7->contentOID = DATA;
  25963. pkcs7->encryptOID = AES256CBCb;
  25964. pkcs7->privateKey = key;
  25965. pkcs7->privateKeySz = keySz;
  25966. AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  25967. AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  25968. wc_PKCS7_Free(pkcs7);
  25969. #endif
  25970. /* create bad signed enveloped data */
  25971. sigSz = FOURK_BUF * 2;
  25972. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25973. AssertIntEQ(wc_InitRng(&rng), 0);
  25974. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  25975. pkcs7->content = env;
  25976. pkcs7->contentSz = envSz;
  25977. pkcs7->contentOID = DATA;
  25978. pkcs7->privateKey = key;
  25979. pkcs7->privateKeySz = keySz;
  25980. pkcs7->encryptOID = RSAk;
  25981. pkcs7->hashOID = SHA256h;
  25982. pkcs7->rng = &rng;
  25983. /* Set no certs in bundle for this test. Hang on to the pointer though to
  25984. * free it later. */
  25985. pt = (void*)pkcs7->certList;
  25986. pkcs7->certList = NULL; /* no certs in bundle */
  25987. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  25988. pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
  25989. wc_PKCS7_Free(pkcs7);
  25990. /* check verify fails */
  25991. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  25992. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25993. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  25994. PKCS7_SIGNEEDS_CHECK);
  25995. /* try verifying the signature manually */
  25996. {
  25997. RsaKey rKey;
  25998. word32 idx = 0;
  25999. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26000. WC_MAX_DIGEST_SIZE];
  26001. int digestSz;
  26002. AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26003. AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26004. digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
  26005. digest, sizeof(digest), &rKey);
  26006. AssertIntGT(digestSz, 0);
  26007. AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26008. AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26009. AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
  26010. /* verify was success */
  26011. }
  26012. wc_PKCS7_Free(pkcs7);
  26013. /* initializing the PKCS7 struct with the signing certificate should pass */
  26014. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26015. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26016. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26017. wc_PKCS7_Free(pkcs7);
  26018. /* create valid degenerate bundle */
  26019. sigSz = FOURK_BUF * 2;
  26020. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26021. pkcs7->content = env;
  26022. pkcs7->contentSz = envSz;
  26023. pkcs7->contentOID = DATA;
  26024. pkcs7->privateKey = key;
  26025. pkcs7->privateKeySz = keySz;
  26026. pkcs7->encryptOID = RSAk;
  26027. pkcs7->hashOID = SHA256h;
  26028. pkcs7->rng = &rng;
  26029. AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26030. AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26031. wc_PKCS7_Free(pkcs7);
  26032. wc_FreeRng(&rng);
  26033. /* check verify */
  26034. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26035. AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26036. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26037. AssertNotNull(pkcs7->content);
  26038. #ifdef HAVE_AES_CBC
  26039. /* check decode */
  26040. AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
  26041. AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26042. inner->privateKey = key;
  26043. inner->privateKeySz = keySz;
  26044. AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26045. pkcs7->contentSz, decoded, decodedSz)), 0);
  26046. wc_PKCS7_Free(inner);
  26047. #endif
  26048. wc_PKCS7_Free(pkcs7);
  26049. #ifdef HAVE_AES_CBC
  26050. /* check cert set */
  26051. AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26052. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26053. AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26054. AssertNotNull(pkcs7->singleCert);
  26055. AssertIntNE(pkcs7->singleCertSz, 0);
  26056. wc_PKCS7_Free(pkcs7);
  26057. #endif
  26058. printf(resultFmt, passed);
  26059. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26060. return 0;
  26061. }
  26062. static int test_wc_PKCS7_NoDefaultSignedAttribs (void)
  26063. {
  26064. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26065. && !defined(NO_AES)
  26066. PKCS7* pkcs7;
  26067. void* heap = NULL;
  26068. printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
  26069. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26070. AssertNotNull(pkcs7);
  26071. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26072. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26073. AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26074. wc_PKCS7_Free(pkcs7);
  26075. printf(resultFmt, passed);
  26076. #endif
  26077. return 0;
  26078. }
  26079. static int test_wc_PKCS7_SetOriEncryptCtx (void)
  26080. {
  26081. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26082. && !defined(NO_AES)
  26083. PKCS7* pkcs7;
  26084. void* heap = NULL;
  26085. WOLFSSL_CTX* ctx;
  26086. ctx = NULL;
  26087. printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
  26088. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26089. AssertNotNull(pkcs7);
  26090. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26091. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26092. AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26093. wc_PKCS7_Free(pkcs7);
  26094. printf(resultFmt, passed);
  26095. #endif
  26096. return 0;
  26097. }
  26098. static int test_wc_PKCS7_SetOriDecryptCtx (void)
  26099. {
  26100. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26101. && !defined(NO_AES)
  26102. PKCS7* pkcs7;
  26103. void* heap = NULL;
  26104. WOLFSSL_CTX* ctx;
  26105. ctx = NULL;
  26106. printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
  26107. pkcs7 = wc_PKCS7_New(heap, testDevId);
  26108. AssertNotNull(pkcs7);
  26109. AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26110. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26111. AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26112. wc_PKCS7_Free(pkcs7);
  26113. printf(resultFmt, passed);
  26114. #endif
  26115. return 0;
  26116. }
  26117. static int test_wc_PKCS7_DecodeCompressedData(void)
  26118. {
  26119. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26120. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26121. PKCS7* pkcs7;
  26122. void* heap = NULL;
  26123. byte out[4096];
  26124. byte *decompressed;
  26125. int outSz, decompressedSz;
  26126. const char* cert = "./certs/client-cert.pem";
  26127. byte* cert_buf = NULL;
  26128. size_t cert_sz = 0;
  26129. printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
  26130. AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26131. AssertNotNull((decompressed =
  26132. (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
  26133. decompressedSz = (int)cert_sz;
  26134. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26135. pkcs7->content = (byte*)cert_buf;
  26136. pkcs7->contentSz = (word32)cert_sz;
  26137. pkcs7->contentOID = DATA;
  26138. AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26139. sizeof(out))), 0);
  26140. wc_PKCS7_Free(pkcs7);
  26141. /* compressed key should be smaller than when started */
  26142. AssertIntLT(outSz, cert_sz);
  26143. /* test decompression */
  26144. AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26145. AssertIntEQ(pkcs7->contentOID, 0);
  26146. /* fail case with out buffer too small */
  26147. AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26148. decompressed, outSz), 0);
  26149. /* success case */
  26150. AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26151. decompressed, decompressedSz), cert_sz);
  26152. AssertIntEQ(pkcs7->contentOID, DATA);
  26153. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26154. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26155. decompressed = NULL;
  26156. /* test decompression function with different 'max' inputs */
  26157. outSz = sizeof(out);
  26158. AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26159. 0);
  26160. AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26161. out, outSz, 0, heap), 0);
  26162. AssertNull(decompressed);
  26163. AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26164. out, outSz, 0, heap), 0);
  26165. AssertNotNull(decompressed);
  26166. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26167. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26168. decompressed = NULL;
  26169. AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26170. out, outSz, 0, heap), 0);
  26171. AssertNotNull(decompressed);
  26172. AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26173. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26174. if (cert_buf)
  26175. free(cert_buf);
  26176. wc_PKCS7_Free(pkcs7);
  26177. printf(resultFmt, passed);
  26178. #endif
  26179. return 0;
  26180. }
  26181. static int test_wc_i2d_PKCS12(void)
  26182. {
  26183. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26184. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26185. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26186. WC_PKCS12* pkcs12 = NULL;
  26187. unsigned char der[FOURK_BUF * 2];
  26188. unsigned char* pt;
  26189. int derSz;
  26190. unsigned char out[FOURK_BUF * 2];
  26191. int outSz = FOURK_BUF * 2;
  26192. const char p12_f[] = "./certs/test-servercert.p12";
  26193. XFILE f;
  26194. printf(testingFmt, "wc_i2d_PKCS12");
  26195. f = XFOPEN(p12_f, "rb");
  26196. AssertNotNull(f);
  26197. derSz = (int)XFREAD(der, 1, sizeof(der), f);
  26198. AssertIntGT(derSz, 0);
  26199. XFCLOSE(f);
  26200. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26201. AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26202. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26203. AssertIntEQ(outSz, derSz);
  26204. outSz = derSz - 1;
  26205. pt = out;
  26206. AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26207. outSz = derSz;
  26208. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26209. AssertIntEQ((pt == out), 0);
  26210. pt = NULL;
  26211. AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26212. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26213. wc_PKCS12_free(pkcs12);
  26214. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26215. AssertNotNull(pkcs12 = wc_PKCS12_new());
  26216. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26217. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26218. AssertIntEQ(outSz, derSz);
  26219. wc_PKCS12_free(pkcs12);
  26220. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26221. pkcs12 = NULL;
  26222. AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26223. AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26224. AssertIntEQ(outSz, derSz);
  26225. wc_PKCS12_free(pkcs12);
  26226. printf(resultFmt, passed);
  26227. #endif
  26228. return 0;
  26229. }
  26230. /* Testing wc_SignatureGetSize() for signature type ECC */
  26231. static int test_wc_SignatureGetSize_ecc(void)
  26232. {
  26233. int ret = 0;
  26234. #ifndef NO_SIG_WRAPPER
  26235. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  26236. enum wc_SignatureType sig_type;
  26237. word32 key_len;
  26238. /* Initialize ECC Key */
  26239. ecc_key ecc;
  26240. const char* qx =
  26241. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26242. const char* qy =
  26243. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26244. const char* d =
  26245. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26246. ret = wc_ecc_init(&ecc);
  26247. if (ret == 0) {
  26248. ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
  26249. }
  26250. printf(testingFmt, "wc_SigntureGetSize_ecc()");
  26251. if (ret == 0) {
  26252. /* Input for signature type ECC */
  26253. sig_type = WC_SIGNATURE_TYPE_ECC;
  26254. key_len = sizeof(ecc_key);
  26255. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26256. /* Test bad args */
  26257. if (ret > 0) {
  26258. sig_type = (enum wc_SignatureType) 100;
  26259. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26260. if (ret == BAD_FUNC_ARG) {
  26261. sig_type = WC_SIGNATURE_TYPE_ECC;
  26262. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26263. }
  26264. if (ret >= 0) {
  26265. key_len = (word32) 0;
  26266. ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
  26267. }
  26268. if (ret == BAD_FUNC_ARG) {
  26269. ret = SIG_TYPE_E;
  26270. }
  26271. }
  26272. } else {
  26273. ret = WOLFSSL_FATAL_ERROR;
  26274. }
  26275. wc_ecc_free(&ecc);
  26276. #else
  26277. ret = SIG_TYPE_E;
  26278. #endif
  26279. if (ret == SIG_TYPE_E) {
  26280. ret = 0;
  26281. }
  26282. else {
  26283. ret = WOLFSSL_FATAL_ERROR;
  26284. }
  26285. printf(resultFmt, ret == 0 ? passed : failed);
  26286. fflush(stdout);
  26287. #endif /* NO_SIG_WRAPPER */
  26288. return ret;
  26289. }/* END test_wc_SignatureGetSize_ecc() */
  26290. /* Testing wc_SignatureGetSize() for signature type rsa */
  26291. static int test_wc_SignatureGetSize_rsa(void)
  26292. {
  26293. int ret = 0;
  26294. #ifndef NO_SIG_WRAPPER
  26295. #ifndef NO_RSA
  26296. enum wc_SignatureType sig_type;
  26297. word32 key_len;
  26298. word32 idx = 0;
  26299. /* Initialize RSA Key */
  26300. RsaKey rsa_key;
  26301. byte* tmp = NULL;
  26302. size_t bytes;
  26303. #ifdef USE_CERT_BUFFERS_1024
  26304. bytes = (size_t)sizeof_client_key_der_1024;
  26305. if (bytes < (size_t)sizeof_client_key_der_1024)
  26306. bytes = (size_t)sizeof_client_cert_der_1024;
  26307. #elif defined(USE_CERT_BUFFERS_2048)
  26308. bytes = (size_t)sizeof_client_key_der_2048;
  26309. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26310. bytes = (size_t)sizeof_client_cert_der_2048;
  26311. #else
  26312. bytes = FOURK_BUF;
  26313. #endif
  26314. tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26315. if (tmp != NULL) {
  26316. #ifdef USE_CERT_BUFFERS_1024
  26317. XMEMCPY(tmp, client_key_der_1024,
  26318. (size_t)sizeof_client_key_der_1024);
  26319. #elif defined(USE_CERT_BUFFERS_2048)
  26320. XMEMCPY(tmp, client_key_der_2048,
  26321. (size_t)sizeof_client_key_der_2048);
  26322. #elif !defined(NO_FILESYSTEM)
  26323. file = XFOPEN(clientKey, "rb");
  26324. if (file != XBADFILE) {
  26325. bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
  26326. XFCLOSE(file);
  26327. }
  26328. else {
  26329. ret = WOLFSSL_FATAL_ERROR;
  26330. }
  26331. #else
  26332. ret = WOLFSSL_FATAL_ERROR;
  26333. #endif
  26334. } else {
  26335. ret = WOLFSSL_FATAL_ERROR;
  26336. }
  26337. if (ret == 0) {
  26338. ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
  26339. }
  26340. if (ret == 0) {
  26341. ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
  26342. }
  26343. printf(testingFmt, "wc_SigntureGetSize_rsa()");
  26344. if (ret == 0) {
  26345. /* Input for signature type RSA */
  26346. sig_type = WC_SIGNATURE_TYPE_RSA;
  26347. key_len = sizeof(RsaKey);
  26348. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26349. /* Test bad args */
  26350. if (ret > 0) {
  26351. sig_type = (enum wc_SignatureType) 100;
  26352. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26353. if (ret == BAD_FUNC_ARG) {
  26354. sig_type = WC_SIGNATURE_TYPE_RSA;
  26355. ret = wc_SignatureGetSize(sig_type, NULL, key_len);
  26356. }
  26357. #ifndef HAVE_USER_RSA
  26358. if (ret == BAD_FUNC_ARG) {
  26359. #else
  26360. if (ret == 0) {
  26361. #endif
  26362. key_len = (word32)0;
  26363. ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
  26364. }
  26365. if (ret == BAD_FUNC_ARG) {
  26366. ret = SIG_TYPE_E;
  26367. }
  26368. }
  26369. } else {
  26370. ret = WOLFSSL_FATAL_ERROR;
  26371. }
  26372. wc_FreeRsaKey(&rsa_key);
  26373. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26374. #else
  26375. ret = SIG_TYPE_E;
  26376. #endif
  26377. if (ret == SIG_TYPE_E) {
  26378. ret = 0;
  26379. }else {
  26380. ret = WOLFSSL_FATAL_ERROR;
  26381. }
  26382. printf(resultFmt, ret == 0 ? passed : failed);
  26383. #endif /* NO_SIG_WRAPPER */
  26384. return ret;
  26385. }/* END test_wc_SignatureGetSize_rsa(void) */
  26386. /*----------------------------------------------------------------------------*
  26387. | hash.h Tests
  26388. *----------------------------------------------------------------------------*/
  26389. static int test_wc_HashInit(void)
  26390. {
  26391. int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
  26392. wc_HashAlg hash;
  26393. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26394. enum wc_HashType enumArray[] = {
  26395. #ifndef NO_MD5
  26396. WC_HASH_TYPE_MD5,
  26397. #endif
  26398. #ifndef NO_SHA
  26399. WC_HASH_TYPE_SHA,
  26400. #endif
  26401. #ifndef WOLFSSL_SHA224
  26402. WC_HASH_TYPE_SHA224,
  26403. #endif
  26404. #ifndef NO_SHA256
  26405. WC_HASH_TYPE_SHA256,
  26406. #endif
  26407. #ifndef WOLFSSL_SHA384
  26408. WC_HASH_TYPE_SHA384,
  26409. #endif
  26410. #ifndef WOLFSSL_SHA512
  26411. WC_HASH_TYPE_SHA512,
  26412. #endif
  26413. };
  26414. /* dynamically finds the length */
  26415. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26416. /* For loop to test various arguments... */
  26417. for (i = 0; i < enumlen; i++) {
  26418. /* check for bad args */
  26419. if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
  26420. ret = 1;
  26421. break;
  26422. }
  26423. wc_HashFree(&hash, enumArray[i]);
  26424. /* check for null ptr */
  26425. if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
  26426. ret = 1;
  26427. break;
  26428. }
  26429. } /* end of for loop */
  26430. printf(testingFmt, "wc_HashInit()");
  26431. if (ret==0) { /* all tests have passed */
  26432. printf(resultFmt, passed);
  26433. }
  26434. else { /* a test has failed */
  26435. printf(resultFmt, failed);
  26436. }
  26437. return ret;
  26438. } /* end of test_wc_HashInit */
  26439. /*
  26440. * Unit test function for wc_HashSetFlags()
  26441. */
  26442. static int test_wc_HashSetFlags(void)
  26443. {
  26444. int ret = 0;
  26445. #ifdef WOLFSSL_HASH_FLAGS
  26446. wc_HashAlg hash;
  26447. word32 flags = 0;
  26448. int i, j;
  26449. int notSupportedLen;
  26450. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26451. enum wc_HashType enumArray[] = {
  26452. #ifndef NO_MD5
  26453. WC_HASH_TYPE_MD5,
  26454. #endif
  26455. #ifndef NO_SHA
  26456. WC_HASH_TYPE_SHA,
  26457. #endif
  26458. #ifdef WOLFSSL_SHA224
  26459. WC_HASH_TYPE_SHA224,
  26460. #endif
  26461. #ifndef NO_SHA256
  26462. WC_HASH_TYPE_SHA256,
  26463. #endif
  26464. #ifdef WOLFSSL_SHA384
  26465. WC_HASH_TYPE_SHA384,
  26466. #endif
  26467. #ifdef WOLFSSL_SHA512
  26468. WC_HASH_TYPE_SHA512,
  26469. #endif
  26470. #ifdef WOLFSSL_SHA3
  26471. WC_HASH_TYPE_SHA3_224,
  26472. #endif
  26473. };
  26474. enum wc_HashType notSupported[] = {
  26475. WC_HASH_TYPE_MD5_SHA,
  26476. WC_HASH_TYPE_MD2,
  26477. WC_HASH_TYPE_MD4,
  26478. WC_HASH_TYPE_BLAKE2B,
  26479. WC_HASH_TYPE_BLAKE2S,
  26480. WC_HASH_TYPE_NONE,
  26481. };
  26482. /* dynamically finds the length */
  26483. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26484. printf(testingFmt, "wc_HashSetFlags()");
  26485. /* For loop to test various arguments... */
  26486. for (i = 0; i < enumlen; i++) {
  26487. ret = wc_HashInit(&hash, enumArray[i]);
  26488. if (ret == 0) {
  26489. ret = wc_HashSetFlags(&hash, enumArray[i], flags);
  26490. }
  26491. if (ret == 0) {
  26492. if (flags & WC_HASH_FLAG_ISCOPY) {
  26493. ret = 0;
  26494. }
  26495. }
  26496. if (ret == 0) {
  26497. ret = wc_HashSetFlags(NULL, enumArray[i], flags);
  26498. if (ret == BAD_FUNC_ARG) {
  26499. ret = 0;
  26500. }
  26501. }
  26502. wc_HashFree(&hash, enumArray[i]);
  26503. }
  26504. /* For loop to test not supported cases */
  26505. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26506. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  26507. ret = wc_HashInit(&hash, notSupported[j]);
  26508. if (ret == 0) {
  26509. ret = -1;
  26510. }
  26511. else if (ret == BAD_FUNC_ARG){
  26512. ret = wc_HashSetFlags(&hash, notSupported[j], flags);
  26513. if (ret == 0) {
  26514. ret = -1;
  26515. }
  26516. else if (ret == BAD_FUNC_ARG) {
  26517. ret = 0;
  26518. }
  26519. }
  26520. if (ret == 0) {
  26521. ret = wc_HashFree(&hash, notSupported[j]);
  26522. if (ret == 0) {
  26523. ret = -1;
  26524. }
  26525. else if (ret == BAD_FUNC_ARG) {
  26526. ret = 0;
  26527. }
  26528. }
  26529. }
  26530. printf(resultFmt, ret == 0 ? passed : failed);
  26531. fflush(stdout);
  26532. #endif
  26533. return ret;
  26534. } /* END test_wc_HashSetFlags */
  26535. /*
  26536. * Unit test function for wc_HashGetFlags()
  26537. */
  26538. static int test_wc_HashGetFlags(void)
  26539. {
  26540. int ret = 0;
  26541. #ifdef WOLFSSL_HASH_FLAGS
  26542. wc_HashAlg hash;
  26543. word32 flags = 0;
  26544. int i, j;
  26545. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26546. enum wc_HashType enumArray[] = {
  26547. #ifndef NO_MD5
  26548. WC_HASH_TYPE_MD5,
  26549. #endif
  26550. #ifndef NO_SHA
  26551. WC_HASH_TYPE_SHA,
  26552. #endif
  26553. #ifdef WOLFSSL_SHA224
  26554. WC_HASH_TYPE_SHA224,
  26555. #endif
  26556. #ifndef NO_SHA256
  26557. WC_HASH_TYPE_SHA256,
  26558. #endif
  26559. #ifdef WOLFSSL_SHA384
  26560. WC_HASH_TYPE_SHA384,
  26561. #endif
  26562. #ifdef WOLFSSL_SHA512
  26563. WC_HASH_TYPE_SHA512,
  26564. #endif
  26565. #ifdef WOLFSSL_SHA3
  26566. WC_HASH_TYPE_SHA3_224,
  26567. #endif
  26568. };
  26569. enum wc_HashType notSupported[] = {
  26570. WC_HASH_TYPE_MD5_SHA,
  26571. WC_HASH_TYPE_MD2,
  26572. WC_HASH_TYPE_MD4,
  26573. WC_HASH_TYPE_BLAKE2B,
  26574. WC_HASH_TYPE_BLAKE2S,
  26575. WC_HASH_TYPE_NONE,
  26576. };
  26577. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26578. int notSupportedLen;
  26579. printf(testingFmt, "wc_HashGetFlags()");
  26580. /* For loop to test various arguments... */
  26581. for (i = 0; i < enumlen; i++) {
  26582. ret = wc_HashInit(&hash, enumArray[i]);
  26583. if (ret == 0) {
  26584. ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
  26585. }
  26586. if (ret == 0) {
  26587. if (flags & WC_HASH_FLAG_ISCOPY) {
  26588. ret = 0;
  26589. }
  26590. }
  26591. if (ret == 0) {
  26592. ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
  26593. if (ret == BAD_FUNC_ARG) {
  26594. ret = 0;
  26595. }
  26596. }
  26597. wc_HashFree(&hash, enumArray[i]);
  26598. if (ret != 0) {
  26599. break;
  26600. }
  26601. }
  26602. /* For loop to test not supported cases */
  26603. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26604. for (j = 0; ret == 0 && j < notSupportedLen; j++){
  26605. ret = wc_HashInit(&hash, notSupported[j]);
  26606. if (ret == 0) {
  26607. ret = -1;
  26608. }
  26609. else if (ret == BAD_FUNC_ARG){
  26610. ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
  26611. if (ret == 0) {
  26612. ret = -1;
  26613. }
  26614. else if (ret == BAD_FUNC_ARG) {
  26615. ret = 0;
  26616. }
  26617. }
  26618. if (ret == 0) {
  26619. ret = wc_HashFree(&hash, notSupported[j]);
  26620. if (ret == 0) {
  26621. ret = -1;
  26622. }
  26623. if (ret == BAD_FUNC_ARG) {
  26624. ret = 0;
  26625. }
  26626. }
  26627. }
  26628. printf(resultFmt, ret == 0 ? passed : failed);
  26629. fflush(stdout);
  26630. #endif
  26631. return ret;
  26632. } /* END test_wc_HashGetFlags */
  26633. /*----------------------------------------------------------------------------*
  26634. | Compatibility Tests
  26635. *----------------------------------------------------------------------------*/
  26636. static int test_wolfSSL_lhash(void)
  26637. {
  26638. #ifdef OPENSSL_ALL
  26639. const char testStr[] = "Like a true nature's child\n"
  26640. "We were born\n"
  26641. "Born to be wild";
  26642. printf(testingFmt, "wolfSSL_LH_strhash()");
  26643. AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
  26644. printf(resultFmt, passed);
  26645. #endif
  26646. return 0;
  26647. }
  26648. static int test_wolfSSL_X509_NAME(void)
  26649. {
  26650. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  26651. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26652. && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  26653. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  26654. defined(OPENSSL_EXTRA))
  26655. X509* x509;
  26656. const unsigned char* c;
  26657. unsigned char buf[4096];
  26658. int bytes;
  26659. XFILE f;
  26660. const X509_NAME* a;
  26661. const X509_NAME* b;
  26662. X509_NAME* d2i_name = NULL;
  26663. int sz;
  26664. unsigned char* tmp;
  26665. char file[] = "./certs/ca-cert.der";
  26666. #ifndef OPENSSL_EXTRA_X509_SMALL
  26667. byte empty[] = { /* CN=empty emailAddress= */
  26668. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  26669. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  26670. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  26671. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26672. 0x01, 0x16, 0x00
  26673. };
  26674. #endif
  26675. printf(testingFmt, "wolfSSL_X509_NAME()");
  26676. #ifndef OPENSSL_EXTRA_X509_SMALL
  26677. /* test compile of deprecated function, returns 0 */
  26678. AssertIntEQ(CRYPTO_thread_id(), 0);
  26679. #endif
  26680. AssertNotNull(a = X509_NAME_new());
  26681. X509_NAME_free((X509_NAME*)a);
  26682. f = XFOPEN(file, "rb");
  26683. AssertTrue(f != XBADFILE);
  26684. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  26685. XFCLOSE(f);
  26686. c = buf;
  26687. AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
  26688. /* test cmp function */
  26689. AssertNotNull(a = X509_get_issuer_name(x509));
  26690. AssertNotNull(b = X509_get_subject_name(x509));
  26691. #ifndef OPENSSL_EXTRA_X509_SMALL
  26692. AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  26693. #endif
  26694. tmp = buf;
  26695. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  26696. if (sz > 0 && tmp == buf) {
  26697. printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__);
  26698. printf(" Expected pointer to be incremented\n");
  26699. abort();
  26700. }
  26701. #ifndef OPENSSL_EXTRA_X509_SMALL
  26702. tmp = buf;
  26703. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26704. #endif
  26705. /* if output parameter is NULL, should still return required size. */
  26706. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  26707. /* retry but with the function creating a buffer */
  26708. tmp = NULL;
  26709. AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  26710. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  26711. AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
  26712. #ifndef OPENSSL_EXTRA_X509_SMALL
  26713. AssertIntEQ(X509_NAME_cmp(a, b), 0);
  26714. #endif
  26715. X509_NAME_free((X509_NAME*)b);
  26716. X509_NAME_free(d2i_name);
  26717. X509_free(x509);
  26718. #ifndef OPENSSL_EXTRA_X509_SMALL
  26719. /* test with an empty domain component */
  26720. tmp = empty;
  26721. sz = sizeof(empty);
  26722. AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  26723. AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
  26724. /* size of empty emailAddress will be 0 */
  26725. tmp = buf;
  26726. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  26727. (char*)tmp, sizeof(buf)), 0);
  26728. /* should contain no organization name */
  26729. tmp = buf;
  26730. AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  26731. (char*)tmp, sizeof(buf)), -1);
  26732. X509_NAME_free(d2i_name);
  26733. #endif
  26734. printf(resultFmt, passed);
  26735. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  26736. return 0;
  26737. }
  26738. static int test_wolfSSL_X509_NAME_hash(void)
  26739. {
  26740. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
  26741. && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  26742. BIO* bio;
  26743. X509* x509 = NULL;
  26744. printf(testingFmt, "wolfSSL_X509_NAME_hash");
  26745. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26746. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26747. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26748. AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  26749. AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  26750. X509_free(x509);
  26751. BIO_free(bio);
  26752. printf(resultFmt, passed);
  26753. #endif
  26754. return 0;
  26755. }
  26756. static int test_wolfSSL_X509_NAME_print_ex(void)
  26757. {
  26758. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  26759. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  26760. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  26761. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  26762. !defined(NO_BIO) && !defined(NO_RSA)
  26763. int memSz;
  26764. byte* mem = NULL;
  26765. BIO* bio = NULL;
  26766. BIO* membio = NULL;
  26767. X509* x509 = NULL;
  26768. X509_NAME* name = NULL;
  26769. const char* expNormal = "C=US, CN=wolfssl.com";
  26770. const char* expReverse = "CN=wolfssl.com, C=US";
  26771. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  26772. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  26773. const char* expRFC5523 =
  26774. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  26775. printf(testingFmt, "wolfSSL_X509_NAME_print_ex");
  26776. /* Test with real cert (svrCertFile) first */
  26777. AssertNotNull(bio = BIO_new(BIO_s_file()));
  26778. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  26779. AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  26780. AssertNotNull(name = X509_get_subject_name(x509));
  26781. /* Test without flags */
  26782. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26783. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26784. BIO_free(membio);
  26785. /* Test flag: XN_FLAG_RFC2253 */
  26786. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26787. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26788. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26789. BIO_free(membio);
  26790. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  26791. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26792. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26793. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26794. BIO_free(membio);
  26795. X509_free(x509);
  26796. BIO_free(bio);
  26797. /* Test normal case without escaped characters */
  26798. {
  26799. /* Create name: "/C=US/CN=wolfssl.com" */
  26800. AssertNotNull(name = X509_NAME_new());
  26801. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26802. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  26803. WOLFSSL_SUCCESS);
  26804. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26805. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  26806. WOLFSSL_SUCCESS);
  26807. /* Test without flags */
  26808. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26809. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26810. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26811. AssertIntEQ(memSz, XSTRLEN(expNormal));
  26812. AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  26813. BIO_free(membio);
  26814. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  26815. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26816. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26817. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26818. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26819. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26820. BIO_free(membio);
  26821. /* Test flags: XN_FLAG_DN_REV - reversed */
  26822. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26823. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26824. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26825. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26826. AssertIntEQ(memSz, XSTRLEN(expReverse));
  26827. AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  26828. BIO_free(membio);
  26829. X509_NAME_free(name);
  26830. }
  26831. /* Test RFC2253 characters are escaped with backslashes */
  26832. {
  26833. AssertNotNull(name = X509_NAME_new());
  26834. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  26835. /* space at beginning and end, and: ,+"\ */
  26836. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  26837. WOLFSSL_SUCCESS);
  26838. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  26839. /* # at beginning, and: <>;*/
  26840. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  26841. WOLFSSL_SUCCESS);
  26842. /* Test without flags */
  26843. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26844. AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  26845. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26846. AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
  26847. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  26848. XSTRLEN(expNotEscaped)), 0);
  26849. BIO_free(membio);
  26850. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  26851. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26852. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26853. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  26854. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26855. AssertIntEQ(memSz, XSTRLEN(expRFC5523));
  26856. AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  26857. BIO_free(membio);
  26858. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  26859. AssertNotNull(membio = BIO_new(BIO_s_mem()));
  26860. AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
  26861. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  26862. AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  26863. AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  26864. AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  26865. XSTRLEN(expNotEscapedRev)), 0);
  26866. BIO_free(membio);
  26867. X509_NAME_free(name);
  26868. }
  26869. printf(resultFmt, passed);
  26870. #endif
  26871. return 0;
  26872. }
  26873. #ifndef NO_BIO
  26874. static int test_wolfSSL_X509_INFO_multiple_info(void)
  26875. {
  26876. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26877. STACK_OF(X509_INFO) *info_stack;
  26878. X509_INFO *info;
  26879. int len;
  26880. int i;
  26881. const char* files[] = {
  26882. cliCertFile,
  26883. cliKeyFile,
  26884. /* This needs to be the order as svrCertFile contains the
  26885. * intermediate cert as well. */
  26886. svrKeyFile,
  26887. svrCertFile,
  26888. NULL,
  26889. };
  26890. const char** curFile;
  26891. BIO *fileBIO;
  26892. BIO *concatBIO = NULL;
  26893. byte tmp[FOURK_BUF];
  26894. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  26895. * to group objects together. */
  26896. AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
  26897. for (curFile = files; *curFile != NULL; curFile++) {
  26898. int fileLen;
  26899. AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  26900. fileLen = wolfSSL_BIO_get_len(fileBIO);
  26901. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  26902. AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
  26903. fileLen -= len;
  26904. }
  26905. /* Make sure we read the entire file */
  26906. AssertIntEQ(fileLen, 0);
  26907. BIO_free(fileBIO);
  26908. }
  26909. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  26910. NULL));
  26911. AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
  26912. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26913. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26914. AssertNotNull(info->x509);
  26915. AssertNull(info->crl);
  26916. if (i != 0) {
  26917. AssertNotNull(info->x_pkey);
  26918. AssertIntEQ(X509_check_private_key(info->x509,
  26919. info->x_pkey->dec_pkey), 1);
  26920. }
  26921. else {
  26922. AssertNull(info->x_pkey);
  26923. }
  26924. }
  26925. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26926. BIO_free(concatBIO);
  26927. #endif
  26928. return 0;
  26929. }
  26930. #endif
  26931. #ifndef NO_BIO
  26932. static int test_wolfSSL_X509_INFO(void)
  26933. {
  26934. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  26935. STACK_OF(X509_INFO) *info_stack;
  26936. X509_INFO *info;
  26937. BIO *cert;
  26938. int i;
  26939. /* PEM in hex format to avoid null terminator */
  26940. byte data[] = {
  26941. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  26942. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  26943. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  26944. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  26945. 0x2d, 0x2d, 0x2d
  26946. };
  26947. /* PEM in hex format to avoid null terminator */
  26948. byte data2[] = {
  26949. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  26950. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  26951. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  26952. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  26953. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  26954. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  26955. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  26956. };
  26957. printf(testingFmt, "wolfSSL_X509_INFO");
  26958. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26959. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26960. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  26961. AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
  26962. AssertNotNull(info->x509);
  26963. AssertNull(info->crl);
  26964. AssertNull(info->x_pkey);
  26965. }
  26966. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26967. BIO_free(cert);
  26968. AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  26969. AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26970. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26971. BIO_free(cert);
  26972. /* This case should fail due to invalid input. */
  26973. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  26974. AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  26975. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26976. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26977. BIO_free(cert);
  26978. AssertNotNull(cert = BIO_new(BIO_s_mem()));
  26979. AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  26980. AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  26981. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  26982. BIO_free(cert);
  26983. printf(resultFmt, passed);
  26984. #endif
  26985. return 0;
  26986. }
  26987. #endif
  26988. static int test_wolfSSL_X509_subject_name_hash(void)
  26989. {
  26990. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  26991. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  26992. X509* x509;
  26993. X509_NAME* subjectName = NULL;
  26994. unsigned long ret1 = 0;
  26995. unsigned long ret2 = 0;
  26996. printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
  26997. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  26998. SSL_FILETYPE_PEM));
  26999. AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  27000. /* These two
  27001. * - X509_subject_name_hash(x509)
  27002. * - X509_NAME_hash(X509_get_subject_name(x509))
  27003. * should give the same hash, if !defined(NO_SHA) is true. */
  27004. ret1 = X509_subject_name_hash(x509);
  27005. AssertIntNE(ret1, 0);
  27006. #if !defined(NO_SHA)
  27007. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  27008. AssertIntNE(ret2, 0);
  27009. AssertIntEQ(ret1, ret2);
  27010. #else
  27011. (void) ret2;
  27012. #endif
  27013. X509_free(x509);
  27014. printf(resultFmt, passed);
  27015. #endif
  27016. return 0;
  27017. }
  27018. static int test_wolfSSL_X509_issuer_name_hash(void)
  27019. {
  27020. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27021. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  27022. X509* x509;
  27023. X509_NAME* issuertName = NULL;
  27024. unsigned long ret1 = 0;
  27025. unsigned long ret2 = 0;
  27026. printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
  27027. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27028. SSL_FILETYPE_PEM));
  27029. AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  27030. /* These two
  27031. * - X509_issuer_name_hash(x509)
  27032. * - X509_NAME_hash(X509_get_issuer_name(x509))
  27033. * should give the same hash, if !defined(NO_SHA) is true. */
  27034. ret1 = X509_issuer_name_hash(x509);
  27035. AssertIntNE(ret1, 0);
  27036. #if !defined(NO_SHA)
  27037. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  27038. AssertIntNE(ret2, 0);
  27039. AssertIntEQ(ret1, ret2);
  27040. #else
  27041. (void) ret2;
  27042. #endif
  27043. X509_free(x509);
  27044. printf(resultFmt, passed);
  27045. #endif
  27046. return 0;
  27047. }
  27048. static int test_wolfSSL_X509_check_host(void)
  27049. {
  27050. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  27051. && !defined(NO_SHA) && !defined(NO_RSA)
  27052. X509* x509;
  27053. const char altName[] = "example.com";
  27054. printf(testingFmt, "wolfSSL_X509_check_host()");
  27055. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27056. SSL_FILETYPE_PEM));
  27057. AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  27058. WOLFSSL_SUCCESS);
  27059. AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  27060. WOLFSSL_FAILURE);
  27061. X509_free(x509);
  27062. AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  27063. WOLFSSL_FAILURE);
  27064. printf(resultFmt, passed);
  27065. #endif
  27066. return 0;
  27067. }
  27068. static int test_wolfSSL_X509_check_email(void)
  27069. {
  27070. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  27071. X509* x509;
  27072. const char goodEmail[] = "info@wolfssl.com";
  27073. const char badEmail[] = "disinfo@wolfssl.com";
  27074. printf(testingFmt, "wolfSSL_X509_check_email()");
  27075. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  27076. SSL_FILETYPE_PEM));
  27077. /* Should fail on non-matching email address */
  27078. AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  27079. WOLFSSL_FAILURE);
  27080. /* Should succeed on matching email address */
  27081. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  27082. WOLFSSL_SUCCESS);
  27083. /* Should compute length internally when not provided */
  27084. AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  27085. WOLFSSL_SUCCESS);
  27086. /* Should fail when email address is NULL */
  27087. AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  27088. WOLFSSL_FAILURE);
  27089. X509_free(x509);
  27090. /* Should fail when x509 is NULL */
  27091. AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  27092. WOLFSSL_FAILURE);
  27093. printf(resultFmt, passed);
  27094. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  27095. return 0;
  27096. }
  27097. static int test_wolfSSL_DES(void)
  27098. {
  27099. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  27100. const_DES_cblock myDes;
  27101. DES_cblock iv;
  27102. DES_key_schedule key;
  27103. word32 i;
  27104. DES_LONG dl;
  27105. unsigned char msg[] = "hello wolfssl";
  27106. printf(testingFmt, "wolfSSL_DES()");
  27107. DES_check_key(1);
  27108. DES_set_key(&myDes, &key);
  27109. /* check, check of odd parity */
  27110. XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
  27111. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27112. AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
  27113. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27114. /* set odd parity for success case */
  27115. DES_set_odd_parity(&myDes);
  27116. AssertIntEQ(DES_check_key_parity(&myDes), 1);
  27117. printf("%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2], myDes[3]);
  27118. AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
  27119. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27120. AssertIntEQ(key[i], myDes[i]);
  27121. }
  27122. AssertIntEQ(DES_is_weak_key(&myDes), 0);
  27123. /* check weak key */
  27124. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  27125. XMEMSET(key, 5, sizeof(DES_key_schedule));
  27126. AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
  27127. AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
  27128. /* now do unchecked copy of a weak key over */
  27129. DES_set_key_unchecked(&myDes, &key);
  27130. /* compare arrays, should be the same */
  27131. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27132. AssertIntEQ(key[i], myDes[i]);
  27133. }
  27134. AssertIntEQ(DES_is_weak_key(&myDes), 1);
  27135. /* check DES_key_sched API */
  27136. XMEMSET(key, 1, sizeof(DES_key_schedule));
  27137. AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
  27138. AssertIntEQ(DES_key_sched(NULL, &key), 0);
  27139. AssertIntEQ(DES_key_sched(&myDes, &key), 0);
  27140. /* compare arrays, should be the same */
  27141. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  27142. AssertIntEQ(key[i], myDes[i]);
  27143. }
  27144. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  27145. * DES_cbc_encrypt on the input */
  27146. XMEMSET(iv, 0, sizeof(DES_cblock));
  27147. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  27148. AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  27149. AssertIntEQ(dl, 480052723);
  27150. printf(resultFmt, passed);
  27151. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  27152. return 0;
  27153. }
  27154. static int test_wc_PemToDer(void)
  27155. {
  27156. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27157. int ret;
  27158. DerBuffer* pDer = NULL;
  27159. const char* ca_cert = "./certs/server-cert.pem";
  27160. byte* cert_buf = NULL;
  27161. size_t cert_sz = 0;
  27162. int eccKey = 0;
  27163. EncryptedInfo info;
  27164. printf(testingFmt, "wc_PemToDer()");
  27165. XMEMSET(&info, 0, sizeof(info));
  27166. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27167. if (ret == 0) {
  27168. ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
  27169. &pDer, NULL, &info, &eccKey);
  27170. AssertIntEQ(ret, 0);
  27171. wc_FreeDer(&pDer);
  27172. }
  27173. if (cert_buf)
  27174. free(cert_buf);
  27175. #ifdef HAVE_ECC
  27176. {
  27177. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  27178. byte key_buf[256] = {0};
  27179. /* Test fail of loading a key with cert type */
  27180. AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  27181. key_buf[0] = '\n';
  27182. XMEMCPY(key_buf + 1, cert_buf, cert_sz);
  27183. AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  27184. &pDer, NULL, &info, &eccKey)), 0);
  27185. #ifdef OPENSSL_EXTRA
  27186. AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  27187. &pDer, NULL, &info, &eccKey)), 0);
  27188. #endif
  27189. wc_FreeDer(&pDer);
  27190. if (cert_buf)
  27191. free(cert_buf);
  27192. }
  27193. #endif
  27194. printf(resultFmt, passed);
  27195. #endif
  27196. return 0;
  27197. }
  27198. static int test_wc_AllocDer(void)
  27199. {
  27200. #if !defined(NO_CERTS)
  27201. int ret;
  27202. DerBuffer* pDer = NULL;
  27203. word32 testSize = 1024;
  27204. printf(testingFmt, "wc_AllocDer()");
  27205. ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
  27206. AssertIntEQ(ret, 0);
  27207. AssertNotNull(pDer);
  27208. wc_FreeDer(&pDer);
  27209. printf(resultFmt, passed);
  27210. #endif
  27211. return 0;
  27212. }
  27213. static int test_wc_CertPemToDer(void)
  27214. {
  27215. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  27216. int ret;
  27217. const char* ca_cert = "./certs/ca-cert.pem";
  27218. byte* cert_buf = NULL;
  27219. size_t cert_sz = 0, cert_dersz = 0;
  27220. byte* cert_der = NULL;
  27221. printf(testingFmt, "wc_CertPemToDer()");
  27222. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27223. if (ret == 0) {
  27224. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27225. cert_der = (byte*)malloc(cert_dersz);
  27226. if (cert_der) {
  27227. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27228. cert_der, (int)cert_dersz, CERT_TYPE);
  27229. AssertIntGE(ret, 0);
  27230. }
  27231. }
  27232. if (cert_der)
  27233. free(cert_der);
  27234. if (cert_buf)
  27235. free(cert_buf);
  27236. printf(resultFmt, passed);
  27237. #endif
  27238. return 0;
  27239. }
  27240. static int test_wc_PubKeyPemToDer(void)
  27241. {
  27242. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  27243. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27244. int ret;
  27245. const char* key = "./certs/ecc-client-keyPub.pem";
  27246. byte* cert_buf = NULL;
  27247. size_t cert_sz = 0, cert_dersz = 0;
  27248. byte* cert_der = NULL;
  27249. printf(testingFmt, "wc_PubKeyPemToDer()");
  27250. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27251. cert_der, (int)cert_dersz);
  27252. AssertIntGE(ret, BAD_FUNC_ARG);
  27253. ret = load_file(key, &cert_buf, &cert_sz);
  27254. if (ret == 0) {
  27255. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  27256. cert_der = (byte*)malloc(cert_dersz);
  27257. if (cert_der) {
  27258. ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  27259. cert_der, (int)cert_dersz);
  27260. AssertIntGE(ret, 0);
  27261. }
  27262. }
  27263. if (cert_der)
  27264. free(cert_der);
  27265. if (cert_buf)
  27266. free(cert_buf);
  27267. printf(resultFmt, passed);
  27268. #endif
  27269. return 0;
  27270. }
  27271. static int test_wc_PemPubKeyToDer(void)
  27272. {
  27273. #if !defined(NO_FILESYSTEM) && \
  27274. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  27275. int ret;
  27276. const char* key = "./certs/ecc-client-keyPub.pem";
  27277. size_t cert_dersz = 1024;
  27278. byte* cert_der = (byte*)malloc(cert_dersz);
  27279. printf(testingFmt, "wc_PemPubKeyToDer()");
  27280. ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
  27281. AssertIntGE(ret, BAD_FUNC_ARG);
  27282. if (cert_der) {
  27283. ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
  27284. AssertIntGE(ret, 0);
  27285. free(cert_der);
  27286. }
  27287. printf(resultFmt, passed);
  27288. #endif
  27289. return 0;
  27290. }
  27291. static int test_wc_GetPubKeyDerFromCert(void)
  27292. {
  27293. #if !defined(NO_RSA) || defined(HAVE_ECC)
  27294. int ret;
  27295. word32 idx = 0;
  27296. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27297. word32 keyDerSz = (word32)sizeof(keyDer);
  27298. DecodedCert decoded;
  27299. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ)
  27300. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  27301. word32 certBufSz = sizeof(certBuf);
  27302. #endif
  27303. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  27304. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA)
  27305. XFILE fp;
  27306. #endif
  27307. #ifndef NO_RSA
  27308. RsaKey rsaKey;
  27309. #if defined(USE_CERT_BUFFERS_2048)
  27310. byte* rsaCertDer = (byte*)client_cert_der_2048;
  27311. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  27312. #elif defined(USE_CERT_BUFFERS_1024)
  27313. byte* rsaCertDer = (byte*)client_cert_der_1024;
  27314. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  27315. #else
  27316. unsigned char rsaCertDer[TWOK_BUF];
  27317. word32 rsaCertDerSz;
  27318. #endif
  27319. #endif
  27320. #ifdef HAVE_ECC
  27321. ecc_key eccKey;
  27322. #if defined(USE_CERT_BUFFERS_256)
  27323. byte* eccCert = (byte*)cliecc_cert_der_256;
  27324. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  27325. #else
  27326. unsigned char eccCert[ONEK_BUF];
  27327. word32 eccCertSz;
  27328. XFILE fp2;
  27329. #endif
  27330. #endif
  27331. printf(testingFmt, "wc_GetPubKeyDerFromCert()");
  27332. #ifndef NO_RSA
  27333. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  27334. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  27335. AssertTrue((fp != XBADFILE));
  27336. rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
  27337. XFCLOSE(fp);
  27338. #endif
  27339. /* good test case - RSA DER cert */
  27340. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  27341. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27342. AssertIntEQ(ret, 0);
  27343. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27344. AssertIntEQ(ret, 0);
  27345. AssertIntGT(keyDerSz, 0);
  27346. /* sanity check, verify we can import DER public key */
  27347. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27348. AssertIntEQ(ret, 0);
  27349. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27350. AssertIntEQ(ret, 0);
  27351. wc_FreeRsaKey(&rsaKey);
  27352. /* test LENGTH_ONLY_E case */
  27353. keyDerSz = 0;
  27354. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27355. AssertIntEQ(ret, LENGTH_ONLY_E);
  27356. AssertIntGT(keyDerSz, 0);
  27357. /* bad args: DecodedCert NULL */
  27358. ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
  27359. AssertIntEQ(ret, BAD_FUNC_ARG);
  27360. /* bad args: output key buff size */
  27361. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
  27362. AssertIntEQ(ret, BAD_FUNC_ARG);
  27363. /* bad args: zero size output key buffer */
  27364. keyDerSz = 0;
  27365. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27366. AssertIntEQ(ret, BAD_FUNC_ARG);
  27367. wc_FreeDecodedCert(&decoded);
  27368. /* Certificate Request Tests */
  27369. #ifdef WOLFSSL_CERT_REQ
  27370. {
  27371. XMEMSET(certBuf, 0, sizeof(certBuf));
  27372. fp = XFOPEN("./certs/csr.signed.der", "rb");
  27373. AssertTrue((fp != XBADFILE));
  27374. certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
  27375. XFCLOSE(fp);
  27376. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  27377. ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
  27378. AssertIntEQ(ret, 0);
  27379. /* good test case - RSA DER certificate request */
  27380. keyDerSz = sizeof(keyDer);
  27381. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27382. AssertIntEQ(ret, 0);
  27383. AssertIntGT(keyDerSz, 0);
  27384. /* sanity check, verify we can import DER public key */
  27385. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  27386. AssertIntEQ(ret, 0);
  27387. idx = 0;
  27388. ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
  27389. AssertIntEQ(ret, 0);
  27390. wc_FreeRsaKey(&rsaKey);
  27391. wc_FreeDecodedCert(&decoded);
  27392. }
  27393. #endif /* WOLFSSL_CERT_REQ */
  27394. #endif /* NO_RSA */
  27395. #ifdef HAVE_ECC
  27396. #ifndef USE_CERT_BUFFERS_256
  27397. fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
  27398. AssertTrue((fp2 != XBADFILE));
  27399. eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
  27400. XFCLOSE(fp2);
  27401. #endif
  27402. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  27403. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27404. AssertIntEQ(ret, 0);
  27405. /* good test case - ECC */
  27406. XMEMSET(keyDer, 0, sizeof(keyDer));
  27407. keyDerSz = sizeof(keyDer);
  27408. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27409. AssertIntEQ(ret, 0);
  27410. AssertIntGT(keyDerSz, 0);
  27411. /* sanity check, verify we can import DER public key */
  27412. ret = wc_ecc_init(&eccKey);
  27413. AssertIntEQ(ret, 0);
  27414. idx = 0; /* reset idx to 0, used above in RSA case */
  27415. ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
  27416. AssertIntEQ(ret, 0);
  27417. wc_ecc_free(&eccKey);
  27418. /* test LENGTH_ONLY_E case */
  27419. keyDerSz = 0;
  27420. ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
  27421. AssertIntEQ(ret, LENGTH_ONLY_E);
  27422. AssertIntGT(keyDerSz, 0);
  27423. wc_FreeDecodedCert(&decoded);
  27424. #endif
  27425. printf(resultFmt, passed);
  27426. #endif /* !NO_RSA || HAVE_ECC */
  27427. return 0;
  27428. }
  27429. static int test_wc_CheckCertSigPubKey(void)
  27430. {
  27431. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27432. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  27433. int ret;
  27434. const char* ca_cert = "./certs/ca-cert.pem";
  27435. byte* cert_buf = NULL;
  27436. size_t cert_sz = 0;
  27437. byte* cert_der = NULL;
  27438. word32 cert_dersz = 0;
  27439. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  27440. word32 keyDerSz = (word32)sizeof(keyDer);
  27441. DecodedCert decoded;
  27442. printf(testingFmt, "wc_CheckCertSigPubKey()");
  27443. ret = load_file(ca_cert, &cert_buf, &cert_sz);
  27444. if (ret == 0) {
  27445. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  27446. cert_der = (byte*)malloc(cert_dersz);
  27447. if (cert_der) {
  27448. ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
  27449. cert_der, (int)cert_dersz, CERT_TYPE);
  27450. AssertIntGE(ret, 0);
  27451. }
  27452. }
  27453. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  27454. ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
  27455. AssertIntEQ(ret, 0);
  27456. ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
  27457. AssertIntEQ(ret, 0);
  27458. AssertIntGT(keyDerSz, 0);
  27459. /* Good test case. */
  27460. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27461. RSAk);
  27462. AssertIntEQ(ret, 0);
  27463. /* No certificate. */
  27464. ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  27465. ECDSAk);
  27466. AssertIntEQ(ret, BAD_FUNC_ARG);
  27467. /* Bad cert size. */
  27468. ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  27469. RSAk);
  27470. AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  27471. /* No public key. */
  27472. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
  27473. RSAk);
  27474. AssertIntEQ(ret, ASN_NO_SIGNER_E);
  27475. /* Bad public key size. */
  27476. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  27477. RSAk);
  27478. AssertIntEQ(ret, BAD_FUNC_ARG);
  27479. /* Wrong aglo. */
  27480. ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
  27481. ECDSAk);
  27482. AssertIntEQ(ret, ASN_PARSE_E);
  27483. wc_FreeDecodedCert(&decoded);
  27484. if (cert_der)
  27485. free(cert_der);
  27486. if (cert_buf)
  27487. free(cert_buf);
  27488. printf(resultFmt, passed);
  27489. #endif
  27490. return 0;
  27491. }
  27492. static int test_wolfSSL_certs(void)
  27493. {
  27494. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  27495. !defined(NO_RSA)
  27496. X509* x509ext;
  27497. #ifdef OPENSSL_ALL
  27498. X509* x509;
  27499. WOLFSSL_X509_EXTENSION* ext;
  27500. ASN1_OBJECT* obj;
  27501. #endif
  27502. WOLFSSL* ssl;
  27503. WOLFSSL_CTX* ctx;
  27504. STACK_OF(ASN1_OBJECT)* sk;
  27505. ASN1_STRING* asn1_str;
  27506. AUTHORITY_KEYID* akey;
  27507. BASIC_CONSTRAINTS* bc;
  27508. int crit;
  27509. printf(testingFmt, "wolfSSL_certs()");
  27510. #ifndef NO_WOLFSSL_SERVER
  27511. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  27512. #else
  27513. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  27514. #endif
  27515. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  27516. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27517. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  27518. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27519. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  27520. #endif
  27521. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  27522. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27523. AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  27524. #endif
  27525. AssertNotNull(ssl = SSL_new(ctx));
  27526. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27527. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27528. #endif
  27529. #ifdef HAVE_PK_CALLBACKS
  27530. AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  27531. #endif /* HAVE_PK_CALLBACKS */
  27532. /* create and use x509 */
  27533. #ifdef OPENSSL_ALL
  27534. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  27535. AssertNotNull(x509);
  27536. #endif
  27537. x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
  27538. AssertNotNull(x509ext);
  27539. AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  27540. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27541. /* with loading in a new cert the check on private key should now fail */
  27542. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27543. #endif
  27544. #if defined(USE_CERT_BUFFERS_2048)
  27545. AssertIntEQ(SSL_use_certificate_ASN1(ssl,
  27546. (unsigned char*)server_cert_der_2048,
  27547. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  27548. #endif
  27549. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  27550. /************* Get Digest of Certificate ******************/
  27551. {
  27552. byte digest[64]; /* max digest size */
  27553. word32 digestSz;
  27554. XMEMSET(digest, 0, sizeof(digest));
  27555. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  27556. WOLFSSL_SUCCESS);
  27557. AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  27558. WOLFSSL_SUCCESS);
  27559. AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  27560. WOLFSSL_FAILURE);
  27561. }
  27562. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  27563. /* test and checkout X509 extensions */
  27564. bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
  27565. &crit, NULL);
  27566. AssertNotNull(bc);
  27567. AssertIntEQ(crit, 0);
  27568. #ifdef OPENSSL_ALL
  27569. ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
  27570. AssertNotNull(ext);
  27571. X509_EXTENSION_free(ext);
  27572. AssertNotNull(ext = X509_EXTENSION_new());
  27573. X509_EXTENSION_set_critical(ext, 1);
  27574. AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  27575. AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  27576. ASN1_OBJECT_free(obj);
  27577. X509_EXTENSION_free(ext);
  27578. AssertNotNull(ext = X509_EXTENSION_new());
  27579. X509_EXTENSION_set_critical(ext, 0);
  27580. AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  27581. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  27582. NULL);
  27583. AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  27584. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  27585. * and X509_get_ext_d2i has created new */
  27586. X509_EXTENSION_free(ext);
  27587. #endif
  27588. BASIC_CONSTRAINTS_free(bc);
  27589. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
  27590. AssertNotNull(asn1_str);
  27591. AssertIntEQ(crit, 1);
  27592. AssertIntEQ(asn1_str->type, NID_key_usage);
  27593. #ifdef OPENSSL_ALL
  27594. ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
  27595. AssertNotNull(ext);
  27596. X509_EXTENSION_free(ext);
  27597. #endif
  27598. ASN1_STRING_free(asn1_str);
  27599. #ifdef OPENSSL_ALL
  27600. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
  27601. &crit, NULL);
  27602. AssertNotNull(sk);
  27603. ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
  27604. AssertNotNull(ext);
  27605. X509_EXTENSION_free(ext);
  27606. sk_ASN1_OBJECT_pop_free(sk, NULL);
  27607. #else
  27608. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  27609. &crit, NULL);
  27610. AssertNull(sk);
  27611. #endif
  27612. akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  27613. NID_authority_key_identifier, &crit, NULL);
  27614. AssertNotNull(akey);
  27615. #ifdef OPENSSL_ALL
  27616. ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
  27617. AssertNotNull(ext);
  27618. X509_EXTENSION_free(ext);
  27619. #endif
  27620. wolfSSL_AUTHORITY_KEYID_free(akey);
  27621. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27622. NID_private_key_usage_period, &crit, NULL);
  27623. /* AssertNotNull(sk); NID not yet supported */
  27624. AssertIntEQ(crit, -1);
  27625. sk_ASN1_OBJECT_free(sk);
  27626. sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
  27627. &crit, NULL);
  27628. {
  27629. int i;
  27630. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  27631. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  27632. AssertIntEQ(gen->type, GEN_DNS);
  27633. AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  27634. }
  27635. }
  27636. /* AssertNotNull(sk); no alt names set */
  27637. sk_GENERAL_NAME_free(sk);
  27638. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
  27639. &crit, NULL);
  27640. /* AssertNotNull(sk); NID not yet supported */
  27641. AssertIntEQ(crit, -1);
  27642. sk_ASN1_OBJECT_free(sk);
  27643. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
  27644. NULL);
  27645. /* AssertNotNull(sk); no auth info set */
  27646. sk_ASN1_OBJECT_free(sk);
  27647. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
  27648. &crit, NULL);
  27649. /* AssertNotNull(sk); NID not yet supported */
  27650. AssertIntEQ(crit, -1);
  27651. sk_ASN1_OBJECT_free(sk);
  27652. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
  27653. &crit, NULL);
  27654. /* AssertNotNull(sk); NID not yet supported */
  27655. AssertIntEQ(crit, -1);
  27656. sk_ASN1_OBJECT_free(sk);
  27657. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  27658. NID_certificate_policies, &crit, NULL);
  27659. #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
  27660. AssertNull(sk);
  27661. #else
  27662. /* AssertNotNull(sk); no cert policy set */
  27663. #endif
  27664. sk_ASN1_OBJECT_free(sk);
  27665. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
  27666. &crit, NULL);
  27667. /* AssertNotNull(sk); NID not yet supported */
  27668. AssertIntEQ(crit, -1);
  27669. sk_ASN1_OBJECT_free(sk);
  27670. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
  27671. &crit, NULL);
  27672. /* AssertNotNull(sk); NID not yet supported */
  27673. AssertIntEQ(crit, -1);
  27674. sk_ASN1_OBJECT_free(sk);
  27675. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
  27676. &crit, NULL);
  27677. /* AssertNotNull(sk); NID not yet supported */
  27678. AssertIntEQ(crit, -1);
  27679. sk_ASN1_OBJECT_free(sk);
  27680. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
  27681. NULL);
  27682. /* AssertNotNull(sk); NID not yet supported */
  27683. AssertIntEQ(crit, -1);
  27684. sk_ASN1_OBJECT_free(sk);
  27685. /* test invalid cases */
  27686. crit = 0;
  27687. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
  27688. AssertNull(sk);
  27689. AssertIntEQ(crit, -1);
  27690. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
  27691. NULL, NULL);
  27692. AssertNull(sk);
  27693. AssertIntEQ(SSL_get_hit(ssl), 0);
  27694. #ifdef OPENSSL_ALL
  27695. X509_free(x509);
  27696. #endif
  27697. X509_free(x509ext);
  27698. SSL_free(ssl);
  27699. SSL_CTX_free(ctx);
  27700. printf(resultFmt, passed);
  27701. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  27702. return 0;
  27703. }
  27704. static int test_wolfSSL_X509_check_private_key(void)
  27705. {
  27706. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  27707. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  27708. X509* x509;
  27709. EVP_PKEY* pkey = NULL;
  27710. const byte* key;
  27711. printf(testingFmt, "wolfSSL_X509_check_private_key()");
  27712. /* Check with correct key */
  27713. AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
  27714. SSL_FILETYPE_PEM)));
  27715. key = client_key_der_2048;
  27716. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27717. &key, (long)sizeof_client_key_der_2048));
  27718. AssertIntEQ(X509_check_private_key(x509, pkey), 1);
  27719. EVP_PKEY_free(pkey);
  27720. pkey = NULL;
  27721. /* Check with wrong key */
  27722. key = server_key_der_2048;
  27723. AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27724. &key, (long)sizeof_server_key_der_2048));
  27725. AssertIntEQ(X509_check_private_key(x509, pkey), 0);
  27726. /* test for incorrect parameter */
  27727. AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
  27728. AssertIntEQ(X509_check_private_key(x509, NULL), 0);
  27729. AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
  27730. EVP_PKEY_free(pkey);
  27731. X509_free(x509);
  27732. printf(resultFmt, passed);
  27733. #endif
  27734. return 0;
  27735. }
  27736. static int test_wolfSSL_ASN1_TIME_print(void)
  27737. {
  27738. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
  27739. && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  27740. defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
  27741. !defined(NO_BIO)
  27742. BIO* bio;
  27743. X509* x509;
  27744. const unsigned char* der = client_cert_der_2048;
  27745. ASN1_TIME* t;
  27746. unsigned char buf[25];
  27747. printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
  27748. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27749. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  27750. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  27751. AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
  27752. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27753. AssertIntEQ(XMEMCMP(buf, "Feb 15 12:50:24 2022 GMT", sizeof(buf) - 1), 0);
  27754. /* create a bad time and test results */
  27755. AssertNotNull(t = X509_get_notAfter(x509));
  27756. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
  27757. t->data[8] = 0;
  27758. t->data[3] = 0;
  27759. AssertIntNE(ASN1_TIME_print(bio, t), 1);
  27760. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27761. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27762. AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
  27763. BIO_free(bio);
  27764. X509_free(x509);
  27765. printf(resultFmt, passed);
  27766. #endif
  27767. return 0;
  27768. }
  27769. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  27770. {
  27771. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  27772. BIO* bio;
  27773. ASN1_UTCTIME* utc = NULL;
  27774. unsigned char buf[25];
  27775. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  27776. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  27777. printf(testingFmt, "ASN1_UTCTIME_print()");
  27778. /* NULL parameter check */
  27779. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27780. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27781. BIO_free(bio);
  27782. /* Valid date */
  27783. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27784. AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  27785. DYNAMIC_TYPE_ASN1));
  27786. utc->type = ASN_UTC_TIME;
  27787. utc->length = ASN_UTC_TIME_SIZE;
  27788. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  27789. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  27790. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  27791. AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  27792. XMEMSET(buf, 0, sizeof(buf));
  27793. BIO_free(bio);
  27794. /* Invalid format */
  27795. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  27796. utc->type = ASN_UTC_TIME;
  27797. utc->length = ASN_UTC_TIME_SIZE;
  27798. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  27799. AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  27800. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  27801. AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  27802. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  27803. BIO_free(bio);
  27804. printf(resultFmt, passed);
  27805. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  27806. return 0;
  27807. }
  27808. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  27809. {
  27810. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  27811. ASN1_TIME* fromTime;
  27812. ASN1_TIME* toTime;
  27813. int daysDiff;
  27814. int secsDiff;
  27815. printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
  27816. AssertNotNull((fromTime = ASN1_TIME_new()));
  27817. /* Feb 22, 2003, 21:15:15 */
  27818. AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
  27819. AssertNotNull((toTime = ASN1_TIME_new()));
  27820. /* Dec 19, 2010, 18:10:11 */
  27821. AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
  27822. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
  27823. /* Error conditions. */
  27824. AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
  27825. WOLFSSL_FAILURE);
  27826. AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
  27827. WOLFSSL_FAILURE);
  27828. /* If both times are NULL, difference is 0. */
  27829. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
  27830. WOLFSSL_SUCCESS);
  27831. AssertIntEQ(daysDiff, 0);
  27832. AssertIntEQ(secsDiff, 0);
  27833. /* If one time is NULL, it defaults to the current time. */
  27834. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
  27835. WOLFSSL_SUCCESS);
  27836. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
  27837. WOLFSSL_SUCCESS);
  27838. /* Normal operation. Both times non-NULL. */
  27839. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27840. WOLFSSL_SUCCESS);
  27841. AssertIntEQ(daysDiff, 2856);
  27842. AssertIntEQ(secsDiff, 75296);
  27843. /* Swapping the times should return negative values. */
  27844. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
  27845. WOLFSSL_SUCCESS);
  27846. AssertIntEQ(daysDiff, -2856);
  27847. AssertIntEQ(secsDiff, -75296);
  27848. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27849. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27850. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27851. /* Compare regression test: No seconds difference, just difference in days.
  27852. */
  27853. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  27854. ASN1_TIME_set_string(toTime, "19800101000000Z");
  27855. AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  27856. AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  27857. AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  27858. /* Edge case with Unix epoch. */
  27859. AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  27860. AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  27861. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27862. WOLFSSL_SUCCESS);
  27863. AssertIntEQ(daysDiff, 3652);
  27864. AssertIntEQ(secsDiff, 0);
  27865. /* Edge case with year > 2038 (year 2038 problem). */
  27866. AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  27867. AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
  27868. WOLFSSL_SUCCESS);
  27869. AssertIntEQ(daysDiff, 2932896);
  27870. AssertIntEQ(secsDiff, 86399);
  27871. ASN1_TIME_free(fromTime);
  27872. ASN1_TIME_free(toTime);
  27873. printf(resultFmt, passed);
  27874. #endif
  27875. return 0;
  27876. }
  27877. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  27878. {
  27879. #if defined(OPENSSL_EXTRA)
  27880. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
  27881. unsigned char nullstr[32];
  27882. printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
  27883. XMEMSET(nullstr, 0, 32);
  27884. asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  27885. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
  27886. DYNAMIC_TYPE_TMP_BUFFER);
  27887. if (asn1_gtime) {
  27888. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  27889. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  27890. AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  27891. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27892. }
  27893. printf(resultFmt, passed);
  27894. #endif /* OPENSSL_EXTRA */
  27895. return 0;
  27896. }
  27897. static int test_wolfSSL_private_keys(void)
  27898. {
  27899. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  27900. !defined(NO_FILESYSTEM)
  27901. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  27902. WOLFSSL* ssl;
  27903. WOLFSSL_CTX* ctx;
  27904. EVP_PKEY* pkey = NULL;
  27905. printf(testingFmt, "wolfSSL_private_keys()");
  27906. OpenSSL_add_all_digests();
  27907. OpenSSL_add_all_algorithms();
  27908. #ifndef NO_RSA
  27909. #ifndef NO_WOLFSSL_SERVER
  27910. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27911. #else
  27912. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27913. #endif
  27914. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  27915. /* Have to load a cert before you can check the private key against that
  27916. * certificates public key! */
  27917. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27918. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  27919. #endif
  27920. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  27921. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27922. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27923. #endif
  27924. AssertNotNull(ssl = SSL_new(ctx));
  27925. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27926. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27927. #endif
  27928. #ifdef USE_CERT_BUFFERS_2048
  27929. {
  27930. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  27931. unsigned char buf[FOURK_BUF];
  27932. word32 bufSz;
  27933. AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  27934. (unsigned char*)client_key_der_2048,
  27935. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27936. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27937. /* Should mismatch now that a different private key loaded */
  27938. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27939. #endif
  27940. AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  27941. (unsigned char*)server_key,
  27942. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27943. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27944. /* After loading back in DER format of original key, should match */
  27945. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  27946. #endif
  27947. /* test loading private key to the WOLFSSL_CTX */
  27948. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27949. (unsigned char*)client_key_der_2048,
  27950. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  27951. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27952. /* Should mismatch now that a different private key loaded */
  27953. AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27954. #endif
  27955. AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  27956. (unsigned char*)server_key,
  27957. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  27958. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  27959. /* After loading back in DER format of original key, should match */
  27960. AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  27961. #endif
  27962. /* pkey not set yet, expecting to fail */
  27963. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  27964. /* set PKEY and test again */
  27965. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27966. &server_key, (long)sizeof_server_key_der_2048));
  27967. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27968. /* reuse PKEY structure and test
  27969. * this should be checked with a memory management sanity checker */
  27970. AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
  27971. server_key = (const unsigned char*)server_key_der_2048;
  27972. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  27973. &server_key, (long)sizeof_server_key_der_2048));
  27974. AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  27975. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  27976. bufSz = FOURK_BUF;
  27977. AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  27978. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  27979. RSAk, NULL, 0)), 0);
  27980. server_key = (const unsigned char*)buf;
  27981. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  27982. (long)bufSz));
  27983. }
  27984. #endif
  27985. EVP_PKEY_free(pkey);
  27986. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  27987. SSL_CTX_free(ctx);
  27988. #endif /* end of RSA private key match tests */
  27989. #ifdef HAVE_ECC
  27990. #ifndef NO_WOLFSSL_SERVER
  27991. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  27992. #else
  27993. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  27994. #endif
  27995. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  27996. WOLFSSL_FILETYPE_PEM));
  27997. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  27998. WOLFSSL_FILETYPE_PEM));
  27999. AssertNotNull(ssl = SSL_new(ctx));
  28000. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28001. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28002. #endif
  28003. SSL_free(ssl);
  28004. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  28005. WOLFSSL_FILETYPE_PEM));
  28006. AssertNotNull(ssl = SSL_new(ctx));
  28007. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  28008. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28009. #endif
  28010. SSL_free(ssl);
  28011. SSL_CTX_free(ctx);
  28012. #endif /* end of ECC private key match tests */
  28013. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  28014. #ifndef NO_WOLFSSL_SERVER
  28015. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28016. #else
  28017. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28018. #endif
  28019. AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  28020. WOLFSSL_FILETYPE_PEM));
  28021. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  28022. WOLFSSL_FILETYPE_PEM));
  28023. AssertNotNull(ssl = SSL_new(ctx));
  28024. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28025. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28026. #endif
  28027. SSL_free(ssl);
  28028. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  28029. WOLFSSL_FILETYPE_PEM));
  28030. AssertNotNull(ssl = SSL_new(ctx));
  28031. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28032. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28033. #endif
  28034. SSL_free(ssl);
  28035. SSL_CTX_free(ctx);
  28036. #endif /* end of Ed25519 private key match tests */
  28037. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  28038. #ifndef NO_WOLFSSL_SERVER
  28039. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28040. #else
  28041. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28042. #endif
  28043. AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  28044. WOLFSSL_FILETYPE_PEM));
  28045. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  28046. WOLFSSL_FILETYPE_PEM));
  28047. AssertNotNull(ssl = SSL_new(ctx));
  28048. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28049. AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28050. #endif
  28051. SSL_free(ssl);
  28052. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  28053. WOLFSSL_FILETYPE_PEM));
  28054. AssertNotNull(ssl = SSL_new(ctx));
  28055. #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
  28056. AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  28057. #endif
  28058. SSL_free(ssl);
  28059. SSL_CTX_free(ctx);
  28060. #endif /* end of Ed448 private key match tests */
  28061. EVP_cleanup();
  28062. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  28063. CONF_modules_free();
  28064. ENGINE_cleanup();
  28065. CONF_modules_unload();
  28066. (void)ssl;
  28067. (void)ctx;
  28068. (void)pkey;
  28069. printf(resultFmt, passed);
  28070. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28071. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28072. return 0;
  28073. }
  28074. static int test_wolfSSL_PEM_read_PrivateKey(void)
  28075. {
  28076. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28077. && !defined(NO_FILESYSTEM)
  28078. XFILE file;
  28079. const char* fname = "./certs/server-key.pem";
  28080. EVP_PKEY* pkey;
  28081. RSA* rsa;
  28082. WOLFSSL_EVP_PKEY_CTX* ctx;
  28083. unsigned char* sig;
  28084. size_t sigLen;
  28085. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  28086. size_t tbsLen = sizeof(tbs);
  28087. printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
  28088. /* Check error case. */
  28089. AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  28090. /* Read in an RSA key. */
  28091. file = XFOPEN(fname, "rb");
  28092. AssertTrue(file != XBADFILE);
  28093. AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  28094. XFCLOSE(file);
  28095. /* Make sure the key is usable by signing some data with it. */
  28096. AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  28097. AssertIntGT((sigLen = RSA_size(rsa)), 0);
  28098. AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  28099. DYNAMIC_TYPE_TMP_BUFFER));
  28100. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  28101. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  28102. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  28103. WOLFSSL_SUCCESS);
  28104. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  28105. EVP_PKEY_CTX_free(ctx);
  28106. EVP_PKEY_free(pkey);
  28107. printf(resultFmt, passed);
  28108. #endif
  28109. return 0;
  28110. }
  28111. static int test_wolfSSL_PEM_read_PUBKEY(void)
  28112. {
  28113. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  28114. && !defined(NO_FILESYSTEM)
  28115. XFILE file;
  28116. const char* fname = "./certs/client-keyPub.pem";
  28117. EVP_PKEY* pkey;
  28118. printf(testingFmt, "test_wolfSSL_PEM_read_PUBKEY()");
  28119. /* Check error case. */
  28120. AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  28121. /* Read in an RSA key. */
  28122. file = XFOPEN(fname, "rb");
  28123. AssertTrue(file != XBADFILE);
  28124. AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  28125. EVP_PKEY_free(pkey);
  28126. XFCLOSE(file);
  28127. #endif
  28128. return 0;
  28129. }
  28130. static int test_wolfSSL_PEM_PrivateKey(void)
  28131. {
  28132. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28133. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  28134. #ifndef NO_BIO
  28135. BIO* bio = NULL;
  28136. #endif
  28137. EVP_PKEY* pkey = NULL;
  28138. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  28139. #ifndef NO_BIO
  28140. /* test creating new EVP_PKEY with bad arg */
  28141. AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  28142. /* test loading RSA key using BIO */
  28143. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  28144. {
  28145. XFILE file;
  28146. const char* fname = "./certs/server-key.pem";
  28147. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  28148. size_t sz;
  28149. byte* buf;
  28150. EVP_PKEY* pkey2;
  28151. EVP_PKEY* pkey3;
  28152. RSA* rsa_key = NULL;
  28153. file = XFOPEN(fname, "rb");
  28154. AssertTrue((file != XBADFILE));
  28155. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28156. sz = XFTELL(file);
  28157. XREWIND(file);
  28158. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28159. if (buf) {
  28160. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28161. }
  28162. XFCLOSE(file);
  28163. /* Test using BIO new mem and loading PEM private key */
  28164. bio = BIO_new_mem_buf(buf, (int)sz);
  28165. AssertNotNull(bio);
  28166. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28167. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28168. BIO_free(bio);
  28169. bio = NULL;
  28170. AssertNotNull(pkey2 = EVP_PKEY_new());
  28171. pkey2->type = EVP_PKEY_RSA;
  28172. /* Test parameter copy */
  28173. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  28174. EVP_PKEY_free(pkey2);
  28175. EVP_PKEY_free(pkey);
  28176. pkey = NULL;
  28177. /* Qt unit test case : rsa pkcs8 key */
  28178. file = XFOPEN(fname_rsa_p8, "rb");
  28179. AssertTrue((file != XBADFILE));
  28180. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28181. sz = XFTELL(file);
  28182. XREWIND(file);
  28183. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28184. if (buf)
  28185. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28186. XFCLOSE(file);
  28187. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28188. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28189. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28190. BIO_free(bio);
  28191. bio = NULL;
  28192. AssertNotNull(pkey3 = EVP_PKEY_new());
  28193. AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  28194. AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  28195. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28196. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28197. #else
  28198. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28199. #endif
  28200. RSA_free(rsa_key);
  28201. EVP_PKEY_free(pkey3);
  28202. EVP_PKEY_free(pkey);
  28203. pkey = NULL;
  28204. }
  28205. #endif
  28206. /* test loading ECC key using BIO */
  28207. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28208. {
  28209. XFILE file;
  28210. const char* fname = "./certs/ecc-key.pem";
  28211. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  28212. size_t sz;
  28213. byte* buf;
  28214. EVP_PKEY* pkey2;
  28215. EVP_PKEY* pkey3;
  28216. EC_KEY* ec_key;
  28217. int nid = 0;
  28218. file = XFOPEN(fname, "rb");
  28219. AssertTrue((file != XBADFILE));
  28220. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28221. sz = XFTELL(file);
  28222. XREWIND(file);
  28223. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28224. if (buf)
  28225. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28226. XFCLOSE(file);
  28227. /* Test using BIO new mem and loading PEM private key */
  28228. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28229. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28230. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28231. BIO_free(bio);
  28232. bio = NULL;
  28233. AssertNotNull(pkey2 = EVP_PKEY_new());
  28234. AssertNotNull(pkey3 = EVP_PKEY_new());
  28235. pkey2->type = EVP_PKEY_EC;
  28236. /* Test parameter copy */
  28237. AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  28238. /* Qt unit test case 1*/
  28239. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28240. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28241. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28242. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28243. #else
  28244. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28245. #endif
  28246. /* Test default digest */
  28247. AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  28248. AssertIntEQ(nid, NID_sha256);
  28249. EC_KEY_free(ec_key);
  28250. EVP_PKEY_free(pkey3);
  28251. EVP_PKEY_free(pkey2);
  28252. EVP_PKEY_free(pkey);
  28253. pkey = NULL;
  28254. /* Qt unit test case ec pkcs8 key */
  28255. file = XFOPEN(fname_ecc_p8, "rb");
  28256. AssertTrue((file != XBADFILE));
  28257. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  28258. sz = XFTELL(file);
  28259. XREWIND(file);
  28260. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28261. if (buf)
  28262. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28263. XFCLOSE(file);
  28264. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28265. AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28266. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28267. BIO_free(bio);
  28268. bio = NULL;
  28269. AssertNotNull(pkey3 = EVP_PKEY_new());
  28270. /* Qt unit test case */
  28271. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28272. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  28273. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28274. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  28275. #else
  28276. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  28277. #endif
  28278. EC_KEY_free(ec_key);
  28279. EVP_PKEY_free(pkey3);
  28280. EVP_PKEY_free(pkey);
  28281. pkey = NULL;
  28282. }
  28283. #endif
  28284. #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
  28285. defined(WOLFSSL_CERT_GEN))
  28286. {
  28287. #define BIO_PEM_TEST_CHAR 'a'
  28288. EVP_PKEY* pkey2 = NULL;
  28289. unsigned char extra[10];
  28290. int i;
  28291. BIO* pub_bio = NULL;
  28292. printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
  28293. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  28294. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28295. AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  28296. AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28297. AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  28298. AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
  28299. &server_key, (long)sizeof_server_key_der_2048));
  28300. AssertNull(pkey);
  28301. AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  28302. &server_key, (long)sizeof_server_key_der_2048));
  28303. AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  28304. NULL), WOLFSSL_FAILURE);
  28305. AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  28306. NULL), WOLFSSL_FAILURE);
  28307. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  28308. NULL), WOLFSSL_SUCCESS);
  28309. AssertIntGT(BIO_pending(bio), 0);
  28310. AssertIntEQ(BIO_pending(bio), 1679);
  28311. /* Check if the pubkey API writes only the public key */
  28312. #ifdef WOLFSSL_KEY_GEN
  28313. AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  28314. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  28315. AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  28316. AssertIntGT(BIO_pending(pub_bio), 0);
  28317. /* Previously both the private key and the pubkey calls would write
  28318. * out the private key and the PEM header was the only difference.
  28319. * The public PEM should be significantly shorter than the
  28320. * private key versison. */
  28321. AssertIntEQ(BIO_pending(pub_bio), 451);
  28322. #endif
  28323. /* test creating new EVP_PKEY with good args */
  28324. AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  28325. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
  28326. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
  28327. /* test of reuse of EVP_PKEY */
  28328. AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28329. AssertIntEQ(BIO_pending(bio), 0);
  28330. AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  28331. SSL_SUCCESS);
  28332. AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
  28333. AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  28334. AssertNotNull(pkey);
  28335. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  28336. AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
  28337. }
  28338. AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
  28339. AssertIntEQ(BIO_read(bio, extra, 10), 10);
  28340. for (i = 0; i < 10; i++) {
  28341. AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  28342. }
  28343. BIO_free(pub_bio);
  28344. BIO_free(bio);
  28345. bio = NULL;
  28346. EVP_PKEY_free(pkey);
  28347. pkey = NULL;
  28348. EVP_PKEY_free(pkey2);
  28349. }
  28350. #endif
  28351. /* key is DES encrypted */
  28352. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  28353. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  28354. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
  28355. !defined(HAVE_USER_RSA) && !defined(NO_RSA)
  28356. {
  28357. XFILE f;
  28358. wc_pem_password_cb* passwd_cb;
  28359. void* passwd_cb_userdata;
  28360. SSL_CTX* ctx;
  28361. char passwd[] = "bad password";
  28362. #ifndef WOLFSSL_NO_TLS12
  28363. #ifndef NO_WOLFSSL_SERVER
  28364. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28365. #else
  28366. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28367. #endif
  28368. #else
  28369. #ifndef NO_WOLFSSL_SERVER
  28370. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28371. #else
  28372. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28373. #endif
  28374. #endif
  28375. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28376. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  28377. AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  28378. AssertNull(passwd_cb_userdata =
  28379. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  28380. /* fail case with password call back */
  28381. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  28382. (void*)passwd));
  28383. BIO_free(bio);
  28384. AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  28385. AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28386. (void*)passwd));
  28387. BIO_free(bio);
  28388. f = XFOPEN("./certs/server-keyEnc.pem", "rb");
  28389. AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  28390. /* use callback that works */
  28391. AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  28392. (void*)"yassl123"));
  28393. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28394. EVP_PKEY_free(pkey);
  28395. pkey = NULL;
  28396. BIO_free(bio);
  28397. bio = NULL;
  28398. SSL_CTX_free(ctx);
  28399. }
  28400. #endif /* !defined(NO_DES3) */
  28401. #endif /* !NO_BIO */
  28402. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28403. {
  28404. unsigned char buf[2048];
  28405. size_t bytes;
  28406. XFILE f;
  28407. SSL_CTX* ctx;
  28408. #ifndef WOLFSSL_NO_TLS12
  28409. #ifndef NO_WOLFSSL_SERVER
  28410. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  28411. #else
  28412. AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  28413. #endif
  28414. #else
  28415. #ifndef NO_WOLFSSL_SERVER
  28416. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  28417. #else
  28418. AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  28419. #endif
  28420. #endif
  28421. f = XFOPEN("./certs/ecc-key.der", "rb");
  28422. AssertTrue((f != XBADFILE));
  28423. bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
  28424. XFCLOSE(f);
  28425. server_key = buf;
  28426. pkey = NULL;
  28427. AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  28428. AssertNull(pkey);
  28429. AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  28430. AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  28431. EVP_PKEY_free(pkey);
  28432. pkey = NULL;
  28433. SSL_CTX_free(ctx);
  28434. }
  28435. #endif
  28436. printf(resultFmt, passed);
  28437. #ifndef NO_BIO
  28438. (void)bio;
  28439. #endif
  28440. (void)pkey;
  28441. (void)server_key;
  28442. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  28443. return 0;
  28444. }
  28445. static int test_wolfSSL_PEM_file_RSAKey(void)
  28446. {
  28447. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28448. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28449. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28450. RSA* rsa = NULL;
  28451. XFILE fp;
  28452. printf(testingFmt, "wolfSSL_PEM_file_RSAKey");
  28453. AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  28454. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  28455. XFCLOSE(fp);
  28456. AssertIntEQ(RSA_size(rsa), 256);
  28457. AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  28458. AssertIntEQ(PEM_write_RSAPublicKey(stdout, NULL), WOLFSSL_FAILURE);
  28459. AssertIntEQ(PEM_write_RSAPublicKey(stdout, rsa), WOLFSSL_SUCCESS);
  28460. AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  28461. AssertIntEQ(PEM_write_RSA_PUBKEY(stdout, NULL), WOLFSSL_FAILURE);
  28462. AssertIntEQ(PEM_write_RSA_PUBKEY(stdout, rsa), WOLFSSL_SUCCESS);
  28463. RSA_free(rsa);
  28464. printf(resultFmt, passed);
  28465. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28466. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28467. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28468. return 0;
  28469. }
  28470. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  28471. {
  28472. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  28473. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
  28474. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  28475. RSA* rsa = NULL;
  28476. XFILE f = NULL;
  28477. printf(testingFmt, "wolfSSL_PEM_file_RSAPrivateKey()");
  28478. f = XFOPEN(svrKeyFile, "r");
  28479. AssertTrue((f != XBADFILE));
  28480. AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28481. AssertIntEQ(RSA_size(rsa), 256);
  28482. AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  28483. NULL), WOLFSSL_FAILURE);
  28484. AssertIntEQ(PEM_write_RSAPrivateKey(stdout, NULL, NULL, NULL, 0, NULL,
  28485. NULL), WOLFSSL_FAILURE);
  28486. AssertIntEQ(PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0, NULL, NULL),
  28487. WOLFSSL_SUCCESS);
  28488. RSA_free(rsa);
  28489. XFCLOSE(f);
  28490. #ifdef HAVE_ECC
  28491. f = XFOPEN(eccKeyFile, "r");
  28492. AssertTrue((f != XBADFILE));
  28493. AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  28494. XFCLOSE(f);
  28495. #endif /* HAVE_ECC */
  28496. printf(resultFmt, passed);
  28497. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28498. return 0;
  28499. }
  28500. #ifndef NO_BIO
  28501. static int test_wolfSSL_PEM_bio_RSAKey(void)
  28502. {
  28503. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28504. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  28505. !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  28506. RSA* rsa = NULL;
  28507. BIO* bio = NULL;
  28508. printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
  28509. /* PrivateKey */
  28510. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28511. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  28512. AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  28513. AssertNotNull(rsa);
  28514. AssertIntEQ(RSA_size(rsa), 256);
  28515. AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28516. NULL), WOLFSSL_FAILURE);
  28517. BIO_free(bio);
  28518. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28519. AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  28520. NULL), WOLFSSL_SUCCESS);
  28521. BIO_free(bio);
  28522. RSA_free(rsa);
  28523. /* PUBKEY */
  28524. AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  28525. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28526. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28527. AssertIntEQ(RSA_size(rsa), 256);
  28528. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28529. BIO_free(bio);
  28530. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28531. AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  28532. BIO_free(bio);
  28533. RSA_free(rsa);
  28534. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  28535. AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  28536. AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28537. BIO_free(bio);
  28538. RSA_free(rsa);
  28539. #ifdef HAVE_ECC
  28540. /* ensure that non-rsa keys do not work */
  28541. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28542. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28543. AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  28544. BIO_free(bio);
  28545. RSA_free(rsa);
  28546. #endif /* HAVE_ECC */
  28547. printf(resultFmt, passed);
  28548. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28549. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  28550. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  28551. return 0;
  28552. }
  28553. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  28554. {
  28555. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28556. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28557. RSA* rsa = NULL;
  28558. RSA* rsa_dup = NULL;
  28559. BIO* bio = NULL;
  28560. printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
  28561. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  28562. AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28563. AssertIntEQ(RSA_size(rsa), 256);
  28564. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  28565. AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
  28566. /* Test duplicating empty key. */
  28567. rsa_dup = RSA_new();
  28568. AssertNull(RSAPublicKey_dup(rsa_dup));
  28569. RSA_free(rsa_dup);
  28570. AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  28571. AssertPtrNE(rsa_dup, rsa);
  28572. #endif
  28573. /* test if valgrind complains about unreleased memory */
  28574. RSA_up_ref(rsa);
  28575. RSA_free(rsa);
  28576. BIO_free(bio);
  28577. RSA_free(rsa);
  28578. RSA_free(rsa_dup);
  28579. #ifdef HAVE_ECC
  28580. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  28581. AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  28582. BIO_free(bio);
  28583. #endif /* HAVE_ECC */
  28584. printf(resultFmt, passed);
  28585. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28586. return 0;
  28587. }
  28588. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  28589. {
  28590. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  28591. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  28592. XFILE file;
  28593. const char* fname = "./certs/client-keyPub.pem";
  28594. RSA *rsa;
  28595. AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  28596. file = XFOPEN(fname, "rb");
  28597. AssertTrue((file != XBADFILE));
  28598. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  28599. AssertIntEQ(RSA_size(rsa), 256);
  28600. RSA_free(rsa);
  28601. XFCLOSE(file);
  28602. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28603. return 0;
  28604. }
  28605. static int test_wolfSSL_PEM_bio_DSAKey(void)
  28606. {
  28607. #ifndef HAVE_SELFTEST
  28608. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  28609. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  28610. DSA* dsa = NULL;
  28611. BIO* bio = NULL;
  28612. printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
  28613. /* PrivateKey */
  28614. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  28615. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  28616. AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28617. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28618. AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
  28619. WOLFSSL_FAILURE);
  28620. BIO_free(bio);
  28621. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28622. AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
  28623. WOLFSSL_SUCCESS);
  28624. BIO_free(bio);
  28625. DSA_free(dsa);
  28626. /* PUBKEY */
  28627. AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  28628. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  28629. AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28630. AssertIntEQ(BN_num_bytes(dsa->g), 128);
  28631. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28632. BIO_free(bio);
  28633. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28634. AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  28635. BIO_free(bio);
  28636. DSA_free(dsa);
  28637. #ifdef HAVE_ECC
  28638. /* ensure that non-dsa keys do not work */
  28639. AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  28640. AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  28641. AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  28642. BIO_free(bio);
  28643. DSA_free(dsa);
  28644. #endif /* HAVE_ECC */
  28645. printf(resultFmt, passed);
  28646. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  28647. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  28648. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  28649. #endif /* HAVE_SELFTEST */
  28650. return 0;
  28651. }
  28652. static int test_wolfSSL_PEM_bio_ECKey(void)
  28653. {
  28654. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  28655. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  28656. EC_KEY* ec = NULL;
  28657. BIO* bio = NULL;
  28658. printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
  28659. /* PrivateKey */
  28660. AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  28661. AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  28662. AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28663. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28664. AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  28665. NULL),WOLFSSL_FAILURE);
  28666. BIO_free(bio);
  28667. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28668. AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  28669. NULL), WOLFSSL_SUCCESS);
  28670. BIO_free(bio);
  28671. EC_KEY_free(ec);
  28672. /* PUBKEY */
  28673. AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  28674. AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  28675. AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28676. AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  28677. AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  28678. BIO_free(bio);
  28679. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  28680. AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  28681. BIO_free(bio);
  28682. /* Same test as above, but with a file pointer rather than a BIO. */
  28683. AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  28684. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, NULL), WOLFSSL_FAILURE);
  28685. AssertIntEQ(PEM_write_EC_PUBKEY(stdout, ec), WOLFSSL_SUCCESS);
  28686. EC_KEY_free(ec);
  28687. #ifndef NO_RSA
  28688. /* ensure that non-ec keys do not work */
  28689. AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  28690. AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  28691. AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  28692. BIO_free(bio);
  28693. EC_KEY_free(ec);
  28694. #endif /* HAVE_ECC */
  28695. printf(resultFmt, passed);
  28696. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  28697. return 0;
  28698. }
  28699. static int test_wolfSSL_PEM_PUBKEY(void)
  28700. {
  28701. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  28702. BIO* bio = NULL;
  28703. EVP_PKEY* pkey = NULL;
  28704. /* test creating new EVP_PKEY with bad arg */
  28705. AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  28706. /* test loading ECC key using BIO */
  28707. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  28708. {
  28709. XFILE file;
  28710. const char* fname = "./certs/ecc-client-keyPub.pem";
  28711. size_t sz;
  28712. byte* buf;
  28713. EVP_PKEY* pkey2;
  28714. EC_KEY* ec_key;
  28715. file = XFOPEN(fname, "rb");
  28716. AssertTrue((file != XBADFILE));
  28717. AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
  28718. sz = XFTELL(file);
  28719. XREWIND(file);
  28720. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  28721. if (buf)
  28722. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  28723. XFCLOSE(file);
  28724. /* Test using BIO new mem and loading PEM private key */
  28725. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  28726. AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  28727. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  28728. BIO_free(bio);
  28729. bio = NULL;
  28730. /* Qt unit test case*/
  28731. AssertNotNull(pkey2 = EVP_PKEY_new());
  28732. AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  28733. AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  28734. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  28735. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  28736. #else
  28737. AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  28738. #endif
  28739. EC_KEY_free(ec_key);
  28740. EVP_PKEY_free(pkey2);
  28741. EVP_PKEY_free(pkey);
  28742. pkey = NULL;
  28743. }
  28744. #endif
  28745. (void)bio;
  28746. (void)pkey;
  28747. #endif
  28748. return 0;
  28749. }
  28750. #endif /* !NO_BIO */
  28751. static int test_DSA_do_sign_verify(void)
  28752. {
  28753. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  28754. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  28755. !defined(NO_DSA)
  28756. unsigned char digest[WC_SHA_DIGEST_SIZE];
  28757. DSA_SIG* sig;
  28758. DSA* dsa;
  28759. word32 bytes;
  28760. byte sigBin[DSA_SIG_SIZE];
  28761. int dsacheck;
  28762. #ifdef USE_CERT_BUFFERS_1024
  28763. byte tmp[ONEK_BUF];
  28764. XMEMSET(tmp, 0, sizeof(tmp));
  28765. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  28766. bytes = sizeof_dsa_key_der_1024;
  28767. #elif defined(USE_CERT_BUFFERS_2048)
  28768. byte tmp[TWOK_BUF];
  28769. XMEMSET(tmp, 0, sizeof(tmp));
  28770. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  28771. bytes = sizeof_dsa_key_der_2048;
  28772. #else
  28773. byte tmp[TWOK_BUF];
  28774. XMEMSET(tmp, 0, sizeof(tmp));
  28775. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  28776. if (fp == XBADFILE) {
  28777. return WOLFSSL_BAD_FILE;
  28778. }
  28779. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  28780. XFCLOSE(fp);
  28781. #endif /* END USE_CERT_BUFFERS_1024 */
  28782. printf(testingFmt, "DSA_do_sign_verify()");
  28783. XMEMSET(digest, 202, sizeof(digest));
  28784. AssertNotNull(dsa = DSA_new());
  28785. AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  28786. AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  28787. AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  28788. AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  28789. AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  28790. DSA_SIG_free(sig);
  28791. DSA_free(dsa);
  28792. #endif
  28793. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  28794. return 0;
  28795. }
  28796. static int test_wolfSSL_tmp_dh(void)
  28797. {
  28798. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  28799. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  28800. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  28801. byte buff[6000];
  28802. char file[] = "./certs/dsaparams.pem";
  28803. XFILE f;
  28804. int bytes;
  28805. DSA* dsa;
  28806. DH* dh;
  28807. #if defined(WOLFSSL_DH_EXTRA) && \
  28808. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28809. DH* dh2;
  28810. #endif
  28811. BIO* bio;
  28812. SSL* ssl;
  28813. SSL_CTX* ctx;
  28814. printf(testingFmt, "wolfSSL_tmp_dh()");
  28815. #ifndef NO_WOLFSSL_SERVER
  28816. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  28817. #else
  28818. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  28819. #endif
  28820. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  28821. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  28822. AssertNotNull(ssl = SSL_new(ctx));
  28823. f = XFOPEN(file, "rb");
  28824. AssertTrue((f != XBADFILE));
  28825. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  28826. XFCLOSE(f);
  28827. bio = BIO_new_mem_buf((void*)buff, bytes);
  28828. AssertNotNull(bio);
  28829. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  28830. AssertNotNull(dsa);
  28831. dh = wolfSSL_DSA_dup_DH(dsa);
  28832. AssertNotNull(dh);
  28833. #if defined(WOLFSSL_DH_EXTRA) && \
  28834. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28835. AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
  28836. #endif
  28837. AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  28838. #ifndef NO_WOLFSSL_SERVER
  28839. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  28840. #else
  28841. AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  28842. #endif
  28843. BIO_free(bio);
  28844. DSA_free(dsa);
  28845. DH_free(dh);
  28846. #if defined(WOLFSSL_DH_EXTRA) && \
  28847. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  28848. DH_free(dh2);
  28849. #endif
  28850. SSL_free(ssl);
  28851. SSL_CTX_free(ctx);
  28852. printf(resultFmt, passed);
  28853. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  28854. #endif
  28855. return 0;
  28856. }
  28857. static int test_wolfSSL_ctrl(void)
  28858. {
  28859. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  28860. byte buff[6000];
  28861. BIO* bio;
  28862. int bytes;
  28863. BUF_MEM* ptr = NULL;
  28864. printf(testingFmt, "wolfSSL_crtl()");
  28865. bytes = sizeof(buff);
  28866. bio = BIO_new_mem_buf((void*)buff, bytes);
  28867. AssertNotNull(bio);
  28868. AssertNotNull(BIO_s_socket());
  28869. AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  28870. /* needs tested after stubs filled out @TODO
  28871. SSL_ctrl
  28872. SSL_CTX_ctrl
  28873. */
  28874. BIO_free(bio);
  28875. printf(resultFmt, passed);
  28876. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  28877. return 0;
  28878. }
  28879. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  28880. {
  28881. #ifdef OPENSSL_EXTRA
  28882. static const unsigned char pw[] = "password";
  28883. static const int pwSz = sizeof(pw) - 1;
  28884. size_t checkPwSz = 0;
  28885. const unsigned char* checkPw = NULL;
  28886. WOLFSSL_EVP_PKEY* key = NULL;
  28887. printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
  28888. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  28889. AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  28890. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
  28891. if (key) {
  28892. AssertIntEQ(key->type, EVP_PKEY_HMAC);
  28893. AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
  28894. AssertIntEQ(key->pkey_sz, pwSz);
  28895. AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  28896. }
  28897. AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  28898. AssertIntEQ((int)checkPwSz, pwSz);
  28899. if (checkPw) {
  28900. AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  28901. }
  28902. wolfSSL_EVP_PKEY_free(key);
  28903. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
  28904. if (key) {
  28905. AssertIntEQ(key->pkey_sz, 0);
  28906. }
  28907. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28908. (void)checkPw;
  28909. AssertIntEQ((int)checkPwSz, 0);
  28910. wolfSSL_EVP_PKEY_free(key);
  28911. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
  28912. if (key) {
  28913. AssertIntEQ(key->pkey_sz, 0);
  28914. }
  28915. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  28916. (void)checkPw;
  28917. AssertIntEQ((int)checkPwSz, 0);
  28918. wolfSSL_EVP_PKEY_free(key);
  28919. printf(resultFmt, passed);
  28920. #endif /* OPENSSL_EXTRA */
  28921. return 0;
  28922. }
  28923. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  28924. {
  28925. #ifdef OPENSSL_EXTRA
  28926. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  28927. const char *priv = "ABCDEFGHIJKLMNOP";
  28928. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  28929. WOLFSSL_EVP_PKEY* key = NULL;
  28930. printf(testingFmt, "wolfSSL_EVP_PKEY_new_CMAC_key()");
  28931. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28932. NULL, NULL, AES_128_KEY_SIZE, cipher));
  28933. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28934. NULL, (const unsigned char *)priv, 0, cipher));
  28935. AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28936. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  28937. AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  28938. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  28939. wolfSSL_EVP_PKEY_free(key);
  28940. printf(resultFmt, passed);
  28941. #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
  28942. #endif /* OPENSSL_EXTRA */
  28943. return 0;
  28944. }
  28945. static int test_wolfSSL_EVP_Digest(void)
  28946. {
  28947. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  28948. const char* in = "abc";
  28949. int inLen = (int)XSTRLEN(in);
  28950. byte out[WC_SHA256_DIGEST_SIZE];
  28951. unsigned int outLen;
  28952. const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  28953. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  28954. "\x15\xAD";
  28955. printf(testingFmt, "wolfSSL_EVP_Digest()");
  28956. AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
  28957. AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  28958. AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  28959. printf(resultFmt, passed);
  28960. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  28961. return 0;
  28962. }
  28963. static int test_wolfSSL_EVP_Digest_all(void)
  28964. {
  28965. #ifdef OPENSSL_EXTRA
  28966. const char* digests[] = {
  28967. #ifndef NO_MD5
  28968. "MD5",
  28969. #endif
  28970. #ifndef NO_SHA
  28971. "SHA",
  28972. #endif
  28973. #ifdef WOLFSSL_SHA224
  28974. "SHA224",
  28975. #endif
  28976. #ifndef NO_SHA256
  28977. "SHA256",
  28978. #endif
  28979. #ifdef WOLFSSL_SHA384
  28980. "SHA384",
  28981. #endif
  28982. #ifdef WOLFSSL_SHA512
  28983. "SHA512",
  28984. #endif
  28985. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  28986. "SHA512_224",
  28987. #endif
  28988. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  28989. "SHA512_256",
  28990. #endif
  28991. #ifdef WOLFSSL_SHA3
  28992. #ifndef WOLFSSL_NOSHA3_224
  28993. "SHA3_224",
  28994. #endif
  28995. #ifndef WOLFSSL_NOSHA3_256
  28996. "SHA3_256",
  28997. #endif
  28998. "SHA3_384",
  28999. #ifndef WOLFSSL_NOSHA3_512
  29000. "SHA3_512",
  29001. #endif
  29002. #endif /* WOLFSSL_SHA3 */
  29003. NULL
  29004. };
  29005. const char** d;
  29006. const unsigned char in[] = "abc";
  29007. int inLen = XSTR_SIZEOF(in);
  29008. byte out[WC_MAX_DIGEST_SIZE];
  29009. unsigned int outLen;
  29010. printf(testingFmt, "wolfSSL_EVP_Digest_all");
  29011. for (d = digests; *d != NULL; d++) {
  29012. AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  29013. AssertIntGT(outLen, 0);
  29014. AssertIntEQ(EVP_MD_size(*d), outLen);
  29015. }
  29016. printf(resultFmt, passed);
  29017. #endif
  29018. return 0;
  29019. }
  29020. static int test_wolfSSL_EVP_MD_size(void)
  29021. {
  29022. #ifdef OPENSSL_EXTRA
  29023. WOLFSSL_EVP_MD_CTX mdCtx;
  29024. printf(testingFmt, "wolfSSL_EVP_MD_size()");
  29025. #ifdef WOLFSSL_SHA3
  29026. #ifndef WOLFSSL_NOSHA3_224
  29027. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29028. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  29029. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  29030. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  29031. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29032. #endif
  29033. #ifndef WOLFSSL_NOSHA3_256
  29034. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29035. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  29036. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  29037. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  29038. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29039. #endif
  29040. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29041. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  29042. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  29043. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  29044. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29045. #ifndef WOLFSSL_NOSHA3_512
  29046. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29047. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  29048. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  29049. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  29050. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29051. #endif
  29052. #endif /* WOLFSSL_SHA3 */
  29053. #ifndef NO_SHA256
  29054. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29055. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  29056. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
  29057. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
  29058. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  29059. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  29060. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29061. #endif
  29062. #ifndef NO_MD5
  29063. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29064. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  29065. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
  29066. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
  29067. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  29068. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  29069. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29070. #endif
  29071. #ifdef WOLFSSL_SHA224
  29072. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29073. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  29074. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
  29075. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
  29076. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  29077. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  29078. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29079. #endif
  29080. #ifdef WOLFSSL_SHA384
  29081. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29082. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  29083. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
  29084. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
  29085. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  29086. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  29087. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29088. #endif
  29089. #ifdef WOLFSSL_SHA512
  29090. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29091. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  29092. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
  29093. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
  29094. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  29095. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  29096. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29097. #endif
  29098. #ifndef NO_SHA
  29099. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29100. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  29101. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29102. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29103. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29104. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29105. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29106. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29107. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  29108. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
  29109. AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
  29110. AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  29111. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  29112. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29113. #endif
  29114. /* error case */
  29115. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29116. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  29117. AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
  29118. AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  29119. /* Cleanup is valid on uninit'ed struct */
  29120. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29121. printf(resultFmt, passed);
  29122. #endif /* OPENSSL_EXTRA */
  29123. return 0;
  29124. }
  29125. static int test_wolfSSL_EVP_MD_pkey_type(void)
  29126. {
  29127. #ifdef OPENSSL_EXTRA
  29128. const WOLFSSL_EVP_MD* md;
  29129. printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
  29130. #ifndef NO_MD5
  29131. AssertNotNull(md = EVP_md5());
  29132. AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  29133. #endif
  29134. #ifndef NO_SHA
  29135. AssertNotNull(md = EVP_sha1());
  29136. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  29137. #endif
  29138. #ifdef WOLFSSL_SHA224
  29139. AssertNotNull(md = EVP_sha224());
  29140. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  29141. #endif
  29142. AssertNotNull(md = EVP_sha256());
  29143. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  29144. #ifdef WOLFSSL_SHA384
  29145. AssertNotNull(md = EVP_sha384());
  29146. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  29147. #endif
  29148. #ifdef WOLFSSL_SHA512
  29149. AssertNotNull(md = EVP_sha512());
  29150. AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  29151. #endif
  29152. printf(resultFmt, passed);
  29153. #endif
  29154. return 0;
  29155. }
  29156. #ifdef OPENSSL_EXTRA
  29157. static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  29158. size_t testKeySz, const char* testData, size_t testDataSz,
  29159. const byte* testResult, size_t testResultSz)
  29160. {
  29161. unsigned char check[WC_MAX_DIGEST_SIZE];
  29162. size_t checkSz = -1;
  29163. WOLFSSL_EVP_PKEY* key;
  29164. WOLFSSL_EVP_MD_CTX mdCtx;
  29165. printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
  29166. AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  29167. testKey, (int)testKeySz));
  29168. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29169. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29170. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29171. (unsigned int)testDataSz), 1);
  29172. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29173. AssertIntEQ((int)checkSz, (int)testResultSz);
  29174. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29175. AssertIntEQ((int)checkSz,(int)testResultSz);
  29176. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29177. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29178. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29179. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29180. (unsigned int)testDataSz), 1);
  29181. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29182. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29183. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29184. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  29185. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29186. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29187. AssertIntEQ((int)checkSz, (int)testResultSz);
  29188. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29189. AssertIntEQ((int)checkSz,(int)testResultSz);
  29190. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29191. (unsigned int)testDataSz - 4), 1);
  29192. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29193. AssertIntEQ((int)checkSz,(int)testResultSz);
  29194. AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  29195. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29196. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  29197. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29198. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29199. (unsigned int)testDataSz - 4), 1);
  29200. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  29201. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29202. wolfSSL_EVP_PKEY_free(key);
  29203. }
  29204. #endif
  29205. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  29206. {
  29207. #ifdef OPENSSL_EXTRA
  29208. static const unsigned char testKey[] =
  29209. {
  29210. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29211. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  29212. 0x0b, 0x0b, 0x0b, 0x0b
  29213. };
  29214. static const char testData[] = "Hi There";
  29215. #ifdef WOLFSSL_SHA224
  29216. static const unsigned char testResultSha224[] =
  29217. {
  29218. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  29219. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  29220. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  29221. 0x53, 0x68, 0x4b, 0x22
  29222. };
  29223. #endif
  29224. #ifndef NO_SHA256
  29225. static const unsigned char testResultSha256[] =
  29226. {
  29227. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  29228. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  29229. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  29230. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  29231. };
  29232. #endif
  29233. #ifdef WOLFSSL_SHA384
  29234. static const unsigned char testResultSha384[] =
  29235. {
  29236. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  29237. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  29238. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  29239. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  29240. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  29241. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  29242. };
  29243. #endif
  29244. #ifdef WOLFSSL_SHA512
  29245. static const unsigned char testResultSha512[] =
  29246. {
  29247. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  29248. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  29249. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  29250. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  29251. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  29252. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  29253. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  29254. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  29255. };
  29256. #endif
  29257. #ifdef WOLFSSL_SHA3
  29258. #ifndef WOLFSSL_NOSHA3_224
  29259. static const unsigned char testResultSha3_224[] =
  29260. {
  29261. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  29262. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  29263. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  29264. 0xf3, 0xc8, 0x60, 0xf7
  29265. };
  29266. #endif
  29267. #ifndef WOLFSSL_NOSHA3_256
  29268. static const unsigned char testResultSha3_256[] =
  29269. {
  29270. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  29271. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  29272. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  29273. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  29274. };
  29275. #endif
  29276. #ifndef WOLFSSL_NOSHA3_384
  29277. static const unsigned char testResultSha3_384[] =
  29278. {
  29279. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  29280. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  29281. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  29282. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  29283. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  29284. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  29285. };
  29286. #endif
  29287. #ifndef WOLFSSL_NOSHA3_512
  29288. static const unsigned char testResultSha3_512[] =
  29289. {
  29290. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  29291. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  29292. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  29293. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  29294. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  29295. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  29296. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  29297. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  29298. };
  29299. #endif
  29300. #endif
  29301. #ifndef NO_SHA256
  29302. test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
  29303. XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
  29304. #endif
  29305. #ifdef WOLFSSL_SHA224
  29306. test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
  29307. XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
  29308. #endif
  29309. #ifdef WOLFSSL_SHA384
  29310. test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
  29311. XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
  29312. #endif
  29313. #ifdef WOLFSSL_SHA512
  29314. test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
  29315. XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
  29316. #endif
  29317. #ifdef WOLFSSL_SHA3
  29318. #ifndef WOLFSSL_NOSHA3_224
  29319. test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
  29320. testData, XSTRLEN(testData), testResultSha3_224,
  29321. sizeof(testResultSha3_224));
  29322. #endif
  29323. #ifndef WOLFSSL_NOSHA3_256
  29324. test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
  29325. testData, XSTRLEN(testData), testResultSha3_256,
  29326. sizeof(testResultSha3_256));
  29327. #endif
  29328. #ifndef WOLFSSL_NOSHA3_384
  29329. test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
  29330. testData, XSTRLEN(testData), testResultSha3_384,
  29331. sizeof(testResultSha3_384));
  29332. #endif
  29333. #ifndef WOLFSSL_NOSHA3_512
  29334. test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
  29335. testData, XSTRLEN(testData), testResultSha3_512,
  29336. sizeof(testResultSha3_512));
  29337. #endif
  29338. #endif
  29339. printf(resultFmt, passed);
  29340. #endif /* OPENSSL_EXTRA */
  29341. return 0;
  29342. }
  29343. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  29344. {
  29345. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  29346. defined(USE_CERT_BUFFERS_2048)
  29347. WOLFSSL_EVP_PKEY* privKey;
  29348. WOLFSSL_EVP_PKEY* pubKey;
  29349. WOLFSSL_EVP_PKEY_CTX* keyCtx;
  29350. const char testData[] = "Hi There";
  29351. WOLFSSL_EVP_MD_CTX mdCtx;
  29352. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  29353. size_t checkSz = -1;
  29354. int sz = 2048 / 8;
  29355. const unsigned char* cp;
  29356. const unsigned char* p;
  29357. unsigned char check[2048/8];
  29358. size_t i;
  29359. int paddings[] = {
  29360. RSA_PKCS1_PADDING,
  29361. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  29362. RSA_PKCS1_PSS_PADDING,
  29363. #endif
  29364. };
  29365. printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
  29366. cp = client_key_der_2048;
  29367. AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  29368. sizeof_client_key_der_2048)));
  29369. p = client_keypub_der_2048;
  29370. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29371. sizeof_client_keypub_der_2048)));
  29372. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29373. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  29374. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29375. NULL, privKey), 1);
  29376. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29377. (unsigned int)XSTRLEN(testData)), 1);
  29378. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29379. AssertIntEQ((int)checkSz, sz);
  29380. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29381. AssertIntEQ((int)checkSz,sz);
  29382. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29383. AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  29384. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
  29385. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29386. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29387. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29388. NULL, pubKey), 1);
  29389. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29390. (unsigned int)XSTRLEN(testData)),
  29391. 1);
  29392. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29393. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29394. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29395. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29396. NULL, privKey), 1);
  29397. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29398. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29399. AssertIntEQ((int)checkSz, sz);
  29400. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29401. AssertIntEQ((int)checkSz, sz);
  29402. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29403. (unsigned int)XSTRLEN(testData) - 4), 1);
  29404. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29405. AssertIntEQ((int)checkSz, sz);
  29406. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29407. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29408. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29409. NULL, pubKey), 1);
  29410. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29411. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29412. (unsigned int)XSTRLEN(testData) - 4),
  29413. 1);
  29414. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29415. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29416. /* Check all signing padding types */
  29417. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  29418. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29419. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  29420. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  29421. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29422. paddings[i]), 1);
  29423. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29424. (unsigned int)XSTRLEN(testData)), 1);
  29425. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29426. AssertIntEQ((int)checkSz, sz);
  29427. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29428. AssertIntEQ((int)checkSz,sz);
  29429. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29430. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29431. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  29432. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  29433. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  29434. paddings[i]), 1);
  29435. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29436. (unsigned int)XSTRLEN(testData)), 1);
  29437. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29438. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29439. }
  29440. wolfSSL_EVP_PKEY_free(pubKey);
  29441. wolfSSL_EVP_PKEY_free(privKey);
  29442. printf(resultFmt, passed);
  29443. #endif
  29444. return 0;
  29445. }
  29446. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  29447. {
  29448. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29449. WOLFSSL_EVP_PKEY* privKey;
  29450. WOLFSSL_EVP_PKEY* pubKey;
  29451. const char testData[] = "Hi There";
  29452. WOLFSSL_EVP_MD_CTX mdCtx;
  29453. size_t checkSz = -1;
  29454. const unsigned char* cp;
  29455. const unsigned char* p;
  29456. unsigned char check[2048/8];
  29457. printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
  29458. cp = ecc_clikey_der_256;
  29459. privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  29460. sizeof_ecc_clikey_der_256);
  29461. AssertNotNull(privKey);
  29462. p = ecc_clikeypub_der_256;
  29463. AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  29464. sizeof_ecc_clikeypub_der_256)));
  29465. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29466. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29467. NULL, privKey), 1);
  29468. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  29469. (unsigned int)XSTRLEN(testData)), 1);
  29470. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29471. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29472. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29473. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29474. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29475. NULL, pubKey), 1);
  29476. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  29477. (unsigned int)XSTRLEN(testData)),
  29478. 1);
  29479. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29480. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29481. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29482. AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29483. NULL, privKey), 1);
  29484. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  29485. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  29486. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29487. AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  29488. (unsigned int)XSTRLEN(testData) - 4), 1);
  29489. AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  29490. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29491. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  29492. AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  29493. NULL, pubKey), 1);
  29494. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  29495. AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  29496. (unsigned int)XSTRLEN(testData) - 4),
  29497. 1);
  29498. AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  29499. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  29500. wolfSSL_EVP_PKEY_free(pubKey);
  29501. wolfSSL_EVP_PKEY_free(privKey);
  29502. printf(resultFmt, passed);
  29503. #endif
  29504. return 0;
  29505. }
  29506. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  29507. {
  29508. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29509. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  29510. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  29511. char caFile[] = "./certs/client-ca.pem";
  29512. char clientFile[] = "./certs/client-cert.pem";
  29513. SSL_CTX* ctx;
  29514. X509* x509;
  29515. BIO *bio = NULL;
  29516. X509 *cert = NULL;
  29517. X509 *ca;
  29518. STACK_OF(X509) *chain = NULL;
  29519. STACK_OF(X509) *chain2 = NULL;
  29520. printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
  29521. #ifndef NO_WOLFSSL_SERVER
  29522. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29523. #else
  29524. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29525. #endif
  29526. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  29527. AssertNotNull(x509);
  29528. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  29529. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  29530. AssertNotNull(x509);
  29531. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  29532. /* additional test of getting EVP_PKEY key size from X509
  29533. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  29534. * allowed with user RSA */
  29535. {
  29536. EVP_PKEY* pkey;
  29537. #if defined(HAVE_ECC)
  29538. X509* ecX509;
  29539. #endif /* HAVE_ECC */
  29540. AssertNotNull(pkey = X509_get_pubkey(x509));
  29541. /* current RSA key is 2048 bit (256 bytes) */
  29542. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  29543. EVP_PKEY_free(pkey);
  29544. #if defined(HAVE_ECC)
  29545. #if defined(USE_CERT_BUFFERS_256)
  29546. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  29547. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  29548. SSL_FILETYPE_ASN1));
  29549. #else
  29550. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
  29551. SSL_FILETYPE_PEM));
  29552. #endif
  29553. pkey = X509_get_pubkey(ecX509);
  29554. AssertNotNull(pkey);
  29555. /* current ECC key is 256 bit (32 bytes) */
  29556. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  29557. X509_free(ecX509);
  29558. EVP_PKEY_free(pkey);
  29559. #endif /* HAVE_ECC */
  29560. }
  29561. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  29562. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  29563. #ifdef WOLFSSL_ENCRYPTED_KEYS
  29564. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  29565. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  29566. #endif
  29567. SSL_CTX_free(ctx);
  29568. #ifndef NO_WOLFSSL_SERVER
  29569. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  29570. #else
  29571. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  29572. #endif
  29573. /* Test haproxy use case */
  29574. AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
  29575. /* Read Certificate */
  29576. AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  29577. AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  29578. AssertNotNull(chain = sk_X509_new_null());
  29579. AssertIntEQ(sk_X509_push(chain, ca), 1);
  29580. AssertNotNull(chain2 = X509_chain_up_ref(chain));
  29581. AssertNotNull(ca = sk_X509_shift(chain2));
  29582. AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  29583. AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  29584. BIO_free(bio);
  29585. X509_free(cert);
  29586. sk_X509_pop_free(chain, X509_free);
  29587. sk_X509_pop_free(chain2, X509_free);
  29588. SSL_CTX_free(ctx);
  29589. printf(resultFmt, passed);
  29590. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  29591. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29592. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  29593. return 0;
  29594. }
  29595. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  29596. static int test_wolfSSL_ERR_peek_last_error_line(void)
  29597. {
  29598. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29599. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  29600. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  29601. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  29602. tcp_ready ready;
  29603. func_args client_args;
  29604. func_args server_args;
  29605. #ifndef SINGLE_THREADED
  29606. THREAD_TYPE serverThread;
  29607. #endif
  29608. callback_functions client_cb;
  29609. callback_functions server_cb;
  29610. int line = 0;
  29611. int flag = ERR_TXT_STRING;
  29612. const char* file = NULL;
  29613. const char* data = NULL;
  29614. printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
  29615. /* create a failed connection and inspect the error */
  29616. #ifdef WOLFSSL_TIRTOS
  29617. fdOpenSession(Task_self());
  29618. #endif
  29619. XMEMSET(&client_args, 0, sizeof(func_args));
  29620. XMEMSET(&server_args, 0, sizeof(func_args));
  29621. StartTCP();
  29622. InitTcpReady(&ready);
  29623. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  29624. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  29625. client_cb.method = wolfTLSv1_1_client_method;
  29626. server_cb.method = wolfTLSv1_2_server_method;
  29627. server_args.signal = &ready;
  29628. server_args.callbacks = &server_cb;
  29629. client_args.signal = &ready;
  29630. client_args.callbacks = &client_cb;
  29631. #ifndef SINGLE_THREADED
  29632. start_thread(test_server_nofail, &server_args, &serverThread);
  29633. wait_tcp_ready(&server_args);
  29634. test_client_nofail(&client_args, NULL);
  29635. join_thread(serverThread);
  29636. #endif
  29637. FreeTcpReady(&ready);
  29638. AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  29639. AssertNotNull(data);
  29640. /* check clearing error state */
  29641. ERR_remove_state(0);
  29642. AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29643. ERR_peek_last_error_line(NULL, &line);
  29644. AssertIntEQ(line, 0);
  29645. ERR_peek_last_error_line(&file, NULL);
  29646. AssertNull(file);
  29647. /* retry connection to fill error queue */
  29648. XMEMSET(&client_args, 0, sizeof(func_args));
  29649. XMEMSET(&server_args, 0, sizeof(func_args));
  29650. StartTCP();
  29651. InitTcpReady(&ready);
  29652. client_cb.method = wolfTLSv1_1_client_method;
  29653. server_cb.method = wolfTLSv1_2_server_method;
  29654. server_args.signal = &ready;
  29655. server_args.callbacks = &server_cb;
  29656. client_args.signal = &ready;
  29657. client_args.callbacks = &client_cb;
  29658. start_thread(test_server_nofail, &server_args, &serverThread);
  29659. wait_tcp_ready(&server_args);
  29660. test_client_nofail(&client_args, NULL);
  29661. join_thread(serverThread);
  29662. FreeTcpReady(&ready);
  29663. /* check that error code was stored */
  29664. AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  29665. ERR_peek_last_error_line(NULL, &line);
  29666. AssertIntNE(line, 0);
  29667. ERR_peek_last_error_line(&file, NULL);
  29668. AssertNotNull(file);
  29669. #ifdef WOLFSSL_TIRTOS
  29670. fdOpenSession(Task_self());
  29671. #endif
  29672. printf(resultFmt, passed);
  29673. printf("\nTesting error print out\n");
  29674. ERR_print_errors_fp(stdout);
  29675. printf("Done testing print out\n\n");
  29676. fflush(stdout);
  29677. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29678. !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  29679. return 0;
  29680. }
  29681. #endif
  29682. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  29683. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29684. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  29685. {
  29686. (void) ok;
  29687. (void) ctx;
  29688. printf("ENTER verify_cb\n");
  29689. return SSL_SUCCESS;
  29690. }
  29691. #endif
  29692. static int test_wolfSSL_X509_Name_canon(void)
  29693. {
  29694. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29695. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  29696. defined(WOLFSSL_CERT_GEN) && \
  29697. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  29698. const long ex_hash1 = 0x0fdb2da4;
  29699. const long ex_hash2 = 0x9f3e8c9e;
  29700. X509_NAME *name = NULL;
  29701. X509 *x509 = NULL;
  29702. FILE* file = NULL;
  29703. unsigned long hash = 0;
  29704. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  29705. byte *pbuf = NULL;
  29706. word32 len = 0;
  29707. (void) ex_hash2;
  29708. printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
  29709. file = XFOPEN(caCertFile, "rb");
  29710. AssertNotNull(file);
  29711. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29712. AssertNotNull(name = X509_get_issuer_name(x509));
  29713. /* When output buffer is NULL, should return necessary output buffer
  29714. * length.*/
  29715. AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  29716. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29717. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29718. hash = (((unsigned long)digest[3] << 24) |
  29719. ((unsigned long)digest[2] << 16) |
  29720. ((unsigned long)digest[1] << 8) |
  29721. ((unsigned long)digest[0]));
  29722. AssertIntEQ(hash, ex_hash1);
  29723. XFCLOSE(file);
  29724. X509_free(x509);
  29725. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29726. pbuf = NULL;
  29727. file = XFOPEN(cliCertFile, "rb");
  29728. AssertNotNull(file);
  29729. AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  29730. AssertNotNull(name = X509_get_issuer_name(x509));
  29731. AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  29732. AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  29733. hash = (((unsigned long)digest[3] << 24) |
  29734. ((unsigned long)digest[2] << 16) |
  29735. ((unsigned long)digest[1] << 8) |
  29736. ((unsigned long)digest[0]));
  29737. AssertIntEQ(hash, ex_hash2);
  29738. XFCLOSE(file);
  29739. X509_free(x509);
  29740. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  29741. printf(resultFmt, passed);
  29742. #endif
  29743. return 0;
  29744. }
  29745. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  29746. {
  29747. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  29748. const int MAX_DIR = 4;
  29749. const char paths[][32] = {
  29750. "./certs/ed25519",
  29751. "./certs/ecc",
  29752. "./certs/crl",
  29753. "./certs/",
  29754. };
  29755. char CertCrl_path[MAX_FILENAME_SZ];
  29756. char *p;
  29757. X509_STORE* str;
  29758. X509_LOOKUP* lookup;
  29759. WOLFSSL_STACK* sk = NULL;
  29760. int len, total_len, i;
  29761. (void) sk;
  29762. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
  29763. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  29764. /* illegal string */
  29765. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29766. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29767. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  29768. SSL_FILETYPE_PEM,NULL), 0);
  29769. /* free store */
  29770. X509_STORE_free(str);
  29771. /* short folder string */
  29772. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29773. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29774. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  29775. SSL_FILETYPE_PEM,NULL), 1);
  29776. #if defined(WOLFSSL_INT_H)
  29777. /* only available when including internal.h */
  29778. AssertNotNull(sk = lookup->dirs->dir_entry);
  29779. #endif
  29780. /* free store */
  29781. X509_STORE_free(str);
  29782. /* typical function check */
  29783. p = &CertCrl_path[0];
  29784. total_len = 0;
  29785. for(i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  29786. len = (int)XSTRLEN((const char*)&paths[i]);
  29787. total_len += len;
  29788. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  29789. p += len;
  29790. if (i != 0) *(p++) = SEPARATOR_CHAR;
  29791. }
  29792. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29793. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29794. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  29795. SSL_FILETYPE_PEM,NULL), 1);
  29796. #if defined(WOLFSSL_INT_H)
  29797. /* only available when including internal.h */
  29798. AssertNotNull(sk = lookup->dirs->dir_entry);
  29799. #endif
  29800. X509_STORE_free(str);
  29801. printf(resultFmt, passed);
  29802. #endif
  29803. return 0;
  29804. }
  29805. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  29806. {
  29807. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  29808. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  29809. defined(WOLFSSL_SIGNER_DER_CERT)
  29810. X509_STORE_CTX* ctx;
  29811. X509_STORE* str;
  29812. X509_LOOKUP* lookup;
  29813. X509* cert1;
  29814. X509* x509Ca;
  29815. X509* x509Svr;
  29816. X509* issuer;
  29817. WOLFSSL_STACK* sk = NULL;
  29818. X509_NAME* caName;
  29819. X509_NAME* issuerName;
  29820. FILE* file1 = NULL;
  29821. int i, cert_count, cmp;
  29822. char der[] = "certs/ca-cert.der";
  29823. #ifdef HAVE_CRL
  29824. char pem[][100] = {
  29825. "./certs/crl/crl.pem",
  29826. "./certs/crl/crl2.pem",
  29827. "./certs/crl/caEccCrl.pem",
  29828. "./certs/crl/eccCliCRL.pem",
  29829. "./certs/crl/eccSrvCRL.pem",
  29830. ""
  29831. };
  29832. #endif
  29833. printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
  29834. AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
  29835. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  29836. fclose(file1);
  29837. AssertNotNull(ctx = X509_STORE_CTX_new());
  29838. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29839. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29840. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29841. SSL_FILETYPE_PEM,NULL), 1);
  29842. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29843. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29844. /* check if CA cert is loaded into the store */
  29845. for (i = 0; i < cert_count; i++) {
  29846. x509Ca = sk_X509_value(sk, i);
  29847. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29848. }
  29849. AssertNotNull((x509Svr =
  29850. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29851. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29852. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29853. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29854. AssertNotNull(issuer);
  29855. caName = X509_get_subject_name(x509Ca);
  29856. AssertNotNull(caName);
  29857. issuerName = X509_get_subject_name(issuer);
  29858. AssertNotNull(issuerName);
  29859. cmp = X509_NAME_cmp(caName, issuerName);
  29860. AssertIntEQ(cmp, 0);
  29861. /* load der format */
  29862. X509_free(issuer);
  29863. X509_STORE_CTX_free(ctx);
  29864. X509_STORE_free(str);
  29865. sk_X509_pop_free(sk, NULL);
  29866. X509_free(x509Svr);
  29867. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29868. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29869. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  29870. SSL_FILETYPE_ASN1,NULL), 1);
  29871. AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  29872. AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
  29873. /* check if CA cert is loaded into the store */
  29874. for (i = 0; i < cert_count; i++) {
  29875. x509Ca = sk_X509_value(sk, i);
  29876. AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  29877. }
  29878. X509_STORE_free(str);
  29879. sk_X509_pop_free(sk, NULL);
  29880. X509_free(cert1);
  29881. #ifdef HAVE_CRL
  29882. AssertNotNull(str = wolfSSL_X509_STORE_new());
  29883. AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  29884. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  29885. SSL_FILETYPE_PEM,NULL), 1);
  29886. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  29887. "certs/server-revoked-cert.pem",
  29888. SSL_FILETYPE_PEM,NULL), 1);
  29889. if (str) {
  29890. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  29891. WOLFSSL_FILETYPE_PEM), 1);
  29892. /* since store hasn't yet known the revoked cert*/
  29893. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29894. "certs/server-revoked-cert.pem",
  29895. WOLFSSL_FILETYPE_PEM), 1);
  29896. }
  29897. for (i = 0; pem[i][0] != '\0'; i++)
  29898. {
  29899. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  29900. SSL_FILETYPE_PEM, NULL), 1);
  29901. }
  29902. if (str) {
  29903. /* since store knows crl list */
  29904. AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
  29905. "certs/server-revoked-cert.pem",
  29906. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  29907. }
  29908. AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  29909. X509_STORE_free(str);
  29910. #endif
  29911. printf(resultFmt, passed);
  29912. #endif
  29913. return 0;
  29914. }
  29915. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  29916. {
  29917. #if defined(OPENSSL_EXTRA)
  29918. printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
  29919. X509_STORE_CTX_cleanup(NULL);
  29920. X509_STORE_CTX_trusted_stack(NULL, NULL);
  29921. AssertTrue(1); /* to confirm previous call gives no harm */
  29922. printf(resultFmt, passed);
  29923. #endif
  29924. return 0;
  29925. }
  29926. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  29927. {
  29928. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  29929. #ifdef WOLFSSL_SIGNER_DER_CERT
  29930. int cmp;
  29931. #endif
  29932. X509_STORE_CTX* ctx;
  29933. X509_STORE* str;
  29934. X509* x509Ca;
  29935. X509* x509Svr;
  29936. X509* issuer;
  29937. X509_NAME* caName;
  29938. X509_NAME* issuerName;
  29939. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
  29940. AssertNotNull(ctx = X509_STORE_CTX_new());
  29941. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  29942. AssertNotNull((x509Ca =
  29943. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  29944. AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  29945. AssertNotNull((x509Svr =
  29946. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  29947. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  29948. AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
  29949. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  29950. AssertNotNull(issuer);
  29951. caName = X509_get_subject_name(x509Ca);
  29952. AssertNotNull(caName);
  29953. issuerName = X509_get_subject_name(issuer);
  29954. AssertNotNull(issuerName);
  29955. #ifdef WOLFSSL_SIGNER_DER_CERT
  29956. cmp = X509_NAME_cmp(caName, issuerName);
  29957. AssertIntEQ(cmp, 0);
  29958. #endif
  29959. X509_free(issuer);
  29960. X509_STORE_CTX_free(ctx);
  29961. X509_free(x509Svr);
  29962. X509_STORE_free(str);
  29963. X509_free(x509Ca);
  29964. printf(resultFmt, passed);
  29965. #endif
  29966. return 0;
  29967. }
  29968. static int test_wolfSSL_PKCS7_certs(void)
  29969. {
  29970. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  29971. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  29972. STACK_OF(X509)* sk = NULL;
  29973. STACK_OF(X509_INFO)* info_sk = NULL;
  29974. PKCS7 *p7 = NULL;
  29975. BIO* bio;
  29976. const byte* p = NULL;
  29977. int buflen = 0;
  29978. int i;
  29979. printf(testingFmt, "wolfSSL_PKCS7_certs()");
  29980. /* Test twice. Once with d2i and once without to test
  29981. * that everything is free'd correctly. */
  29982. for (i = 0; i < 2; i++) {
  29983. AssertNotNull(p7 = PKCS7_new());
  29984. p7->version = 1;
  29985. p7->hashOID = SHAh;
  29986. AssertNotNull(bio = BIO_new(BIO_s_file()));
  29987. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29988. AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  29989. AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
  29990. AssertNotNull(sk = sk_X509_new_null());
  29991. while (sk_X509_INFO_num(info_sk)) {
  29992. X509_INFO* info;
  29993. AssertNotNull(info = sk_X509_INFO_shift(info_sk));
  29994. AssertIntEQ(sk_X509_push(sk, info->x509), 1);
  29995. info->x509 = NULL;
  29996. X509_INFO_free(info);
  29997. }
  29998. sk_X509_INFO_free(info_sk);
  29999. BIO_free(bio);
  30000. bio = BIO_new(BIO_s_mem());
  30001. AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  30002. AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  30003. if (i == 0) {
  30004. PKCS7_free(p7);
  30005. AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
  30006. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
  30007. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  30008. /* PKCS7_free free's the certs */
  30009. AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
  30010. }
  30011. BIO_free(bio);
  30012. PKCS7_free(p7);
  30013. }
  30014. printf(resultFmt, passed);
  30015. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  30016. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  30017. return 0;
  30018. }
  30019. static int test_wolfSSL_X509_STORE_CTX(void)
  30020. {
  30021. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30022. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30023. X509_STORE_CTX* ctx;
  30024. X509_STORE* str;
  30025. X509* x509;
  30026. #ifdef OPENSSL_ALL
  30027. X509* x5092;
  30028. STACK_OF(X509) *sk, *sk2, *sk3;
  30029. #endif
  30030. printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
  30031. AssertNotNull(ctx = X509_STORE_CTX_new());
  30032. AssertNotNull((str = wolfSSL_X509_STORE_new()));
  30033. AssertNotNull((x509 =
  30034. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  30035. AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  30036. #ifdef OPENSSL_ALL
  30037. /* sk_X509_new only in OPENSSL_ALL */
  30038. sk = sk_X509_new_null();
  30039. AssertNotNull(sk);
  30040. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  30041. #else
  30042. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  30043. #endif
  30044. AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  30045. X509_STORE_CTX_set_error(ctx, -5);
  30046. X509_STORE_CTX_set_error(NULL, -5);
  30047. X509_STORE_CTX_free(ctx);
  30048. #ifdef OPENSSL_ALL
  30049. sk_X509_pop_free(sk, NULL);
  30050. #endif
  30051. X509_STORE_free(str);
  30052. X509_free(x509);
  30053. AssertNotNull(ctx = X509_STORE_CTX_new());
  30054. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  30055. X509_STORE_CTX_free(ctx);
  30056. #ifdef OPENSSL_ALL
  30057. /* test X509_STORE_CTX_get(1)_chain */
  30058. AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
  30059. SSL_FILETYPE_PEM)));
  30060. AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  30061. SSL_FILETYPE_PEM)));
  30062. AssertNotNull((sk = sk_X509_new_null()));
  30063. AssertIntEQ(sk_X509_push(sk, x509), 1);
  30064. AssertNotNull((str = X509_STORE_new()));
  30065. AssertNotNull((ctx = X509_STORE_CTX_new()));
  30066. AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  30067. AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  30068. AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  30069. AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  30070. AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  30071. AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  30072. AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  30073. X509_STORE_CTX_free(ctx);
  30074. X509_STORE_free(str);
  30075. /* CTX certs not freed yet */
  30076. X509_free(x5092);
  30077. sk_X509_pop_free(sk, NULL);
  30078. /* sk3 is dup so free here */
  30079. sk_X509_pop_free(sk3, NULL);
  30080. #endif
  30081. /* test X509_STORE_CTX_get/set_ex_data */
  30082. {
  30083. int i = 0, tmpData = 5;
  30084. void* tmpDataRet;
  30085. AssertNotNull(ctx = X509_STORE_CTX_new());
  30086. #ifdef HAVE_EX_DATA
  30087. for (i = 0; i < MAX_EX_DATA; i++) {
  30088. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30089. WOLFSSL_SUCCESS);
  30090. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30091. AssertNotNull(tmpDataRet);
  30092. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30093. }
  30094. #else
  30095. AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  30096. WOLFSSL_FAILURE);
  30097. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  30098. AssertNull(tmpDataRet);
  30099. #endif
  30100. X509_STORE_CTX_free(ctx);
  30101. }
  30102. /* test X509_STORE_get/set_ex_data */
  30103. {
  30104. int i = 0, tmpData = 99;
  30105. void* tmpDataRet;
  30106. AssertNotNull(str = X509_STORE_new());
  30107. #ifdef HAVE_EX_DATA
  30108. for (i = 0; i < MAX_EX_DATA; i++) {
  30109. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30110. WOLFSSL_SUCCESS);
  30111. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30112. AssertNotNull(tmpDataRet);
  30113. AssertIntEQ(tmpData, *(int*)tmpDataRet);
  30114. }
  30115. #else
  30116. AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  30117. WOLFSSL_FAILURE);
  30118. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  30119. AssertNull(tmpDataRet);
  30120. #endif
  30121. X509_STORE_free(str);
  30122. }
  30123. printf(resultFmt, passed);
  30124. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30125. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30126. return 0;
  30127. }
  30128. static int test_wolfSSL_X509_STORE_set_flags(void)
  30129. {
  30130. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30131. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  30132. X509_STORE* store;
  30133. X509* x509;
  30134. printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
  30135. AssertNotNull((store = wolfSSL_X509_STORE_new()));
  30136. AssertNotNull((x509 =
  30137. wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
  30138. AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  30139. #ifdef HAVE_CRL
  30140. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
  30141. #else
  30142. AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  30143. NOT_COMPILED_IN);
  30144. #endif
  30145. wolfSSL_X509_free(x509);
  30146. wolfSSL_X509_STORE_free(store);
  30147. printf(resultFmt, passed);
  30148. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30149. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30150. return 0;
  30151. }
  30152. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  30153. {
  30154. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30155. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  30156. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  30157. WOLFSSL_X509_STORE* store;
  30158. WOLFSSL_X509_LOOKUP* lookup;
  30159. printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
  30160. AssertNotNull(store = wolfSSL_X509_STORE_new());
  30161. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  30162. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  30163. X509_FILETYPE_PEM), 1);
  30164. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  30165. X509_FILETYPE_PEM), 1);
  30166. if (store) {
  30167. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  30168. WOLFSSL_FILETYPE_PEM), 1);
  30169. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30170. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  30171. }
  30172. AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  30173. X509_FILETYPE_PEM), 1);
  30174. if (store) {
  30175. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  30176. WOLFSSL_FILETYPE_PEM), 1);
  30177. }
  30178. wolfSSL_X509_STORE_free(store);
  30179. printf(resultFmt, passed);
  30180. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  30181. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  30182. return 0;
  30183. }
  30184. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  30185. {
  30186. #if defined(OPENSSL_EXTRA)
  30187. WOLFSSL_X509_STORE_CTX* ctx;
  30188. time_t c_time;
  30189. printf(testingFmt, "wolfSSL_X509_set_time()");
  30190. AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  30191. c_time = 365*24*60*60;
  30192. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  30193. AssertTrue(
  30194. (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
  30195. AssertTrue(ctx->param->check_time == c_time);
  30196. wolfSSL_X509_STORE_CTX_free(ctx);
  30197. printf(resultFmt, passed);
  30198. #endif /* OPENSSL_EXTRA */
  30199. return 0;
  30200. }
  30201. static int test_wolfSSL_CTX_get0_set1_param(void)
  30202. {
  30203. #if defined(OPENSSL_EXTRA)
  30204. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30205. int ret;
  30206. SSL_CTX* ctx;
  30207. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30208. WOLFSSL_X509_VERIFY_PARAM* pvpm;
  30209. char testIPv4[] = "127.0.0.1";
  30210. char testhostName[] = "foo.hoge.com";
  30211. printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
  30212. #ifndef NO_WOLFSSL_SERVER
  30213. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30214. #else
  30215. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30216. #endif
  30217. AssertNull(SSL_CTX_get0_param(NULL));
  30218. AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
  30219. pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  30220. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
  30221. AssertNotNull(pvpm);
  30222. XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30223. wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  30224. (int)XSTRLEN(testhostName));
  30225. wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
  30226. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  30227. ret = SSL_CTX_set1_param(ctx, pvpm);
  30228. AssertIntEQ(1, ret);
  30229. AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  30230. (int)XSTRLEN(testhostName)));
  30231. AssertIntEQ(0x01, pParam->hostFlags);
  30232. AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  30233. /* test for incorrect patameter */
  30234. AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  30235. AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  30236. AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  30237. SSL_CTX_free(ctx);
  30238. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  30239. printf(resultFmt, passed);
  30240. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30241. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30242. return 0;
  30243. }
  30244. static int test_wolfSSL_get0_param(void)
  30245. {
  30246. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30247. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30248. SSL_CTX* ctx;
  30249. SSL* ssl;
  30250. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30251. printf(testingFmt, "wolfSSL_get0_param()");
  30252. #ifndef NO_WOLFSSL_SERVER
  30253. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30254. #else
  30255. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30256. #endif
  30257. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30258. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30259. AssertNotNull(ssl = SSL_new(ctx));
  30260. pParam = SSL_get0_param(ssl);
  30261. (void)pParam;
  30262. SSL_free(ssl);
  30263. SSL_CTX_free(ctx);
  30264. printf(resultFmt, passed);
  30265. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30266. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  30267. return 0;
  30268. }
  30269. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  30270. {
  30271. #if defined(OPENSSL_EXTRA)
  30272. const char host[] = "www.example.com";
  30273. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30274. printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
  30275. AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  30276. sizeof(WOLFSSL_X509_VERIFY_PARAM),
  30277. HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  30278. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30279. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  30280. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30281. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  30282. AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30283. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30284. printf(resultFmt, passed);
  30285. #endif /* OPENSSL_EXTRA */
  30286. return 0;
  30287. }
  30288. static int test_wolfSSL_set1_host(void)
  30289. {
  30290. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  30291. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30292. const char host[] = "www.test_wolfSSL_set1_host.com";
  30293. const char emptyStr[] = "";
  30294. SSL_CTX* ctx;
  30295. SSL* ssl;
  30296. WOLFSSL_X509_VERIFY_PARAM* pParam;
  30297. printf(testingFmt, "wolfSSL_set1_host()");
  30298. #ifndef NO_WOLFSSL_SERVER
  30299. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30300. #else
  30301. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30302. #endif
  30303. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30304. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30305. AssertNotNull(ssl = SSL_new(ctx));
  30306. pParam = SSL_get0_param(ssl);
  30307. /* we should get back host string */
  30308. SSL_set1_host(ssl, host);
  30309. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30310. /* we should get back empty string */
  30311. SSL_set1_host(ssl, emptyStr);
  30312. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30313. /* we should get back host string */
  30314. SSL_set1_host(ssl, host);
  30315. AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  30316. /* we should get back empty string */
  30317. SSL_set1_host(ssl, NULL);
  30318. AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  30319. SSL_free(ssl);
  30320. SSL_CTX_free(ctx);
  30321. printf(resultFmt, passed);
  30322. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30323. #endif /* OPENSSL_EXTRA */
  30324. return 0;
  30325. }
  30326. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  30327. {
  30328. #if defined(OPENSSL_EXTRA)
  30329. unsigned char buf[16] = {0};
  30330. WOLFSSL_X509_VERIFY_PARAM* param;
  30331. printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
  30332. AssertNotNull(param = X509_VERIFY_PARAM_new());
  30333. /* test 127.0.0.1 */
  30334. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  30335. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  30336. AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  30337. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  30338. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30339. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  30340. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  30341. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  30342. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30343. AssertIntEQ(XSTRNCMP(param->ipasc,
  30344. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  30345. /* test 2001:db8:: */
  30346. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30347. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30348. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30349. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  30350. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30351. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  30352. /* test ::1234:5678 */
  30353. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  30354. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30355. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30356. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30357. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30358. AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  30359. /* test 2001:db8::1234:5678 */
  30360. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30361. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  30362. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  30363. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  30364. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30365. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  30366. sizeof(param->ipasc)), 0);
  30367. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  30368. /* 2001:db8:1::ab9:c0a8:102 */
  30369. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  30370. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  30371. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  30372. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  30373. AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  30374. AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  30375. sizeof(param->ipasc)), 0);
  30376. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  30377. printf(resultFmt, passed);
  30378. #endif /* OPENSSL_EXTRA */
  30379. return 0;
  30380. }
  30381. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  30382. {
  30383. #if defined(OPENSSL_EXTRA)
  30384. X509_STORE* store;
  30385. X509_STORE_CTX* ctx;
  30386. X509_STORE_CTX* ctx_no_init;
  30387. printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
  30388. AssertNotNull((store = X509_STORE_new()));
  30389. AssertNotNull(ctx = X509_STORE_CTX_new());
  30390. AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
  30391. AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  30392. AssertNull(X509_STORE_CTX_get0_store(NULL));
  30393. /* should return NULL if ctx has not bee initialized */
  30394. AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
  30395. AssertNotNull(X509_STORE_CTX_get0_store(ctx));
  30396. wolfSSL_X509_STORE_CTX_free(ctx);
  30397. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  30398. X509_STORE_free(store);
  30399. printf(resultFmt, passed);
  30400. #endif /* OPENSSL_EXTRA */
  30401. return 0;
  30402. }
  30403. static int test_wolfSSL_CTX_set_client_CA_list(void)
  30404. {
  30405. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30406. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  30407. WOLFSSL_CTX* ctx;
  30408. WOLFSSL* ssl;
  30409. X509_NAME* name = NULL;
  30410. STACK_OF(X509_NAME)* names = NULL;
  30411. STACK_OF(X509_NAME)* ca_list = NULL;
  30412. int i, names_len;
  30413. printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
  30414. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  30415. /* Send two X501 names in cert request */
  30416. names = SSL_load_client_CA_file(cliCertFile);
  30417. AssertNotNull(names);
  30418. ca_list = SSL_load_client_CA_file(caCertFile);
  30419. AssertNotNull(ca_list);
  30420. AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
  30421. SSL_CTX_set_client_CA_list(ctx, names);
  30422. /* This should only free the stack structure */
  30423. sk_X509_NAME_free(ca_list);
  30424. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30425. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30426. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30427. for (i=0; i<names_len; i++) {
  30428. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30429. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30430. }
  30431. /* Needed to be able to create ssl object */
  30432. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30433. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30434. AssertNotNull(ssl = wolfSSL_new(ctx));
  30435. /* load again as old names are responsibility of ctx to free*/
  30436. names = SSL_load_client_CA_file(cliCertFile);
  30437. AssertNotNull(names);
  30438. SSL_set_client_CA_list(ssl, names);
  30439. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
  30440. AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  30441. AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
  30442. for (i=0; i<names_len; i++) {
  30443. AssertNotNull(name = sk_X509_NAME_value(names, i));
  30444. AssertIntEQ(sk_X509_NAME_find(names, name), i);
  30445. }
  30446. printf(resultFmt, passed);
  30447. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  30448. {
  30449. tcp_ready ready;
  30450. func_args server_args;
  30451. callback_functions server_cb;
  30452. THREAD_TYPE serverThread;
  30453. WOLFSSL* ssl_client;
  30454. WOLFSSL_CTX* ctx_client;
  30455. SOCKET_T sockfd = 0;
  30456. printf(testingFmt, "wolfSSL_get_client_CA_list() with handshake");
  30457. StartTCP();
  30458. InitTcpReady(&ready);
  30459. XMEMSET(&server_args, 0, sizeof(func_args));
  30460. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  30461. server_args.signal = &ready;
  30462. server_args.callbacks = &server_cb;
  30463. /* we are responsible for free'ing WOLFSSL_CTX */
  30464. server_cb.ctx = ctx;
  30465. server_cb.isSharedCtx = 1;
  30466. AssertIntEQ(WOLFSSL_SUCCESS,
  30467. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30468. start_thread(test_server_nofail, &server_args, &serverThread);
  30469. wait_tcp_ready(&server_args);
  30470. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30471. AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30472. AssertIntEQ(WOLFSSL_SUCCESS,
  30473. wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
  30474. AssertIntEQ(WOLFSSL_SUCCESS,
  30475. wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  30476. AssertIntEQ(WOLFSSL_SUCCESS,
  30477. wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  30478. AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
  30479. AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  30480. AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  30481. AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  30482. /* We are expecting two cert names to be sent */
  30483. AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
  30484. AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  30485. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  30486. AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
  30487. AssertIntGE(sk_X509_NAME_find(names, name), 0);
  30488. }
  30489. wolfSSL_shutdown(ssl_client);
  30490. wolfSSL_free(ssl_client);
  30491. wolfSSL_CTX_free(ctx_client);
  30492. join_thread(serverThread);
  30493. FreeTcpReady(&ready);
  30494. printf(resultFmt, passed);
  30495. }
  30496. #endif
  30497. wolfSSL_free(ssl);
  30498. wolfSSL_CTX_free(ctx);
  30499. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
  30500. return 0;
  30501. }
  30502. static int test_wolfSSL_CTX_add_client_CA(void)
  30503. {
  30504. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  30505. !defined(NO_WOLFSSL_CLIENT)
  30506. WOLFSSL_CTX* ctx;
  30507. WOLFSSL_X509* x509;
  30508. WOLFSSL_X509* x509_a;
  30509. STACK_OF(X509_NAME)* ca_list;
  30510. int ret = 0;
  30511. printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
  30512. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30513. /* Add client cert */
  30514. x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  30515. AssertNotNull(x509);
  30516. ret = SSL_CTX_add_client_CA(ctx, x509);
  30517. AssertIntEQ(ret, SSL_SUCCESS);
  30518. AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  30519. /* Add another client cert */
  30520. AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  30521. SSL_FILETYPE_PEM));
  30522. AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  30523. /* test for incorrect parameter */
  30524. AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  30525. AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  30526. AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  30527. X509_free(x509);
  30528. X509_free(x509_a);
  30529. SSL_CTX_free(ctx);
  30530. printf(resultFmt, passed);
  30531. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  30532. return 0;
  30533. }
  30534. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30535. static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
  30536. {
  30537. callback_functions* callbacks = ((func_args*)args)->callbacks;
  30538. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
  30539. WOLFSSL* ssl = NULL;
  30540. SOCKET_T sfd = 0;
  30541. SOCKET_T cfd = 0;
  30542. word16 port;
  30543. char msg[] = "I hear you fa shizzle!";
  30544. int len = (int) XSTRLEN(msg);
  30545. char input[1024];
  30546. int idx;
  30547. int ret, err = 0;
  30548. #ifdef WOLFSSL_TIRTOS
  30549. fdOpenSession(Task_self());
  30550. #endif
  30551. ((func_args*)args)->return_code = TEST_FAIL;
  30552. port = ((func_args*)args)->signal->port;
  30553. AssertIntEQ(WOLFSSL_SUCCESS,
  30554. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  30555. AssertIntEQ(WOLFSSL_SUCCESS,
  30556. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  30557. WOLFSSL_FILETYPE_PEM));
  30558. AssertIntEQ(WOLFSSL_SUCCESS,
  30559. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  30560. WOLFSSL_FILETYPE_PEM));
  30561. if (callbacks->ctx_ready)
  30562. callbacks->ctx_ready(ctx);
  30563. ssl = wolfSSL_new(ctx);
  30564. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  30565. CloseSocket(sfd);
  30566. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  30567. if (callbacks->ssl_ready)
  30568. callbacks->ssl_ready(ssl);
  30569. do {
  30570. err = 0; /* Reset error */
  30571. ret = wolfSSL_accept(ssl);
  30572. if (ret != WOLFSSL_SUCCESS) {
  30573. err = wolfSSL_get_error(ssl, 0);
  30574. }
  30575. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  30576. if (ret != WOLFSSL_SUCCESS) {
  30577. char buff[WOLFSSL_MAX_ERROR_SZ];
  30578. printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  30579. }
  30580. else {
  30581. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  30582. input[idx] = 0;
  30583. printf("Client message: %s\n", input);
  30584. }
  30585. AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
  30586. #ifdef WOLFSSL_TIRTOS
  30587. Task_yield();
  30588. #endif
  30589. ((func_args*)args)->return_code = TEST_SUCCESS;
  30590. }
  30591. if (callbacks->on_result)
  30592. callbacks->on_result(ssl);
  30593. wolfSSL_shutdown(ssl);
  30594. wolfSSL_free(ssl);
  30595. wolfSSL_CTX_free(ctx);
  30596. CloseSocket(cfd);
  30597. #ifdef WOLFSSL_TIRTOS
  30598. fdCloseSession(Task_self());
  30599. #endif
  30600. #ifndef WOLFSSL_TIRTOS
  30601. return 0;
  30602. #endif
  30603. }
  30604. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  30605. {
  30606. AssertNotNull(ssl);
  30607. AssertNotNull(line);
  30608. XFILE fp;
  30609. const byte lf = '\n';
  30610. fp = XFOPEN("./MyKeyLog.txt", "a");
  30611. XFWRITE( line, 1, strlen(line),fp);
  30612. XFWRITE( (void*)&lf,1,1,fp);
  30613. XFCLOSE(fp);
  30614. }
  30615. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30616. static int test_wolfSSL_CTX_set_keylog_callback(void)
  30617. {
  30618. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30619. !defined(NO_WOLFSSL_CLIENT)
  30620. SSL_CTX* ctx;
  30621. printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
  30622. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30623. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30624. SSL_CTX_free(ctx);
  30625. SSL_CTX_set_keylog_callback(NULL, NULL);
  30626. printf(resultFmt, passed);
  30627. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30628. return 0;
  30629. }
  30630. static int test_wolfSSL_CTX_get_keylog_callback(void)
  30631. {
  30632. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  30633. !defined(NO_WOLFSSL_CLIENT)
  30634. SSL_CTX* ctx;
  30635. printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
  30636. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30637. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30638. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  30639. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  30640. SSL_CTX_set_keylog_callback(ctx, NULL );
  30641. AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  30642. SSL_CTX_free(ctx);
  30643. printf(resultFmt, passed);
  30644. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  30645. return 0;
  30646. }
  30647. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  30648. {
  30649. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  30650. /* This test is intended for checking whether keylog callback is called
  30651. * in client during TLS handshake between the client and a server.
  30652. */
  30653. tcp_ready ready;
  30654. func_args client_args;
  30655. func_args server_args;
  30656. THREAD_TYPE serverThread;
  30657. callback_functions server_cbf;
  30658. callback_functions client_cbf;
  30659. SOCKET_T sockfd = 0;
  30660. WOLFSSL_CTX* ctx;
  30661. WOLFSSL* ssl;
  30662. XFILE fp;
  30663. char msg[64] = "hello wolfssl!";
  30664. char reply[1024];
  30665. int msgSz = (int)XSTRLEN(msg);
  30666. printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
  30667. #ifdef WOLFSSL_TIRTOS
  30668. fdOpenSession(Task_self());
  30669. #endif
  30670. InitTcpReady(&ready);
  30671. ready.port = 22222;
  30672. XMEMSET(&client_args, 0, sizeof(func_args));
  30673. XMEMSET(&server_args, 0, sizeof(func_args));
  30674. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30675. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30676. server_cbf.method = wolfTLSv1_2_server_method;
  30677. server_args.callbacks = &server_cbf;
  30678. server_args.signal = &ready;
  30679. /* clean up keylog file */
  30680. fp = XFOPEN("./MyKeyLog.txt", "w");
  30681. XFCLOSE(fp);
  30682. /* start server task */
  30683. start_thread(server_task, &server_args, &serverThread);
  30684. wait_tcp_ready(&server_args);
  30685. /* run as a TLS1.2 client */
  30686. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  30687. AssertIntEQ(WOLFSSL_SUCCESS,
  30688. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30689. AssertIntEQ(WOLFSSL_SUCCESS,
  30690. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30691. AssertIntEQ(WOLFSSL_SUCCESS,
  30692. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30693. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30694. /* set keylog callback */
  30695. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30696. /* get connected the server task */
  30697. AssertNotNull(ssl = wolfSSL_new(ctx));
  30698. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30699. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30700. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30701. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30702. wolfSSL_shutdown(ssl);
  30703. wolfSSL_free(ssl);
  30704. wolfSSL_CTX_free(ctx);
  30705. CloseSocket(sockfd);
  30706. join_thread(serverThread);
  30707. FreeTcpReady(&ready);
  30708. #ifdef WOLFSSL_TIRTOS
  30709. fdOpenSession(Task_self());
  30710. #endif
  30711. /* check if the keylog file exists */
  30712. char buff[300] = {0};
  30713. int found = 0;
  30714. fp = XFOPEN("./MyKeyLog.txt", "r");
  30715. AssertNotNull(fp);
  30716. while(XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30717. if(0 == strncmp(buff,"CLIENT_RANDOM ",
  30718. sizeof("CLIENT_RANDOM ")-1)) {
  30719. found = 1;
  30720. break;
  30721. }
  30722. }
  30723. XFCLOSE(fp);
  30724. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  30725. AssertNotNull( found );
  30726. printf(resultFmt, passed);
  30727. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  30728. return 0;
  30729. }
  30730. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  30731. {
  30732. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  30733. defined(HAVE_SECRET_CALLBACK)
  30734. /* This test is intended for checking whether keylog callback is called
  30735. * in client during TLS handshake between the client and a server.
  30736. */
  30737. tcp_ready ready;
  30738. func_args client_args;
  30739. func_args server_args;
  30740. THREAD_TYPE serverThread;
  30741. callback_functions server_cbf;
  30742. callback_functions client_cbf;
  30743. SOCKET_T sockfd = 0;
  30744. WOLFSSL_CTX* ctx;
  30745. WOLFSSL* ssl;
  30746. XFILE fp;
  30747. char msg[64] = "hello wolfssl!";
  30748. char reply[1024];
  30749. int msgSz = (int)XSTRLEN(msg);
  30750. printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
  30751. #ifdef WOLFSSL_TIRTOS
  30752. fdOpenSession(Task_self());
  30753. #endif
  30754. InitTcpReady(&ready);
  30755. ready.port = 22222;
  30756. XMEMSET(&client_args, 0, sizeof(func_args));
  30757. XMEMSET(&server_args, 0, sizeof(func_args));
  30758. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30759. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30760. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  30761. server_args.callbacks = &server_cbf;
  30762. server_args.signal = &ready;
  30763. /* clean up keylog file */
  30764. fp = XFOPEN("./MyKeyLog.txt", "w");
  30765. XFCLOSE(fp);
  30766. /* start server task */
  30767. start_thread(server_task, &server_args, &serverThread);
  30768. wait_tcp_ready(&server_args);
  30769. /* run as a TLS1.3 client */
  30770. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  30771. AssertIntEQ(WOLFSSL_SUCCESS,
  30772. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  30773. AssertIntEQ(WOLFSSL_SUCCESS,
  30774. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  30775. AssertIntEQ(WOLFSSL_SUCCESS,
  30776. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30777. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  30778. /* set keylog callback */
  30779. wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
  30780. /* get connected the server task */
  30781. AssertNotNull(ssl = wolfSSL_new(ctx));
  30782. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  30783. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  30784. AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  30785. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  30786. wolfSSL_free(ssl);
  30787. wolfSSL_CTX_free(ctx);
  30788. join_thread(serverThread);
  30789. FreeTcpReady(&ready);
  30790. #ifdef WOLFSSL_TIRTOS
  30791. fdOpenSession(Task_self());
  30792. #endif
  30793. /* check if the keylog file exists */
  30794. {
  30795. char buff[300] = {0};
  30796. int found[4] = {0};
  30797. int numfnd = 0;
  30798. int i;
  30799. fp = XFOPEN("./MyKeyLog.txt", "r");
  30800. AssertNotNull(fp);
  30801. while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
  30802. if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  30803. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30804. found[0] = 1;
  30805. continue;
  30806. }
  30807. else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  30808. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  30809. found[1] = 1;
  30810. continue;
  30811. }
  30812. else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
  30813. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  30814. found[2] = 1;
  30815. continue;
  30816. }
  30817. else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
  30818. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  30819. found[3] = 1;
  30820. continue;
  30821. }
  30822. }
  30823. XFCLOSE(fp);
  30824. for (i = 0; i < 4; i++) {
  30825. if( found[i] != 0)
  30826. numfnd++;
  30827. }
  30828. AssertIntEQ(numfnd, 4);
  30829. }
  30830. printf(resultFmt, passed);
  30831. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  30832. return 0;
  30833. }
  30834. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30835. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30836. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30837. static void post_auth_version_cb(WOLFSSL* ssl)
  30838. {
  30839. /* do handshake and then test version error */
  30840. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30841. AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  30842. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  30843. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  30844. /* check was added to error queue */
  30845. AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  30846. /* check the string matches expected string */
  30847. AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  30848. "WRONG_SSL_VERSION");
  30849. #endif
  30850. }
  30851. static void post_auth_cb(WOLFSSL* ssl)
  30852. {
  30853. WOLFSSL_X509* x509;
  30854. /* do handshake and then test version error */
  30855. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  30856. AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  30857. AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
  30858. wolfSSL_X509_free(x509);
  30859. AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  30860. }
  30861. static void set_post_auth_cb(WOLFSSL* ssl)
  30862. {
  30863. if (!wolfSSL_is_server(ssl)) {
  30864. AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  30865. }
  30866. else {
  30867. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  30868. }
  30869. }
  30870. #endif
  30871. static int test_wolfSSL_Tls13_postauth(void)
  30872. {
  30873. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  30874. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30875. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  30876. tcp_ready ready;
  30877. func_args client_args;
  30878. func_args server_args;
  30879. callback_functions server_cbf;
  30880. callback_functions client_cbf;
  30881. THREAD_TYPE serverThread;
  30882. printf(testingFmt, "wolfSSL_Tls13_postauth()");
  30883. XMEMSET(&client_args, 0, sizeof(func_args));
  30884. XMEMSET(&server_args, 0, sizeof(func_args));
  30885. StartTCP();
  30886. InitTcpReady(&ready);
  30887. #if defined(USE_WINDOWS_API)
  30888. /* use RNG to get random port if using windows */
  30889. ready.port = GetRandomPort();
  30890. #endif
  30891. server_args.signal = &ready;
  30892. client_args.signal = &ready;
  30893. /* test version failure doing post auth with TLS 1.2 connection */
  30894. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30895. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30896. server_cbf.method = wolfTLSv1_2_server_method;
  30897. server_cbf.ssl_ready = set_post_auth_cb;
  30898. client_cbf.ssl_ready = set_post_auth_cb;
  30899. server_cbf.on_result = post_auth_version_cb;
  30900. server_args.callbacks = &server_cbf;
  30901. client_args.callbacks = &client_cbf;
  30902. start_thread(test_server_nofail, &server_args, &serverThread);
  30903. wait_tcp_ready(&server_args);
  30904. test_client_nofail(&client_args, NULL);
  30905. join_thread(serverThread);
  30906. /* tests on post auth with TLS 1.3 */
  30907. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  30908. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  30909. server_cbf.method = wolfTLSv1_3_server_method;
  30910. server_cbf.ssl_ready = set_post_auth_cb;
  30911. client_cbf.ssl_ready = set_post_auth_cb;
  30912. server_cbf.on_result = post_auth_cb;
  30913. server_args.callbacks = &server_cbf;
  30914. client_args.callbacks = &client_cbf;
  30915. start_thread(test_server_nofail, &server_args, &serverThread);
  30916. wait_tcp_ready(&server_args);
  30917. test_client_nofail(&client_args, NULL);
  30918. join_thread(serverThread);
  30919. FreeTcpReady(&ready);
  30920. printf(resultFmt, passed);
  30921. #endif
  30922. return 0;
  30923. }
  30924. static int test_wolfSSL_X509_NID(void)
  30925. {
  30926. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  30927. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  30928. int sigType;
  30929. int nameSz;
  30930. X509* cert;
  30931. EVP_PKEY* pubKeyTmp;
  30932. X509_NAME* name;
  30933. char commonName[80];
  30934. char countryName[80];
  30935. char localityName[80];
  30936. char stateName[80];
  30937. char orgName[80];
  30938. char orgUnit[80];
  30939. printf(testingFmt, "wolfSSL_X509_NID()");
  30940. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  30941. /* convert cert from DER to internal WOLFSSL_X509 struct */
  30942. AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
  30943. sizeof_client_cert_der_2048));
  30944. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  30945. /* extract PUBLIC KEY from cert */
  30946. AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
  30947. /* extract signatureType */
  30948. AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  30949. /* extract subjectName info */
  30950. AssertNotNull(name = X509_get_subject_name(cert));
  30951. AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  30952. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30953. NULL, 0)), 0);
  30954. AssertIntEQ(nameSz, 15);
  30955. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30956. commonName, sizeof(commonName))), 0);
  30957. AssertIntEQ(nameSz, 15);
  30958. AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  30959. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  30960. commonName, 9)), 0);
  30961. AssertIntEQ(nameSz, 8);
  30962. AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  30963. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  30964. countryName, sizeof(countryName))), 0);
  30965. AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  30966. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  30967. localityName, sizeof(localityName))), 0);
  30968. AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  30969. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
  30970. stateName, sizeof(stateName))), 0);
  30971. AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  30972. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  30973. orgName, sizeof(orgName))), 0);
  30974. AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  30975. AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
  30976. orgUnit, sizeof(orgUnit))), 0);
  30977. AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  30978. EVP_PKEY_free(pubKeyTmp);
  30979. X509_free(cert);
  30980. printf(resultFmt, passed);
  30981. #endif
  30982. return 0;
  30983. }
  30984. static int test_wolfSSL_CTX_set_srp_username(void)
  30985. {
  30986. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  30987. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  30988. WOLFSSL_CTX* ctx;
  30989. WOLFSSL* ssl;
  30990. const char *username = "TESTUSER";
  30991. const char *password = "TESTPASSWORD";
  30992. int r;
  30993. printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
  30994. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  30995. AssertNotNull(ctx);
  30996. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  30997. AssertIntEQ(r,SSL_SUCCESS);
  30998. wolfSSL_CTX_free(ctx);
  30999. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31000. AssertNotNull(ctx);
  31001. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31002. AssertIntEQ(r,SSL_SUCCESS);
  31003. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31004. AssertIntEQ(r,SSL_SUCCESS);
  31005. AssertNotNull(ssl = SSL_new(ctx));
  31006. AssertNotNull(SSL_get_srp_username(ssl));
  31007. AssertStrEQ(SSL_get_srp_username(ssl), username);
  31008. wolfSSL_free(ssl);
  31009. wolfSSL_CTX_free(ctx);
  31010. printf(resultFmt, passed);
  31011. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31012. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31013. return 0;
  31014. }
  31015. static int test_wolfSSL_CTX_set_srp_password(void)
  31016. {
  31017. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  31018. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  31019. WOLFSSL_CTX* ctx;
  31020. const char *username = "TESTUSER";
  31021. const char *password = "TESTPASSWORD";
  31022. int r;
  31023. printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
  31024. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31025. AssertNotNull(ctx);
  31026. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31027. AssertIntEQ(r,SSL_SUCCESS);
  31028. wolfSSL_CTX_free(ctx);
  31029. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31030. AssertNotNull(ctx);
  31031. r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
  31032. AssertIntEQ(r,SSL_SUCCESS);
  31033. r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
  31034. AssertIntEQ(r,SSL_SUCCESS);
  31035. wolfSSL_CTX_free(ctx);
  31036. printf(resultFmt, passed);
  31037. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  31038. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  31039. return 0;
  31040. }
  31041. static int test_wolfSSL_X509_STORE(void)
  31042. {
  31043. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  31044. X509_STORE *store;
  31045. #ifdef HAVE_CRL
  31046. X509_STORE_CTX *storeCtx;
  31047. X509_CRL *crl;
  31048. X509 *ca, *cert;
  31049. const char crlPem[] = "./certs/crl/crl.revoked";
  31050. const char srvCert[] = "./certs/server-revoked-cert.pem";
  31051. const char caCert[] = "./certs/ca-cert.pem";
  31052. XFILE fp;
  31053. printf(testingFmt, "test_wolfSSL_X509_STORE");
  31054. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31055. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31056. SSL_FILETYPE_PEM)));
  31057. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31058. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31059. SSL_FILETYPE_PEM)));
  31060. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31061. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31062. AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31063. X509_STORE_free(store);
  31064. X509_STORE_CTX_free(storeCtx);
  31065. X509_free(cert);
  31066. X509_free(ca);
  31067. /* should fail to verify now after adding in CRL */
  31068. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31069. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  31070. SSL_FILETYPE_PEM)));
  31071. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  31072. fp = XFOPEN(crlPem, "rb");
  31073. AssertTrue((fp != XBADFILE));
  31074. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  31075. NULL, NULL));
  31076. XFCLOSE(fp);
  31077. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  31078. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  31079. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  31080. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  31081. SSL_FILETYPE_PEM)));
  31082. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  31083. AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  31084. AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
  31085. X509_CRL_free(crl);
  31086. X509_STORE_free(store);
  31087. X509_STORE_CTX_free(storeCtx);
  31088. X509_free(cert);
  31089. X509_free(ca);
  31090. #endif /* HAVE_CRL */
  31091. #ifndef WOLFCRYPT_ONLY
  31092. {
  31093. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31094. SSL_CTX* ctx;
  31095. SSL* ssl;
  31096. int i;
  31097. for (i = 0; i < 2; i++) {
  31098. #ifndef NO_WOLFSSL_SERVER
  31099. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31100. #else
  31101. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31102. #endif
  31103. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31104. SSL_CTX_set_cert_store(ctx, store);
  31105. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31106. SSL_CTX_set_cert_store(ctx, store);
  31107. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  31108. AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31109. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31110. AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31111. SSL_FILETYPE_PEM), SSL_SUCCESS);
  31112. AssertNotNull(ssl = SSL_new(ctx));
  31113. if (i == 0) {
  31114. AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
  31115. }
  31116. else {
  31117. AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  31118. X509_STORE_free(store);
  31119. }
  31120. SSL_free(ssl);
  31121. SSL_CTX_free(ctx);
  31122. }
  31123. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31124. }
  31125. #endif
  31126. printf(resultFmt, passed);
  31127. #endif
  31128. return 0;
  31129. }
  31130. static int test_wolfSSL_X509_STORE_load_locations(void)
  31131. {
  31132. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  31133. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31134. SSL_CTX *ctx;
  31135. X509_STORE *store;
  31136. const char ca_file[] = "./certs/ca-cert.pem";
  31137. const char client_pem_file[] = "./certs/client-cert.pem";
  31138. const char client_der_file[] = "./certs/client-cert.der";
  31139. const char ecc_file[] = "./certs/ecc-key.pem";
  31140. const char certs_path[] = "./certs/";
  31141. const char bad_path[] = "./bad-path/";
  31142. #ifdef HAVE_CRL
  31143. const char crl_path[] = "./certs/crl/";
  31144. const char crl_file[] = "./certs/crl/crl.pem";
  31145. #endif
  31146. printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
  31147. #ifndef NO_WOLFSSL_SERVER
  31148. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31149. #else
  31150. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31151. #endif
  31152. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31153. AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
  31154. /* Test bad arguments */
  31155. AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
  31156. AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  31157. AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
  31158. AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
  31159. AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
  31160. #ifdef HAVE_CRL
  31161. /* Test with CRL */
  31162. AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
  31163. AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
  31164. #endif
  31165. /* Test with CA */
  31166. AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
  31167. /* Test with client_cert and certs path */
  31168. AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
  31169. AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
  31170. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  31171. /* Clear nodes */
  31172. ERR_clear_error();
  31173. #endif
  31174. SSL_CTX_free(ctx);
  31175. printf(resultFmt, passed);
  31176. #endif
  31177. return 0;
  31178. }
  31179. static int test_X509_STORE_get0_objects(void)
  31180. {
  31181. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  31182. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  31183. X509_STORE *store;
  31184. X509_STORE *store_cpy;
  31185. SSL_CTX *ctx;
  31186. X509_OBJECT *obj;
  31187. STACK_OF(X509_OBJECT) *objs;
  31188. int i;
  31189. printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
  31190. /* Setup store */
  31191. #ifndef NO_WOLFSSL_SERVER
  31192. AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  31193. #else
  31194. AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  31195. #endif
  31196. AssertNotNull(store_cpy = X509_STORE_new());
  31197. AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
  31198. AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
  31199. AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
  31200. AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
  31201. #ifdef HAVE_CRL
  31202. AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
  31203. #endif
  31204. /* Store ready */
  31205. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  31206. AssertNotNull(objs = X509_STORE_get0_objects(store));
  31207. #ifdef HAVE_CRL
  31208. #ifdef WOLFSSL_SIGNER_DER_CERT
  31209. AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
  31210. #else
  31211. AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
  31212. #endif
  31213. #else
  31214. #ifdef WOLFSSL_SIGNER_DER_CERT
  31215. AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
  31216. #else
  31217. AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
  31218. #endif
  31219. #endif
  31220. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  31221. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  31222. switch (X509_OBJECT_get_type(obj)) {
  31223. case X509_LU_X509:
  31224. AssertNotNull(X509_OBJECT_get0_X509(obj));
  31225. AssertIntEQ(X509_STORE_add_cert(store_cpy,
  31226. X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
  31227. break;
  31228. case X509_LU_CRL:
  31229. #ifdef HAVE_CRL
  31230. AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
  31231. AssertIntEQ(X509_STORE_add_crl(store_cpy,
  31232. X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
  31233. break;
  31234. #endif
  31235. case X509_LU_NONE:
  31236. default:
  31237. Fail(("X509_OBJECT_get_type should return x509 or crl "
  31238. "(when built with crl support)"),
  31239. ("Unrecognized X509_OBJECT type or none"));
  31240. }
  31241. }
  31242. X509_STORE_free(store_cpy);
  31243. SSL_CTX_free(ctx);
  31244. printf(resultFmt, passed);
  31245. #endif
  31246. return 0;
  31247. }
  31248. static int test_wolfSSL_BN(void)
  31249. {
  31250. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  31251. BIGNUM* a;
  31252. BIGNUM* b;
  31253. BIGNUM* c;
  31254. BIGNUM* d;
  31255. ASN1_INTEGER* ai;
  31256. printf(testingFmt, "wolfSSL_BN()");
  31257. AssertNotNull(b = BN_new());
  31258. AssertNotNull(c = BN_new());
  31259. AssertNotNull(d = BN_new());
  31260. ai = ASN1_INTEGER_new();
  31261. AssertNotNull(ai);
  31262. /* at the moment hard setting since no set function */
  31263. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  31264. ai->data[1] = 0x01; /* length of integer */
  31265. ai->data[2] = 0x03;
  31266. AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
  31267. ASN1_INTEGER_free(ai);
  31268. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31269. AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  31270. /* a + 3 = */
  31271. AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
  31272. AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
  31273. /* check result 3 + 3*/
  31274. AssertIntEQ(BN_get_word(a), 6);
  31275. /* set a back to 3 */
  31276. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31277. /* a - 3 = */
  31278. AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
  31279. AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
  31280. /* check result 3 - 3*/
  31281. AssertIntEQ(BN_get_word(a), 0);
  31282. /* set a back to 3 */
  31283. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31284. /* a^b mod c = */
  31285. AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
  31286. AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
  31287. /* check result 3^2 mod 5 */
  31288. AssertIntEQ(BN_get_word(d), 4);
  31289. /* a*b = */
  31290. AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
  31291. AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
  31292. /* check result 3*2 */
  31293. AssertIntEQ(BN_get_word(d), 6);
  31294. /* c/b => db + a */
  31295. AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
  31296. AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
  31297. /* check result 5/2 */
  31298. AssertIntEQ(BN_get_word(d), 2); /* check quotient */
  31299. AssertIntEQ(BN_get_word(a), 1); /* check remainder */
  31300. /* set a back to 3 */
  31301. AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  31302. /* a*b mod c = */
  31303. AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
  31304. AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
  31305. /* check result 3*2 mod 5 */
  31306. AssertIntEQ(BN_get_word(d), 1);
  31307. AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
  31308. AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
  31309. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  31310. /* gcd of a and b */
  31311. AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
  31312. AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
  31313. /* check result gcd(16, 24) */
  31314. AssertIntEQ(BN_get_word(d), 8);
  31315. #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
  31316. AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
  31317. AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
  31318. AssertIntEQ(BN_is_zero(b), 0);
  31319. AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
  31320. AssertIntEQ(BN_is_zero(b), 1);
  31321. AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
  31322. AssertIntEQ(BN_is_zero(b), 0);
  31323. /* set b back to 2 */
  31324. AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  31325. {
  31326. /* BN_mod_inverse test */
  31327. BIGNUM *r = BN_new();
  31328. BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
  31329. AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
  31330. BN_free(val);
  31331. }
  31332. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  31333. defined(WOLFSSL_SP_INT_NEGATIVE))
  31334. AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  31335. AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  31336. AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  31337. AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  31338. AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  31339. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  31340. {
  31341. char* ret;
  31342. AssertNotNull(ret = BN_bn2dec(c));
  31343. AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  31344. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  31345. }
  31346. #endif
  31347. AssertIntEQ(BN_get_word(c), 4);
  31348. #endif
  31349. BN_free(a);
  31350. BN_free(b);
  31351. BN_free(c);
  31352. BN_clear_free(d);
  31353. /* check that converting NULL and the null string returns an error */
  31354. a = NULL;
  31355. AssertIntLE(BN_hex2bn(&a, NULL), 0);
  31356. AssertIntLE(BN_hex2bn(&a, ""), 0);
  31357. AssertNull(a);
  31358. /* check that getting a string and a bin of the same number are equal,
  31359. * and that the comparison works EQ, LT and GT */
  31360. AssertIntGT(BN_hex2bn(&a, "03"), 0);
  31361. AssertNotNull(b = BN_new());
  31362. AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
  31363. AssertNotNull(c = BN_new());
  31364. AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
  31365. AssertIntEQ(BN_cmp(a, b), 0);
  31366. AssertIntLT(BN_cmp(a, c), 0);
  31367. AssertIntGT(BN_cmp(c, b), 0);
  31368. AssertIntEQ(BN_set_word(a, 0), 1);
  31369. AssertIntEQ(BN_is_zero(a), 1);
  31370. AssertIntEQ(BN_set_bit(a, 0x45), 1);
  31371. AssertIntEQ(BN_is_zero(a), 0);
  31372. AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
  31373. AssertIntEQ(BN_clear_bit(a, 0x45), 1);
  31374. AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
  31375. AssertIntEQ(BN_is_zero(a), 1);
  31376. BN_free(a);
  31377. BN_free(b);
  31378. BN_free(c);
  31379. #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
  31380. {
  31381. BIGNUM *ap;
  31382. BIGNUM bv;
  31383. BIGNUM cv;
  31384. BIGNUM dv;
  31385. AssertNotNull(ap = BN_new());
  31386. BN_init(&bv);
  31387. BN_init(&cv);
  31388. BN_init(&dv);
  31389. AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  31390. AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  31391. AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  31392. /* a^b mod c = */
  31393. AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  31394. AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  31395. /* check result 3^2 mod 5 */
  31396. AssertIntEQ(BN_get_word(&dv), 4);
  31397. /* a*b mod c = */
  31398. AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  31399. AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  31400. /* check result 3*2 mod 5 */
  31401. AssertIntEQ(BN_get_word(&dv), 1);
  31402. BN_free(ap);
  31403. }
  31404. #endif
  31405. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  31406. AssertNotNull(a = BN_new());
  31407. AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
  31408. SSL_SUCCESS);
  31409. AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
  31410. BN_free(a);
  31411. #endif
  31412. printf(resultFmt, passed);
  31413. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  31414. return 0;
  31415. }
  31416. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31417. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31418. #define TEST_ARG 0x1234
  31419. static void msg_cb(int write_p, int version, int content_type,
  31420. const void *buf, size_t len, SSL *ssl, void *arg)
  31421. {
  31422. (void)write_p;
  31423. (void)version;
  31424. (void)content_type;
  31425. (void)buf;
  31426. (void)len;
  31427. (void)ssl;
  31428. AssertTrue(arg == (void*)TEST_ARG);
  31429. }
  31430. #endif
  31431. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31432. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31433. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31434. !defined(NO_WOLFSSL_SERVER)
  31435. #ifndef SINGLE_THREADED
  31436. #if defined(SESSION_CERTS)
  31437. #include "wolfssl/internal.h"
  31438. #endif
  31439. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  31440. {
  31441. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31442. STACK_OF(X509)* sk;
  31443. X509* x509;
  31444. int i, num;
  31445. BIO* bio;
  31446. #endif
  31447. (void) ctx;
  31448. printf("\n===== msgcb called ====\n");
  31449. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  31450. AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  31451. AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  31452. AssertNotNull(SSL_get0_verified_chain(ssl));
  31453. #else
  31454. (void) ssl;
  31455. #endif
  31456. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  31457. bio = BIO_new(BIO_s_file());
  31458. BIO_set_fp(bio, stdout, BIO_NOCLOSE);
  31459. sk = SSL_get_peer_cert_chain(ssl);
  31460. AssertNotNull(sk);
  31461. if (!sk) {
  31462. BIO_free(bio);
  31463. return SSL_FAILURE;
  31464. }
  31465. num = sk_X509_num(sk);
  31466. AssertTrue(num > 0);
  31467. for (i = 0; i < num; i++) {
  31468. x509 = sk_X509_value(sk,i);
  31469. AssertNotNull(x509);
  31470. if (!x509)
  31471. break;
  31472. printf("Certificate at index [%d] = :\n",i);
  31473. X509_print(bio,x509);
  31474. printf("\n\n");
  31475. }
  31476. BIO_free(bio);
  31477. #endif
  31478. return SSL_SUCCESS;
  31479. }
  31480. #endif
  31481. #endif
  31482. static int test_wolfSSL_msgCb(void)
  31483. {
  31484. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31485. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  31486. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
  31487. !defined(NO_WOLFSSL_SERVER)
  31488. tcp_ready ready;
  31489. func_args client_args;
  31490. func_args server_args;
  31491. #ifndef SINGLE_THREADED
  31492. THREAD_TYPE serverThread;
  31493. #endif
  31494. callback_functions client_cb;
  31495. callback_functions server_cb;
  31496. printf(testingFmt, "test_wolfSSL_msgCb");
  31497. /* create a failed connection and inspect the error */
  31498. #ifdef WOLFSSL_TIRTOS
  31499. fdOpenSession(Task_self());
  31500. #endif
  31501. XMEMSET(&client_args, 0, sizeof(func_args));
  31502. XMEMSET(&server_args, 0, sizeof(func_args));
  31503. StartTCP();
  31504. InitTcpReady(&ready);
  31505. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31506. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31507. #ifndef WOLFSSL_NO_TLS12
  31508. client_cb.method = wolfTLSv1_2_client_method;
  31509. server_cb.method = wolfTLSv1_2_server_method;
  31510. #else
  31511. client_cb.method = wolfTLSv1_3_client_method;
  31512. server_cb.method = wolfTLSv1_3_server_method;
  31513. #endif
  31514. server_args.signal = &ready;
  31515. server_args.callbacks = &server_cb;
  31516. client_args.signal = &ready;
  31517. client_args.callbacks = &client_cb;
  31518. client_args.return_code = TEST_FAIL;
  31519. #ifndef SINGLE_THREADED
  31520. start_thread(test_server_nofail, &server_args, &serverThread);
  31521. wait_tcp_ready(&server_args);
  31522. test_client_nofail(&client_args, msgCb);
  31523. join_thread(serverThread);
  31524. #endif
  31525. FreeTcpReady(&ready);
  31526. #ifndef SINGLE_THREADED
  31527. AssertTrue(client_args.return_code);
  31528. AssertTrue(server_args.return_code);
  31529. #endif
  31530. #ifdef WOLFSSL_TIRTOS
  31531. fdOpenSession(Task_self());
  31532. #endif
  31533. printf(resultFmt, passed);
  31534. #endif
  31535. return 0;
  31536. }
  31537. static int test_wolfSSL_either_side(void)
  31538. {
  31539. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31540. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31541. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  31542. tcp_ready ready;
  31543. func_args client_args;
  31544. func_args server_args;
  31545. #ifndef SINGLE_THREADED
  31546. THREAD_TYPE serverThread;
  31547. #endif
  31548. callback_functions client_cb;
  31549. callback_functions server_cb;
  31550. printf(testingFmt, "test_wolfSSL_either_side");
  31551. /* create a failed connection and inspect the error */
  31552. #ifdef WOLFSSL_TIRTOS
  31553. fdOpenSession(Task_self());
  31554. #endif
  31555. XMEMSET(&client_args, 0, sizeof(func_args));
  31556. XMEMSET(&server_args, 0, sizeof(func_args));
  31557. StartTCP();
  31558. InitTcpReady(&ready);
  31559. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31560. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31561. /* Use different CTX for client and server */
  31562. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31563. AssertNotNull(client_cb.ctx);
  31564. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  31565. AssertNotNull(server_cb.ctx);
  31566. /* we are responsible for free'ing WOLFSSL_CTX */
  31567. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31568. server_args.signal = &ready;
  31569. server_args.callbacks = &server_cb;
  31570. client_args.signal = &ready;
  31571. client_args.callbacks = &client_cb;
  31572. client_args.return_code = TEST_FAIL;
  31573. #ifndef SINGLE_THREADED
  31574. start_thread(test_server_nofail, &server_args, &serverThread);
  31575. wait_tcp_ready(&server_args);
  31576. test_client_nofail(&client_args, NULL);
  31577. join_thread(serverThread);
  31578. #endif
  31579. wolfSSL_CTX_free(client_cb.ctx);
  31580. wolfSSL_CTX_free(server_cb.ctx);
  31581. FreeTcpReady(&ready);
  31582. #ifndef SINGLE_THREADED
  31583. AssertTrue(client_args.return_code);
  31584. AssertTrue(server_args.return_code);
  31585. #endif
  31586. #ifdef WOLFSSL_TIRTOS
  31587. fdOpenSession(Task_self());
  31588. #endif
  31589. printf(resultFmt, passed);
  31590. #endif
  31591. return 0;
  31592. }
  31593. static int test_wolfSSL_DTLS_either_side(void)
  31594. {
  31595. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  31596. !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  31597. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  31598. defined(WOLFSSL_DTLS)
  31599. tcp_ready ready;
  31600. func_args client_args;
  31601. func_args server_args;
  31602. #ifndef SINGLE_THREADED
  31603. THREAD_TYPE serverThread;
  31604. #endif
  31605. callback_functions client_cb;
  31606. callback_functions server_cb;
  31607. printf(testingFmt, "test_wolfSSL_DTLS_either_side");
  31608. /* create a failed connection and inspect the error */
  31609. #ifdef WOLFSSL_TIRTOS
  31610. fdOpenSession(Task_self());
  31611. #endif
  31612. XMEMSET(&client_args, 0, sizeof(func_args));
  31613. XMEMSET(&server_args, 0, sizeof(func_args));
  31614. StartTCP();
  31615. InitTcpReady(&ready);
  31616. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  31617. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  31618. /* Use different CTX for client and server */
  31619. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31620. AssertNotNull(client_cb.ctx);
  31621. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  31622. AssertNotNull(server_cb.ctx);
  31623. /* we are responsible for free'ing WOLFSSL_CTX */
  31624. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  31625. server_args.signal = &ready;
  31626. server_args.callbacks = &server_cb;
  31627. client_args.signal = &ready;
  31628. client_args.callbacks = &client_cb;
  31629. client_args.return_code = TEST_FAIL;
  31630. #ifndef SINGLE_THREADED
  31631. start_thread(test_server_nofail, &server_args, &serverThread);
  31632. wait_tcp_ready(&server_args);
  31633. test_client_nofail(&client_args, NULL);
  31634. join_thread(serverThread);
  31635. #endif
  31636. wolfSSL_CTX_free(client_cb.ctx);
  31637. wolfSSL_CTX_free(server_cb.ctx);
  31638. FreeTcpReady(&ready);
  31639. #ifndef SINGLE_THREADED
  31640. AssertTrue(client_args.return_code);
  31641. AssertTrue(server_args.return_code);
  31642. #endif
  31643. #ifdef WOLFSSL_TIRTOS
  31644. fdOpenSession(Task_self());
  31645. #endif
  31646. printf(resultFmt, passed);
  31647. #endif
  31648. return 0;
  31649. }
  31650. static int test_generate_cookie(void)
  31651. {
  31652. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  31653. SSL_CTX* ctx;
  31654. SSL* ssl;
  31655. byte buf[FOURK_BUF] = {0};
  31656. printf(testingFmt, "test_generate_cookie");
  31657. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  31658. AssertNotNull(ssl = SSL_new(ctx));
  31659. /* Test unconnected */
  31660. AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  31661. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  31662. wolfSSL_SetCookieCtx(ssl, ctx);
  31663. AssertNotNull(wolfSSL_GetCookieCtx(ssl));
  31664. AssertNull(wolfSSL_GetCookieCtx(NULL));
  31665. SSL_free(ssl);
  31666. SSL_CTX_free(ctx);
  31667. printf(resultFmt, passed);
  31668. #endif
  31669. return 0;
  31670. }
  31671. static int test_wolfSSL_set_options(void)
  31672. {
  31673. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31674. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31675. WOLFSSL* ssl;
  31676. WOLFSSL_CTX* ctx;
  31677. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31678. char appData[] = "extra msg";
  31679. #endif
  31680. #ifdef OPENSSL_EXTRA
  31681. unsigned char protos[] = {
  31682. 7, 't', 'l', 's', '/', '1', '.', '2',
  31683. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  31684. };
  31685. unsigned int len = sizeof(protos);
  31686. void *arg = (void *)TEST_ARG;
  31687. #endif
  31688. printf(testingFmt, "wolfSSL_set_options()");
  31689. #ifndef NO_WOLFSSL_SERVER
  31690. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  31691. #else
  31692. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  31693. #endif
  31694. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31695. WOLFSSL_FILETYPE_PEM));
  31696. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31697. WOLFSSL_FILETYPE_PEM));
  31698. AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  31699. == WOLFSSL_OP_NO_TLSv1);
  31700. AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  31701. AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31702. WOLFSSL_OP_NO_SSLv2)), 0);
  31703. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31704. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31705. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  31706. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31707. AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31708. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31709. AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  31710. WOLFSSL_OP_NO_COMPRESSION));
  31711. wolfSSL_CTX_free(ctx);
  31712. #ifndef NO_WOLFSSL_SERVER
  31713. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  31714. AssertNotNull(ctx);
  31715. #else
  31716. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  31717. AssertNotNull(ctx);
  31718. #endif
  31719. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  31720. WOLFSSL_FILETYPE_PEM));
  31721. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31722. WOLFSSL_FILETYPE_PEM));
  31723. #ifdef OPENSSL_EXTRA
  31724. AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  31725. #endif
  31726. AssertNotNull(ssl = wolfSSL_new(ctx));
  31727. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  31728. #ifdef HAVE_EX_DATA
  31729. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  31730. AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31731. if (ssl) {
  31732. AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  31733. appData, sizeof(appData)), 0);
  31734. }
  31735. #else
  31736. AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  31737. AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  31738. #endif
  31739. #endif
  31740. AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  31741. WOLFSSL_OP_NO_TLSv1);
  31742. AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  31743. AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  31744. WOLFSSL_OP_NO_SSLv2)), 0);
  31745. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  31746. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  31747. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  31748. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  31749. AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31750. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  31751. #ifdef OPENSSL_EXTRA
  31752. AssertNull((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  31753. WOLFSSL_OP_NO_COMPRESSION));
  31754. #endif
  31755. #ifdef OPENSSL_EXTRA
  31756. AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  31757. wolfSSL_set_msg_callback_arg(ssl, arg);
  31758. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31759. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  31760. #else
  31761. AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  31762. #endif
  31763. #endif
  31764. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  31765. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  31766. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  31767. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  31768. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31769. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  31770. #else
  31771. AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  31772. #endif
  31773. #endif /* HAVE_ALPN && !NO_BIO */
  31774. #endif
  31775. wolfSSL_free(ssl);
  31776. wolfSSL_CTX_free(ctx);
  31777. printf(resultFmt, passed);
  31778. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31779. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31780. return 0;
  31781. }
  31782. static int test_wolfSSL_sk_SSL_CIPHER(void)
  31783. {
  31784. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  31785. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31786. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31787. SSL* ssl;
  31788. SSL_CTX* ctx;
  31789. STACK_OF(SSL_CIPHER) *sk, *dupSk;
  31790. printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
  31791. #ifndef NO_WOLFSSL_SERVER
  31792. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31793. #else
  31794. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31795. #endif
  31796. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31797. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31798. AssertNotNull(ssl = SSL_new(ctx));
  31799. AssertNotNull(sk = SSL_get_ciphers(ssl));
  31800. AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  31801. AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
  31802. AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  31803. /* error case because connection has not been established yet */
  31804. AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  31805. sk_SSL_CIPHER_free(dupSk);
  31806. /* sk is pointer to internal struct that should be free'd in SSL_free */
  31807. SSL_free(ssl);
  31808. SSL_CTX_free(ctx);
  31809. printf(resultFmt, passed);
  31810. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31811. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31812. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  31813. return 0;
  31814. }
  31815. static int test_wolfSSL_set1_curves_list(void)
  31816. {
  31817. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31818. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31819. SSL* ssl = NULL;
  31820. SSL_CTX* ctx = NULL;
  31821. #ifndef NO_WOLFSSL_SERVER
  31822. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31823. #else
  31824. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31825. #endif
  31826. AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  31827. SSL_FILETYPE_PEM));
  31828. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  31829. AssertNotNull(ssl = SSL_new(ctx));
  31830. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  31831. #ifdef HAVE_ECC
  31832. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  31833. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  31834. #endif
  31835. #ifdef HAVE_CURVE25519
  31836. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  31837. #else
  31838. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  31839. #endif
  31840. #ifdef HAVE_CURVE448
  31841. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  31842. #else
  31843. AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  31844. #endif
  31845. AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  31846. #ifdef HAVE_ECC
  31847. AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  31848. AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  31849. #endif
  31850. #ifdef HAVE_CURVE25519
  31851. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  31852. #else
  31853. AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  31854. #endif
  31855. #ifdef HAVE_CURVE448
  31856. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  31857. #else
  31858. AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  31859. #endif
  31860. SSL_free(ssl);
  31861. SSL_CTX_free(ctx);
  31862. printf(resultFmt, passed);
  31863. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31864. #endif
  31865. return 0;
  31866. }
  31867. static int test_wolfSSL_set1_sigalgs_list(void)
  31868. {
  31869. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  31870. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31871. SSL* ssl;
  31872. SSL_CTX* ctx;
  31873. #ifndef NO_WOLFSSL_SERVER
  31874. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31875. #else
  31876. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31877. #endif
  31878. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31879. SSL_FILETYPE_PEM));
  31880. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31881. AssertNotNull(ssl = SSL_new(ctx));
  31882. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31883. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  31884. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  31885. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  31886. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  31887. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  31888. #ifndef NO_RSA
  31889. #ifndef NO_SHA256
  31890. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  31891. WOLFSSL_FAILURE);
  31892. AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  31893. WOLFSSL_FAILURE);
  31894. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  31895. WOLFSSL_SUCCESS);
  31896. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  31897. WOLFSSL_SUCCESS);
  31898. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  31899. WOLFSSL_FAILURE);
  31900. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  31901. WOLFSSL_FAILURE);
  31902. #ifdef WC_RSA_PSS
  31903. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  31904. WOLFSSL_SUCCESS);
  31905. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  31906. WOLFSSL_SUCCESS);
  31907. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  31908. WOLFSSL_SUCCESS);
  31909. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  31910. WOLFSSL_SUCCESS);
  31911. #endif
  31912. #ifdef WOLFSSL_SHA512
  31913. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31914. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  31915. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31916. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  31917. #elif defined(WOLFSSL_SHA384)
  31918. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31919. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  31920. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31921. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  31922. #endif
  31923. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  31924. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  31925. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  31926. WOLFSSL_FAILURE);
  31927. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  31928. WOLFSSL_FAILURE);
  31929. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  31930. WOLFSSL_FAILURE);
  31931. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  31932. WOLFSSL_FAILURE);
  31933. #endif
  31934. #endif
  31935. #ifdef HAVE_ECC
  31936. #ifndef NO_SHA256
  31937. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  31938. WOLFSSL_SUCCESS);
  31939. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
  31940. #ifdef WOLFSSL_SHA512
  31941. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31942. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  31943. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31944. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  31945. #elif defined(WOLFSSL_SHA384)
  31946. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  31947. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  31948. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  31949. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  31950. #endif
  31951. #endif
  31952. #endif
  31953. #ifdef HAVE_ED25519
  31954. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  31955. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  31956. #endif
  31957. #ifdef HAVE_ED448
  31958. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  31959. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  31960. #endif
  31961. #ifndef NO_DSA
  31962. #ifndef NO_SHA256
  31963. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  31964. WOLFSSL_SUCCESS);
  31965. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  31966. WOLFSSL_SUCCESS);
  31967. #endif
  31968. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  31969. defined(WOLFSSL_ALLOW_TLS_SHA1))
  31970. AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  31971. WOLFSSL_SUCCESS);
  31972. AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  31973. WOLFSSL_SUCCESS);
  31974. #endif
  31975. #endif
  31976. SSL_free(ssl);
  31977. SSL_CTX_free(ctx);
  31978. printf(resultFmt, passed);
  31979. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31980. #endif
  31981. return 0;
  31982. }
  31983. /* Testing wolfSSL_set_tlsext_status_type function.
  31984. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  31985. */
  31986. static int test_wolfSSL_set_tlsext_status_type(void){
  31987. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  31988. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  31989. SSL* ssl;
  31990. SSL_CTX* ctx;
  31991. printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
  31992. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31993. AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  31994. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  31995. AssertNotNull(ssl = SSL_new(ctx));
  31996. AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  31997. SSL_SUCCESS);
  31998. AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  31999. SSL_free(ssl);
  32000. SSL_CTX_free(ctx);
  32001. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  32002. return 0;
  32003. }
  32004. #ifndef NO_BIO
  32005. static int test_wolfSSL_PEM_read_bio(void)
  32006. {
  32007. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32008. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32009. byte buff[6000];
  32010. XFILE f;
  32011. int bytes;
  32012. X509* x509;
  32013. BIO* bio = NULL;
  32014. BUF_MEM* buf;
  32015. printf(testingFmt, "wolfSSL_PEM_read_bio()");
  32016. f = XFOPEN(cliCertFile, "rb");
  32017. AssertTrue((f != XBADFILE));
  32018. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  32019. XFCLOSE(f);
  32020. AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32021. AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  32022. AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  32023. AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32024. AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  32025. /* BIO should return the set EOF value */
  32026. AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  32027. AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  32028. AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  32029. AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  32030. BIO_free(bio);
  32031. BUF_MEM_free(buf);
  32032. X509_free(x509);
  32033. printf(resultFmt, passed);
  32034. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32035. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32036. return 0;
  32037. }
  32038. #if defined(OPENSSL_EXTRA)
  32039. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  32040. long argl, long ret)
  32041. {
  32042. (void)bio;
  32043. (void)cmd;
  32044. (void)argp;
  32045. (void)argi;
  32046. (void)argl;
  32047. return ret;
  32048. }
  32049. #endif
  32050. static int test_wolfSSL_BIO(void)
  32051. {
  32052. #if defined(OPENSSL_EXTRA)
  32053. const unsigned char* p;
  32054. byte buff[20];
  32055. BIO* bio1;
  32056. BIO* bio2;
  32057. BIO* bio3;
  32058. char* bufPt;
  32059. int i;
  32060. printf(testingFmt, "wolfSSL_BIO()");
  32061. for (i = 0; i < 20; i++) {
  32062. buff[i] = i;
  32063. }
  32064. /* test BIO_free with NULL */
  32065. AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  32066. /* Creating and testing type BIO_s_bio */
  32067. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32068. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  32069. AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
  32070. /* read/write before set up */
  32071. AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32072. AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  32073. AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
  32074. AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  32075. AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  32076. AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  32077. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  32078. XMEMCPY(bufPt, buff, 10);
  32079. AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  32080. /* write buffer full */
  32081. AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  32082. AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  32083. AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
  32084. /* write the other direction with pair */
  32085. AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  32086. XMEMCPY(bufPt, buff, 8);
  32087. AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  32088. /* try read */
  32089. AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
  32090. AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
  32091. /* try read using ctrl function */
  32092. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  32093. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  32094. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  32095. AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  32096. AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  32097. for (i = 0; i < 20; i++) {
  32098. AssertIntEQ((int)bufPt[i], i);
  32099. }
  32100. AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32101. AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  32102. for (i = 0; i < 8; i++) {
  32103. AssertIntEQ((int)bufPt[i], i);
  32104. }
  32105. AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
  32106. AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  32107. /* new pair */
  32108. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  32109. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  32110. AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  32111. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32112. AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
  32113. /* test wrap around... */
  32114. AssertIntEQ(BIO_reset(bio1), 0);
  32115. AssertIntEQ(BIO_reset(bio3), 0);
  32116. /* fill write buffer, read only small amount then write again */
  32117. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32118. XMEMCPY(bufPt, buff, 20);
  32119. AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  32120. for (i = 0; i < 4; i++) {
  32121. AssertIntEQ(bufPt[i], i);
  32122. }
  32123. /* try writing over read index */
  32124. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  32125. XMEMSET(bufPt, 0, 4);
  32126. AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
  32127. /* read and write 0 bytes */
  32128. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32129. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  32130. /* should read only to end of write buffer then need to read again */
  32131. AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  32132. for (i = 0; i < 16; i++) {
  32133. AssertIntEQ(bufPt[i], buff[4 + i]);
  32134. }
  32135. AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  32136. AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
  32137. for (i = 0; i < 4; i++) {
  32138. AssertIntEQ(bufPt[i], 0);
  32139. }
  32140. /* read index should not have advanced with nread0 */
  32141. AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  32142. for (i = 0; i < 4; i++) {
  32143. AssertIntEQ(bufPt[i], 0);
  32144. }
  32145. /* write and fill up buffer checking reset of index state */
  32146. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32147. XMEMCPY(bufPt, buff, 20);
  32148. /* test reset on data in bio1 write buffer */
  32149. AssertIntEQ(BIO_reset(bio1), 0);
  32150. AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
  32151. AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
  32152. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  32153. AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  32154. AssertNotNull(p);
  32155. XMEMCPY(bufPt, buff, 20);
  32156. AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  32157. for (i = 0; i < 6; i++) {
  32158. AssertIntEQ(bufPt[i], i);
  32159. }
  32160. /* test case of writing twice with offset read index */
  32161. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  32162. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  32163. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32164. AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  32165. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32166. AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  32167. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  32168. AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  32169. BIO_free(bio1);
  32170. BIO_free(bio3);
  32171. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  32172. {
  32173. BIO* bioA = NULL;
  32174. BIO* bioB = NULL;
  32175. AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  32176. AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  32177. BIO_free(bioA);
  32178. bioA = NULL;
  32179. BIO_free(bioB);
  32180. bioB = NULL;
  32181. }
  32182. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  32183. /* BIOs with file pointers */
  32184. #if !defined(NO_FILESYSTEM)
  32185. {
  32186. XFILE f1;
  32187. XFILE f2;
  32188. BIO* f_bio1;
  32189. BIO* f_bio2;
  32190. unsigned char cert[300];
  32191. char testFile[] = "tests/bio_write_test.txt";
  32192. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  32193. AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
  32194. AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
  32195. /* Failure due to wrong BIO type */
  32196. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32197. AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  32198. f1 = XFOPEN(svrCertFile, "rwb");
  32199. AssertTrue((f1 != XBADFILE));
  32200. AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  32201. AssertIntEQ(BIO_write_filename(f_bio2, testFile),
  32202. WOLFSSL_SUCCESS);
  32203. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32204. AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
  32205. AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  32206. AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
  32207. AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  32208. AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  32209. AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  32210. AssertIntEQ(BIO_reset(f_bio2), 0);
  32211. AssertIntEQ(BIO_tell(NULL),-1);
  32212. AssertIntEQ(BIO_tell(f_bio2),0);
  32213. AssertIntEQ(BIO_seek(f_bio2, 4), 0);
  32214. AssertIntEQ(BIO_tell(f_bio2),4);
  32215. BIO_free(f_bio1);
  32216. BIO_free(f_bio2);
  32217. AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  32218. AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  32219. AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  32220. BIO_free(f_bio1);
  32221. }
  32222. #endif /* !defined(NO_FILESYSTEM) */
  32223. /* BIO info callback */
  32224. {
  32225. const char* testArg = "test";
  32226. BIO* cb_bio;
  32227. AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
  32228. BIO_set_callback(cb_bio, bioCallback);
  32229. AssertNotNull(BIO_get_callback(cb_bio));
  32230. BIO_set_callback(cb_bio, NULL);
  32231. AssertNull(BIO_get_callback(cb_bio));
  32232. BIO_set_callback_arg(cb_bio, (char*)testArg);
  32233. AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  32234. AssertNull(BIO_get_callback_arg(NULL));
  32235. BIO_free(cb_bio);
  32236. }
  32237. /* BIO_vfree */
  32238. AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
  32239. BIO_vfree(NULL);
  32240. BIO_vfree(bio1);
  32241. printf(resultFmt, passed);
  32242. #endif
  32243. return 0;
  32244. }
  32245. #endif /* !NO_BIO */
  32246. static int test_wolfSSL_ASN1_STRING(void)
  32247. {
  32248. #if defined(OPENSSL_EXTRA)
  32249. ASN1_STRING* str = NULL;
  32250. const char data[] = "hello wolfSSL";
  32251. printf(testingFmt, "wolfSSL_ASN1_STRING()");
  32252. AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  32253. AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  32254. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  32255. AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  32256. AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  32257. ASN1_STRING_free(str);
  32258. printf(resultFmt, passed);
  32259. #endif
  32260. return 0;
  32261. }
  32262. static int test_wolfSSL_ASN1_BIT_STRING(void)
  32263. {
  32264. #ifdef OPENSSL_ALL
  32265. ASN1_BIT_STRING* str;
  32266. printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
  32267. AssertNotNull(str = ASN1_BIT_STRING_new());
  32268. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  32269. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  32270. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  32271. AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  32272. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  32273. AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  32274. ASN1_BIT_STRING_free(str);
  32275. printf(resultFmt, passed);
  32276. #endif
  32277. return 0;
  32278. }
  32279. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  32280. {
  32281. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  32282. BIO *bio, *out;
  32283. ASN1_INTEGER* ai;
  32284. char buf[] = "123456\n12345\n112345678912345678901234567890\n";
  32285. char tmp[1024];
  32286. int tmpSz;
  32287. const char expected1[] = "123456";
  32288. const char expected2[] = "112345678912345678901234567890";
  32289. printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
  32290. AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
  32291. AssertNotNull(out = BIO_new(BIO_s_mem()));
  32292. AssertNotNull(ai = ASN1_INTEGER_new());
  32293. /* read first line */
  32294. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32295. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  32296. XMEMSET(tmp, 0, 1024);
  32297. tmpSz = BIO_read(out, tmp, 1024);
  32298. AssertIntEQ(tmpSz, 6);
  32299. AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  32300. /* fail on second line (not % 2) */
  32301. AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32302. /* read 3rd long line */
  32303. AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
  32304. AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  32305. XMEMSET(tmp, 0, 1024);
  32306. tmpSz = BIO_read(out, tmp, 1024);
  32307. AssertIntEQ(tmpSz, 30);
  32308. AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  32309. BIO_free(out);
  32310. BIO_free(bio);
  32311. ASN1_INTEGER_free(ai);
  32312. printf(resultFmt, passed);
  32313. #endif
  32314. return 0;
  32315. }
  32316. static int test_wolfSSL_a2i_IPADDRESS(void)
  32317. {
  32318. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  32319. const unsigned char* data;
  32320. int dataSz = 0;
  32321. ASN1_OCTET_STRING *st;
  32322. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  32323. const unsigned char ipv6_exp[] = {
  32324. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  32325. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  32326. };
  32327. const unsigned char ipv6_home[] = {
  32328. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  32329. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  32330. };
  32331. printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
  32332. AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  32333. AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  32334. data = ASN1_STRING_get0_data(st);
  32335. dataSz = ASN1_STRING_length(st);
  32336. AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
  32337. AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  32338. ASN1_STRING_free(st);
  32339. AssertNotNull(st = a2i_IPADDRESS("::1"));
  32340. data = ASN1_STRING_get0_data(st);
  32341. dataSz = ASN1_STRING_length(st);
  32342. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32343. AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  32344. ASN1_STRING_free(st);
  32345. AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  32346. data = ASN1_STRING_get0_data(st);
  32347. dataSz = ASN1_STRING_length(st);
  32348. AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
  32349. AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  32350. ASN1_STRING_free(st);
  32351. printf(resultFmt, passed);
  32352. #endif
  32353. return 0;
  32354. }
  32355. static int test_wolfSSL_DES_ecb_encrypt(void)
  32356. {
  32357. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  32358. WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
  32359. WOLFSSL_DES_key_schedule key;
  32360. printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
  32361. XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
  32362. XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
  32363. XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
  32364. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  32365. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  32366. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  32367. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  32368. /* Encrypt messages */
  32369. wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
  32370. wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
  32371. {
  32372. /* Decrypt messages */
  32373. int ret1 = 0;
  32374. int ret2 = 0;
  32375. wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
  32376. ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
  32377. AssertIntEQ(ret1,0);
  32378. wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
  32379. ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
  32380. AssertIntEQ(ret2,0);
  32381. }
  32382. printf(resultFmt, passed);
  32383. #endif
  32384. return 0;
  32385. }
  32386. static int test_wolfSSL_ASN1_TIME_adj(void)
  32387. {
  32388. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32389. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32390. const int year = 365*24*60*60;
  32391. const int day = 24*60*60;
  32392. const int hour = 60*60;
  32393. const int mini = 60;
  32394. const byte asn_utc_time = ASN_UTC_TIME;
  32395. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32396. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  32397. #endif
  32398. WOLFSSL_ASN1_TIME *asn_time, *s;
  32399. int offset_day;
  32400. long offset_sec;
  32401. char date_str[CTC_DATE_SIZE + 1];
  32402. time_t t;
  32403. printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
  32404. AssertNotNull(s = wolfSSL_ASN1_TIME_new());
  32405. /* UTC notation test */
  32406. /* 2000/2/15 20:30:00 */
  32407. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32408. offset_day = 7;
  32409. offset_sec = 45 * mini;
  32410. /* offset_sec = -45 * min;*/
  32411. AssertNotNull(asn_time =
  32412. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  32413. AssertTrue(asn_time->type == asn_utc_time);
  32414. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32415. date_str[CTC_DATE_SIZE] = '\0';
  32416. AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  32417. /* negative offset */
  32418. offset_sec = -45 * mini;
  32419. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32420. AssertNotNull(asn_time);
  32421. AssertTrue(asn_time->type == asn_utc_time);
  32422. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32423. date_str[CTC_DATE_SIZE] = '\0';
  32424. AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  32425. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32426. XMEMSET(date_str, 0, sizeof(date_str));
  32427. /* Generalized time will overflow time_t if not long */
  32428. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  32429. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  32430. DYNAMIC_TYPE_OPENSSL);
  32431. /* GeneralizedTime notation test */
  32432. /* 2055/03/01 09:00:00 */
  32433. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  32434. offset_day = 12;
  32435. offset_sec = 10 * mini;
  32436. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32437. AssertTrue(asn_time->type == asn_gen_time);
  32438. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32439. date_str[CTC_DATE_SIZE] = '\0';
  32440. AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  32441. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  32442. XMEMSET(date_str, 0, sizeof(date_str));
  32443. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  32444. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  32445. s = NULL;
  32446. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  32447. offset_day = 7;
  32448. offset_sec = 45 * mini;
  32449. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  32450. AssertTrue(asn_time->type == asn_utc_time);
  32451. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32452. date_str[CTC_DATE_SIZE] = '\0';
  32453. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32454. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32455. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
  32456. AssertTrue(asn_time->type == asn_utc_time);
  32457. XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
  32458. date_str[CTC_DATE_SIZE] = '\0';
  32459. AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  32460. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  32461. printf(resultFmt, passed);
  32462. #endif
  32463. return 0;
  32464. }
  32465. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  32466. {
  32467. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  32468. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
  32469. && !defined(NO_ASN_TIME)
  32470. ASN1_TIME asnTime;
  32471. struct tm tm;
  32472. printf(testingFmt, "wolfSSL_ASN1_TIME_to_tm()");
  32473. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  32474. AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  32475. AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  32476. AssertIntEQ(tm.tm_sec, 15);
  32477. AssertIntEQ(tm.tm_min, 15);
  32478. AssertIntEQ(tm.tm_hour, 21);
  32479. AssertIntEQ(tm.tm_mday, 22);
  32480. AssertIntEQ(tm.tm_mon, 1);
  32481. AssertIntEQ(tm.tm_year, 100);
  32482. AssertIntEQ(tm.tm_isdst, 0);
  32483. #ifdef XMKTIME
  32484. AssertIntEQ(tm.tm_wday, 2);
  32485. AssertIntEQ(tm.tm_yday, 52);
  32486. #endif
  32487. printf(resultFmt, passed);
  32488. #endif
  32489. return 0;
  32490. }
  32491. static int test_wolfSSL_X509_cmp_time(void)
  32492. {
  32493. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  32494. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  32495. WOLFSSL_ASN1_TIME asn_time;
  32496. time_t t;
  32497. printf(testingFmt, "wolfSSL_X509_cmp_time()");
  32498. AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  32499. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  32500. AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  32501. AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  32502. AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  32503. printf(resultFmt, passed);
  32504. #endif
  32505. return 0;
  32506. }
  32507. static int test_wolfSSL_X509_time_adj(void)
  32508. {
  32509. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  32510. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  32511. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  32512. !defined(NO_ASN_TIME)
  32513. X509* x509;
  32514. time_t t, not_before, not_after;
  32515. printf(testingFmt, "wolfSSL_X509_time_adj()");
  32516. AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  32517. client_cert_der_2048, sizeof_client_cert_der_2048,
  32518. WOLFSSL_FILETYPE_ASN1));
  32519. t = 0;
  32520. not_before = wc_Time(0);
  32521. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  32522. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  32523. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  32524. /* Check X509_gmtime_adj, too. */
  32525. AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  32526. X509_free(x509);
  32527. printf(resultFmt, passed);
  32528. #endif
  32529. return 0;
  32530. }
  32531. static int test_wolfSSL_X509(void)
  32532. {
  32533. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
  32534. && !defined(NO_RSA)
  32535. X509* x509;
  32536. #ifndef NO_BIO
  32537. BIO* bio;
  32538. X509_STORE_CTX* ctx;
  32539. X509_STORE* store;
  32540. #endif
  32541. char der[] = "certs/ca-cert.der";
  32542. XFILE fp;
  32543. printf(testingFmt, "wolfSSL_X509()");
  32544. AssertNotNull(x509 = X509_new());
  32545. X509_free(x509);
  32546. #ifndef NO_BIO
  32547. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
  32548. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  32549. #ifdef WOLFSSL_CERT_GEN
  32550. AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  32551. #endif
  32552. AssertNotNull(ctx = X509_STORE_CTX_new());
  32553. AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  32554. AssertNotNull(store = X509_STORE_new());
  32555. AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  32556. AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  32557. AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  32558. X509_STORE_CTX_free(ctx);
  32559. X509_STORE_free(store);
  32560. X509_free(x509);
  32561. BIO_free(bio);
  32562. #endif
  32563. /** d2i_X509_fp test **/
  32564. fp = XFOPEN(der, "rb");
  32565. AssertTrue((fp != XBADFILE));
  32566. AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  32567. AssertNotNull(x509);
  32568. X509_free(x509);
  32569. XFCLOSE(fp);
  32570. fp = XFOPEN(der, "rb");
  32571. AssertTrue((fp != XBADFILE));
  32572. AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  32573. AssertNotNull(x509);
  32574. X509_free(x509);
  32575. XFCLOSE(fp);
  32576. /* X509_up_ref test */
  32577. AssertIntEQ(X509_up_ref(NULL), 0);
  32578. AssertNotNull(x509 = X509_new()); /* refCount = 1 */
  32579. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  32580. AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  32581. X509_free(x509); /* refCount = 2 */
  32582. X509_free(x509); /* refCount = 1 */
  32583. X509_free(x509); /* refCount = 0, free */
  32584. printf(resultFmt, passed);
  32585. #endif
  32586. return 0;
  32587. }
  32588. static int test_wolfSSL_X509_get_ext_count(void)
  32589. {
  32590. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  32591. !defined(NO_RSA)
  32592. int ret = 0;
  32593. WOLFSSL_X509* x509;
  32594. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  32595. FILE* f;
  32596. printf(testingFmt, "wolfSSL_X509_get_ext_count()");
  32597. /* NULL parameter check */
  32598. AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  32599. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  32600. SSL_FILETYPE_PEM));
  32601. AssertIntEQ(X509_get_ext_count(x509), 5);
  32602. wolfSSL_X509_free(x509);
  32603. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  32604. SSL_FILETYPE_PEM));
  32605. AssertIntEQ(X509_get_ext_count(x509), 5);
  32606. wolfSSL_X509_free(x509);
  32607. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  32608. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  32609. fclose(f);
  32610. printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
  32611. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  32612. printf(resultFmt, ret == 4 ? passed : failed);
  32613. printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
  32614. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  32615. printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
  32616. wolfSSL_X509_free(x509);
  32617. printf(resultFmt, passed);
  32618. #endif
  32619. return 0;
  32620. }
  32621. static int test_wolfSSL_X509_sign2(void)
  32622. {
  32623. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  32624. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  32625. defined(WOLFSSL_CERT_EXT) && \
  32626. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32627. WOLFSSL_X509 *x509, *ca;
  32628. const unsigned char *der;
  32629. const unsigned char *pt;
  32630. WOLFSSL_EVP_PKEY *priv;
  32631. WOLFSSL_X509_NAME *name;
  32632. WOLFSSL_ASN1_TIME *notBefore, *notAfter;
  32633. int derSz;
  32634. const int year = 365*24*60*60;
  32635. const int day = 24*60*60;
  32636. const int hour = 60*60;
  32637. const int mini = 60;
  32638. time_t t;
  32639. const unsigned char expected[] = {
  32640. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xfb, 0xa0, 0x03, 0x02, 0x01,
  32641. 0x02, 0x02, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3, 0xa6,
  32642. 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c, 0x30,
  32643. 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  32644. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55,
  32645. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
  32646. 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61,
  32647. 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42,
  32648. 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30, 0x0f, 0x06, 0x03,
  32649. 0x55, 0x04, 0x0a, 0x0c, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74,
  32650. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0a,
  32651. 0x43, 0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18,
  32652. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77, 0x77,
  32653. 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  32654. 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  32655. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f,
  32656. 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17,
  32657. 0x0d, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  32658. 0x30, 0x5a, 0x17, 0x0d, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  32659. 0x33, 0x30, 0x30, 0x30, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
  32660. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  32661. 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  32662. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07,
  32663. 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x15, 0x30,
  32664. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c, 0x77, 0x6f, 0x6c, 0x66,
  32665. 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  32666. 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50, 0x72, 0x6f, 0x67, 0x72,
  32667. 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32, 0x30, 0x34, 0x38, 0x31,
  32668. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x77,
  32669. 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f,
  32670. 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  32671. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
  32672. 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82,
  32673. 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  32674. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82,
  32675. 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xc3, 0x03, 0xd1, 0x2b, 0xfe,
  32676. 0x39, 0xa4, 0x32, 0x45, 0x3b, 0x53, 0xc8, 0x84, 0x2b, 0x2a, 0x7c, 0x74,
  32677. 0x9a, 0xbd, 0xaa, 0x2a, 0x52, 0x07, 0x47, 0xd6, 0xa6, 0x36, 0xb2, 0x07,
  32678. 0x32, 0x8e, 0xd0, 0xba, 0x69, 0x7b, 0xc6, 0xc3, 0x44, 0x9e, 0xd4, 0x81,
  32679. 0x48, 0xfd, 0x2d, 0x68, 0xa2, 0x8b, 0x67, 0xbb, 0xa1, 0x75, 0xc8, 0x36,
  32680. 0x2c, 0x4a, 0xd2, 0x1b, 0xf7, 0x8b, 0xba, 0xcf, 0x0d, 0xf9, 0xef, 0xec,
  32681. 0xf1, 0x81, 0x1e, 0x7b, 0x9b, 0x03, 0x47, 0x9a, 0xbf, 0x65, 0xcc, 0x7f,
  32682. 0x65, 0x24, 0x69, 0xa6, 0xe8, 0x14, 0x89, 0x5b, 0xe4, 0x34, 0xf7, 0xc5,
  32683. 0xb0, 0x14, 0x93, 0xf5, 0x67, 0x7b, 0x3a, 0x7a, 0x78, 0xe1, 0x01, 0x56,
  32684. 0x56, 0x91, 0xa6, 0x13, 0x42, 0x8d, 0xd2, 0x3c, 0x40, 0x9c, 0x4c, 0xef,
  32685. 0xd1, 0x86, 0xdf, 0x37, 0x51, 0x1b, 0x0c, 0xa1, 0x3b, 0xf5, 0xf1, 0xa3,
  32686. 0x4a, 0x35, 0xe4, 0xe1, 0xce, 0x96, 0xdf, 0x1b, 0x7e, 0xbf, 0x4e, 0x97,
  32687. 0xd0, 0x10, 0xe8, 0xa8, 0x08, 0x30, 0x81, 0xaf, 0x20, 0x0b, 0x43, 0x14,
  32688. 0xc5, 0x74, 0x67, 0xb4, 0x32, 0x82, 0x6f, 0x8d, 0x86, 0xc2, 0x88, 0x40,
  32689. 0x99, 0x36, 0x83, 0xba, 0x1e, 0x40, 0x72, 0x22, 0x17, 0xd7, 0x52, 0x65,
  32690. 0x24, 0x73, 0xb0, 0xce, 0xef, 0x19, 0xcd, 0xae, 0xff, 0x78, 0x6c, 0x7b,
  32691. 0xc0, 0x12, 0x03, 0xd4, 0x4e, 0x72, 0x0d, 0x50, 0x6d, 0x3b, 0xa3, 0x3b,
  32692. 0xa3, 0x99, 0x5e, 0x9d, 0xc8, 0xd9, 0x0c, 0x85, 0xb3, 0xd9, 0x8a, 0xd9,
  32693. 0x54, 0x26, 0xdb, 0x6d, 0xfa, 0xac, 0xbb, 0xff, 0x25, 0x4c, 0xc4, 0xd1,
  32694. 0x79, 0xf4, 0x71, 0xd3, 0x86, 0x40, 0x18, 0x13, 0xb0, 0x63, 0xb5, 0x72,
  32695. 0x4e, 0x30, 0xc4, 0x97, 0x84, 0x86, 0x2d, 0x56, 0x2f, 0xd7, 0x15, 0xf7,
  32696. 0x7f, 0xc0, 0xae, 0xf5, 0xfc, 0x5b, 0xe5, 0xfb, 0xa1, 0xba, 0xd3, 0x02,
  32697. 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x4f, 0x30, 0x82, 0x01, 0x4b,
  32698. 0x30, 0x0c, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  32699. 0x01, 0xff, 0x30, 0x1c, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x15, 0x30,
  32700. 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e, 0x63,
  32701. 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01, 0x30, 0x1d, 0x06, 0x03,
  32702. 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  32703. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  32704. 0x85, 0x65, 0xc0, 0x30, 0x81, 0xde, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04,
  32705. 0x81, 0xd6, 0x30, 0x81, 0xd3, 0x80, 0x14, 0x33, 0xd8, 0x45, 0x66, 0xd7,
  32706. 0x68, 0x87, 0x18, 0x7e, 0x54, 0x0d, 0x70, 0x27, 0x91, 0xc7, 0x26, 0xd7,
  32707. 0x85, 0x65, 0xc0, 0xa1, 0x81, 0xa4, 0xa4, 0x81, 0xa1, 0x30, 0x81, 0x9e,
  32708. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  32709. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x07,
  32710. 0x4d, 0x6f, 0x6e, 0x74, 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  32711. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f, 0x7a, 0x65, 0x6d, 0x61,
  32712. 0x6e, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0c,
  32713. 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x5f, 0x32, 0x30, 0x34, 0x38,
  32714. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x10, 0x50,
  32715. 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x6d, 0x69, 0x6e, 0x67, 0x2d, 0x32,
  32716. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  32717. 0x0c, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73,
  32718. 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
  32719. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e,
  32720. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63,
  32721. 0x6f, 0x6d, 0x82, 0x14, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3,
  32722. 0xa6, 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c,
  32723. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  32724. 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2b,
  32725. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  32726. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82,
  32727. 0x01, 0x01, 0x00, 0xa3, 0x41, 0x43, 0x93, 0x30, 0x92, 0x98, 0xfe, 0x57,
  32728. 0xd0, 0x39, 0x7c, 0x50, 0x06, 0x50, 0x20, 0x80, 0x0e, 0x28, 0x95, 0x79,
  32729. 0xb4, 0xf1, 0x6b, 0x6a, 0xab, 0x78, 0x30, 0x93, 0x49, 0x0a, 0x6a, 0x19,
  32730. 0x09, 0xae, 0x31, 0xc6, 0x8e, 0xcc, 0x69, 0x26, 0x89, 0x37, 0xc1, 0x57,
  32731. 0x58, 0x75, 0xae, 0xbf, 0x13, 0xc8, 0xd6, 0xad, 0xd0, 0x0f, 0x57, 0xcd,
  32732. 0x32, 0xa8, 0xda, 0xa8, 0x1b, 0xbf, 0xb5, 0xcd, 0x16, 0x14, 0x56, 0x86,
  32733. 0x84, 0xb4, 0xab, 0x93, 0x52, 0x74, 0xfd, 0x96, 0x9f, 0x6d, 0xbe, 0xdb,
  32734. 0x75, 0x5e, 0x76, 0xfe, 0xa6, 0x37, 0xe5, 0x5f, 0xcb, 0x62, 0x77, 0xc7,
  32735. 0xd6, 0xcb, 0xb4, 0xf6, 0x43, 0xc8, 0x47, 0xdf, 0x12, 0x16, 0x28, 0x29,
  32736. 0x61, 0xd1, 0xdc, 0x9d, 0x37, 0x9f, 0xe5, 0x71, 0x52, 0xae, 0xb8, 0x12,
  32737. 0xec, 0x32, 0x9f, 0x03, 0x1a, 0x66, 0x98, 0xd8, 0xb0, 0x40, 0x71, 0x4c,
  32738. 0xee, 0x64, 0x15, 0x48, 0x0c, 0x5c, 0x8a, 0x47, 0x20, 0xbd, 0x07, 0xc0,
  32739. 0x30, 0xf8, 0x84, 0xe6, 0x29, 0x6d, 0xa9, 0x32, 0x53, 0x02, 0x4d, 0x3c,
  32740. 0x99, 0x6e, 0x63, 0xfe, 0x39, 0x9c, 0x05, 0xa6, 0xa0, 0x0c, 0x1e, 0x11,
  32741. 0xa4, 0x86, 0x6a, 0x89, 0x76, 0x54, 0x17, 0x68, 0x5d, 0x35, 0x9a, 0xd7,
  32742. 0x5e, 0x27, 0x0e, 0xbb, 0xba, 0x67, 0x4d, 0x62, 0x12, 0xa8, 0x46, 0x1f,
  32743. 0x0e, 0xd8, 0x7d, 0xc0, 0xae, 0x30, 0xc2, 0x45, 0x71, 0xab, 0xb1, 0xc1,
  32744. 0xfb, 0xdc, 0x03, 0x7a, 0x52, 0xe6, 0x57, 0xf9, 0x7f, 0x65, 0x6b, 0x4e,
  32745. 0x44, 0x64, 0xe8, 0x77, 0x82, 0x1c, 0xc8, 0xfa, 0x09, 0xc7, 0x2f, 0xa9,
  32746. 0x40, 0x87, 0x8e, 0x0e, 0x49, 0xc2, 0x7d, 0x97, 0x27, 0x79, 0x90, 0xc2,
  32747. 0x90, 0x13, 0xa7, 0x49, 0xb7, 0xd7, 0xc5, 0x02, 0x32, 0x4f, 0x1e, 0x34,
  32748. 0x4a, 0xa6, 0xe4, 0xbd, 0xa5, 0xc6, 0xec
  32749. };
  32750. printf(testingFmt, "wolfSSL_X509_sign2");
  32751. pt = ca_key_der_2048;
  32752. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  32753. sizeof_ca_key_der_2048));
  32754. pt = client_cert_der_2048;
  32755. AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  32756. sizeof_client_cert_der_2048));
  32757. pt = ca_cert_der_2048;
  32758. AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  32759. AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
  32760. AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  32761. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  32762. AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  32763. AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  32764. AssertIntEQ(notAfter->length, 13);
  32765. AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  32766. AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  32767. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  32768. AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  32769. AssertIntEQ(derSz, sizeof(expected));
  32770. AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
  32771. wolfSSL_X509_free(ca);
  32772. wolfSSL_X509_free(x509);
  32773. wolfSSL_EVP_PKEY_free(priv);
  32774. wolfSSL_ASN1_TIME_free(notBefore);
  32775. wolfSSL_ASN1_TIME_free(notAfter);
  32776. printf(resultFmt, passed);
  32777. #endif
  32778. return 0;
  32779. }
  32780. static int test_wolfSSL_X509_sign(void)
  32781. {
  32782. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32783. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  32784. int ret;
  32785. char *cn;
  32786. word32 cnSz;
  32787. X509_NAME *name;
  32788. X509 *x509, *ca;
  32789. DecodedCert dCert;
  32790. EVP_PKEY *pub;
  32791. EVP_PKEY *priv;
  32792. EVP_MD_CTX *mctx;
  32793. #if defined(USE_CERT_BUFFERS_1024)
  32794. const unsigned char* rsaPriv = client_key_der_1024;
  32795. const unsigned char* rsaPub = client_keypub_der_1024;
  32796. const unsigned char* certIssuer = client_cert_der_1024;
  32797. long clientKeySz = (long)sizeof_client_key_der_1024;
  32798. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  32799. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  32800. #elif defined(USE_CERT_BUFFERS_2048)
  32801. const unsigned char* rsaPriv = client_key_der_2048;
  32802. const unsigned char* rsaPub = client_keypub_der_2048;
  32803. const unsigned char* certIssuer = client_cert_der_2048;
  32804. long clientKeySz = (long)sizeof_client_key_der_2048;
  32805. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  32806. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  32807. #endif
  32808. byte sn[16];
  32809. int snSz = sizeof(sn);
  32810. printf(testingFmt, "wolfSSL_X509_sign");
  32811. /* Set X509_NAME fields */
  32812. AssertNotNull(name = X509_NAME_new());
  32813. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  32814. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  32815. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  32816. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  32817. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  32818. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  32819. /* Get private and public keys */
  32820. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  32821. clientKeySz));
  32822. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  32823. AssertNotNull(x509 = X509_new());
  32824. /* Set version 3 */
  32825. AssertIntNE(X509_set_version(x509, 2L), 0);
  32826. /* Set subject name, add pubkey, and sign certificate */
  32827. AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  32828. X509_NAME_free(name);
  32829. AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  32830. #ifdef WOLFSSL_ALT_NAMES
  32831. /* Add some subject alt names */
  32832. AssertIntNE(wolfSSL_X509_add_altname(NULL,
  32833. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  32834. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32835. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  32836. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32837. "sphygmomanometer",
  32838. ASN_DNS_TYPE), SSL_SUCCESS);
  32839. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32840. "supercalifragilisticexpialidocious",
  32841. ASN_DNS_TYPE), SSL_SUCCESS);
  32842. AssertIntEQ(wolfSSL_X509_add_altname(x509,
  32843. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  32844. ASN_DNS_TYPE), SSL_SUCCESS);
  32845. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32846. {
  32847. unsigned char ip4_type[] = {127,128,0,255};
  32848. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  32849. 0xff, 0xee, 0x99, 0x88,
  32850. 0x77, 0x66, 0x55, 0x44,
  32851. 0x00, 0x33, 0x22, 0x11};
  32852. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  32853. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  32854. AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  32855. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  32856. }
  32857. #endif
  32858. #endif /* WOLFSSL_ALT_NAMES */
  32859. /* test valid sign case */
  32860. ret = X509_sign(x509, priv, EVP_sha256());
  32861. /* test valid X509_sign_ctx case */
  32862. AssertNotNull(mctx = EVP_MD_CTX_new());
  32863. AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  32864. AssertIntGT(X509_sign_ctx(x509, mctx), 0);
  32865. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  32866. AssertIntEQ(X509_get_ext_count(x509), 1);
  32867. #endif
  32868. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  32869. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  32870. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  32871. #endif
  32872. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  32873. WOLFSSL_SUCCESS);
  32874. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  32875. /* Variation in size depends on ASN.1 encoding when MSB is set.
  32876. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  32877. * with the MSB set. See GenerateInteger in asn.c */
  32878. #ifndef USE_CERT_BUFFERS_1024
  32879. #ifndef WOLFSSL_ALT_NAMES
  32880. /* Valid case - size should be 798-797 with 16 byte serial number */
  32881. AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
  32882. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32883. /* Valid case - size should be 955-956 with 16 byte serial number */
  32884. AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
  32885. #else
  32886. /* Valid case - size should be 926-927 with 16 byte serial number */
  32887. AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
  32888. #endif
  32889. #else
  32890. #ifndef WOLFSSL_ALT_NAMES
  32891. /* Valid case - size should be 537-538 with 16 byte serial number */
  32892. AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
  32893. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  32894. /* Valid case - size should be 695-696 with 16 byte serial number */
  32895. AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
  32896. #else
  32897. /* Valid case - size should be 666-667 with 16 byte serial number */
  32898. AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
  32899. #endif
  32900. #endif
  32901. /* check that issuer name is as expected after signature */
  32902. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  32903. AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  32904. AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  32905. AssertNotNull(name = X509_get_subject_name(ca));
  32906. cnSz = X509_NAME_get_sz(name);
  32907. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32908. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  32909. AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  32910. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32911. #ifdef WOLFSSL_MULTI_ATTRIB
  32912. /* test adding multiple OU's to the signer */
  32913. AssertNotNull(name = X509_get_subject_name(ca));
  32914. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32915. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  32916. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  32917. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  32918. AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  32919. #endif
  32920. AssertNotNull(name = X509_get_subject_name(ca));
  32921. AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  32922. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  32923. AssertNotNull(name = X509_get_issuer_name(x509));
  32924. cnSz = X509_NAME_get_sz(name);
  32925. AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  32926. AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  32927. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  32928. AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  32929. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  32930. FreeDecodedCert(&dCert);
  32931. /* Test invalid parameters */
  32932. AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  32933. AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  32934. AssertIntEQ(X509_sign(x509, priv, NULL), 0);
  32935. AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
  32936. EVP_MD_CTX_free(mctx);
  32937. AssertNotNull(mctx = EVP_MD_CTX_new());
  32938. AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
  32939. AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
  32940. /* test invalid version number */
  32941. #if defined(OPENSSL_ALL)
  32942. AssertIntNE(X509_set_version(x509, 6L), 0);
  32943. AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  32944. /* uses ParseCert which fails on bad version number */
  32945. AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  32946. #endif
  32947. EVP_MD_CTX_free(mctx);
  32948. EVP_PKEY_free(priv);
  32949. EVP_PKEY_free(pub);
  32950. X509_free(x509);
  32951. X509_free(ca);
  32952. printf(resultFmt, passed);
  32953. #endif
  32954. return 0;
  32955. }
  32956. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  32957. {
  32958. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  32959. X509* x509 = NULL;
  32960. const X509_ALGOR* alg;
  32961. printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
  32962. AssertNotNull(x509 = X509_new());
  32963. AssertNull(alg = X509_get0_tbs_sigalg(NULL));
  32964. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  32965. X509_free(x509);
  32966. printf(resultFmt, passed);
  32967. #endif
  32968. return 0;
  32969. }
  32970. static int test_wolfSSL_X509_ALGOR_get0(void)
  32971. {
  32972. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  32973. !defined(NO_SHA256) && !defined(NO_RSA)
  32974. X509* x509 = NULL;
  32975. const ASN1_OBJECT* obj = NULL;
  32976. const X509_ALGOR* alg;
  32977. int pptype = 0;
  32978. const void *ppval = NULL;
  32979. printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
  32980. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  32981. SSL_FILETYPE_PEM));
  32982. AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
  32983. /* Invalid case */
  32984. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  32985. AssertNull(obj);
  32986. /* Valid case */
  32987. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  32988. AssertNotNull(obj);
  32989. AssertNull(ppval);
  32990. AssertIntNE(pptype, 0);
  32991. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  32992. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  32993. X509_free(x509);
  32994. printf(resultFmt, passed);
  32995. #endif
  32996. return 0;
  32997. }
  32998. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  32999. {
  33000. #if defined(OPENSSL_EXTRA)
  33001. X509_VERIFY_PARAM *paramTo;
  33002. X509_VERIFY_PARAM *paramFrom;
  33003. int ret;
  33004. char testIPv4[] = "127.0.0.1";
  33005. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  33006. char testhostName1[] = "foo.hoge.com";
  33007. char testhostName2[] = "foobar.hoge.com";
  33008. printf(testingFmt, "wolfSSL_X509()");
  33009. paramTo = X509_VERIFY_PARAM_new();
  33010. AssertNotNull(paramTo);
  33011. XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
  33012. paramFrom = X509_VERIFY_PARAM_new();
  33013. AssertNotNull(paramFrom);
  33014. XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
  33015. ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  33016. (int)XSTRLEN(testhostName1));
  33017. AssertIntEQ(1, ret);
  33018. AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  33019. (int)XSTRLEN(testhostName1)));
  33020. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  33021. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  33022. AssertIntEQ(0x01, paramFrom->hostFlags);
  33023. ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
  33024. AssertIntEQ(0, ret);
  33025. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
  33026. AssertIntEQ(1, ret);
  33027. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33028. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
  33029. AssertIntEQ(1, ret);
  33030. ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
  33031. AssertIntEQ(1, ret);
  33032. AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33033. /* null pointer */
  33034. ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
  33035. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33036. /* in the case of "from" null, returns success */
  33037. ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
  33038. AssertIntEQ(WOLFSSL_SUCCESS, ret);
  33039. ret = X509_VERIFY_PARAM_set1(NULL, NULL);
  33040. AssertIntEQ(WOLFSSL_FAILURE, ret);
  33041. /* inherit flags test : VPARAM_DEFAULT */
  33042. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33043. AssertIntEQ(1, ret);
  33044. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33045. (int)XSTRLEN(testhostName1)));
  33046. AssertIntEQ(0x01, paramTo->hostFlags);
  33047. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33048. /* inherit flags test : VPARAM OVERWRITE */
  33049. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33050. (int)XSTRLEN(testhostName2));
  33051. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33052. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33053. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  33054. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33055. AssertIntEQ(1, ret);
  33056. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33057. (int)XSTRLEN(testhostName1)));
  33058. AssertIntEQ(0x01, paramTo->hostFlags);
  33059. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33060. /* inherit flags test : VPARAM_RESET_FLAGS */
  33061. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33062. (int)XSTRLEN(testhostName2));
  33063. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33064. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  33065. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  33066. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33067. AssertIntEQ(1, ret);
  33068. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  33069. (int)XSTRLEN(testhostName1)));
  33070. AssertIntEQ(0x01, paramTo->hostFlags);
  33071. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  33072. /* inherit flags test : VPARAM_LOCKED */
  33073. X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  33074. (int)XSTRLEN(testhostName2));
  33075. X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
  33076. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  33077. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  33078. ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
  33079. AssertIntEQ(1, ret);
  33080. AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  33081. (int)XSTRLEN(testhostName2)));
  33082. AssertIntEQ(0x00, paramTo->hostFlags);
  33083. AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33084. /* test for incorrect parameters */
  33085. ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
  33086. AssertIntEQ(0, ret);
  33087. ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
  33088. AssertIntEQ(0, ret);
  33089. /* inherit flags test : VPARAM_ONCE, not testable yet */
  33090. ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33091. AssertIntEQ(1, ret);
  33092. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33093. AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
  33094. ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
  33095. AssertIntEQ(1, ret);
  33096. ret = X509_VERIFY_PARAM_get_flags(paramTo);
  33097. AssertIntEQ(0, ret);
  33098. X509_VERIFY_PARAM_free(paramTo);
  33099. X509_VERIFY_PARAM_free(paramFrom);
  33100. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  33101. printf(resultFmt, passed);
  33102. #endif
  33103. return 0;
  33104. }
  33105. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  33106. static int test_wolfSSL_check_domain_verify_count = 0;
  33107. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  33108. WOLFSSL_X509_STORE_CTX* store)
  33109. {
  33110. AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
  33111. AssertIntEQ(preverify, 1);
  33112. test_wolfSSL_check_domain_verify_count++;
  33113. return 1;
  33114. }
  33115. static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  33116. {
  33117. X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
  33118. /* Domain check should only be done on the leaf cert */
  33119. X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  33120. AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
  33121. "wolfSSL Server Chain", 0), 1);
  33122. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  33123. test_wolfSSL_check_domain_verify_cb);
  33124. }
  33125. static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  33126. {
  33127. /* Use a cert with different domains in chain */
  33128. AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  33129. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  33130. }
  33131. static int test_wolfSSL_check_domain(void)
  33132. {
  33133. tcp_ready ready;
  33134. func_args client_args;
  33135. func_args server_args;
  33136. THREAD_TYPE serverThread;
  33137. callback_functions func_cb_client;
  33138. callback_functions func_cb_server;
  33139. printf(testingFmt, "wolfSSL_check_domain");
  33140. XMEMSET(&client_args, 0, sizeof(func_args));
  33141. XMEMSET(&server_args, 0, sizeof(func_args));
  33142. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  33143. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  33144. #ifdef WOLFSSL_TIRTOS
  33145. fdOpenSession(Task_self());
  33146. #endif
  33147. StartTCP();
  33148. InitTcpReady(&ready);
  33149. #if defined(USE_WINDOWS_API)
  33150. /* use RNG to get random port if using windows */
  33151. ready.port = GetRandomPort();
  33152. #endif
  33153. server_args.signal = &ready;
  33154. client_args.signal = &ready;
  33155. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  33156. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  33157. client_args.callbacks = &func_cb_client;
  33158. server_args.callbacks = &func_cb_server;
  33159. start_thread(test_server_nofail, &server_args, &serverThread);
  33160. wait_tcp_ready(&server_args);
  33161. test_client_nofail(&client_args, NULL);
  33162. join_thread(serverThread);
  33163. AssertTrue(client_args.return_code);
  33164. AssertTrue(server_args.return_code);
  33165. FreeTcpReady(&ready);
  33166. /* Should have been called once for each cert in sent chain */
  33167. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  33168. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  33169. #else
  33170. AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  33171. #endif
  33172. printf(resultFmt, passed);
  33173. return 0;
  33174. }
  33175. #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
  33176. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  33177. {
  33178. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  33179. X509* x509 = NULL;
  33180. X509_PUBKEY* pubKey;
  33181. printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
  33182. AssertNotNull(x509 = X509_new());
  33183. AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  33184. AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  33185. X509_free(x509);
  33186. printf(resultFmt, passed);
  33187. #endif
  33188. return 0;
  33189. }
  33190. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  33191. {
  33192. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  33193. !defined(NO_SHA256) && !defined(NO_RSA)
  33194. X509* x509 = NULL;
  33195. ASN1_OBJECT* obj = NULL;
  33196. const ASN1_OBJECT* pa_oid = NULL;
  33197. X509_PUBKEY* pubKey;
  33198. X509_PUBKEY* pubKey2;
  33199. EVP_PKEY* evpKey;
  33200. const unsigned char *pk;
  33201. int ppklen, pptype;
  33202. X509_ALGOR *pa;
  33203. const void *pval;
  33204. printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
  33205. AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33206. SSL_FILETYPE_PEM));
  33207. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33208. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33209. AssertNotNull(pk);
  33210. AssertNotNull(pa);
  33211. AssertNotNull(pubKey);
  33212. AssertIntGT(ppklen, 0);
  33213. AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  33214. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33215. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33216. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33217. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33218. AssertNotNull(pk);
  33219. AssertNotNull(pa);
  33220. AssertIntGT(ppklen, 0);
  33221. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33222. AssertNotNull(pa_oid);
  33223. AssertNull(pval);
  33224. AssertIntEQ(pptype, V_ASN1_NULL);
  33225. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  33226. X509_PUBKEY_free(pubKey2);
  33227. X509_free(x509);
  33228. EVP_PKEY_free(evpKey);
  33229. printf(resultFmt, passed);
  33230. #endif
  33231. return 0;
  33232. }
  33233. static int test_wolfSSL_X509_PUBKEY_EC(void)
  33234. {
  33235. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  33236. X509* x509 = NULL;
  33237. ASN1_OBJECT* obj = NULL;
  33238. ASN1_OBJECT* poid;
  33239. const ASN1_OBJECT* pa_oid = NULL;
  33240. X509_PUBKEY* pubKey;
  33241. X509_PUBKEY* pubKey2;
  33242. EVP_PKEY* evpKey;
  33243. const unsigned char *pk;
  33244. int ppklen, pptype;
  33245. X509_ALGOR *pa;
  33246. const void *pval;
  33247. char buf[50];
  33248. printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
  33249. AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  33250. SSL_FILETYPE_PEM));
  33251. AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  33252. AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
  33253. AssertNotNull(pubKey2 = X509_PUBKEY_new());
  33254. AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  33255. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  33256. AssertNotNull(pk);
  33257. AssertNotNull(pa);
  33258. AssertIntGT(ppklen, 0);
  33259. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33260. AssertNotNull(pa_oid);
  33261. AssertNotNull(pval);
  33262. AssertIntEQ(pptype, V_ASN1_OBJECT);
  33263. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  33264. poid = (ASN1_OBJECT *)pval;
  33265. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  33266. AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  33267. X509_PUBKEY_free(pubKey2);
  33268. X509_free(x509);
  33269. EVP_PKEY_free(evpKey);
  33270. printf(resultFmt, passed);
  33271. #endif
  33272. return 0;
  33273. }
  33274. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  33275. {
  33276. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  33277. word32 bytes;
  33278. #ifdef USE_CERT_BUFFERS_1024
  33279. byte tmp[ONEK_BUF];
  33280. #elif defined(USE_CERT_BUFFERS_2048)
  33281. byte tmp[TWOK_BUF];
  33282. #else
  33283. byte tmp[TWOK_BUF];
  33284. #endif /* END USE_CERT_BUFFERS_1024 */
  33285. const unsigned char* dsaKeyDer = tmp;
  33286. ASN1_OBJECT* obj = NULL;
  33287. ASN1_STRING* str;
  33288. const ASN1_OBJECT* pa_oid = NULL;
  33289. X509_PUBKEY* pubKey = NULL;
  33290. EVP_PKEY* evpKey = NULL;
  33291. const unsigned char *pk;
  33292. int ppklen, pptype;
  33293. X509_ALGOR *pa;
  33294. const void *pval;
  33295. printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
  33296. #ifdef USE_CERT_BUFFERS_1024
  33297. XMEMSET(tmp, 0, sizeof(tmp));
  33298. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  33299. bytes = sizeof_dsa_key_der_1024;
  33300. #elif defined(USE_CERT_BUFFERS_2048)
  33301. XMEMSET(tmp, 0, sizeof(tmp));
  33302. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  33303. bytes = sizeof_dsa_key_der_2048;
  33304. #else
  33305. {
  33306. XFILE fp;
  33307. XMEMSET(tmp, 0, sizeof(tmp));
  33308. fp = XFOPEN("./certs/dsa2048.der", "rb");
  33309. if (fp == XBADFILE) {
  33310. return WOLFSSL_BAD_FILE;
  33311. }
  33312. bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  33313. XFCLOSE(fp);
  33314. }
  33315. #endif
  33316. /* Initialize pkey with der format dsa key */
  33317. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  33318. AssertNotNull(pubKey = X509_PUBKEY_new());
  33319. AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  33320. AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  33321. AssertNotNull(pk);
  33322. AssertNotNull(pa);
  33323. AssertIntGT(ppklen, 0);
  33324. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  33325. AssertNotNull(pa_oid);
  33326. AssertNotNull(pval);
  33327. AssertIntEQ(pptype, V_ASN1_SEQUENCE);
  33328. AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  33329. str = (ASN1_STRING *)pval;
  33330. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  33331. #ifdef USE_CERT_BUFFERS_1024
  33332. AssertIntEQ(ASN1_STRING_length(str), 291);
  33333. #else
  33334. AssertIntEQ(ASN1_STRING_length(str), 549);
  33335. #endif /* END USE_CERT_BUFFERS_1024 */
  33336. X509_PUBKEY_free(pubKey);
  33337. EVP_PKEY_free(evpKey);
  33338. printf(resultFmt, passed);
  33339. #endif
  33340. return 0;
  33341. }
  33342. static int test_wolfSSL_RAND(void)
  33343. {
  33344. #if defined(OPENSSL_EXTRA)
  33345. byte seed[16];
  33346. printf(testingFmt, "wolfSSL_RAND()");
  33347. RAND_seed(seed, sizeof(seed));
  33348. AssertIntEQ(RAND_poll(), 1);
  33349. RAND_cleanup();
  33350. AssertIntEQ(RAND_egd(NULL), -1);
  33351. #ifndef NO_FILESYSTEM
  33352. {
  33353. char fname[100];
  33354. AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  33355. AssertIntEQ(RAND_write_file(NULL), 0);
  33356. }
  33357. #endif
  33358. printf(resultFmt, passed);
  33359. #endif
  33360. return 0;
  33361. }
  33362. static int test_wolfSSL_BUF(void)
  33363. {
  33364. #if defined(OPENSSL_EXTRA)
  33365. BUF_MEM* buf;
  33366. AssertNotNull(buf = BUF_MEM_new());
  33367. AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
  33368. AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
  33369. BUF_MEM_free(buf);
  33370. #endif /* OPENSSL_EXTRA */
  33371. return 0;
  33372. }
  33373. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33374. static int stub_rand_seed(const void *buf, int num)
  33375. {
  33376. (void)buf;
  33377. (void)num;
  33378. return 123;
  33379. }
  33380. static int stub_rand_bytes(unsigned char *buf, int num)
  33381. {
  33382. (void)buf;
  33383. (void)num;
  33384. return 456;
  33385. }
  33386. static byte* was_stub_rand_cleanup_called(void)
  33387. {
  33388. static byte was_called = 0;
  33389. return &was_called;
  33390. }
  33391. static void stub_rand_cleanup(void)
  33392. {
  33393. byte* was_called = was_stub_rand_cleanup_called();
  33394. *was_called = 1;
  33395. return;
  33396. }
  33397. static byte* was_stub_rand_add_called(void)
  33398. {
  33399. static byte was_called = 0;
  33400. return &was_called;
  33401. }
  33402. static int stub_rand_add(const void *buf, int num, double entropy)
  33403. {
  33404. byte* was_called = was_stub_rand_add_called();
  33405. (void)buf;
  33406. (void)num;
  33407. (void)entropy;
  33408. *was_called = 1;
  33409. return 0;
  33410. }
  33411. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  33412. {
  33413. (void)buf;
  33414. (void)num;
  33415. return 9876;
  33416. }
  33417. static int stub_rand_status(void)
  33418. {
  33419. return 5432;
  33420. }
  33421. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33422. static int test_wolfSSL_RAND_set_rand_method(void)
  33423. {
  33424. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  33425. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  33426. unsigned char* buf = NULL;
  33427. int num = 0;
  33428. double entropy = 0;
  33429. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  33430. byte* was_add_called = was_stub_rand_add_called();
  33431. printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
  33432. buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  33433. DYNAMIC_TYPE_TMP_BUFFER);
  33434. AssertIntNE(wolfSSL_RAND_status(), 5432);
  33435. AssertIntEQ(*was_cleanup_called, 0);
  33436. RAND_cleanup();
  33437. AssertIntEQ(*was_cleanup_called, 0);
  33438. rand_methods.seed = &stub_rand_seed;
  33439. rand_methods.bytes = &stub_rand_bytes;
  33440. rand_methods.cleanup = &stub_rand_cleanup;
  33441. rand_methods.add = &stub_rand_add;
  33442. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  33443. rand_methods.status = &stub_rand_status;
  33444. AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  33445. AssertIntEQ(RAND_seed(buf, num), 123);
  33446. AssertIntEQ(RAND_bytes(buf, num), 456);
  33447. AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  33448. AssertIntEQ(RAND_status(), 5432);
  33449. AssertIntEQ(*was_add_called, 0);
  33450. /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
  33451. RAND_add(buf, num, entropy);
  33452. AssertIntEQ(*was_add_called, 1);
  33453. was_add_called = 0;
  33454. AssertIntEQ(*was_cleanup_called, 0);
  33455. RAND_cleanup();
  33456. AssertIntEQ(*was_cleanup_called, 1);
  33457. *was_cleanup_called = 0;
  33458. AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
  33459. AssertIntNE(RAND_status(), 5432);
  33460. AssertIntEQ(*was_cleanup_called, 0);
  33461. RAND_cleanup();
  33462. AssertIntEQ(*was_cleanup_called, 0);
  33463. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33464. printf(resultFmt, passed);
  33465. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  33466. return 0;
  33467. }
  33468. static int test_wolfSSL_RAND_bytes(void)
  33469. {
  33470. #if defined(OPENSSL_EXTRA)
  33471. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  33472. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  33473. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  33474. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  33475. int max_bufsize;
  33476. byte *my_buf;
  33477. printf(testingFmt, "test_wolfSSL_RAND_bytes()");
  33478. /* sanity check */
  33479. AssertIntEQ(RAND_bytes(NULL, 16), 0);
  33480. AssertIntEQ(RAND_bytes(NULL, 0), 0);
  33481. max_bufsize = size4;
  33482. my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  33483. DYNAMIC_TYPE_TMP_BUFFER);
  33484. AssertIntEQ(RAND_bytes(my_buf, 0), 1);
  33485. AssertIntEQ(RAND_bytes(my_buf, -1), 0);
  33486. AssertNotNull(my_buf);
  33487. XMEMSET(my_buf, 0, max_bufsize);
  33488. AssertIntEQ(RAND_bytes(my_buf, size1), 1);
  33489. AssertIntEQ(RAND_bytes(my_buf, size2), 1);
  33490. AssertIntEQ(RAND_bytes(my_buf, size3), 1);
  33491. AssertIntEQ(RAND_bytes(my_buf, size4), 1);
  33492. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  33493. printf(resultFmt, passed);
  33494. #endif
  33495. return 0;
  33496. }
  33497. static int test_wolfSSL_BN_rand(void)
  33498. {
  33499. #if defined(OPENSSL_EXTRA)
  33500. BIGNUM* bn;
  33501. BIGNUM* range;
  33502. printf(testingFmt, "wolfSSL_BN_rand()");
  33503. /* Error conditions. */
  33504. /* NULL BN. */
  33505. AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
  33506. AssertNotNull(bn = BN_new());
  33507. /* Negative bits. */
  33508. AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
  33509. /* 0 bits and top is not -1. */
  33510. AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
  33511. /* 0 bits and bottom is not 0. */
  33512. AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
  33513. /* 1 bit and top is 1. */
  33514. AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
  33515. AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
  33516. AssertIntEQ(BN_num_bits(bn), 0);
  33517. AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33518. AssertIntEQ(BN_num_bits(bn), 8);
  33519. /* When top is 0, top bit should be 1. */
  33520. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33521. AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
  33522. /* When top is 1, top 2 bits should be 1. */
  33523. AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
  33524. AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
  33525. AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
  33526. /* When bottom is 1, bottom bit should be 1. */
  33527. AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
  33528. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  33529. * requested number of bits up to the nearest multiple of 8. E.g. in this
  33530. * case, requesting a 13-bit random number would actually return a 16-bit
  33531. * random number. */
  33532. AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
  33533. AssertIntEQ(BN_num_bits(bn), 13);
  33534. AssertNotNull(range = BN_new());
  33535. AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
  33536. AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
  33537. BN_free(bn);
  33538. BN_free(range);
  33539. printf(resultFmt, passed);
  33540. #endif
  33541. return 0;
  33542. }
  33543. static int test_wolfSSL_pseudo_rand(void)
  33544. {
  33545. #if defined(OPENSSL_EXTRA)
  33546. BIGNUM* bn;
  33547. unsigned char bin[8];
  33548. int i;
  33549. printf(testingFmt, "wolfSSL_pseudo_rand()");
  33550. /* BN_pseudo_rand returns 1 on success 0 on failure
  33551. * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
  33552. for (i = 0; i < 10; i++) {
  33553. AssertNotNull(bn = BN_new());
  33554. AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
  33555. AssertIntGT(BN_bn2bin(bn, bin),0);
  33556. AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
  33557. BN_free(bn);
  33558. }
  33559. for (i = 0; i < 10; i++) {
  33560. AssertNotNull(bn = BN_new());
  33561. AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
  33562. AssertIntGT(BN_bn2bin(bn, bin),0);
  33563. AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
  33564. BN_free(bn);
  33565. }
  33566. printf(resultFmt, passed);
  33567. #endif
  33568. return 0;
  33569. }
  33570. static int test_wolfSSL_PKCS8_Compat(void)
  33571. {
  33572. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  33573. #ifndef NO_BIO
  33574. PKCS8_PRIV_KEY_INFO* pt;
  33575. BIO* bio;
  33576. XFILE f;
  33577. int bytes;
  33578. char pkcs8_buffer[512];
  33579. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33580. EVP_PKEY *pkey = NULL;
  33581. #endif
  33582. printf(testingFmt, "wolfSSL_pkcs8()");
  33583. /* file from wolfssl/certs/ directory */
  33584. f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
  33585. AssertTrue(f != XBADFILE);
  33586. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
  33587. XFCLOSE(f);
  33588. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33589. AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  33590. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  33591. AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
  33592. AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  33593. /* gets PKCS8 pointer to pkey */
  33594. AssertNotNull(EVP_PKEY2PKCS8(pkey));
  33595. EVP_PKEY_free(pkey);
  33596. #endif
  33597. BIO_free(bio);
  33598. PKCS8_PRIV_KEY_INFO_free(pt);
  33599. printf(resultFmt, passed);
  33600. #endif
  33601. #endif
  33602. return 0;
  33603. }
  33604. static int test_wolfSSL_PKCS8_d2i(void)
  33605. {
  33606. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  33607. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  33608. * requires a 12 byte password in FIPS mode. This test ends up
  33609. * trying to use an 8 byte password. */
  33610. #ifndef NO_FILESYSTEM
  33611. unsigned char pkcs8_buffer[2048];
  33612. const unsigned char* p;
  33613. int bytes;
  33614. XFILE file;
  33615. WOLFSSL_EVP_PKEY* pkey = NULL;
  33616. #ifndef NO_BIO
  33617. BIO* bio;
  33618. #if defined(OPENSSL_ALL) && \
  33619. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  33620. defined(HAVE_ECC)) && \
  33621. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33622. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  33623. #endif
  33624. #endif
  33625. #ifndef NO_RSA
  33626. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  33627. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  33628. #ifndef NO_DES3
  33629. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  33630. #endif
  33631. #endif /* NO_RSA */
  33632. #ifdef HAVE_ECC
  33633. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  33634. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  33635. #ifndef NO_DES3
  33636. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  33637. #endif
  33638. #endif /* HAVE_ECC */
  33639. #endif /* !NO_FILESYSTEM */
  33640. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  33641. #ifndef NO_RSA
  33642. #ifdef USE_CERT_BUFFERS_1024
  33643. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  33644. int rsaSz = sizeof_server_key_der_1024;
  33645. #else
  33646. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  33647. int rsaSz = sizeof_server_key_der_2048;
  33648. #endif
  33649. #endif
  33650. #ifdef HAVE_ECC
  33651. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  33652. int ecSz = sizeof_ecc_key_der_256;
  33653. #endif
  33654. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  33655. #ifndef NO_FILESYSTEM
  33656. (void)pkcs8_buffer;
  33657. (void)p;
  33658. (void)bytes;
  33659. (void)file;
  33660. #ifndef NO_BIO
  33661. (void)bio;
  33662. #endif
  33663. #endif
  33664. #ifdef OPENSSL_ALL
  33665. #ifndef NO_RSA
  33666. /* Try to auto-detect normal RSA private key */
  33667. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  33668. EVP_PKEY_free(pkey);
  33669. #endif
  33670. #ifdef HAVE_ECC
  33671. /* Try to auto-detect normal EC private key */
  33672. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  33673. EVP_PKEY_free(pkey);
  33674. #endif
  33675. #endif /* OPENSSL_ALL */
  33676. #ifndef NO_FILESYSTEM
  33677. #ifndef NO_RSA
  33678. /* Get DER encoded RSA PKCS#8 data. */
  33679. file = XFOPEN(rsaDerPkcs8File, "rb");
  33680. AssertTrue(file != XBADFILE);
  33681. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33682. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33683. file)), 0);
  33684. XFCLOSE(file);
  33685. p = pkcs8_buffer;
  33686. #ifdef OPENSSL_ALL
  33687. /* Try to decode - auto-detect key type. */
  33688. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33689. #else
  33690. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  33691. #endif
  33692. /* Get PEM encoded RSA PKCS#8 data. */
  33693. file = XFOPEN(rsaPemPkcs8File, "rb");
  33694. AssertTrue(file != XBADFILE);
  33695. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33696. file)), 0);
  33697. XFCLOSE(file);
  33698. #if defined(OPENSSL_ALL) && \
  33699. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33700. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33701. /* Write PKCS#8 PEM to BIO. */
  33702. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33703. NULL), bytes);
  33704. /* Compare file and written data */
  33705. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33706. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33707. BIO_free(bio);
  33708. #ifndef NO_DES3
  33709. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33710. /* Write Encrypted PKCS#8 PEM to BIO. */
  33711. bytes = 1834;
  33712. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  33713. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33714. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33715. (void*)"yassl123"));
  33716. EVP_PKEY_free(evpPkey);
  33717. BIO_free(bio);
  33718. #endif /* !NO_DES3 */
  33719. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33720. EVP_PKEY_free(pkey);
  33721. /* PKCS#8 encrypted RSA key */
  33722. #ifndef NO_DES3
  33723. file = XFOPEN(rsaDerPkcs8EncFile, "rb");
  33724. AssertTrue(file != XBADFILE);
  33725. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33726. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33727. file)), 0);
  33728. XFCLOSE(file);
  33729. #if defined(OPENSSL_ALL) && \
  33730. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33731. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33732. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33733. (void*)"yassl123"));
  33734. EVP_PKEY_free(pkey);
  33735. BIO_free(bio);
  33736. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33737. #endif /* !NO_DES3 */
  33738. #endif /* NO_RSA */
  33739. #ifdef HAVE_ECC
  33740. /* PKCS#8 encode EC key */
  33741. file = XFOPEN(ecDerPkcs8File, "rb");
  33742. AssertTrue(file != XBADFILE);
  33743. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33744. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33745. file)), 0);
  33746. XFCLOSE(file);
  33747. p = pkcs8_buffer;
  33748. #ifdef OPENSSL_ALL
  33749. /* Try to decode - auto-detect key type. */
  33750. AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  33751. #else
  33752. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  33753. #endif
  33754. /* Get PEM encoded RSA PKCS#8 data. */
  33755. file = XFOPEN(ecPemPkcs8File, "rb");
  33756. AssertTrue(file != XBADFILE);
  33757. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33758. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33759. file)), 0);
  33760. XFCLOSE(file);
  33761. #if defined(OPENSSL_ALL) && \
  33762. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  33763. defined(HAVE_AES_CBC)
  33764. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33765. /* Write PKCS#8 PEM to BIO. */
  33766. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  33767. NULL), bytes);
  33768. /* Compare file and written data */
  33769. AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
  33770. AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  33771. BIO_free(bio);
  33772. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33773. /* Write Encrypted PKCS#8 PEM to BIO. */
  33774. bytes = 379;
  33775. AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  33776. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  33777. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  33778. (void*)"yassl123"));
  33779. EVP_PKEY_free(evpPkey);
  33780. BIO_free(bio);
  33781. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  33782. EVP_PKEY_free(pkey);
  33783. /* PKCS#8 encrypted EC key */
  33784. #ifndef NO_DES3
  33785. file = XFOPEN(ecDerPkcs8EncFile, "rb");
  33786. AssertTrue(file != XBADFILE);
  33787. XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
  33788. AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  33789. file)), 0);
  33790. XFCLOSE(file);
  33791. #if defined(OPENSSL_ALL) && \
  33792. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  33793. AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  33794. AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  33795. (void*)"yassl123"));
  33796. EVP_PKEY_free(pkey);
  33797. BIO_free(bio);
  33798. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  33799. #endif /* !NO_DES3 */
  33800. #endif /* HAVE_ECC */
  33801. #endif /* !NO_FILESYSTEM */
  33802. printf(resultFmt, passed);
  33803. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  33804. return 0;
  33805. }
  33806. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33807. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33808. #define LOGGING_THREADS 5
  33809. #define ERROR_COUNT 10
  33810. static volatile int loggingThreadsReady;
  33811. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  33812. {
  33813. const char* file;
  33814. int line;
  33815. int err;
  33816. int errorCount = 0;
  33817. int i;
  33818. (void)args;
  33819. while (!loggingThreadsReady);
  33820. for (i = 0; i < ERROR_COUNT; i++)
  33821. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33822. while ((err = ERR_get_error_line(&file, &line))) {
  33823. AssertIntEQ(err, 990 + errorCount);
  33824. errorCount++;
  33825. }
  33826. AssertIntEQ(errorCount, ERROR_COUNT);
  33827. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  33828. errorCount = 0;
  33829. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  33830. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  33831. while ((err = ERR_get_error_line(&file, &line))) {
  33832. AssertIntEQ(err, 990 + errorCount);
  33833. errorCount++;
  33834. }
  33835. /* test that the 3 errors over the max were dropped */
  33836. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  33837. return 0;
  33838. }
  33839. #endif
  33840. static int test_error_queue_per_thread(void)
  33841. {
  33842. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  33843. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  33844. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  33845. int i;
  33846. printf(testingFmt, "error_queue_per_thread()");
  33847. ERR_clear_error(); /* clear out any error nodes */
  33848. loggingThreadsReady = 0;
  33849. for (i = 0; i < LOGGING_THREADS; i++)
  33850. start_thread(test_logging, NULL, &loggingThreads[i]);
  33851. loggingThreadsReady = 1;
  33852. for (i = 0; i < LOGGING_THREADS; i++)
  33853. join_thread(loggingThreads[i]);
  33854. printf(resultFmt, passed);
  33855. #endif
  33856. return 0;
  33857. }
  33858. static int test_wolfSSL_ERR_put_error(void)
  33859. {
  33860. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33861. defined(DEBUG_WOLFSSL)
  33862. const char* file;
  33863. int line;
  33864. printf(testingFmt, "wolfSSL_ERR_put_error()");
  33865. ERR_clear_error(); /* clear out any error nodes */
  33866. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33867. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33868. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  33869. AssertIntEQ(ERR_get_error_line(&file, &line), 1);
  33870. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  33871. AssertIntEQ(ERR_get_error_line(&file, &line), 2);
  33872. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  33873. AssertIntEQ(ERR_get_error_line(&file, &line), 3);
  33874. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  33875. AssertIntEQ(ERR_get_error_line(&file, &line), 4);
  33876. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  33877. AssertIntEQ(ERR_get_error_line(&file, &line), 5);
  33878. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  33879. AssertIntEQ(ERR_get_error_line(&file, &line), 6);
  33880. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  33881. AssertIntEQ(ERR_get_error_line(&file, &line), 7);
  33882. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  33883. AssertIntEQ(ERR_get_error_line(&file, &line), 8);
  33884. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  33885. AssertIntEQ(ERR_get_error_line(&file, &line), 9);
  33886. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  33887. AssertIntEQ(ERR_get_error_line(&file, &line), 10);
  33888. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  33889. AssertIntEQ(ERR_get_error_line(&file, &line), 11);
  33890. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  33891. AssertIntEQ(ERR_get_error_line(&file, &line), 12);
  33892. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  33893. AssertIntEQ(ERR_get_error_line(&file, &line), 13);
  33894. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  33895. AssertIntEQ(ERR_get_error_line(&file, &line), 14);
  33896. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  33897. AssertIntEQ(ERR_get_error_line(&file, &line), 15);
  33898. #ifdef WOLFSSL_PYTHON
  33899. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  33900. "this file", 100);
  33901. AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  33902. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33903. AssertIntEQ(line, 100);
  33904. AssertIntEQ(wolfSSL_ERR_peek_error(),
  33905. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  33906. AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  33907. #endif
  33908. /* try reading past end of error queue */
  33909. file = NULL;
  33910. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33911. AssertNull(file);
  33912. AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  33913. PEMerr(4,4);
  33914. AssertIntEQ(ERR_get_error(), 4);
  33915. /* Empty and free up all error nodes */
  33916. ERR_clear_error();
  33917. /* Verify all nodes are cleared */
  33918. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  33919. ERR_clear_error();
  33920. AssertIntEQ(ERR_get_error_line(&file, &line), 0);
  33921. printf(resultFmt, passed);
  33922. #endif
  33923. return 0;
  33924. }
  33925. /*
  33926. * This is a regression test for a bug where the peek/get error functions were
  33927. * drawing from the end of the queue rather than the front.
  33928. */
  33929. static int test_wolfSSL_ERR_get_error_order(void)
  33930. {
  33931. #ifdef WOLFSSL_HAVE_ERROR_QUEUE
  33932. printf(testingFmt, "test_wolfSSL_ERR_get_error_order");
  33933. /* Empty the queue. */
  33934. wolfSSL_ERR_clear_error();
  33935. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  33936. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  33937. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  33938. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  33939. AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  33940. AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  33941. printf(resultFmt, passed);
  33942. #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
  33943. return 0;
  33944. }
  33945. #ifndef NO_BIO
  33946. static int test_wolfSSL_ERR_print_errors(void)
  33947. {
  33948. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33949. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  33950. BIO* bio;
  33951. char buf[1024];
  33952. printf(testingFmt, "wolfSSL_ERR_print_errors()");
  33953. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33954. ERR_clear_error(); /* clear out any error nodes */
  33955. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33956. /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
  33957. ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
  33958. ERR_print_errors(bio);
  33959. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  33960. AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
  33961. buf, 55), 0);
  33962. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  33963. AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
  33964. buf, 56), 0);
  33965. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  33966. AssertIntEQ(buf[0], '\0');
  33967. AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
  33968. BIO_free(bio);
  33969. printf(resultFmt, passed);
  33970. #endif
  33971. return 0;
  33972. }
  33973. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33974. defined(DEBUG_WOLFSSL)
  33975. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  33976. {
  33977. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  33978. return 0;
  33979. }
  33980. #endif
  33981. static int test_wolfSSL_ERR_print_errors_cb(void)
  33982. {
  33983. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  33984. defined(DEBUG_WOLFSSL)
  33985. BIO* bio;
  33986. char buf[1024];
  33987. printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
  33988. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  33989. ERR_clear_error(); /* clear out any error nodes */
  33990. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  33991. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  33992. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  33993. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  33994. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  33995. buf, 53), 0);
  33996. AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
  33997. buf + 53, 55), 0);
  33998. AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  33999. BIO_free(bio);
  34000. printf(resultFmt, passed);
  34001. #endif
  34002. return 0;
  34003. }
  34004. /*
  34005. * Testing WOLFSSL_ERROR_MSG
  34006. */
  34007. static int test_WOLFSSL_ERROR_MSG(void)
  34008. {
  34009. int ret = 0;
  34010. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  34011. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  34012. const char* msg = TEST_STRING;
  34013. printf(testingFmt, "WOLFSSL_ERROR_MSG()");
  34014. WOLFSSL_ERROR_MSG(msg);
  34015. printf(resultFmt, ret == 0 ? passed : failed);
  34016. fflush(stdout);
  34017. #endif
  34018. return ret;
  34019. }/*End test_WOLFSSL_ERROR_MSG*/
  34020. /*
  34021. * Testing wc_ERR_remove_state
  34022. */
  34023. static int test_wc_ERR_remove_state(void)
  34024. {
  34025. int ret = 0;
  34026. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34027. printf(testingFmt, "wc_ERR_remove_state()");
  34028. wc_ERR_remove_state();
  34029. printf(resultFmt, ret == 0 ? passed : failed);
  34030. fflush(stdout);
  34031. #endif
  34032. return ret;
  34033. }/*End test_wc_ERR_remove_state*/
  34034. /*
  34035. * Testing wc_ERR_print_errors_fp
  34036. */
  34037. static int test_wc_ERR_print_errors_fp(void)
  34038. {
  34039. int ret = 0;
  34040. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  34041. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  34042. long sz;
  34043. XFILE fp;
  34044. printf(testingFmt, "wc_ERR_print_errors_fp()");
  34045. WOLFSSL_ERROR(BAD_FUNC_ARG);
  34046. fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
  34047. wc_ERR_print_errors_fp(fp);
  34048. #if defined(DEBUG_WOLFSSL)
  34049. AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  34050. sz = XFTELL(fp);
  34051. #ifdef NO_ERROR_QUEUE
  34052. /* File should be empty when NO_ERROR_QUEUE is defined */
  34053. if (sz != 0) {
  34054. ret = BAD_FUNC_ARG;
  34055. }
  34056. #else
  34057. if (sz == 0) {
  34058. ret = BAD_FUNC_ARG;
  34059. }
  34060. #endif
  34061. #endif
  34062. printf(resultFmt, ret == 0 ? passed : failed);
  34063. fflush(stdout);
  34064. XFCLOSE(fp);
  34065. (void)sz;
  34066. #endif
  34067. return ret;
  34068. }/*End test_wc_ERR_print_errors_fp*/
  34069. #ifdef DEBUG_WOLFSSL
  34070. static void Logging_cb(const int logLevel, const char *const logMessage)
  34071. {
  34072. (void)logLevel;
  34073. (void)logMessage;
  34074. }
  34075. #endif
  34076. /*
  34077. * Testing wolfSSL_GetLoggingCb
  34078. */
  34079. static int test_wolfSSL_GetLoggingCb(void)
  34080. {
  34081. int ret = 0;
  34082. printf(testingFmt, "wolfSSL_GetLoggingCb()");
  34083. #ifdef DEBUG_WOLFSSL
  34084. /* Testing without wolfSSL_SetLoggingCb() */
  34085. if (ret == 0) {
  34086. if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
  34087. ret = 0;
  34088. }
  34089. if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
  34090. ret = -1;
  34091. }
  34092. }
  34093. /* Testing with wolfSSL_SetLoggingCb() */
  34094. if (ret == 0) {
  34095. ret = wolfSSL_SetLoggingCb(Logging_cb);
  34096. if (ret == 0){
  34097. if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
  34098. ret = -1;
  34099. }
  34100. if (ret == 0) {
  34101. if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
  34102. ret = 0;
  34103. }
  34104. }
  34105. /* reset logging callback */
  34106. wolfSSL_SetLoggingCb(NULL);
  34107. }
  34108. }
  34109. #endif
  34110. if (ret == 0) {
  34111. if (wolfSSL_GetLoggingCb() != NULL) {
  34112. ret = -1;
  34113. }
  34114. }
  34115. printf(resultFmt, ret == 0 ? passed : failed);
  34116. fflush(stdout);
  34117. return ret;
  34118. }/*End test_wolfSSL_GetLoggingCb*/
  34119. #endif /* !NO_BIO */
  34120. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34121. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34122. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34123. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  34124. {
  34125. static const unsigned char key[] = "simple test key";
  34126. HMAC_CTX* hmac;
  34127. ENGINE* e = NULL;
  34128. unsigned char hash[WC_MAX_DIGEST_SIZE];
  34129. unsigned int len;
  34130. AssertNotNull(hmac = HMAC_CTX_new());
  34131. HMAC_CTX_init(hmac);
  34132. AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
  34133. SSL_SUCCESS);
  34134. /* re-using test key as data to hash */
  34135. AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
  34136. AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
  34137. AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
  34138. AssertIntEQ(len, md_len);
  34139. AssertIntEQ(HMAC_size(hmac), md_len);
  34140. AssertStrEQ(HMAC_CTX_get_md(hmac), md);
  34141. HMAC_cleanup(hmac);
  34142. HMAC_CTX_free(hmac);
  34143. len = 0;
  34144. AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  34145. AssertIntEQ(len, md_len);
  34146. return 0;
  34147. }
  34148. #endif
  34149. static int test_wolfSSL_HMAC(void)
  34150. {
  34151. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  34152. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  34153. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  34154. printf(testingFmt, "wolfSSL_HMAC()");
  34155. #ifndef NO_SHA256
  34156. test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
  34157. #endif
  34158. #ifdef WOLFSSL_SHA224
  34159. test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
  34160. #endif
  34161. #ifdef WOLFSSL_SHA384
  34162. test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
  34163. #endif
  34164. #ifdef WOLFSSL_SHA512
  34165. test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
  34166. #endif
  34167. #ifdef WOLFSSL_SHA3
  34168. #ifndef WOLFSSL_NOSHA3_224
  34169. test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
  34170. #endif
  34171. #ifndef WOLFSSL_NOSHA3_256
  34172. test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
  34173. #endif
  34174. #ifndef WOLFSSL_NOSHA3_384
  34175. test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
  34176. #endif
  34177. #ifndef WOLFSSL_NOSHA3_512
  34178. test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
  34179. #endif
  34180. #endif
  34181. #ifndef NO_SHA
  34182. test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
  34183. #endif
  34184. printf(resultFmt, passed);
  34185. #endif
  34186. return 0;
  34187. }
  34188. static int test_wolfSSL_CMAC(void)
  34189. {
  34190. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  34191. defined(WOLFSSL_AES_DIRECT)
  34192. int i;
  34193. byte key[AES_128_KEY_SIZE];
  34194. CMAC_CTX* cmacCtx = NULL;
  34195. byte out[AES_BLOCK_SIZE];
  34196. size_t outLen = AES_BLOCK_SIZE;
  34197. printf(testingFmt, "test_wolfSSL_CMAC()");
  34198. for (i=0; i < AES_128_KEY_SIZE; ++i) {
  34199. key[i] = i;
  34200. }
  34201. AssertNotNull(cmacCtx = CMAC_CTX_new());
  34202. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  34203. AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  34204. AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  34205. NULL), SSL_SUCCESS);
  34206. /* re-using test key as data to hash */
  34207. AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
  34208. AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
  34209. AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
  34210. AssertIntEQ(outLen, AES_BLOCK_SIZE);
  34211. CMAC_CTX_free(cmacCtx);
  34212. printf(resultFmt, passed);
  34213. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  34214. return 0;
  34215. }
  34216. static int test_wolfSSL_OBJ(void)
  34217. {
  34218. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  34219. * mode
  34220. */
  34221. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  34222. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  34223. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
  34224. ASN1_OBJECT *obj = NULL;
  34225. ASN1_OBJECT *obj2 = NULL;
  34226. char buf[50];
  34227. XFILE fp;
  34228. X509 *x509 = NULL;
  34229. X509_NAME *x509Name;
  34230. X509_NAME_ENTRY *x509NameEntry;
  34231. ASN1_OBJECT *asn1Name = NULL;
  34232. int numNames;
  34233. BIO *bio = NULL;
  34234. int nid;
  34235. int i, j;
  34236. const char *f[] = {
  34237. #ifndef NO_RSA
  34238. "./certs/ca-cert.der",
  34239. #endif
  34240. #ifdef HAVE_ECC
  34241. "./certs/ca-ecc-cert.der",
  34242. "./certs/ca-ecc384-cert.der",
  34243. #endif
  34244. NULL};
  34245. ASN1_OBJECT *field_name_obj = NULL;
  34246. int lastpos = -1;
  34247. int tmp = -1;
  34248. ASN1_STRING *asn1 = NULL;
  34249. unsigned char *buf_dyn = NULL;
  34250. printf(testingFmt, "wolfSSL_OBJ()");
  34251. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  34252. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34253. AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  34254. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  34255. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34256. ASN1_OBJECT_free(obj);
  34257. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34258. AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
  34259. AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  34260. #ifdef WOLFSSL_CERT_EXT
  34261. AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
  34262. #endif
  34263. AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  34264. AssertNotNull(obj2 = OBJ_dup(obj));
  34265. AssertIntEQ(OBJ_cmp(obj, obj2), 0);
  34266. ASN1_OBJECT_free(obj);
  34267. ASN1_OBJECT_free(obj2);
  34268. for (i = 0; f[i] != NULL; i++)
  34269. {
  34270. AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  34271. AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
  34272. XFCLOSE(fp);
  34273. AssertNotNull(x509Name = X509_get_issuer_name(x509));
  34274. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34275. /* Get the Common Name by using OBJ_txt2obj */
  34276. AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  34277. do
  34278. {
  34279. lastpos = tmp;
  34280. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  34281. } while (tmp > -1);
  34282. AssertIntNE(lastpos, -1);
  34283. ASN1_OBJECT_free(field_name_obj);
  34284. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  34285. AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  34286. AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  34287. /*
  34288. * All Common Names should be www.wolfssl.com
  34289. * This makes testing easier as we can test for the expected value.
  34290. */
  34291. AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
  34292. OPENSSL_free(buf_dyn);
  34293. bio = BIO_new(BIO_s_mem());
  34294. AssertTrue(bio != NULL);
  34295. for (j = 0; j < numNames; j++)
  34296. {
  34297. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34298. AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  34299. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34300. }
  34301. BIO_free(bio);
  34302. X509_free(x509);
  34303. }
  34304. #ifdef HAVE_PKCS12
  34305. {
  34306. PKCS12 *p12;
  34307. int boolRet;
  34308. EVP_PKEY *pkey = NULL;
  34309. const char *p12_f[] = {
  34310. #if !defined(NO_DES3) && !defined(NO_RSA)
  34311. "./certs/test-servercert.p12",
  34312. #endif
  34313. NULL};
  34314. for (i = 0; p12_f[i] != NULL; i++)
  34315. {
  34316. AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  34317. AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  34318. XFCLOSE(fp);
  34319. AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  34320. &pkey, &x509, NULL)) > 0);
  34321. wc_PKCS12_free(p12);
  34322. EVP_PKEY_free(pkey);
  34323. x509Name = X509_get_issuer_name(x509);
  34324. AssertNotNull(x509Name);
  34325. AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  34326. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34327. for (j = 0; j < numNames; j++)
  34328. {
  34329. AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  34330. AssertNotNull(asn1Name =
  34331. X509_NAME_ENTRY_get_object(x509NameEntry));
  34332. AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  34333. }
  34334. BIO_free(bio);
  34335. X509_free(x509);
  34336. }
  34337. }
  34338. #endif /* HAVE_PKCS12 */
  34339. printf(resultFmt, passed);
  34340. #endif
  34341. return 0;
  34342. }
  34343. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  34344. {
  34345. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  34346. ASN1_OBJECT *obj = NULL;
  34347. BIO *bio = NULL;
  34348. AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
  34349. AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  34350. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  34351. AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  34352. AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  34353. BIO_free(bio);
  34354. ASN1_OBJECT_free(obj);
  34355. #endif
  34356. return 0;
  34357. }
  34358. static int test_wolfSSL_OBJ_cmp(void)
  34359. {
  34360. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  34361. ASN1_OBJECT *obj = NULL;
  34362. ASN1_OBJECT *obj2 = NULL;
  34363. printf(testingFmt, "wolfSSL_OBJ_cmp()");
  34364. AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
  34365. AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  34366. AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  34367. AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  34368. AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  34369. AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  34370. AssertIntEQ(OBJ_cmp(obj, obj), 0);
  34371. AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
  34372. ASN1_OBJECT_free(obj);
  34373. ASN1_OBJECT_free(obj2);
  34374. printf(resultFmt, passed);
  34375. #endif
  34376. return 0;
  34377. }
  34378. static int test_wolfSSL_OBJ_txt2nid(void)
  34379. {
  34380. #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
  34381. int i;
  34382. static const struct {
  34383. const char* sn;
  34384. const char* ln;
  34385. const char* oid;
  34386. int nid;
  34387. } testVals[] = {
  34388. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  34389. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  34390. NID_id_on_dnsSRV },
  34391. { "msUPN", "Microsoft User Principal Name",
  34392. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  34393. { NULL, NULL, NULL, NID_undef }
  34394. };
  34395. printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
  34396. /* Invalid cases */
  34397. AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
  34398. AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  34399. /* Valid cases */
  34400. for (i = 0; testVals[i].sn != NULL; i++) {
  34401. AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  34402. AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  34403. AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  34404. }
  34405. printf(resultFmt, passed);
  34406. #endif
  34407. return 0;
  34408. }
  34409. static int test_wolfSSL_OBJ_txt2obj(void)
  34410. {
  34411. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  34412. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  34413. int i;
  34414. char buf[50];
  34415. ASN1_OBJECT* obj;
  34416. static const struct {
  34417. const char* oidStr;
  34418. const char* sn;
  34419. const char* ln;
  34420. } objs_list[] = {
  34421. #if defined(WOLFSSL_APACHE_HTTPD)
  34422. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  34423. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  34424. #endif
  34425. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  34426. { NULL, NULL, NULL }
  34427. };
  34428. static const struct {
  34429. const char* numeric;
  34430. const char* name;
  34431. } objs_named[] = {
  34432. /* In dictionary but not in normal list. */
  34433. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  34434. /* Made up OID. */
  34435. { "1.3.5.7", "1.3.5.7" },
  34436. { NULL, NULL }
  34437. };
  34438. printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
  34439. AssertNull(obj = OBJ_txt2obj("Bad name", 0));
  34440. AssertNull(obj = OBJ_txt2obj(NULL, 0));
  34441. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  34442. /* Test numerical value of oid (oidStr) */
  34443. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  34444. /* Convert object back to text to confirm oid is correct */
  34445. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34446. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34447. ASN1_OBJECT_free(obj);
  34448. XMEMSET(buf, 0, sizeof(buf));
  34449. /* Test short name (sn) */
  34450. AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  34451. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  34452. /* Convert object back to text to confirm oid is correct */
  34453. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34454. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34455. ASN1_OBJECT_free(obj);
  34456. XMEMSET(buf, 0, sizeof(buf));
  34457. /* Test long name (ln) - should fail when no_name = 1 */
  34458. AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  34459. AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  34460. /* Convert object back to text to confirm oid is correct */
  34461. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34462. AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  34463. ASN1_OBJECT_free(obj);
  34464. XMEMSET(buf, 0, sizeof(buf));
  34465. }
  34466. for (i = 0; objs_named[i].numeric != NULL; i++) {
  34467. AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  34468. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  34469. AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  34470. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  34471. AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  34472. ASN1_OBJECT_free(obj);
  34473. }
  34474. printf(resultFmt, passed);
  34475. #endif
  34476. return 0;
  34477. }
  34478. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  34479. {
  34480. #if defined(OPENSSL_EXTRA) && \
  34481. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  34482. char buf[50] = {0};
  34483. ASN1_OBJECT* obj;
  34484. const char* oid = "2.5.29.19";
  34485. const char* ln = "X509v3 Basic Constraints";
  34486. printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
  34487. obj = NULL;
  34488. AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
  34489. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
  34490. AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
  34491. AssertNotNull(obj = OBJ_txt2obj(oid, 0));
  34492. XMEMSET(buf, 0, sizeof(buf));
  34493. AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  34494. AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  34495. ASN1_OBJECT_free(obj);
  34496. printf(resultFmt, passed);
  34497. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  34498. return 0;
  34499. }
  34500. static int test_wolfSSL_PEM_write_bio_X509(void)
  34501. {
  34502. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
  34503. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
  34504. !defined(NO_BIO) && !defined(NO_RSA)
  34505. /* This test contains the hard coded expected
  34506. * lengths. Update if necessary */
  34507. BIO* input;
  34508. BIO* output;
  34509. X509* x509a = NULL;
  34510. X509* x509b = NULL;
  34511. ASN1_TIME* notBeforeA = NULL;
  34512. ASN1_TIME* notAfterA = NULL;
  34513. ASN1_TIME* notBeforeB = NULL;
  34514. ASN1_TIME* notAfterB = NULL;
  34515. int expectedLen;
  34516. printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
  34517. AssertNotNull(input = BIO_new_file(
  34518. "certs/test/cert-ext-multiple.pem", "rb"));
  34519. AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
  34520. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  34521. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34522. AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
  34523. AssertNotNull(notAfterA = X509_get_notAfter(x509a));
  34524. /* write X509 back to PEM BIO */
  34525. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34526. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34527. /* compare length against expected */
  34528. expectedLen = 2000;
  34529. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34530. /* read exported X509 PEM back into struct, sanity check on export,
  34531. * make sure notBefore/notAfter are the same. */
  34532. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34533. AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
  34534. AssertNotNull(notAfterB = X509_get_notAfter(x509b));
  34535. AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  34536. AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  34537. X509_free(x509b);
  34538. /* Reset output buffer */
  34539. BIO_free(output);
  34540. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34541. /* Test forcing the AKID to be generated just from KeyIdentifier */
  34542. if (x509a->authKeyIdSrc != NULL) {
  34543. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  34544. x509a->authKeyId = x509a->authKeyIdSrc;
  34545. x509a->authKeyIdSrc = NULL;
  34546. x509a->authKeyIdSrcSz = 0;
  34547. }
  34548. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34549. /* Check that we generate a smaller output since the AKID will
  34550. * only contain the KeyIdentifier without any additional
  34551. * information */
  34552. /* Here we copy the validity struct from the original */
  34553. expectedLen = 1688;
  34554. AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  34555. /* Reset buffers and x509 */
  34556. BIO_free(input);
  34557. BIO_free(output);
  34558. X509_free(x509a);
  34559. /* test CA and basicConstSet values are encoded when
  34560. * the cert is a CA */
  34561. AssertNotNull(input = BIO_new_file(
  34562. "certs/server-cert.pem", "rb"));
  34563. /* read PEM into X509 struct */
  34564. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34565. /* write X509 back to PEM BIO */
  34566. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34567. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34568. /* read exported X509 PEM back into struct, ensure isCa and
  34569. * basicConstSet values are maintained */
  34570. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34571. AssertIntEQ(x509b->isCa, 1);
  34572. AssertIntEQ(x509b->basicConstSet, 1);
  34573. X509_free(x509a);
  34574. X509_free(x509b);
  34575. BIO_free(input);
  34576. BIO_free(output);
  34577. /* test CA and basicConstSet values are encoded when
  34578. * the cert is not CA */
  34579. AssertNotNull(input = BIO_new_file(
  34580. "certs/client-uri-cert.pem", "rb"));
  34581. /* read PEM into X509 struct */
  34582. AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  34583. /* write X509 back to PEM BIO */
  34584. AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  34585. AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  34586. /* read exported X509 PEM back into struct, ensure isCa and
  34587. * basicConstSet values are maintained */
  34588. AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  34589. AssertIntEQ(x509b->isCa, 0);
  34590. AssertIntEQ(x509b->basicConstSet, 1);
  34591. X509_free(x509a);
  34592. X509_free(x509b);
  34593. BIO_free(input);
  34594. BIO_free(output);
  34595. printf(resultFmt, passed);
  34596. #endif
  34597. return 0;
  34598. }
  34599. static int test_wolfSSL_X509_NAME_ENTRY(void)
  34600. {
  34601. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  34602. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  34603. X509* x509;
  34604. #ifndef NO_BIO
  34605. BIO* bio;
  34606. #endif
  34607. X509_NAME* nm;
  34608. X509_NAME_ENTRY* entry;
  34609. unsigned char cn[] = "another name to add";
  34610. #ifdef OPENSSL_ALL
  34611. int i, names_len;
  34612. #endif
  34613. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
  34614. AssertNotNull(x509 =
  34615. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34616. #ifndef NO_BIO
  34617. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34618. AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  34619. #endif
  34620. #ifdef WOLFSSL_CERT_REQ
  34621. {
  34622. X509_REQ* req;
  34623. #ifndef NO_BIO
  34624. BIO* bReq;
  34625. #endif
  34626. AssertNotNull(req =
  34627. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  34628. #ifndef NO_BIO
  34629. AssertNotNull(bReq = BIO_new(BIO_s_mem()));
  34630. AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  34631. BIO_free(bReq);
  34632. #endif
  34633. X509_free(req);
  34634. }
  34635. #endif
  34636. AssertNotNull(nm = X509_get_subject_name(x509));
  34637. /* Test add entry */
  34638. AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  34639. 0x0c, cn, (int)sizeof(cn)));
  34640. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34641. #ifdef WOLFSSL_CERT_EXT
  34642. AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  34643. (byte*)"support@wolfssl.com", 19, -1,
  34644. 1), WOLFSSL_SUCCESS);
  34645. #endif
  34646. X509_NAME_ENTRY_free(entry);
  34647. #ifdef WOLFSSL_CERT_REQ
  34648. {
  34649. unsigned char srv_pkcs9p[] = "Server";
  34650. char* subject;
  34651. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  34652. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  34653. subject = X509_NAME_oneline(nm, 0, 0);
  34654. #ifdef DEBUG_WOLFSSL
  34655. printf("\n\t%s\n", subject);
  34656. #endif
  34657. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  34658. }
  34659. #endif
  34660. /* Test add entry by text */
  34661. AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  34662. 0x0c, cn, (int)sizeof(cn)));
  34663. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  34664. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  34665. AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  34666. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  34667. #endif
  34668. AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  34669. X509_NAME_ENTRY_free(entry);
  34670. /* Test add entry by NID */
  34671. AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  34672. cn, -1, -1, 0), SSL_SUCCESS);
  34673. #ifdef OPENSSL_ALL
  34674. /* stack of name entry */
  34675. AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  34676. for (i=0; i<names_len; i++) {
  34677. AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  34678. }
  34679. #endif
  34680. #ifndef NO_BIO
  34681. BIO_free(bio);
  34682. #endif
  34683. X509_free(x509); /* free's nm */
  34684. printf(resultFmt, passed);
  34685. #endif
  34686. return 0;
  34687. }
  34688. static int test_wolfSSL_X509_set_name(void)
  34689. {
  34690. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  34691. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34692. X509* x509;
  34693. X509_NAME* name;
  34694. printf(testingFmt, "wolfSSL_X509_set_name()");
  34695. AssertNotNull(name = X509_NAME_new());
  34696. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  34697. (byte*)"wolfssl.com", 11, 0, 1),
  34698. WOLFSSL_SUCCESS);
  34699. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  34700. (byte*)"support@wolfssl.com", 19, -1,
  34701. 1), WOLFSSL_SUCCESS);
  34702. AssertNotNull(x509 = X509_new());
  34703. AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  34704. AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  34705. AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  34706. AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  34707. AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  34708. AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  34709. AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  34710. AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  34711. X509_free(x509);
  34712. X509_NAME_free(name);
  34713. printf(resultFmt, passed);
  34714. #endif /* OPENSSL_ALL && !NO_CERTS */
  34715. return 0;
  34716. }
  34717. static int test_wolfSSL_X509_set_notAfter(void)
  34718. {
  34719. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34720. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34721. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34722. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  34723. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  34724. /* Generalized time will overflow time_t if not long */
  34725. X509* x;
  34726. BIO* bio;
  34727. ASN1_TIME *asn_time, *time_check;
  34728. const int year = 365*24*60*60;
  34729. const int day = 24*60*60;
  34730. const int hour = 60*60;
  34731. const int mini = 60;
  34732. int offset_day;
  34733. unsigned char buf[25];
  34734. time_t t;
  34735. printf(testingFmt, "wolfSSL_X509_set_notAfter()");
  34736. /*
  34737. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34738. */
  34739. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  34740. offset_day = 7;
  34741. /*
  34742. * Free these.
  34743. */
  34744. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34745. AssertNotNull(asn_time);
  34746. AssertNotNull(x = X509_new());
  34747. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34748. /*
  34749. * Tests
  34750. */
  34751. AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  34752. /* time_check is simply (ANS1_TIME*)x->notAfter */
  34753. AssertNotNull(time_check = X509_get_notAfter(x));
  34754. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34755. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34756. /* Convert to human readable format and compare to intended date */
  34757. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34758. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34759. AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  34760. /*
  34761. * Cleanup
  34762. */
  34763. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  34764. X509_free(x);
  34765. BIO_free(bio);
  34766. printf(resultFmt, passed);
  34767. #endif
  34768. return 0;
  34769. }
  34770. static int test_wolfSSL_X509_set_notBefore(void)
  34771. {
  34772. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  34773. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  34774. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  34775. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  34776. X509* x;
  34777. BIO* bio;
  34778. ASN1_TIME *asn_time, *time_check;
  34779. const int year = 365*24*60*60;
  34780. const int day = 24*60*60;
  34781. const int hour = 60*60;
  34782. const int mini = 60;
  34783. int offset_day;
  34784. unsigned char buf[25];
  34785. time_t t;
  34786. printf(testingFmt, "wolfSSL_X509_set_notBefore()");
  34787. /*
  34788. * Setup asn_time. APACHE HTTPD uses time(NULL)
  34789. */
  34790. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  34791. offset_day = 7;
  34792. /*
  34793. * Free these.
  34794. */
  34795. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  34796. AssertNotNull(asn_time);
  34797. AssertNotNull(x = X509_new());
  34798. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34799. AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  34800. /*
  34801. * Main Tests
  34802. */
  34803. AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  34804. /* time_check == (ANS1_TIME*)x->notBefore */
  34805. AssertNotNull(time_check = X509_get_notBefore(x));
  34806. /* ANS1_TIME_check validates by checking if argument can be parsed */
  34807. AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  34808. /* Convert to human readable format and compare to intended date */
  34809. AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
  34810. AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  34811. AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  34812. /*
  34813. * Cleanup
  34814. */
  34815. XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
  34816. X509_free(x);
  34817. BIO_free(bio);
  34818. printf(resultFmt, passed);
  34819. #endif
  34820. return 0;
  34821. }
  34822. static int test_wolfSSL_X509_set_version(void)
  34823. {
  34824. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34825. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  34826. X509* x509;
  34827. long v = 2L;
  34828. long maxInt = INT_MAX;
  34829. AssertNotNull(x509 = X509_new());
  34830. /* These should pass. */
  34831. AssertTrue(wolfSSL_X509_set_version(x509, v));
  34832. AssertIntEQ(v, wolfSSL_X509_get_version(x509));
  34833. /* Fail Case: When v(long) is greater than x509->version(int). */
  34834. v = maxInt+1;
  34835. AssertFalse(wolfSSL_X509_set_version(x509, v));
  34836. /* Cleanup */
  34837. X509_free(x509);
  34838. printf(resultFmt, passed);
  34839. #endif
  34840. return 0;
  34841. }
  34842. #ifndef NO_BIO
  34843. static int test_wolfSSL_BIO_gets(void)
  34844. {
  34845. #if defined(OPENSSL_EXTRA)
  34846. BIO* bio;
  34847. BIO* bio2;
  34848. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  34849. char emp[] = "";
  34850. char bio_buffer[20];
  34851. int bufferSz = 20;
  34852. printf(testingFmt, "wolfSSL_BIO_gets()");
  34853. /* try with bad args */
  34854. AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  34855. /* try with real msg */
  34856. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  34857. XMEMSET(bio_buffer, 0, bufferSz);
  34858. AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  34859. AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  34860. AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  34861. AssertFalse(bio2 != BIO_next(bio));
  34862. /* make buffer filled with no terminating characters */
  34863. XMEMSET(bio_buffer, 1, bufferSz);
  34864. /* BIO_gets reads a line of data */
  34865. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  34866. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  34867. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  34868. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  34869. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  34870. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  34871. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  34872. /* check not null terminated string */
  34873. BIO_free(bio);
  34874. msg[0] = 0x33;
  34875. msg[1] = 0x33;
  34876. msg[2] = 0x33;
  34877. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  34878. AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  34879. AssertIntEQ(bio_buffer[0], msg[0]);
  34880. AssertIntEQ(bio_buffer[1], msg[1]);
  34881. AssertIntNE(bio_buffer[2], msg[2]);
  34882. BIO_free(bio);
  34883. msg[3] = 0x33;
  34884. bio_buffer[3] = 0x33;
  34885. AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  34886. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  34887. AssertIntEQ(bio_buffer[0], msg[0]);
  34888. AssertIntEQ(bio_buffer[1], msg[1]);
  34889. AssertIntEQ(bio_buffer[2], msg[2]);
  34890. AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  34891. /* check reading an empty string */
  34892. BIO_free(bio);
  34893. AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  34894. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  34895. AssertStrEQ(emp, bio_buffer);
  34896. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  34897. /* check error cases */
  34898. BIO_free(bio);
  34899. AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  34900. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34901. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  34902. #if !defined(NO_FILESYSTEM)
  34903. {
  34904. BIO* f_bio;
  34905. XFILE f;
  34906. AssertNotNull(f_bio = BIO_new(BIO_s_file()));
  34907. AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  34908. f = XFOPEN(svrCertFile, "rb");
  34909. AssertTrue((f != XBADFILE));
  34910. AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  34911. AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  34912. BIO_free(f_bio);
  34913. }
  34914. #endif /* NO_FILESYSTEM */
  34915. BIO_free(bio);
  34916. BIO_free(bio2);
  34917. /* try with type BIO */
  34918. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  34919. sizeof(msg));
  34920. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  34921. AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  34922. AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
  34923. AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  34924. AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  34925. AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  34926. AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  34927. AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  34928. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  34929. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  34930. AssertStrEQ(bio_buffer, "hello wolfSSL\n");
  34931. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  34932. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  34933. AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  34934. BIO_free(bio);
  34935. BIO_free(bio2);
  34936. /* check reading an empty string */
  34937. AssertNotNull(bio = BIO_new(BIO_s_bio()));
  34938. AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  34939. AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  34940. AssertStrEQ(emp, bio_buffer);
  34941. BIO_free(bio);
  34942. printf(resultFmt, passed);
  34943. #endif
  34944. return 0;
  34945. }
  34946. static int test_wolfSSL_BIO_puts(void)
  34947. {
  34948. #if defined(OPENSSL_EXTRA)
  34949. BIO* bio;
  34950. char input[] = "hello\0world\n.....ok\n\0";
  34951. char output[128];
  34952. printf(testingFmt, "wolfSSL_BIO_puts()");
  34953. XMEMSET(output, 0, sizeof(output));
  34954. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  34955. AssertIntEQ(BIO_puts(bio, input), 5);
  34956. AssertIntEQ(BIO_pending(bio), 5);
  34957. AssertIntEQ(BIO_puts(bio, input + 6), 14);
  34958. AssertIntEQ(BIO_pending(bio), 19);
  34959. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  34960. AssertStrEQ(output, "helloworld\n");
  34961. AssertIntEQ(BIO_pending(bio), 8);
  34962. AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  34963. AssertStrEQ(output, ".....ok\n");
  34964. AssertIntEQ(BIO_pending(bio), 0);
  34965. AssertIntEQ(BIO_puts(bio, ""), -1);
  34966. BIO_free(bio);
  34967. printf(resultFmt, passed);
  34968. #endif
  34969. return 0;
  34970. }
  34971. static int test_wolfSSL_BIO_dump(void)
  34972. {
  34973. #if defined(OPENSSL_EXTRA)
  34974. BIO* bio;
  34975. static const unsigned char data[] = {
  34976. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  34977. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  34978. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  34979. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  34980. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  34981. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  34982. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  34983. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  34984. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  34985. 0xB4
  34986. };
  34987. /* Generated with OpenSSL. */
  34988. static const char expected[] =
  34989. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  34990. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  34991. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  34992. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  34993. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  34994. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  34995. static const char expectedAll[] =
  34996. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  34997. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  34998. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  34999. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  35000. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  35001. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  35002. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  35003. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  35004. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  35005. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  35006. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  35007. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  35008. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  35009. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  35010. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  35011. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  35012. char output[16 * 80];
  35013. int i;
  35014. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35015. /* Example key dumped. */
  35016. AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  35017. sizeof(expected) - 1);
  35018. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  35019. AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  35020. /* Try every possible value for a character. */
  35021. for (i = 0; i < 256; i++)
  35022. output[i] = i;
  35023. AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  35024. AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  35025. AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  35026. BIO_free(bio);
  35027. printf(resultFmt, passed);
  35028. #endif
  35029. return 0;
  35030. }
  35031. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35032. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35033. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35034. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  35035. {
  35036. (void)ssl;
  35037. (void)buf;
  35038. (void)sz;
  35039. (void)ctx;
  35040. return WOLFSSL_CBIO_ERR_WANT_READ;
  35041. }
  35042. #endif
  35043. static int test_wolfSSL_BIO_should_retry(void)
  35044. {
  35045. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35046. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  35047. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  35048. tcp_ready ready;
  35049. func_args server_args;
  35050. THREAD_TYPE serverThread;
  35051. SOCKET_T sockfd = 0;
  35052. WOLFSSL_CTX* ctx;
  35053. WOLFSSL* ssl;
  35054. char msg[64] = "hello wolfssl!";
  35055. char reply[1024];
  35056. int msgSz = (int)XSTRLEN(msg);
  35057. int ret;
  35058. BIO* bio;
  35059. printf(testingFmt, "wolfSSL_BIO_should_retry()");
  35060. XMEMSET(&server_args, 0, sizeof(func_args));
  35061. #ifdef WOLFSSL_TIRTOS
  35062. fdOpenSession(Task_self());
  35063. #endif
  35064. StartTCP();
  35065. InitTcpReady(&ready);
  35066. #if defined(USE_WINDOWS_API)
  35067. /* use RNG to get random port if using windows */
  35068. ready.port = GetRandomPort();
  35069. #endif
  35070. server_args.signal = &ready;
  35071. start_thread(test_server_nofail, &server_args, &serverThread);
  35072. wait_tcp_ready(&server_args);
  35073. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35074. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  35075. AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  35076. #endif
  35077. AssertIntEQ(WOLFSSL_SUCCESS,
  35078. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35079. AssertIntEQ(WOLFSSL_SUCCESS,
  35080. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35081. AssertIntEQ(WOLFSSL_SUCCESS,
  35082. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35083. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  35084. /* force retry */
  35085. ssl = wolfSSL_new(ctx);
  35086. AssertNotNull(ssl);
  35087. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35088. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  35089. AssertNotNull(bio = BIO_new(BIO_f_ssl()));
  35090. BIO_set_ssl(bio, ssl, BIO_CLOSE);
  35091. AssertIntLE(BIO_write(bio, msg, msgSz), 0);
  35092. AssertIntNE(BIO_should_retry(bio), 0);
  35093. /* now perform successful connection */
  35094. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  35095. AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  35096. BIO_read(bio, reply, sizeof(reply));
  35097. ret = wolfSSL_get_error(ssl, -1);
  35098. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  35099. AssertIntNE(BIO_should_retry(bio), 0);
  35100. }
  35101. else {
  35102. AssertIntEQ(BIO_should_retry(bio), 0);
  35103. }
  35104. AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  35105. XSTRLEN("I hear you fa shizzle!")), 0);
  35106. BIO_free(bio);
  35107. wolfSSL_CTX_free(ctx);
  35108. join_thread(serverThread);
  35109. FreeTcpReady(&ready);
  35110. #ifdef WOLFSSL_TIRTOS
  35111. fdOpenSession(Task_self());
  35112. #endif
  35113. printf(resultFmt, passed);
  35114. #endif
  35115. return 0;
  35116. }
  35117. static int test_wolfSSL_BIO_connect(void)
  35118. {
  35119. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35120. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  35121. tcp_ready ready;
  35122. func_args server_args;
  35123. THREAD_TYPE serverThread;
  35124. BIO *tcpBio;
  35125. BIO *sslBio;
  35126. SSL_CTX* ctx;
  35127. SSL *ssl;
  35128. SSL *sslPtr;
  35129. char msg[] = "hello wolfssl!";
  35130. char reply[30];
  35131. char buff[10] = {0};
  35132. printf(testingFmt, "wolfSSL_BIO_new_connect()");
  35133. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35134. AssertIntEQ(WOLFSSL_SUCCESS,
  35135. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  35136. AssertIntEQ(WOLFSSL_SUCCESS,
  35137. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  35138. AssertIntEQ(WOLFSSL_SUCCESS,
  35139. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  35140. /* Setup server */
  35141. XMEMSET(&server_args, 0, sizeof(func_args));
  35142. StartTCP();
  35143. InitTcpReady(&ready);
  35144. #if defined(USE_WINDOWS_API)
  35145. /* use RNG to get random port if using windows */
  35146. ready.port = GetRandomPort();
  35147. #endif
  35148. server_args.signal = &ready;
  35149. start_thread(test_server_nofail, &server_args, &serverThread);
  35150. wait_tcp_ready(&server_args);
  35151. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35152. /* Start the test proper */
  35153. /* Setup the TCP BIO */
  35154. AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  35155. AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  35156. /* Setup the SSL object */
  35157. AssertNotNull(ssl = SSL_new(ctx));
  35158. SSL_set_connect_state(ssl);
  35159. /* Setup the SSL BIO */
  35160. AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
  35161. AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  35162. /* Verify that BIO_get_ssl works. */
  35163. AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  35164. AssertPtrEq(ssl, sslPtr);
  35165. /* Link BIO's so that sslBio uses tcpBio for IO */
  35166. AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  35167. /* Do TCP connect */
  35168. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35169. /* Do TLS handshake */
  35170. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35171. /* Test writing */
  35172. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35173. /* Expect length of default wolfSSL reply */
  35174. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35175. /* Clean it all up */
  35176. BIO_free_all(sslBio);
  35177. /* Server clean up */
  35178. join_thread(serverThread);
  35179. FreeTcpReady(&ready);
  35180. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  35181. * after. */
  35182. XMEMSET(&server_args, 0, sizeof(func_args));
  35183. StartTCP();
  35184. InitTcpReady(&ready);
  35185. #if defined(USE_WINDOWS_API)
  35186. /* use RNG to get random port if using windows */
  35187. ready.port = GetRandomPort();
  35188. #endif
  35189. server_args.signal = &ready;
  35190. start_thread(test_server_nofail, &server_args, &serverThread);
  35191. wait_tcp_ready(&server_args);
  35192. AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  35193. AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
  35194. AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  35195. AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  35196. AssertIntEQ(BIO_do_connect(sslBio), 1);
  35197. AssertIntEQ(BIO_do_handshake(sslBio), 1);
  35198. AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  35199. AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  35200. /* Attempt to close the TLS connection gracefully. */
  35201. BIO_ssl_shutdown(sslBio);
  35202. BIO_free_all(sslBio);
  35203. join_thread(serverThread);
  35204. FreeTcpReady(&ready);
  35205. SSL_CTX_free(ctx);
  35206. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35207. wc_ecc_fp_free(); /* free per thread cache */
  35208. #endif
  35209. printf(resultFmt, passed);
  35210. #endif
  35211. return 0;
  35212. }
  35213. static int test_wolfSSL_BIO_tls(void)
  35214. {
  35215. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  35216. SSL_CTX* ctx;
  35217. SSL *ssl;
  35218. BIO *readBio;
  35219. BIO *writeBio;
  35220. int ret, err = 0;
  35221. printf(testingFmt, "test_wolfSSL_BIO_tls()");
  35222. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35223. AssertNotNull(ssl = SSL_new(ctx));
  35224. AssertNotNull(readBio = BIO_new(BIO_s_mem()));
  35225. AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
  35226. /* Qt reads data from write-bio,
  35227. * then writes the read data into plain packet.
  35228. * Qt reads data from plain packet,
  35229. * then writes the read data into read-bio.
  35230. */
  35231. SSL_set_bio(ssl, readBio, writeBio);
  35232. do {
  35233. #ifdef WOLFSSL_ASYNC_CRYPT
  35234. if (err == WC_PENDING_E) {
  35235. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35236. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35237. }
  35238. #endif
  35239. ret = SSL_connect(ssl);
  35240. err = SSL_get_error(ssl, 0);
  35241. } while (err == WC_PENDING_E);
  35242. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  35243. /* in this use case, should return WANT READ
  35244. * so that Qt will read the data from plain packet for next state.
  35245. */
  35246. AssertIntEQ(err, SSL_ERROR_WANT_READ);
  35247. SSL_free(ssl);
  35248. SSL_CTX_free(ctx);
  35249. printf(resultFmt, passed);
  35250. #endif
  35251. return 0;
  35252. }
  35253. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35254. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  35255. {
  35256. BIO* clientBio;
  35257. SSL* sslClient;
  35258. SSL_CTX* ctx;
  35259. char connectAddr[20]; /* IP + port */;
  35260. (void)args;
  35261. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  35262. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  35263. AssertIntEQ(BIO_do_connect(clientBio), 1);
  35264. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35265. AssertNotNull(sslClient = SSL_new(ctx));
  35266. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  35267. SSL_set_bio(sslClient, clientBio, clientBio);
  35268. AssertIntEQ(SSL_connect(sslClient), 1);
  35269. SSL_free(sslClient);
  35270. SSL_CTX_free(ctx);
  35271. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35272. wc_ecc_fp_free(); /* free per thread cache */
  35273. #endif
  35274. return 0;
  35275. }
  35276. #endif
  35277. static int test_wolfSSL_BIO_accept(void)
  35278. {
  35279. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
  35280. BIO* serverBindBio;
  35281. BIO* serverAcceptBio;
  35282. SSL* sslServer;
  35283. SSL_CTX* ctx;
  35284. func_args args;
  35285. THREAD_TYPE thread;
  35286. char port[10]; /* 10 bytes should be enough to store the string
  35287. * representation of the port */
  35288. printf(testingFmt, "wolfSSL_BIO_new_accept()");
  35289. AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  35290. AssertNotNull(serverBindBio = BIO_new_accept(port));
  35291. /* First BIO_do_accept binds the port */
  35292. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35293. XMEMSET(&args, 0, sizeof(func_args));
  35294. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  35295. AssertIntEQ(BIO_do_accept(serverBindBio), 1);
  35296. /* Let's plug it into SSL to test */
  35297. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  35298. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35299. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  35300. AssertNotNull(sslServer = SSL_new(ctx));
  35301. AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  35302. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  35303. AssertIntEQ(SSL_accept(sslServer), 1);
  35304. join_thread(thread);
  35305. BIO_free(serverBindBio);
  35306. SSL_free(sslServer);
  35307. SSL_CTX_free(ctx);
  35308. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  35309. wc_ecc_fp_free(); /* free per thread cache */
  35310. #endif
  35311. printf(resultFmt, passed);
  35312. #endif
  35313. return 0;
  35314. }
  35315. static int test_wolfSSL_BIO_write(void)
  35316. {
  35317. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  35318. BIO* bio;
  35319. BIO* bio64;
  35320. BIO* ptr;
  35321. int sz;
  35322. char msg[] = "conversion test";
  35323. char out[40];
  35324. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  35325. void* bufPtr = NULL;
  35326. BUF_MEM* buf = NULL;
  35327. printf(testingFmt, "wolfSSL_BIO_write()");
  35328. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35329. AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  35330. /* now should convert to base64 then write to memory */
  35331. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35332. BIO_flush(bio);
  35333. /* test BIO chain */
  35334. AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  35335. AssertNotNull(buf);
  35336. AssertIntEQ(buf->length, 25);
  35337. AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  35338. AssertPtrEq(buf->data, bufPtr);
  35339. AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  35340. sz = sizeof(out);
  35341. XMEMSET(out, 0, sz);
  35342. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  35343. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35344. /* write then read should return the same message */
  35345. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35346. sz = sizeof(out);
  35347. XMEMSET(out, 0, sz);
  35348. AssertIntEQ(BIO_read(bio, out, sz), 16);
  35349. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35350. /* now try encoding with no line ending */
  35351. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  35352. #ifdef HAVE_EX_DATA
  35353. BIO_set_ex_data(bio64, 0, (void*) "data");
  35354. AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  35355. #endif
  35356. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35357. BIO_flush(bio);
  35358. sz = sizeof(out);
  35359. XMEMSET(out, 0, sz);
  35360. AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  35361. AssertIntEQ(XMEMCMP(out, expected, sz), 0);
  35362. BIO_free_all(bio); /* frees bio64 also */
  35363. /* test with more than one bio64 in list */
  35364. AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
  35365. AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  35366. AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
  35367. /* now should convert to base64 when stored and then decode with read */
  35368. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  35369. BIO_flush(bio);
  35370. sz = sizeof(out);
  35371. XMEMSET(out, 0, sz);
  35372. AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
  35373. AssertIntEQ(XMEMCMP(out, msg, sz), 0);
  35374. BIO_clear_flags(bio64, ~0);
  35375. BIO_set_retry_read(bio);
  35376. BIO_free_all(bio); /* frees bio64s also */
  35377. AssertNotNull(bio = BIO_new_mem_buf(out, 0));
  35378. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  35379. BIO_free(bio);
  35380. printf(resultFmt, passed);
  35381. #endif
  35382. return 0;
  35383. }
  35384. static int test_wolfSSL_BIO_printf(void)
  35385. {
  35386. #if defined(OPENSSL_ALL)
  35387. BIO* bio;
  35388. int sz = 7;
  35389. char msg[] = "TLS 1.3 for the world";
  35390. char out[60];
  35391. char expected[] = "TLS 1.3 for the world : sz = 7";
  35392. printf(testingFmt, "wolfSSL_BIO_printf()");
  35393. XMEMSET(out, 0, sizeof(out));
  35394. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  35395. AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  35396. AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  35397. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  35398. AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  35399. BIO_free(bio);
  35400. printf(resultFmt, passed);
  35401. #endif
  35402. return 0;
  35403. }
  35404. static int test_wolfSSL_BIO_f_md(void)
  35405. {
  35406. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  35407. BIO *bio, *mem;
  35408. char msg[] = "message to hash";
  35409. char out[60];
  35410. EVP_MD_CTX* ctx;
  35411. const unsigned char testKey[] =
  35412. {
  35413. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35414. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  35415. 0x0b, 0x0b, 0x0b, 0x0b
  35416. };
  35417. const char testData[] = "Hi There";
  35418. const unsigned char testResult[] =
  35419. {
  35420. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  35421. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  35422. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  35423. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  35424. };
  35425. const unsigned char expectedHash[] =
  35426. {
  35427. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  35428. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  35429. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  35430. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  35431. };
  35432. const unsigned char emptyHash[] =
  35433. {
  35434. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  35435. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  35436. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  35437. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  35438. };
  35439. unsigned char check[sizeof(testResult) + 1];
  35440. size_t checkSz = -1;
  35441. EVP_PKEY* key;
  35442. printf(testingFmt, "wolfSSL_BIO_f_md()");
  35443. XMEMSET(out, 0, sizeof(out));
  35444. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35445. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35446. AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  35447. AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  35448. /* should not be able to write/read yet since just digest wrapper and no
  35449. * data is passing through the bio */
  35450. AssertIntEQ(BIO_write(bio, msg, 0), 0);
  35451. AssertIntEQ(BIO_pending(bio), 0);
  35452. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  35453. AssertIntEQ(BIO_gets(bio, out, 3), 0);
  35454. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35455. AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  35456. BIO_reset(bio);
  35457. /* append BIO mem to bio in order to read/write */
  35458. AssertNotNull(bio = BIO_push(bio, mem));
  35459. XMEMSET(out, 0, sizeof(out));
  35460. AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  35461. AssertIntEQ(BIO_pending(bio), 16);
  35462. /* this just reads the message and does not hash it (gets calls final) */
  35463. AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  35464. AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  35465. /* create a message digest using BIO */
  35466. XMEMSET(out, 0, sizeof(out));
  35467. AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  35468. AssertIntEQ(BIO_pending(mem), 16);
  35469. AssertIntEQ(BIO_pending(bio), 16);
  35470. AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  35471. AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  35472. BIO_free(bio);
  35473. BIO_free(mem);
  35474. /* test with HMAC */
  35475. XMEMSET(out, 0, sizeof(out));
  35476. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35477. AssertNotNull(mem = BIO_new(BIO_s_mem()));
  35478. BIO_get_md_ctx(bio, &ctx);
  35479. AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  35480. testKey, (int)sizeof(testKey)));
  35481. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  35482. AssertNotNull(bio = BIO_push(bio, mem));
  35483. BIO_write(bio, testData, (int)strlen(testData));
  35484. EVP_DigestSignFinal(ctx, NULL, &checkSz);
  35485. EVP_DigestSignFinal(ctx, check, &checkSz);
  35486. AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  35487. EVP_PKEY_free(key);
  35488. BIO_free(bio);
  35489. BIO_free(mem);
  35490. printf(resultFmt, passed);
  35491. #endif
  35492. return 0;
  35493. }
  35494. static int test_wolfSSL_BIO_up_ref(void)
  35495. {
  35496. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  35497. BIO* bio;
  35498. printf(testingFmt, "wolfSSL_BIO_up_ref()");
  35499. AssertNotNull(bio = BIO_new(BIO_f_md()));
  35500. AssertIntEQ(BIO_up_ref(NULL), 0);
  35501. AssertIntEQ(BIO_up_ref(bio), 1);
  35502. BIO_free(bio);
  35503. AssertIntEQ(BIO_up_ref(bio), 1);
  35504. BIO_free(bio);
  35505. BIO_free(bio);
  35506. printf(resultFmt, "passed");
  35507. #endif
  35508. return 0;
  35509. }
  35510. #endif /* !NO_BIO */
  35511. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35512. /* test that the callback arg is correct */
  35513. static int certCbArg = 0;
  35514. static int clientCertCb(WOLFSSL* ssl, void* arg)
  35515. {
  35516. if (ssl == NULL || arg != &certCbArg)
  35517. return 0;
  35518. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  35519. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35520. return 0;
  35521. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  35522. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35523. return 0;
  35524. return 1;
  35525. }
  35526. static void clientCertSetupCb(WOLFSSL_CTX* ctx)
  35527. {
  35528. SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
  35529. }
  35530. /**
  35531. * This is only done because test_client_nofail has no way to stop
  35532. * certificate and key loading
  35533. */
  35534. static void clientCertClearCb(WOLFSSL* ssl)
  35535. {
  35536. /* Clear the loaded certs to force the callbacks to set them up */
  35537. SSL_certs_clear(ssl);
  35538. }
  35539. static int serverCertCb(WOLFSSL* ssl, void* arg)
  35540. {
  35541. if (ssl == NULL || arg != &certCbArg)
  35542. return 0;
  35543. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  35544. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35545. return 0;
  35546. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  35547. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  35548. return 0;
  35549. return 1;
  35550. }
  35551. static void serverCertSetupCb(WOLFSSL_CTX* ctx)
  35552. {
  35553. SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
  35554. }
  35555. /**
  35556. * This is only done because test_server_nofail has no way to stop
  35557. * certificate and key loading
  35558. */
  35559. static void serverCertClearCb(WOLFSSL* ssl)
  35560. {
  35561. /* Clear the loaded certs to force the callbacks to set them up */
  35562. SSL_certs_clear(ssl);
  35563. }
  35564. #endif
  35565. static int test_wolfSSL_cert_cb(void)
  35566. {
  35567. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  35568. callback_functions func_cb_client;
  35569. callback_functions func_cb_server;
  35570. tcp_ready ready;
  35571. func_args client_args;
  35572. func_args server_args;
  35573. THREAD_TYPE serverThread;
  35574. XMEMSET(&client_args, 0, sizeof(func_args));
  35575. XMEMSET(&server_args, 0, sizeof(func_args));
  35576. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  35577. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  35578. #ifdef WOLFSSL_TIRTOS
  35579. fdOpenSession(Task_self());
  35580. #endif
  35581. StartTCP();
  35582. InitTcpReady(&ready);
  35583. #if defined(USE_WINDOWS_API)
  35584. /* use RNG to get random port if using windows */
  35585. ready.port = GetRandomPort();
  35586. #endif
  35587. server_args.signal = &ready;
  35588. client_args.signal = &ready;
  35589. client_args.callbacks = &func_cb_client;
  35590. server_args.callbacks = &func_cb_server;
  35591. func_cb_client.ctx_ready = clientCertSetupCb;
  35592. func_cb_client.ssl_ready = clientCertClearCb;
  35593. func_cb_server.ctx_ready = serverCertSetupCb;
  35594. func_cb_server.ssl_ready = serverCertClearCb;
  35595. start_thread(test_server_nofail, &server_args, &serverThread);
  35596. wait_tcp_ready(&server_args);
  35597. test_client_nofail(&client_args, NULL);
  35598. join_thread(serverThread);
  35599. AssertTrue(client_args.return_code);
  35600. AssertTrue(server_args.return_code);
  35601. FreeTcpReady(&ready);
  35602. #ifdef WOLFSSL_TIRTOS
  35603. fdOpenSession(Task_self());
  35604. #endif
  35605. #endif
  35606. return 0;
  35607. }
  35608. static int test_wolfSSL_SESSION(void)
  35609. {
  35610. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  35611. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35612. !defined(NO_SESSION_CACHE)
  35613. WOLFSSL* ssl;
  35614. WOLFSSL_CTX* ctx;
  35615. WOLFSSL_SESSION* sess;
  35616. WOLFSSL_SESSION* sess_copy;
  35617. #ifdef OPENSSL_EXTRA
  35618. unsigned char* sessDer = NULL;
  35619. unsigned char* ptr = NULL;
  35620. const unsigned char context[] = "user app context";
  35621. unsigned int contextSz = (unsigned int)sizeof(context);
  35622. int sz;
  35623. #endif
  35624. int ret, err;
  35625. SOCKET_T sockfd;
  35626. tcp_ready ready;
  35627. func_args server_args;
  35628. THREAD_TYPE serverThread;
  35629. char msg[80];
  35630. const char* sendGET = "GET";
  35631. printf(testingFmt, "wolfSSL_SESSION()");
  35632. /* TLS v1.3 requires session tickets */
  35633. /* CHACHA and POLY1305 required for myTicketEncCb */
  35634. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  35635. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  35636. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  35637. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  35638. #else
  35639. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35640. #endif
  35641. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  35642. WOLFSSL_FILETYPE_PEM));
  35643. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  35644. WOLFSSL_FILETYPE_PEM));
  35645. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  35646. WOLFSSL_SUCCESS);
  35647. #ifdef WOLFSSL_ENCRYPTED_KEYS
  35648. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  35649. #endif
  35650. #ifdef HAVE_SESSION_TICKET
  35651. /* Use session tickets, for ticket tests below */
  35652. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  35653. #endif
  35654. XMEMSET(&server_args, 0, sizeof(func_args));
  35655. #ifdef WOLFSSL_TIRTOS
  35656. fdOpenSession(Task_self());
  35657. #endif
  35658. StartTCP();
  35659. InitTcpReady(&ready);
  35660. #if defined(USE_WINDOWS_API)
  35661. /* use RNG to get random port if using windows */
  35662. ready.port = GetRandomPort();
  35663. #endif
  35664. server_args.signal = &ready;
  35665. start_thread(test_server_nofail, &server_args, &serverThread);
  35666. wait_tcp_ready(&server_args);
  35667. /* client connection */
  35668. ssl = wolfSSL_new(ctx);
  35669. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  35670. AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  35671. #ifdef WOLFSSL_ASYNC_CRYPT
  35672. err = 0; /* Reset error */
  35673. #endif
  35674. do {
  35675. #ifdef WOLFSSL_ASYNC_CRYPT
  35676. if (err == WC_PENDING_E) {
  35677. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35678. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35679. }
  35680. #endif
  35681. ret = wolfSSL_connect(ssl);
  35682. err = wolfSSL_get_error(ssl, 0);
  35683. } while (err == WC_PENDING_E);
  35684. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  35685. #ifdef WOLFSSL_ASYNC_CRYPT
  35686. err = 0; /* Reset error */
  35687. #endif
  35688. do {
  35689. #ifdef WOLFSSL_ASYNC_CRYPT
  35690. if (err == WC_PENDING_E) {
  35691. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35692. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35693. }
  35694. #endif
  35695. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  35696. err = wolfSSL_get_error(ssl, 0);
  35697. } while (err == WC_PENDING_E);
  35698. AssertIntEQ(ret, (int)XSTRLEN(sendGET));
  35699. #ifdef WOLFSSL_ASYNC_CRYPT
  35700. err = 0; /* Reset error */
  35701. #endif
  35702. do {
  35703. #ifdef WOLFSSL_ASYNC_CRYPT
  35704. if (err == WC_PENDING_E) {
  35705. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  35706. if (ret < 0) { break; } else if (ret == 0) { continue; }
  35707. }
  35708. #endif
  35709. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  35710. err = wolfSSL_get_error(ssl, 0);
  35711. } while (err == WC_PENDING_E);
  35712. AssertIntEQ(ret, 23);
  35713. AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  35714. AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  35715. #ifdef HAVE_EXT_CACHE
  35716. AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
  35717. * HAVE_EXT_CACHE we get new objects each time */
  35718. #endif
  35719. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  35720. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  35721. sess = wolfSSL_get_session(ssl);
  35722. #ifdef OPENSSL_EXTRA
  35723. AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  35724. AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
  35725. AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  35726. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  35727. #ifdef HAVE_SESSION_TICKET
  35728. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  35729. AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  35730. SESSION_TICKET_HINT_DEFAULT);
  35731. #else
  35732. AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  35733. #endif
  35734. #else
  35735. (void)sess;
  35736. #endif /* OPENSSL_EXTRA */
  35737. /* Retain copy of the session for later testing */
  35738. AssertNotNull(sess = wolfSSL_get1_session(ssl));
  35739. wolfSSL_shutdown(ssl);
  35740. wolfSSL_free(ssl);
  35741. join_thread(serverThread);
  35742. FreeTcpReady(&ready);
  35743. #ifdef WOLFSSL_TIRTOS
  35744. fdOpenSession(Task_self());
  35745. #endif
  35746. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  35747. {
  35748. X509 *x509;
  35749. char buf[30];
  35750. int bufSz;
  35751. AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
  35752. AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
  35753. X509_get_subject_name(x509), NID_organizationalUnitName,
  35754. buf, sizeof(buf))), 0);
  35755. AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  35756. if (bufSz == 7) {
  35757. AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  35758. }
  35759. if (bufSz == 16) {
  35760. AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  35761. }
  35762. }
  35763. #endif
  35764. #ifdef HAVE_EXT_CACHE
  35765. AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  35766. wolfSSL_SESSION_free(sess_copy);
  35767. sess_copy = NULL;
  35768. #endif
  35769. #ifdef OPENSSL_EXTRA
  35770. /* get session from DER and update the timeout */
  35771. AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  35772. AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  35773. wolfSSL_SESSION_free(sess);
  35774. sess = NULL;
  35775. ptr = sessDer;
  35776. AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  35777. AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  35778. (const unsigned char**)&ptr, sz));
  35779. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  35780. sessDer = NULL;
  35781. AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
  35782. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  35783. #endif
  35784. /* successful set session test */
  35785. AssertNotNull(ssl = wolfSSL_new(ctx));
  35786. AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  35787. #ifdef HAVE_SESSION_TICKET
  35788. /* Test set/get session ticket */
  35789. {
  35790. const char* ticket = "This is a session ticket";
  35791. char buf[64] = {0};
  35792. word32 bufSz = (word32)sizeof(buf);
  35793. AssertIntEQ(SSL_SUCCESS,
  35794. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  35795. (word32)XSTRLEN(ticket)));
  35796. AssertIntEQ(SSL_SUCCESS,
  35797. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  35798. AssertStrEQ(ticket, buf);
  35799. }
  35800. #endif
  35801. #ifdef OPENSSL_EXTRA
  35802. /* session timeout case */
  35803. /* make the session to be expired */
  35804. AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  35805. XSLEEP_MS(1200);
  35806. /* SSL_set_session should reject specified session but return success
  35807. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  35808. */
  35809. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  35810. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  35811. #else
  35812. AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  35813. #endif
  35814. AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  35815. /* fail case with miss match session context IDs (use compatibility API) */
  35816. AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  35817. SSL_SUCCESS);
  35818. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  35819. wolfSSL_free(ssl);
  35820. AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  35821. SSL_FAILURE);
  35822. AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  35823. SSL_SUCCESS);
  35824. AssertNotNull(ssl = wolfSSL_new(ctx));
  35825. AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  35826. #endif /* OPENSSL_EXTRA */
  35827. wolfSSL_free(ssl);
  35828. wolfSSL_SESSION_free(sess);
  35829. wolfSSL_CTX_free(ctx);
  35830. printf(resultFmt, passed);
  35831. #endif
  35832. return 0;
  35833. }
  35834. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35835. defined(HAVE_EX_DATA)
  35836. static int clientSessRemCountMalloc = 0;
  35837. static int serverSessRemCountMalloc = 0;
  35838. static int clientSessRemCountFree = 0;
  35839. static int serverSessRemCountFree = 0;
  35840. static WOLFSSL_CTX* serverSessCtx = NULL;
  35841. static WOLFSSL_SESSION* serverSess = NULL;
  35842. #ifndef NO_SESSION_CACHE_REF
  35843. static WOLFSSL_CTX* clientSessCtx = NULL;
  35844. static WOLFSSL_SESSION* clientSess = NULL;
  35845. #endif
  35846. static int serverSessRemIdx = 3;
  35847. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  35848. {
  35849. int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  35850. (void)ctx;
  35851. AssertNotNull(mallocedData);
  35852. if (!*mallocedData)
  35853. clientSessRemCountFree++;
  35854. else
  35855. serverSessRemCountFree++;
  35856. XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
  35857. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  35858. }
  35859. static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  35860. {
  35861. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  35862. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  35863. !defined(NO_SESSION_CACHE_REF)
  35864. /* Allow downgrade, set min version, and disable TLS 1.3.
  35865. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  35866. * reference to the session cache. But with WOLFSSL_TLS13 and without
  35867. * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
  35868. * session in the cache. In this case we need to downgrade to previous
  35869. * versions to just use the legacy session ID field. */
  35870. AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
  35871. AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
  35872. #endif
  35873. }
  35874. static void SessRemSslSetupCb(WOLFSSL* ssl)
  35875. {
  35876. int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
  35877. AssertNotNull(mallocedData);
  35878. *mallocedData = SSL_is_server(ssl);
  35879. if (!*mallocedData) {
  35880. clientSessRemCountMalloc++;
  35881. #ifndef NO_SESSION_CACHE_REF
  35882. AssertNotNull(clientSess = SSL_get1_session(ssl));
  35883. AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  35884. SSL_SUCCESS);
  35885. #endif
  35886. }
  35887. else {
  35888. serverSessRemCountMalloc++;
  35889. AssertNotNull(serverSess = SSL_get1_session(ssl));
  35890. AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  35891. SSL_SUCCESS);
  35892. }
  35893. AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
  35894. mallocedData), SSL_SUCCESS);
  35895. }
  35896. #endif
  35897. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  35898. {
  35899. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  35900. defined(HAVE_EX_DATA)
  35901. /* Check that the remove callback gets called for external data in a
  35902. * session object */
  35903. callback_functions func_cb;
  35904. tcp_ready ready;
  35905. func_args client_args;
  35906. func_args server_args;
  35907. THREAD_TYPE serverThread;
  35908. printf(testingFmt, "wolfSSL_CTX_sess_set_remove_cb()");
  35909. XMEMSET(&client_args, 0, sizeof(func_args));
  35910. XMEMSET(&server_args, 0, sizeof(func_args));
  35911. XMEMSET(&func_cb, 0, sizeof(callback_functions));
  35912. #ifdef WOLFSSL_TIRTOS
  35913. fdOpenSession(Task_self());
  35914. #endif
  35915. StartTCP();
  35916. InitTcpReady(&ready);
  35917. #if defined(USE_WINDOWS_API)
  35918. /* use RNG to get random port if using windows */
  35919. ready.port = GetRandomPort();
  35920. #endif
  35921. server_args.signal = &ready;
  35922. client_args.signal = &ready;
  35923. client_args.callbacks = &func_cb;
  35924. server_args.callbacks = &func_cb;
  35925. func_cb.ctx_ready = SessRemCtxSetupCb;
  35926. func_cb.on_result = SessRemSslSetupCb;
  35927. start_thread(test_server_nofail, &server_args, &serverThread);
  35928. wait_tcp_ready(&server_args);
  35929. test_client_nofail(&client_args, NULL);
  35930. join_thread(serverThread);
  35931. AssertTrue(client_args.return_code);
  35932. AssertTrue(server_args.return_code);
  35933. FreeTcpReady(&ready);
  35934. #ifdef WOLFSSL_TIRTOS
  35935. fdOpenSession(Task_self());
  35936. #endif
  35937. /* Both should have been allocated */
  35938. AssertIntEQ(clientSessRemCountMalloc, 1);
  35939. AssertIntEQ(serverSessRemCountMalloc, 1);
  35940. #ifdef NO_SESSION_CACHE_REF
  35941. /* Client session should not be added to cache so this should be free'd when
  35942. * the SSL object was being free'd */
  35943. AssertIntEQ(clientSessRemCountFree, 1);
  35944. #else
  35945. /* Client session is in cache due to requiring a persistent reference */
  35946. AssertIntEQ(clientSessRemCountFree, 0);
  35947. /* Force a cache lookup */
  35948. AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  35949. /* Force a cache update */
  35950. AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  35951. /* This should set the timeout to 0 and call the remove callback from within
  35952. * the session cache. */
  35953. AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  35954. AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  35955. AssertIntEQ(clientSessRemCountFree, 1);
  35956. #endif
  35957. /* Server session is in the cache so ex_data isn't free'd with the SSL
  35958. * object */
  35959. AssertIntEQ(serverSessRemCountFree, 0);
  35960. /* Force a cache lookup */
  35961. AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  35962. /* Force a cache update */
  35963. AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  35964. /* This should set the timeout to 0 and call the remove callback from within
  35965. * the session cache. */
  35966. AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  35967. AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  35968. AssertIntEQ(serverSessRemCountFree, 1);
  35969. /* Need to free the references that we kept */
  35970. SSL_CTX_free(serverSessCtx);
  35971. SSL_SESSION_free(serverSess);
  35972. #ifndef NO_SESSION_CACHE_REF
  35973. SSL_CTX_free(clientSessCtx);
  35974. SSL_SESSION_free(clientSess);
  35975. #endif
  35976. printf(resultFmt, passed);
  35977. #endif
  35978. return 0;
  35979. }
  35980. static int test_wolfSSL_ticket_keys(void)
  35981. {
  35982. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  35983. !defined(NO_WOLFSSL_SERVER)
  35984. WOLFSSL_CTX* ctx;
  35985. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  35986. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35987. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  35988. WOLFSSL_FAILURE);
  35989. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  35990. WOLFSSL_FAILURE);
  35991. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  35992. WOLFSSL_FAILURE);
  35993. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  35994. WOLFSSL_FAILURE);
  35995. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  35996. WOLFSSL_FAILURE);
  35997. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  35998. WOLFSSL_FAILURE);
  35999. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36000. WOLFSSL_FAILURE);
  36001. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  36002. WOLFSSL_FAILURE);
  36003. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  36004. WOLFSSL_FAILURE);
  36005. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  36006. WOLFSSL_FAILURE);
  36007. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  36008. WOLFSSL_FAILURE);
  36009. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  36010. WOLFSSL_FAILURE);
  36011. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  36012. WOLFSSL_FAILURE);
  36013. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  36014. WOLFSSL_FAILURE);
  36015. AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36016. WOLFSSL_SUCCESS);
  36017. AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  36018. WOLFSSL_SUCCESS);
  36019. wolfSSL_CTX_free(ctx);
  36020. #endif
  36021. return 0;
  36022. }
  36023. #ifndef NO_BIO
  36024. static int test_wolfSSL_d2i_PUBKEY(void)
  36025. {
  36026. #if defined(OPENSSL_EXTRA)
  36027. BIO* bio;
  36028. EVP_PKEY* pkey;
  36029. printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
  36030. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36031. AssertNull(d2i_PUBKEY_bio(NULL, NULL));
  36032. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  36033. /* RSA PUBKEY test */
  36034. AssertIntGT(BIO_write(bio, client_keypub_der_2048,
  36035. sizeof_client_keypub_der_2048), 0);
  36036. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36037. EVP_PKEY_free(pkey);
  36038. #endif
  36039. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  36040. /* ECC PUBKEY test */
  36041. AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  36042. sizeof_ecc_clikeypub_der_256), 0);
  36043. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36044. EVP_PKEY_free(pkey);
  36045. #endif
  36046. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  36047. /* DSA PUBKEY test */
  36048. AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  36049. sizeof_dsa_pub_key_der_2048), 0);
  36050. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36051. EVP_PKEY_free(pkey);
  36052. #endif
  36053. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  36054. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  36055. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  36056. (HAVE_FIPS_VERSION > 2))
  36057. /* DH PUBKEY test */
  36058. AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
  36059. sizeof_dh_pub_key_der_2048), 0);
  36060. AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  36061. EVP_PKEY_free(pkey);
  36062. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  36063. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  36064. BIO_free(bio);
  36065. (void)pkey;
  36066. printf(resultFmt, passed);
  36067. #endif
  36068. return 0;
  36069. }
  36070. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  36071. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  36072. {
  36073. BIO* bio = NULL;
  36074. EVP_PKEY* pkey = NULL;
  36075. #ifndef NO_RSA
  36076. #endif
  36077. WOLFSSL_CTX* ctx;
  36078. #if defined(WOLFSSL_KEY_GEN)
  36079. unsigned char buff[4096];
  36080. unsigned char* bufPtr = buff;
  36081. #endif
  36082. printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
  36083. /* test creating new EVP_PKEY with bad arg */
  36084. AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  36085. /* test loading RSA key using BIO */
  36086. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  36087. {
  36088. XFILE file;
  36089. const char* fname = "./certs/server-key.der";
  36090. size_t sz;
  36091. byte* buf;
  36092. file = XFOPEN(fname, "rb");
  36093. AssertTrue((file != XBADFILE));
  36094. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36095. sz = XFTELL(file);
  36096. XREWIND(file);
  36097. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36098. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36099. XFCLOSE(file);
  36100. /* Test using BIO new mem and loading DER private key */
  36101. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36102. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36103. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36104. BIO_free(bio);
  36105. bio = NULL;
  36106. EVP_PKEY_free(pkey);
  36107. pkey = NULL;
  36108. }
  36109. #endif
  36110. /* test loading ECC key using BIO */
  36111. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  36112. {
  36113. XFILE file;
  36114. const char* fname = "./certs/ecc-key.der";
  36115. size_t sz;
  36116. byte* buf;
  36117. file = XFOPEN(fname, "rb");
  36118. AssertTrue((file != XBADFILE));
  36119. AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  36120. sz = XFTELL(file);
  36121. XREWIND(file);
  36122. AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  36123. AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
  36124. XFCLOSE(file);
  36125. /* Test using BIO new mem and loading DER private key */
  36126. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  36127. AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  36128. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  36129. BIO_free(bio);
  36130. bio = NULL;
  36131. EVP_PKEY_free(pkey);
  36132. pkey = NULL;
  36133. }
  36134. #endif
  36135. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  36136. #ifndef NO_WOLFSSL_SERVER
  36137. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  36138. #else
  36139. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  36140. #endif
  36141. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  36142. !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  36143. {
  36144. RSA* rsa = NULL;
  36145. /* Tests bad parameters */
  36146. AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  36147. /* RSA not set yet, expecting to fail*/
  36148. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  36149. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  36150. /* set RSA using bio*/
  36151. AssertIntGT(BIO_write(bio, client_key_der_2048,
  36152. sizeof_client_key_der_2048), 0);
  36153. AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  36154. AssertNotNull(rsa);
  36155. AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  36156. /*i2d RSAprivate key tests */
  36157. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  36158. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  36159. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36160. sizeof_client_key_der_2048);
  36161. bufPtr -= sizeof_client_key_der_2048;
  36162. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36163. sizeof_client_key_der_2048), 0);
  36164. bufPtr = NULL;
  36165. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  36166. sizeof_client_key_der_2048);
  36167. AssertNotNull(bufPtr);
  36168. AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  36169. sizeof_client_key_der_2048), 0);
  36170. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  36171. RSA_free(rsa);
  36172. rsa = RSA_new();
  36173. AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  36174. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  36175. RSA_free(rsa);
  36176. }
  36177. #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
  36178. SSL_CTX_free(ctx);
  36179. ctx = NULL;
  36180. BIO_free(bio);
  36181. bio = NULL;
  36182. printf(resultFmt, passed);
  36183. return 0;
  36184. }
  36185. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  36186. #endif /* !NO_BIO */
  36187. static int test_wolfSSL_sk_GENERAL_NAME(void)
  36188. {
  36189. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36190. !defined(NO_RSA)
  36191. X509* x509;
  36192. GENERAL_NAME* gn;
  36193. unsigned char buf[4096];
  36194. const unsigned char* bufPt;
  36195. int bytes, i;
  36196. int j;
  36197. XFILE f;
  36198. STACK_OF(GENERAL_NAME)* sk;
  36199. printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
  36200. f = XFOPEN(cliCertDerFileExt, "rb");
  36201. AssertTrue((f != XBADFILE));
  36202. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36203. XFCLOSE(f);
  36204. for (j = 0; j < 2; ++j) {
  36205. bufPt = buf;
  36206. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36207. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36208. NID_subject_alt_name, NULL, NULL));
  36209. AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
  36210. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  36211. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  36212. switch (gn->type) {
  36213. case GEN_DNS:
  36214. printf("found type GEN_DNS\n");
  36215. break;
  36216. case GEN_EMAIL:
  36217. printf("found type GEN_EMAIL\n");
  36218. break;
  36219. case GEN_URI:
  36220. printf("found type GEN_URI\n");
  36221. break;
  36222. }
  36223. }
  36224. X509_free(x509);
  36225. if (j == 0) {
  36226. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36227. }
  36228. else {
  36229. /*
  36230. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  36231. * it was supposed to. This is a regression test for that bug.
  36232. */
  36233. GENERAL_NAMES_free(sk);
  36234. }
  36235. }
  36236. printf(resultFmt, passed);
  36237. #endif
  36238. return 0;
  36239. }
  36240. static int test_wolfSSL_GENERAL_NAME_print(void)
  36241. {
  36242. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  36243. X509* x509;
  36244. GENERAL_NAME* gn;
  36245. unsigned char buf[4096];
  36246. const unsigned char* bufPt;
  36247. int bytes;
  36248. XFILE f;
  36249. STACK_OF(GENERAL_NAME)* sk;
  36250. BIO* out;
  36251. unsigned char outbuf[128];
  36252. X509_EXTENSION* ext;
  36253. AUTHORITY_INFO_ACCESS* aia;
  36254. ACCESS_DESCRIPTION* ad;
  36255. const unsigned char v4Addr[] = {192,168,53,1};
  36256. const unsigned char v6Addr[] =
  36257. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36258. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  36259. const unsigned char email[] =
  36260. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  36261. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  36262. const char* dnsStr = "DNS:example.com";
  36263. const char* uriStr = "URI:http://127.0.0.1:22220";
  36264. const char* v4addStr = "IP Address:192.168.53.1";
  36265. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  36266. const char* emailStr = "email:info@wolfssl.com";
  36267. const char* othrStr = "othername:<unsupported>";
  36268. const char* x400Str = "X400Name:<unsupported>";
  36269. const char* ediStr = "EdiPartyName:<unsupported>";
  36270. printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
  36271. /* BIO to output */
  36272. AssertNotNull(out = BIO_new(BIO_s_mem()));
  36273. /* test for NULL param */
  36274. gn = NULL;
  36275. AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  36276. AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  36277. AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
  36278. /* test for GEN_DNS */
  36279. f = XFOPEN(cliCertDerFileExt, "rb");
  36280. AssertTrue((f != XBADFILE));
  36281. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36282. XFCLOSE(f);
  36283. bufPt = buf;
  36284. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36285. AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  36286. NID_subject_alt_name, NULL, NULL));
  36287. AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  36288. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36289. XMEMSET(outbuf,0,sizeof(outbuf));
  36290. BIO_read(out, outbuf, sizeof(outbuf));
  36291. AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  36292. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  36293. X509_free(x509);
  36294. /* test for GEN_URI */
  36295. f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
  36296. AssertTrue((f != XBADFILE));
  36297. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36298. XFCLOSE(f);
  36299. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  36300. aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36301. AssertNotNull(aia);
  36302. ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
  36303. gn = ad->location;
  36304. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36305. XMEMSET(outbuf,0,sizeof(outbuf));
  36306. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36307. AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  36308. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  36309. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  36310. AssertNotNull(aia);
  36311. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  36312. X509_free(x509);
  36313. /* test for GEN_IPADD */
  36314. /* ip v4 address */
  36315. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36316. gn->type = GEN_IPADD;
  36317. gn->d.iPAddress->length = sizeof(v4Addr);
  36318. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  36319. sizeof(v4Addr)), 1);
  36320. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36321. XMEMSET(outbuf,0,sizeof(outbuf));
  36322. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36323. AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  36324. GENERAL_NAME_free(gn);
  36325. /* ip v6 address */
  36326. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36327. gn->type = GEN_IPADD;
  36328. gn->d.iPAddress->length = sizeof(v6Addr);
  36329. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  36330. sizeof(v6Addr)), 1);
  36331. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36332. XMEMSET(outbuf,0,sizeof(outbuf));
  36333. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36334. AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  36335. GENERAL_NAME_free(gn);
  36336. /* test for GEN_EMAIL */
  36337. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36338. gn->type = GEN_EMAIL;
  36339. gn->d.rfc822Name->length = sizeof(email);
  36340. AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
  36341. sizeof(email)), 1);
  36342. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36343. XMEMSET(outbuf,0,sizeof(outbuf));
  36344. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36345. AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  36346. GENERAL_NAME_free(gn);
  36347. /* test for GEN_OTHERNAME */
  36348. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36349. gn->type = GEN_OTHERNAME;
  36350. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36351. XMEMSET(outbuf,0,sizeof(outbuf));
  36352. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36353. AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  36354. GENERAL_NAME_free(gn);
  36355. /* test for GEN_X400 */
  36356. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36357. gn->type = GEN_X400;
  36358. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36359. XMEMSET(outbuf,0,sizeof(outbuf));
  36360. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36361. AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  36362. GENERAL_NAME_free(gn);
  36363. /* test for GEN_EDIPARTY */
  36364. AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
  36365. gn->type = GEN_EDIPARTY;
  36366. AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
  36367. XMEMSET(outbuf,0,sizeof(outbuf));
  36368. AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  36369. AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  36370. GENERAL_NAME_free(gn);
  36371. BIO_free(out);
  36372. printf(resultFmt, passed);
  36373. #endif /* OPENSSL_ALL */
  36374. return 0;
  36375. }
  36376. static int test_wolfSSL_sk_DIST_POINT(void)
  36377. {
  36378. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  36379. !defined(NO_RSA)
  36380. X509* x509;
  36381. unsigned char buf[4096];
  36382. const unsigned char* bufPt;
  36383. int bytes, i, j;
  36384. XFILE f;
  36385. DIST_POINT* dp;
  36386. DIST_POINT_NAME* dpn;
  36387. GENERAL_NAME* gn;
  36388. ASN1_IA5STRING* uri;
  36389. STACK_OF(DIST_POINT)* dps;
  36390. STACK_OF(GENERAL_NAME)* gns;
  36391. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  36392. printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
  36393. f = XFOPEN(cliCertDerCrlDistPoint, "rb");
  36394. AssertTrue((f != XBADFILE));
  36395. AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  36396. XFCLOSE(f);
  36397. bufPt = buf;
  36398. AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  36399. AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  36400. NID_crl_distribution_points, NULL, NULL));
  36401. AssertIntEQ(sk_DIST_POINT_num(dps), 1);
  36402. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  36403. AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
  36404. AssertNotNull(dpn = dp->distpoint);
  36405. /* this should be type 0, fullname */
  36406. AssertIntEQ(dpn->type, 0);
  36407. gns = dp->distpoint->name.fullname;
  36408. AssertNotNull(gns);
  36409. AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
  36410. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  36411. gn = sk_GENERAL_NAME_value(gns, j);
  36412. AssertIntEQ(gn->type, GEN_URI);
  36413. AssertNotNull(uri = gn->d.uniformResourceIdentifier);
  36414. AssertNotNull(uri->data);
  36415. AssertIntGT(uri->length, 0);
  36416. }
  36417. }
  36418. X509_free(x509);
  36419. CRL_DIST_POINTS_free(dps);
  36420. printf(resultFmt, passed);
  36421. #endif
  36422. return 0;
  36423. }
  36424. static int test_wolfSSL_MD4(void)
  36425. {
  36426. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  36427. MD4_CTX md4;
  36428. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  36429. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  36430. "789012345678901234567890";
  36431. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  36432. "\xcc\x05\x36";
  36433. int msgSz = (int)XSTRLEN(msg);
  36434. printf(testingFmt, "wolfSSL_MD4()");
  36435. XMEMSET(out, 0, sizeof(out));
  36436. MD4_Init(&md4);
  36437. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  36438. MD4_Final(out, &md4);
  36439. AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  36440. printf(resultFmt, passed);
  36441. #endif
  36442. return 0;
  36443. }
  36444. static int test_wolfSSL_verify_mode(void)
  36445. {
  36446. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  36447. WOLFSSL* ssl;
  36448. WOLFSSL_CTX* ctx;
  36449. printf(testingFmt, "test_wolfSSL_verify()");
  36450. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36451. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36452. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36453. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36454. AssertNotNull(ssl = SSL_new(ctx));
  36455. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36456. SSL_free(ssl);
  36457. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  36458. AssertNotNull(ssl = SSL_new(ctx));
  36459. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36460. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36461. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36462. AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  36463. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36464. SSL_free(ssl);
  36465. wolfSSL_CTX_set_verify(ctx,
  36466. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36467. AssertNotNull(ssl = SSL_new(ctx));
  36468. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  36469. AssertIntEQ(SSL_get_verify_mode(ssl),
  36470. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36471. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  36472. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36473. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36474. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  36475. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  36476. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  36477. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  36478. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36479. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  36480. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  36481. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  36482. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  36483. AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  36484. #endif
  36485. AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
  36486. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  36487. SSL_free(ssl);
  36488. SSL_CTX_free(ctx);
  36489. printf(resultFmt, passed);
  36490. #endif
  36491. return 0;
  36492. }
  36493. static int test_wolfSSL_verify_depth(void)
  36494. {
  36495. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36496. WOLFSSL* ssl;
  36497. WOLFSSL_CTX* ctx;
  36498. long depth;
  36499. printf(testingFmt, "test_wolfSSL_verify_depth()");
  36500. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36501. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  36502. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  36503. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
  36504. AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  36505. AssertNotNull(ssl = SSL_new(ctx));
  36506. AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  36507. SSL_free(ssl);
  36508. SSL_CTX_set_verify_depth(ctx, -1);
  36509. AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  36510. SSL_CTX_set_verify_depth(ctx, 2);
  36511. AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  36512. AssertNotNull(ssl = SSL_new(ctx));
  36513. AssertIntEQ(2, SSL_get_verify_depth(ssl));
  36514. SSL_free(ssl);
  36515. SSL_CTX_free(ctx);
  36516. printf(resultFmt, passed);
  36517. #endif
  36518. return 0;
  36519. }
  36520. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36521. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  36522. * buffer of 64 bytes.
  36523. *
  36524. * returns the size of the digest buffer on success and a negative value on
  36525. * failure.
  36526. */
  36527. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
  36528. {
  36529. HMAC_CTX ctx1;
  36530. HMAC_CTX ctx2;
  36531. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  36532. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  36533. unsigned char long_key[] =
  36534. "0123456789012345678901234567890123456789"
  36535. "0123456789012345678901234567890123456789"
  36536. "0123456789012345678901234567890123456789"
  36537. "0123456789012345678901234567890123456789";
  36538. unsigned char msg[] = "message to hash";
  36539. unsigned int digestSz = 64;
  36540. int keySz = sizeof(key);
  36541. int long_keySz = sizeof(long_key);
  36542. int msgSz = sizeof(msg);
  36543. unsigned char digest2[64];
  36544. unsigned int digestSz2 = 64;
  36545. HMAC_CTX_init(&ctx1);
  36546. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36547. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36548. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36549. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36550. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36551. HMAC_CTX_cleanup(&ctx1);
  36552. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36553. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  36554. HMAC_CTX_cleanup(&ctx2);
  36555. AssertIntEQ(digestSz, digestSz2);
  36556. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36557. /* test HMAC_Init with NULL key */
  36558. /* init after copy */
  36559. printf("test HMAC_Init with NULL key (0)\n");
  36560. HMAC_CTX_init(&ctx1);
  36561. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36562. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36563. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36564. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36565. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36566. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36567. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36568. HMAC_CTX_cleanup(&ctx1);
  36569. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36570. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36571. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36572. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36573. HMAC_CTX_cleanup(&ctx2);
  36574. AssertIntEQ(digestSz, digestSz2);
  36575. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36576. /* long key */
  36577. printf("test HMAC_Init with NULL key (1)\n");
  36578. HMAC_CTX_init(&ctx1);
  36579. AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
  36580. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36581. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36582. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36583. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36584. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36585. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36586. HMAC_CTX_cleanup(&ctx1);
  36587. AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  36588. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36589. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36590. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36591. HMAC_CTX_cleanup(&ctx2);
  36592. AssertIntEQ(digestSz, digestSz2);
  36593. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36594. /* init before copy */
  36595. printf("test HMAC_Init with NULL key (2)\n");
  36596. HMAC_CTX_init(&ctx1);
  36597. AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  36598. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36599. AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  36600. AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  36601. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36602. AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  36603. AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  36604. HMAC_CTX_cleanup(&ctx1);
  36605. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36606. AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  36607. AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  36608. HMAC_CTX_cleanup(&ctx2);
  36609. AssertIntEQ(digestSz, digestSz2);
  36610. AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  36611. return digestSz;
  36612. }
  36613. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  36614. static int test_wolfSSL_HMAC_CTX(void)
  36615. {
  36616. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  36617. unsigned char digest[64];
  36618. int digestSz;
  36619. printf(testingFmt, "wolfSSL_HMAC_CTX()");
  36620. #ifndef NO_SHA
  36621. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
  36622. AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  36623. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  36624. #endif /* !NO_SHA */
  36625. #ifdef WOLFSSL_SHA224
  36626. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
  36627. AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  36628. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  36629. "\x02\x0E", digest, digestSz), 0);
  36630. #endif /* WOLFSSL_SHA224 */
  36631. #ifndef NO_SHA256
  36632. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
  36633. AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  36634. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  36635. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  36636. #endif /* !NO_SHA256 */
  36637. #ifdef WOLFSSL_SHA384
  36638. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
  36639. AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  36640. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  36641. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  36642. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  36643. digest, digestSz), 0);
  36644. #endif /* WOLFSSL_SHA384 */
  36645. #ifdef WOLFSSL_SHA512
  36646. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
  36647. AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  36648. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  36649. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  36650. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  36651. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  36652. digest, digestSz), 0);
  36653. #endif /* WOLFSSL_SHA512 */
  36654. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
  36655. AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
  36656. AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  36657. "\xE4\x98\xDD", digest, digestSz), 0);
  36658. #endif /* !NO_MD5 */
  36659. printf(resultFmt, passed);
  36660. #endif
  36661. return 0;
  36662. }
  36663. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36664. static void sslMsgCb(int w, int version, int type, const void* buf,
  36665. size_t sz, SSL* ssl, void* arg)
  36666. {
  36667. int i;
  36668. unsigned char* pt = (unsigned char*)buf;
  36669. printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
  36670. (int)sz, version, type);
  36671. for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
  36672. printf("\n");
  36673. (void)ssl;
  36674. (void)arg;
  36675. }
  36676. #endif /* OPENSSL_EXTRA */
  36677. static int test_wolfSSL_msg_callback(void)
  36678. {
  36679. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  36680. WOLFSSL* ssl;
  36681. WOLFSSL_CTX* ctx;
  36682. printf(testingFmt, "wolfSSL_msg_callback()");
  36683. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  36684. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  36685. SSL_FILETYPE_PEM));
  36686. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  36687. SSL_FILETYPE_PEM));
  36688. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  36689. SSL_SUCCESS);
  36690. AssertNotNull(ssl = SSL_new(ctx));
  36691. AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  36692. AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  36693. AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  36694. SSL_free(ssl);
  36695. SSL_CTX_free(ctx);
  36696. printf(resultFmt, passed);
  36697. #endif
  36698. return 0;
  36699. }
  36700. static int test_wolfSSL_SHA(void)
  36701. {
  36702. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  36703. printf(testingFmt, "wolfSSL_SHA()");
  36704. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  36705. (!defined(HAVE_FIPS) || \
  36706. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  36707. {
  36708. const unsigned char in[] = "abc";
  36709. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  36710. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  36711. unsigned char out[WC_SHA_DIGEST_SIZE];
  36712. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36713. AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
  36714. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36715. /* SHA interface test */
  36716. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  36717. AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
  36718. AssertNotNull(SHA(in, 0, out));
  36719. AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  36720. AssertNotNull(SHA(NULL, 0, out));
  36721. AssertNotNull(SHA(NULL, 0, NULL));
  36722. AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
  36723. AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  36724. }
  36725. #endif
  36726. #if !defined(NO_SHA256)
  36727. {
  36728. const unsigned char in[] = "abc";
  36729. unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  36730. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  36731. "\x15\xAD";
  36732. unsigned char out[WC_SHA256_DIGEST_SIZE];
  36733. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  36734. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36735. AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
  36736. #else
  36737. AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  36738. #endif
  36739. AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  36740. }
  36741. #endif
  36742. #if defined(WOLFSSL_SHA384)
  36743. {
  36744. const unsigned char in[] = "abc";
  36745. unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  36746. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  36747. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  36748. "\xc8\x25\xa7";
  36749. unsigned char out[WC_SHA384_DIGEST_SIZE];
  36750. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  36751. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36752. AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
  36753. #else
  36754. AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  36755. #endif
  36756. AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  36757. }
  36758. #endif
  36759. #if defined(WOLFSSL_SHA512)
  36760. {
  36761. const unsigned char in[] = "abc";
  36762. unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  36763. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  36764. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  36765. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  36766. "\xa5\x4c\xa4\x9f";
  36767. unsigned char out[WC_SHA512_DIGEST_SIZE];
  36768. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  36769. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  36770. AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
  36771. #else
  36772. AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  36773. #endif
  36774. AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  36775. }
  36776. #endif
  36777. printf(resultFmt, passed);
  36778. #endif
  36779. return 0;
  36780. }
  36781. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  36782. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36783. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36784. static void binary_dump(void *ptr, int size)
  36785. {
  36786. #ifdef WOLFSSL_EVP_PRINT
  36787. int i = 0;
  36788. unsigned char *p = (unsigned char *) ptr;
  36789. printf("{");
  36790. while((p != NULL) && (i < size)) {
  36791. if((i % 8) == 0) {
  36792. printf("\n");
  36793. printf(" ");
  36794. }
  36795. printf("0x%02x, ", p[i]);
  36796. i++;
  36797. }
  36798. printf("\n};\n");
  36799. #else
  36800. (void) ptr;
  36801. (void) size;
  36802. #endif
  36803. }
  36804. static int last_val = 0x0f;
  36805. static int check_result(unsigned char *data, int len)
  36806. {
  36807. int i;
  36808. for( ; len; ) {
  36809. last_val = (last_val + 1) % 16;
  36810. for(i = 0; i < 16; len--, i++, data++)
  36811. if(*data != last_val) {
  36812. return -1;
  36813. }
  36814. }
  36815. return 0;
  36816. }
  36817. static int r_offset;
  36818. static int w_offset;
  36819. static void init_offset(void)
  36820. {
  36821. r_offset = 0;
  36822. w_offset = 0;
  36823. }
  36824. static void get_record(unsigned char *data, unsigned char *buf, int len)
  36825. {
  36826. XMEMCPY(buf, data+r_offset, len);
  36827. r_offset += len;
  36828. }
  36829. static void set_record(unsigned char *data, unsigned char *buf, int len)
  36830. {
  36831. XMEMCPY(data+w_offset, buf, len);
  36832. w_offset += len;
  36833. }
  36834. static void set_plain(unsigned char *plain, int rec)
  36835. {
  36836. int i, j;
  36837. unsigned char *p = plain;
  36838. #define BLOCKSZ 16
  36839. for(i=0; i<(rec/BLOCKSZ); i++){
  36840. for(j=0; j<BLOCKSZ; j++)
  36841. *p++ = (i % 16);
  36842. }
  36843. }
  36844. #endif
  36845. static int test_wolfSSL_EVP_Cipher_extra(void)
  36846. {
  36847. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  36848. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  36849. /* aes128-cbc, keylen=16, ivlen=16 */
  36850. byte aes128_cbc_key[] = {
  36851. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36852. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  36853. };
  36854. byte aes128_cbc_iv[] = {
  36855. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  36856. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  36857. };
  36858. /* teset data size table */
  36859. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  36860. int test_drive2[] = {8, 3, 8, 512, 0};
  36861. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  36862. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  36863. int test_drive_len[100];
  36864. int ret = 0;
  36865. EVP_CIPHER_CTX *evp = NULL;
  36866. int ilen = 0;
  36867. int klen = 0;
  36868. int i, j;
  36869. const EVP_CIPHER *type;
  36870. byte *iv;
  36871. byte *key;
  36872. int ivlen;
  36873. int keylen;
  36874. #define RECORDS 16
  36875. #define BUFFSZ 512
  36876. byte plain [BUFFSZ * RECORDS];
  36877. byte cipher[BUFFSZ * RECORDS];
  36878. byte inb[BUFFSZ];
  36879. byte outb[BUFFSZ+16];
  36880. int outl, inl;
  36881. iv = aes128_cbc_iv;
  36882. ivlen = sizeof(aes128_cbc_iv);
  36883. key = aes128_cbc_key;
  36884. keylen = sizeof(aes128_cbc_key);
  36885. type = EVP_aes_128_cbc();
  36886. set_plain(plain, BUFFSZ * RECORDS);
  36887. SSL_library_init();
  36888. AssertNotNull(evp = EVP_CIPHER_CTX_new());
  36889. AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  36890. AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  36891. klen = EVP_CIPHER_CTX_key_length(evp);
  36892. if (klen > 0 && keylen != klen) {
  36893. AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  36894. }
  36895. ilen = EVP_CIPHER_CTX_iv_length(evp);
  36896. if (ilen > 0 && ivlen != ilen) {
  36897. AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  36898. }
  36899. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  36900. for (j = 0; j<RECORDS; j++)
  36901. {
  36902. inl = BUFFSZ;
  36903. get_record(plain, inb, inl);
  36904. AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  36905. set_record(cipher, outb, outl);
  36906. }
  36907. for (i = 0; test_drive[i]; i++) {
  36908. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  36909. init_offset();
  36910. test_drive_len[i] = 0;
  36911. for (j = 0; test_drive[i][j]; j++)
  36912. {
  36913. inl = test_drive[i][j];
  36914. test_drive_len[i] += inl;
  36915. get_record(plain, inb, inl);
  36916. AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
  36917. /* output to cipher buffer, so that following Dec test can detect
  36918. if any error */
  36919. set_record(cipher, outb, outl);
  36920. }
  36921. EVP_CipherFinal(evp, outb, &outl);
  36922. if(outl > 0)
  36923. set_record(cipher, outb, outl);
  36924. }
  36925. for (i = 0; test_drive[i]; i++) {
  36926. last_val = 0x0f;
  36927. AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  36928. init_offset();
  36929. for (j = 0; test_drive[i][j]; j++){
  36930. inl = test_drive[i][j];
  36931. get_record(cipher, inb, inl);
  36932. AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
  36933. binary_dump(outb, outl);
  36934. AssertIntEQ((ret = check_result(outb, outl)), 0);
  36935. AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
  36936. }
  36937. ret = EVP_CipherFinal(evp, outb, &outl);
  36938. binary_dump(outb, outl);
  36939. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  36940. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  36941. AssertTrue(ret);
  36942. }
  36943. EVP_CIPHER_CTX_free(evp);
  36944. #endif /* test_EVP_Cipher */
  36945. return 0;
  36946. }
  36947. static int test_wolfSSL_PEM_read_DHparams(void)
  36948. {
  36949. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  36950. !defined(NO_FILESYSTEM)
  36951. DH* dh;
  36952. XFILE fp;
  36953. unsigned char derOut[300];
  36954. unsigned char* derOutBuf = derOut;
  36955. int derOutSz = 0;
  36956. unsigned char derExpected[300];
  36957. int derExpectedSz = 0;
  36958. printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
  36959. XMEMSET(derOut, 0, sizeof(derOut));
  36960. XMEMSET(derExpected, 0, sizeof(derExpected));
  36961. /* open DH param file, read into DH struct */
  36962. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  36963. /* bad args */
  36964. AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  36965. AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  36966. /* good args */
  36967. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  36968. XFCLOSE(fp);
  36969. /* read in certs/dh2048.der for comparison against exported params */
  36970. fp = XFOPEN("./certs/dh2048.der", "rb");
  36971. AssertTrue(fp != XBADFILE);
  36972. derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
  36973. XFCLOSE(fp);
  36974. /* export DH back to DER and compare */
  36975. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  36976. AssertIntEQ(derOutSz, derExpectedSz);
  36977. AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  36978. DH_free(dh);
  36979. dh = NULL;
  36980. /* Test parsing with X9.42 header */
  36981. fp = XFOPEN("./certs/x942dh2048.pem", "rb");
  36982. AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  36983. XFCLOSE(fp);
  36984. DH_free(dh);
  36985. printf(resultFmt, passed);
  36986. #endif
  36987. return 0;
  36988. }
  36989. static int test_wolfSSL_AES_ecb_encrypt(void)
  36990. {
  36991. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
  36992. AES_KEY aes;
  36993. const byte msg[] =
  36994. {
  36995. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  36996. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  36997. };
  36998. const byte verify[] =
  36999. {
  37000. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  37001. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  37002. };
  37003. const byte key[] =
  37004. {
  37005. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  37006. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  37007. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  37008. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  37009. };
  37010. byte out[AES_BLOCK_SIZE];
  37011. printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
  37012. AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  37013. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37014. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  37015. AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  37016. #ifdef HAVE_AES_DECRYPT
  37017. AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  37018. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37019. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  37020. AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  37021. #endif
  37022. /* test bad arguments */
  37023. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  37024. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  37025. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  37026. printf(resultFmt, passed);
  37027. #endif
  37028. return 0;
  37029. }
  37030. static int test_wolfSSL_MD5(void)
  37031. {
  37032. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37033. byte input1[] = "";
  37034. byte input2[] = "message digest";
  37035. byte hash[WC_MD5_DIGEST_SIZE];
  37036. unsigned char output1[] =
  37037. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37038. unsigned char output2[] =
  37039. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37040. WOLFSSL_MD5_CTX md5;
  37041. printf(testingFmt, "wolfSSL_MD5()");
  37042. XMEMSET(&md5, 0, sizeof(md5));
  37043. /* Test cases for illegal parameters */
  37044. AssertIntEQ(MD5_Init(NULL), 0);
  37045. AssertIntEQ(MD5_Init(&md5), 1);
  37046. AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
  37047. AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37048. AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37049. AssertIntEQ(MD5_Final(NULL, &md5), 0);
  37050. AssertIntEQ(MD5_Final(hash, NULL), 0);
  37051. AssertIntEQ(MD5_Final(NULL, NULL), 0);
  37052. /* Init MD5 CTX */
  37053. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37054. AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
  37055. XSTRLEN((const char*)&input1)), 1);
  37056. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37057. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37058. /* Init MD5 CTX */
  37059. AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37060. AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37061. (int)XSTRLEN((const char*)input2)), 1);
  37062. AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37063. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37064. #if !defined(NO_OLD_NAMES) && \
  37065. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37066. AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37067. AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37068. AssertPtrNE(MD5(input1, 1, NULL), NULL);
  37069. AssertPtrNE(MD5(NULL, 0, NULL), NULL);
  37070. AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
  37071. AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37072. AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
  37073. AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37074. {
  37075. byte data[] = "Data to be hashed.";
  37076. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37077. AssertNotNull(MD5(data, sizeof(data), NULL));
  37078. AssertNotNull(MD5(data, sizeof(data), hash));
  37079. AssertNotNull(MD5(NULL, 0, hash));
  37080. AssertNull(MD5(NULL, sizeof(data), hash));
  37081. }
  37082. #endif
  37083. printf(resultFmt, passed);
  37084. #endif
  37085. return 0;
  37086. }
  37087. static int test_wolfSSL_MD5_Transform(void)
  37088. {
  37089. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37090. byte input1[] = "";
  37091. byte input2[] = "abc";
  37092. byte local[WC_MD5_BLOCK_SIZE];
  37093. word32 sLen = 0;
  37094. #ifdef BIG_ENDIAN_ORDER
  37095. unsigned char output1[] =
  37096. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37097. unsigned char output2[] =
  37098. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37099. #else
  37100. unsigned char output1[] =
  37101. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37102. unsigned char output2[] =
  37103. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37104. #endif
  37105. union {
  37106. wc_Md5 native;
  37107. MD5_CTX compat;
  37108. } md5;
  37109. printf(testingFmt, "wolfSSL_MD5_Transform()");
  37110. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37111. XMEMSET(&local, 0, sizeof(local));
  37112. /* sanity check */
  37113. AssertIntEQ(MD5_Transform(NULL, NULL), 0);
  37114. AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37115. AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37116. AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37117. AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37118. AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37119. /* Init MD5 CTX */
  37120. AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37121. /* Do Transform*/
  37122. sLen = (word32)XSTRLEN((char*)input1);
  37123. XMEMCPY(local, input1, sLen);
  37124. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37125. AssertIntEQ(XMEMCMP(md5.native.digest, output1,
  37126. WC_MD5_DIGEST_SIZE), 0);
  37127. /* Init MD5 CTX */
  37128. AssertIntEQ(MD5_Init(&md5.compat), 1);
  37129. sLen = (word32)XSTRLEN((char*)input2);
  37130. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37131. XMEMCPY(local, input2, sLen);
  37132. AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37133. AssertIntEQ(XMEMCMP(md5.native.digest, output2,
  37134. WC_MD5_DIGEST_SIZE), 0);
  37135. printf(resultFmt, passed);
  37136. #endif
  37137. return 0;
  37138. }
  37139. static int test_wolfSSL_SHA224(void)
  37140. {
  37141. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37142. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37143. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37144. unsigned char input[] =
  37145. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37146. unsigned char output[] =
  37147. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37148. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37149. size_t inLen;
  37150. byte hash[WC_SHA224_DIGEST_SIZE];
  37151. printf(testingFmt, "wolfSSL_SHA224()");
  37152. inLen = XSTRLEN((char*)input);
  37153. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37154. AssertNull(SHA224(NULL, inLen, hash));
  37155. AssertNotNull(SHA224(input, 0, hash));
  37156. AssertNotNull(SHA224(input, inLen, NULL));
  37157. AssertNotNull(SHA224(NULL, 0, hash));
  37158. AssertNotNull(SHA224(NULL, 0, NULL));
  37159. AssertNotNull(SHA224(input, inLen, hash));
  37160. AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37161. printf(resultFmt, passed);
  37162. #endif
  37163. return 0;
  37164. }
  37165. static int test_wolfSSL_SHA_Transform(void)
  37166. {
  37167. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37168. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37169. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37170. byte input1[] = "";
  37171. byte input2[] = "abc";
  37172. byte local[WC_SHA_BLOCK_SIZE];
  37173. word32 sLen = 0;
  37174. #ifdef BIG_ENDIAN_ORDER
  37175. unsigned char output1[] =
  37176. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37177. "\x09\xf7\x3a\x93";
  37178. unsigned char output2[] =
  37179. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37180. "\x7c\x6e\x08\xb8";
  37181. #else
  37182. unsigned char output1[] =
  37183. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37184. "\x93\x3a\xf7\x09";
  37185. unsigned char output2[] =
  37186. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37187. "\xb8\x08\x6e\x7c";
  37188. #endif
  37189. union {
  37190. wc_Sha native;
  37191. SHA_CTX compat;
  37192. } sha;
  37193. union {
  37194. wc_Sha native;
  37195. SHA_CTX compat;
  37196. } sha1;
  37197. printf(testingFmt, "wolfSSL_SHA_Transform()");
  37198. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37199. XMEMSET(&local, 0, sizeof(local));
  37200. /* sanity check */
  37201. AssertIntEQ(SHA_Transform(NULL, NULL), 0);
  37202. AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37203. AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37204. AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
  37205. AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37206. AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37207. AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37208. AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37209. AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37210. /* Init SHA CTX */
  37211. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37212. /* Do Transform*/
  37213. sLen = (word32)XSTRLEN((char*)input1);
  37214. XMEMCPY(local, input1, sLen);
  37215. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37216. AssertIntEQ(XMEMCMP(sha.native.digest, output1,
  37217. WC_SHA_DIGEST_SIZE), 0);
  37218. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37219. /* Init SHA CTX */
  37220. AssertIntEQ(SHA_Init(&sha.compat), 1);
  37221. sLen = (word32)XSTRLEN((char*)input2);
  37222. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37223. XMEMCPY(local, input2, sLen);
  37224. AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37225. AssertIntEQ(XMEMCMP(sha.native.digest, output2,
  37226. WC_SHA_DIGEST_SIZE), 0);
  37227. AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37228. /* SHA1 */
  37229. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37230. /* Init SHA CTX */
  37231. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37232. /* Do Transform*/
  37233. sLen = (word32)XSTRLEN((char*)input1);
  37234. XMEMCPY(local, input1, sLen);
  37235. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37236. AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
  37237. WC_SHA_DIGEST_SIZE), 0);
  37238. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37239. /* Init SHA CTX */
  37240. AssertIntEQ(SHA1_Init(&sha1.compat), 1);
  37241. sLen = (word32)XSTRLEN((char*)input2);
  37242. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37243. XMEMCPY(local, input2, sLen);
  37244. AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37245. AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
  37246. WC_SHA_DIGEST_SIZE), 0);
  37247. AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37248. printf(resultFmt, passed);
  37249. #endif
  37250. #endif
  37251. return 0;
  37252. }
  37253. static int test_wolfSSL_SHA256_Transform(void)
  37254. {
  37255. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37256. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37257. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37258. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH)
  37259. byte input1[] = "";
  37260. byte input2[] = "abc";
  37261. byte local[WC_SHA256_BLOCK_SIZE];
  37262. word32 sLen = 0;
  37263. #ifdef BIG_ENDIAN_ORDER
  37264. unsigned char output1[] =
  37265. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37266. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37267. unsigned char output2[] =
  37268. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37269. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37270. #else
  37271. unsigned char output1[] =
  37272. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37273. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37274. unsigned char output2[] =
  37275. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37276. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37277. #endif
  37278. union {
  37279. wc_Sha256 native;
  37280. SHA256_CTX compat;
  37281. } sha256;
  37282. printf(testingFmt, "wolfSSL_SHA256_Transform()");
  37283. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37284. XMEMSET(&local, 0, sizeof(local));
  37285. /* sanity check */
  37286. AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
  37287. AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37288. AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37289. AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37290. AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37291. AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37292. /* Init SHA256 CTX */
  37293. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37294. /* Do Transform*/
  37295. sLen = (word32)XSTRLEN((char*)input1);
  37296. XMEMCPY(local, input1, sLen);
  37297. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37298. AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
  37299. WC_SHA256_DIGEST_SIZE), 0);
  37300. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37301. /* Init SHA256 CTX */
  37302. AssertIntEQ(SHA256_Init(&sha256.compat), 1);
  37303. sLen = (word32)XSTRLEN((char*)input2);
  37304. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37305. XMEMCPY(local, input2, sLen);
  37306. AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37307. AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
  37308. WC_SHA256_DIGEST_SIZE), 0);
  37309. AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37310. printf(resultFmt, passed);
  37311. #endif
  37312. #endif
  37313. return 0;
  37314. }
  37315. static int test_wolfSSL_SHA256(void)
  37316. {
  37317. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37318. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37319. unsigned char input[] =
  37320. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37321. unsigned char output[] =
  37322. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37323. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37324. "\x06\xC1";
  37325. size_t inLen;
  37326. byte hash[WC_SHA256_DIGEST_SIZE];
  37327. printf(testingFmt, "wolfSSL_SHA256()");
  37328. inLen = XSTRLEN((char*)input);
  37329. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37330. AssertNotNull(SHA256(input, inLen, hash));
  37331. AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37332. printf(resultFmt, passed);
  37333. #endif
  37334. return 0;
  37335. }
  37336. static int test_wolfSSL_SHA512_Transform(void)
  37337. {
  37338. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37339. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37340. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37341. byte input1[] = "";
  37342. byte input2[] = "abc";
  37343. byte local[WC_SHA512_BLOCK_SIZE];
  37344. word32 sLen = 0;
  37345. #ifdef BIG_ENDIAN_ORDER
  37346. unsigned char output1[] =
  37347. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37348. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37349. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37350. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37351. unsigned char output2[] =
  37352. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37353. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37354. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37355. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37356. #else
  37357. unsigned char output1[] =
  37358. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37359. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37360. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37361. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37362. unsigned char output2[] =
  37363. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37364. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37365. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37366. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37367. #endif
  37368. union {
  37369. wc_Sha512 native;
  37370. SHA512_CTX compat;
  37371. } sha512;
  37372. printf(testingFmt, "wolfSSL_SHA512_Transform()");
  37373. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37374. XMEMSET(&local, 0, sizeof(local));
  37375. /* sanity check */
  37376. AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
  37377. AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37378. AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37379. AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37380. AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37381. AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37382. /* Init SHA512 CTX */
  37383. AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37384. /* Do Transform*/
  37385. sLen = (word32)XSTRLEN((char*)input1);
  37386. XMEMCPY(local, input1, sLen);
  37387. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37388. AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
  37389. WC_SHA512_DIGEST_SIZE), 0);
  37390. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37391. /* Init SHA512 CTX */
  37392. AssertIntEQ(SHA512_Init(&sha512.compat), 1);
  37393. sLen = (word32)XSTRLEN((char*)input2);
  37394. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37395. XMEMCPY(local, input2, sLen);
  37396. AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37397. AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
  37398. WC_SHA512_DIGEST_SIZE), 0);
  37399. AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37400. (void)input1;
  37401. printf(resultFmt, passed);
  37402. #endif
  37403. #endif
  37404. return 0;
  37405. }
  37406. static int test_wolfSSL_X509_get_serialNumber(void)
  37407. {
  37408. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  37409. ASN1_INTEGER* a;
  37410. BIGNUM* bn;
  37411. X509* x509;
  37412. char *serialHex;
  37413. byte serial[3];
  37414. int serialSz;
  37415. printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
  37416. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  37417. SSL_FILETYPE_PEM));
  37418. AssertNotNull(a = X509_get_serialNumber(x509));
  37419. /* check on value of ASN1 Integer */
  37420. AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  37421. /* test setting serial number and then retrieving it */
  37422. AssertNotNull(a = ASN1_INTEGER_new());
  37423. ASN1_INTEGER_set(a, 3);
  37424. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37425. serialSz = sizeof(serial);
  37426. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37427. WOLFSSL_SUCCESS);
  37428. AssertIntEQ(serialSz, 1);
  37429. AssertIntEQ(serial[0], 3);
  37430. ASN1_INTEGER_free(a);
  37431. /* test setting serial number with 0's in it */
  37432. serial[0] = 0x01;
  37433. serial[1] = 0x00;
  37434. serial[2] = 0x02;
  37435. AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
  37436. a->data[0] = ASN_INTEGER;
  37437. a->data[1] = sizeof(serial);
  37438. XMEMCPY(&a->data[2], serial, sizeof(serial));
  37439. a->length = sizeof(serial) + 2;
  37440. AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  37441. XMEMSET(serial, 0, sizeof(serial));
  37442. serialSz = sizeof(serial);
  37443. AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  37444. WOLFSSL_SUCCESS);
  37445. AssertIntEQ(serialSz, 3);
  37446. AssertIntEQ(serial[0], 0x01);
  37447. AssertIntEQ(serial[1], 0x00);
  37448. AssertIntEQ(serial[2], 0x02);
  37449. ASN1_INTEGER_free(a);
  37450. X509_free(x509); /* free's a */
  37451. AssertNotNull(serialHex = BN_bn2hex(bn));
  37452. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  37453. AssertStrEQ(serialHex, "01");
  37454. #else
  37455. AssertStrEQ(serialHex, "1");
  37456. #endif
  37457. OPENSSL_free(serialHex);
  37458. AssertIntEQ(BN_get_word(bn), 1);
  37459. BN_free(bn);
  37460. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  37461. a = ASN1_INTEGER_new();
  37462. if (a) {
  37463. AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  37464. DYNAMIC_TYPE_OPENSSL));
  37465. a->isDynamic = 1;
  37466. ASN1_INTEGER_free(a);
  37467. }
  37468. printf(resultFmt, passed);
  37469. #endif
  37470. return 0;
  37471. }
  37472. static int test_wolfSSL_OpenSSL_add_all_algorithms(void){
  37473. #if defined(OPENSSL_EXTRA)
  37474. printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
  37475. AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
  37476. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
  37477. AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
  37478. printf(resultFmt, passed);
  37479. #endif
  37480. return 0;
  37481. }
  37482. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  37483. {
  37484. #if defined(OPENSSL_EXTRA)
  37485. #define MAX_HEXSTR_BUFSZ 9
  37486. #define NUM_CASES 5
  37487. struct Output {
  37488. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  37489. long ret;
  37490. };
  37491. int i;
  37492. int j;
  37493. const char* inputs[NUM_CASES] = {
  37494. "aabcd1357e",
  37495. "01:12:23:34:a5:b6:c7:d8:e9",
  37496. ":01:02",
  37497. "012",
  37498. ":ab:ac:d"
  37499. };
  37500. struct Output expectedOutputs[NUM_CASES] = {
  37501. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  37502. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  37503. {{0x01, 0x02}, 2},
  37504. {{0x00}, 0},
  37505. {{0x00}, 0}
  37506. };
  37507. long len = 0;
  37508. unsigned char* returnedBuf = NULL;
  37509. printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
  37510. for (i = 0; i < NUM_CASES; ++i) {
  37511. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  37512. if (returnedBuf == NULL) {
  37513. AssertIntEQ(expectedOutputs[i].ret, 0);
  37514. continue;
  37515. }
  37516. AssertIntEQ(expectedOutputs[i].ret, len);
  37517. for (j = 0; j < len; ++j) {
  37518. AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  37519. }
  37520. OPENSSL_free(returnedBuf);
  37521. }
  37522. printf(resultFmt, passed);
  37523. #endif
  37524. return 0;
  37525. }
  37526. static int test_wolfSSL_ASN1_STRING_print_ex(void){
  37527. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  37528. #ifndef NO_BIO
  37529. ASN1_STRING* asn_str;
  37530. const char data[] = "Hello wolfSSL!";
  37531. ASN1_STRING* esc_str;
  37532. const char esc_data[] = "a+;<>";
  37533. BIO *bio;
  37534. unsigned long flags;
  37535. int p_len;
  37536. unsigned char rbuf[255];
  37537. printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
  37538. /* setup */
  37539. XMEMSET(rbuf, 0, 255);
  37540. bio = BIO_new(BIO_s_mem());
  37541. BIO_set_write_buf_size(bio,255);
  37542. asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  37543. ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
  37544. esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  37545. ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
  37546. /* no flags */
  37547. XMEMSET(rbuf, 0, 255);
  37548. flags = 0;
  37549. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37550. AssertIntEQ(p_len, 15);
  37551. BIO_read(bio, (void*)rbuf, 15);
  37552. AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
  37553. /* RFC2253 Escape */
  37554. XMEMSET(rbuf, 0, 255);
  37555. flags = ASN1_STRFLGS_ESC_2253;
  37556. p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
  37557. AssertIntEQ(p_len, 9);
  37558. BIO_read(bio, (void*)rbuf, 9);
  37559. AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  37560. /* Show type */
  37561. XMEMSET(rbuf, 0, 255);
  37562. flags = ASN1_STRFLGS_SHOW_TYPE;
  37563. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37564. AssertIntEQ(p_len, 28);
  37565. BIO_read(bio, (void*)rbuf, 28);
  37566. AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  37567. /* Dump All */
  37568. XMEMSET(rbuf, 0, 255);
  37569. flags = ASN1_STRFLGS_DUMP_ALL;
  37570. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37571. AssertIntEQ(p_len, 31);
  37572. BIO_read(bio, (void*)rbuf, 31);
  37573. AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  37574. /* Dump Der */
  37575. XMEMSET(rbuf, 0, 255);
  37576. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  37577. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37578. AssertIntEQ(p_len, 35);
  37579. BIO_read(bio, (void*)rbuf, 35);
  37580. AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  37581. /* Dump All + Show type */
  37582. XMEMSET(rbuf, 0, 255);
  37583. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  37584. p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
  37585. AssertIntEQ(p_len, 44);
  37586. BIO_read(bio, (void*)rbuf, 44);
  37587. AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  37588. BIO_free(bio);
  37589. ASN1_STRING_free(asn_str);
  37590. ASN1_STRING_free(esc_str);
  37591. printf(resultFmt, passed);
  37592. #endif /* !NO_BIO */
  37593. #endif
  37594. return 0;
  37595. }
  37596. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
  37597. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  37598. WOLFSSL_ASN1_TIME *t;
  37599. WOLFSSL_ASN1_TIME *out;
  37600. WOLFSSL_ASN1_TIME *gtime;
  37601. int tlen = 0;
  37602. unsigned char *data;
  37603. printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
  37604. /* UTC Time test */
  37605. AssertNotNull(t = wolfSSL_ASN1_TIME_new());
  37606. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  37607. AssertNotNull(out = wolfSSL_ASN1_TIME_new());
  37608. t->type = ASN_UTC_TIME;
  37609. t->length = ASN_UTC_TIME_SIZE;
  37610. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  37611. tlen = wolfSSL_ASN1_TIME_get_length(t);
  37612. AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
  37613. data = wolfSSL_ASN1_TIME_get_data(t);
  37614. AssertStrEQ((char*)data, "050727123456Z");
  37615. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  37616. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37617. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37618. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37619. /* Generalized Time test */
  37620. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  37621. XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
  37622. XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
  37623. t->type = ASN_GENERALIZED_TIME;
  37624. t->length = ASN_GENERALIZED_TIME_SIZE;
  37625. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  37626. tlen = wolfSSL_ASN1_TIME_get_length(t);
  37627. AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
  37628. data = wolfSSL_ASN1_TIME_get_data(t);
  37629. AssertStrEQ((char*)data, "20050727123456Z");
  37630. gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
  37631. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37632. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37633. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37634. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37635. /* Null parameter test */
  37636. XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
  37637. gtime = NULL;
  37638. out = NULL;
  37639. t->type = ASN_UTC_TIME;
  37640. t->length = ASN_UTC_TIME_SIZE;
  37641. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  37642. AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  37643. AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  37644. AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  37645. AssertStrEQ((char*)gtime->data, "20050727123456Z");
  37646. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37647. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37648. printf(resultFmt, passed);
  37649. #endif
  37650. return 0;
  37651. }
  37652. static int test_wolfSSL_X509_CA_num(void){
  37653. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  37654. defined(HAVE_ECC) && !defined(NO_RSA)
  37655. WOLFSSL_X509_STORE *store;
  37656. WOLFSSL_X509 *x509_1, *x509_2;
  37657. int ca_num = 0;
  37658. printf(testingFmt, "wolfSSL_X509_CA_num()");
  37659. store = wolfSSL_X509_STORE_new();
  37660. x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37661. wolfSSL_X509_STORE_add_cert(store, x509_1);
  37662. ca_num = wolfSSL_X509_CA_num(store);
  37663. AssertIntEQ(ca_num, 1);
  37664. x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
  37665. wolfSSL_X509_STORE_add_cert(store, x509_2);
  37666. ca_num = wolfSSL_X509_CA_num(store);
  37667. AssertIntEQ(ca_num, 2);
  37668. wolfSSL_X509_free(x509_1);
  37669. wolfSSL_X509_free(x509_2);
  37670. wolfSSL_X509_STORE_free(store);
  37671. printf(resultFmt, passed);
  37672. #endif
  37673. return 0;
  37674. }
  37675. static int test_wolfSSL_X509_check_ca(void){
  37676. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37677. WOLFSSL_X509 *x509;
  37678. printf(testingFmt, "wolfSSL_X509_check_ca()");
  37679. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37680. AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
  37681. wolfSSL_X509_free(x509);
  37682. printf(resultFmt, passed);
  37683. #endif
  37684. return 0;
  37685. }
  37686. static int test_wolfSSL_X509_check_ip_asc(void){
  37687. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  37688. WOLFSSL_X509 *x509;
  37689. printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
  37690. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  37691. #if 0
  37692. /* TODO: add cert gen for testing positive case */
  37693. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  37694. #endif
  37695. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  37696. AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  37697. wolfSSL_X509_free(x509);
  37698. printf(resultFmt, passed);
  37699. #endif
  37700. return 0;
  37701. }
  37702. static int test_wolfSSL_make_cert(void)
  37703. {
  37704. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  37705. int ret;
  37706. Cert cert;
  37707. CertName name;
  37708. RsaKey key;
  37709. WC_RNG rng;
  37710. byte der[FOURK_BUF];
  37711. word32 idx;
  37712. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  37713. #ifdef OPENSSL_EXTRA
  37714. const unsigned char* pt;
  37715. int certSz;
  37716. X509* x509;
  37717. X509_NAME* x509name;
  37718. X509_NAME_ENTRY* entry;
  37719. ASN1_STRING* entryValue;
  37720. #endif
  37721. printf(testingFmt, "wolfSSL Make Certs");
  37722. XMEMSET(&name, 0, sizeof(CertName));
  37723. /* set up cert name */
  37724. XMEMCPY(name.country, "US", sizeof("US"));
  37725. name.countryEnc = CTC_PRINTABLE;
  37726. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  37727. name.stateEnc = CTC_UTF8;
  37728. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  37729. name.localityEnc = CTC_UTF8;
  37730. XMEMCPY(name.sur, "Test", sizeof("Test"));
  37731. name.surEnc = CTC_UTF8;
  37732. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  37733. name.orgEnc = CTC_UTF8;
  37734. XMEMCPY(name.unit, "Development", sizeof("Development"));
  37735. name.unitEnc = CTC_UTF8;
  37736. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  37737. name.commonNameEnc = CTC_UTF8;
  37738. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  37739. name.serialDevEnc = CTC_PRINTABLE;
  37740. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  37741. name.userIdEnc = CTC_PRINTABLE;
  37742. #ifdef WOLFSSL_MULTI_ATTRIB
  37743. #if CTC_MAX_ATTRIB > 2
  37744. {
  37745. NameAttrib* n;
  37746. n = &name.name[0];
  37747. n->id = ASN_DOMAIN_COMPONENT;
  37748. n->type = CTC_UTF8;
  37749. n->sz = sizeof("com");
  37750. XMEMCPY(n->value, "com", sizeof("com"));
  37751. n = &name.name[1];
  37752. n->id = ASN_DOMAIN_COMPONENT;
  37753. n->type = CTC_UTF8;
  37754. n->sz = sizeof("wolfssl");
  37755. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  37756. }
  37757. #endif
  37758. #endif /* WOLFSSL_MULTI_ATTRIB */
  37759. AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  37760. #ifndef HAVE_FIPS
  37761. AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  37762. #else
  37763. AssertIntEQ(wc_InitRng(&rng), 0);
  37764. #endif
  37765. /* load test RSA key */
  37766. idx = 0;
  37767. #if defined(USE_CERT_BUFFERS_1024)
  37768. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  37769. sizeof_server_key_der_1024), 0);
  37770. #elif defined(USE_CERT_BUFFERS_2048)
  37771. AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  37772. sizeof_server_key_der_2048), 0);
  37773. #else
  37774. /* error case, no RSA key loaded, happens later */
  37775. (void)idx;
  37776. #endif
  37777. XMEMSET(&cert, 0 , sizeof(Cert));
  37778. AssertIntEQ(wc_InitCert(&cert), 0);
  37779. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  37780. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  37781. cert.serialSz = (int)sizeof(mySerial);
  37782. cert.isCA = 1;
  37783. #ifndef NO_SHA256
  37784. cert.sigType = CTC_SHA256wRSA;
  37785. #else
  37786. cert.sigType = CTC_SHAwRSA;
  37787. #endif
  37788. /* add SKID from the Public Key */
  37789. AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37790. /* add AKID from the Public Key */
  37791. AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  37792. ret = 0;
  37793. do {
  37794. #if defined(WOLFSSL_ASYNC_CRYPT)
  37795. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  37796. #endif
  37797. if (ret >= 0) {
  37798. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  37799. }
  37800. } while (ret == WC_PENDING_E);
  37801. AssertIntGT(ret, 0);
  37802. #ifdef OPENSSL_EXTRA
  37803. /* der holds a certificate with DC's now check X509 parsing of it */
  37804. certSz = ret;
  37805. pt = der;
  37806. AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  37807. AssertNotNull(x509name = X509_get_subject_name(x509));
  37808. #ifdef WOLFSSL_MULTI_ATTRIB
  37809. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37810. -1)), 5);
  37811. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37812. idx)), 6);
  37813. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37814. idx)), -1);
  37815. #endif /* WOLFSSL_MULTI_ATTRIB */
  37816. /* compare DN at index 0 */
  37817. AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
  37818. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37819. AssertIntEQ(ASN1_STRING_length(entryValue), 2);
  37820. AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  37821. #ifdef WOLFSSL_MULTI_ATTRIB
  37822. /* get first and second DC and compare result */
  37823. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37824. -1)), 5);
  37825. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37826. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37827. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  37828. AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  37829. idx)), 6);
  37830. AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
  37831. AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  37832. AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  37833. #endif /* WOLFSSL_MULTI_ATTRIB */
  37834. /* try invalid index locations for regression test and sanity check */
  37835. AssertNull(entry = X509_NAME_get_entry(x509name, 11));
  37836. AssertNull(entry = X509_NAME_get_entry(x509name, 20));
  37837. X509_free(x509);
  37838. #endif /* OPENSSL_EXTRA */
  37839. wc_FreeRsaKey(&key);
  37840. wc_FreeRng(&rng);
  37841. printf(resultFmt, passed);
  37842. #endif
  37843. return 0;
  37844. }
  37845. static int test_wolfSSL_X509_get_version(void){
  37846. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  37847. WOLFSSL_X509 *x509;
  37848. printf(testingFmt, "wolfSSL_X509_get_version()");
  37849. x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  37850. AssertNotNull(x509);
  37851. AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  37852. wolfSSL_X509_free(x509);
  37853. printf(resultFmt, passed);
  37854. #endif
  37855. return 0;
  37856. }
  37857. static int test_wolfSSL_DES_ncbc(void){
  37858. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  37859. const_DES_cblock myDes;
  37860. DES_cblock iv = {1};
  37861. DES_key_schedule key = {0};
  37862. unsigned char msg[] = "hello wolfssl";
  37863. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  37864. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  37865. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  37866. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  37867. printf(testingFmt, "wolfSSL_DES_ncbc()");
  37868. /* partial block test */
  37869. DES_set_key(&key, &myDes);
  37870. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  37871. AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  37872. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37873. DES_set_key(&key, &myDes);
  37874. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37875. *((byte*)&iv) = 1;
  37876. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  37877. AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
  37878. AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  37879. /* full block test */
  37880. DES_set_key(&key, &myDes);
  37881. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  37882. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37883. *((byte*)&iv) = 1;
  37884. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  37885. AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  37886. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37887. DES_set_key(&key, &myDes);
  37888. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  37889. *((byte*)&iv) = 1;
  37890. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  37891. AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
  37892. AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  37893. printf(resultFmt, passed);
  37894. #endif
  37895. return 0;
  37896. }
  37897. static int test_wolfSSL_AES_cbc_encrypt(void)
  37898. {
  37899. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
  37900. AES_KEY aes;
  37901. AES_KEY* aesN = NULL;
  37902. size_t len = 0;
  37903. size_t lenB = 0;
  37904. int keySz0 = 0;
  37905. int keySzN = -1;
  37906. byte out[AES_BLOCK_SIZE] = {0};
  37907. byte* outN = NULL;
  37908. /* Test vectors retrieved from:
  37909. * <begin URL>
  37910. * https://csrc.nist.gov/
  37911. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  37912. * documents/aes/KAT_AES.zip
  37913. * </end URL>
  37914. */
  37915. const byte* pt128N = NULL;
  37916. byte* key128N = NULL;
  37917. byte* iv128N = NULL;
  37918. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  37919. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37920. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37921. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  37922. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  37923. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  37924. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  37925. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  37926. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  37927. len = sizeof(pt128);
  37928. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  37929. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  37930. AssertIntNE(XMEMCMP(b, g, h), i)
  37931. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  37932. printf(testingFmt, "Stressing wolfSSL_AES_cbc_encrypt()");
  37933. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37934. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  37935. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  37936. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37937. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  37938. AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37939. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  37940. printf(resultFmt, "Stress Tests: passed");
  37941. printf(testingFmt, "Stressing wolfSSL_AES_set_encrypt_key");
  37942. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37943. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  37944. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  37945. AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  37946. printf(resultFmt, "Stress Tests: passed");
  37947. printf(testingFmt, "Stressing wolfSSL_AES_set_decrypt_key");
  37948. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  37949. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  37950. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  37951. AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  37952. printf(resultFmt, "Stress Tests: passed");
  37953. #ifdef WOLFSSL_AES_128
  37954. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit");
  37955. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37956. RESET_IV(iv128tmp, iv128);
  37957. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37958. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  37959. AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  37960. printf(resultFmt, "passed");
  37961. #ifdef HAVE_AES_DECRYPT
  37962. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode");
  37963. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37964. RESET_IV(iv128tmp, iv128);
  37965. len = sizeof(ct128);
  37966. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  37967. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  37968. AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  37969. printf(resultFmt, "passed");
  37970. #endif
  37971. #endif /* WOLFSSL_AES_128 */
  37972. #ifdef WOLFSSL_AES_192
  37973. {
  37974. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  37975. * Appendix F.2.3 */
  37976. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  37977. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  37978. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  37979. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  37980. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  37981. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  37982. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  37983. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  37984. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  37985. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  37986. len = sizeof(pt192);
  37987. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit");
  37988. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37989. RESET_IV(iv192tmp, iv192);
  37990. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  37991. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  37992. AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  37993. printf(resultFmt, "passed");
  37994. #ifdef HAVE_AES_DECRYPT
  37995. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode");
  37996. len = sizeof(ct192);
  37997. RESET_IV(iv192tmp, iv192);
  37998. XMEMSET(out, 0, AES_BLOCK_SIZE);
  37999. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38000. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38001. AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38002. printf(resultFmt, "passed");
  38003. #endif
  38004. }
  38005. #endif /* WOLFSSL_AES_192 */
  38006. #ifdef WOLFSSL_AES_256
  38007. {
  38008. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38009. * Appendix F.2.5 */
  38010. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38011. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38012. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38013. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38014. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38015. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38016. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38017. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38018. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38019. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38020. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38021. len = sizeof(pt256);
  38022. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit");
  38023. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38024. RESET_IV(iv256tmp, iv256);
  38025. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38026. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38027. AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38028. printf(resultFmt, "passed");
  38029. #ifdef HAVE_AES_DECRYPT
  38030. printf(testingFmt, "wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode");
  38031. len = sizeof(ct256);
  38032. RESET_IV(iv256tmp, iv256);
  38033. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38034. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38035. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  38036. AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  38037. printf(resultFmt, "passed");
  38038. #endif
  38039. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38040. {
  38041. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  38042. byte wrapPlain[sizeof(key256)] = { 0 };
  38043. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  38044. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit NULL iv");
  38045. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38046. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38047. 15), WOLFSSL_FAILURE);
  38048. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  38049. sizeof(key256)), sizeof(wrapCipher));
  38050. printf(resultFmt, "passed");
  38051. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit NULL iv");
  38052. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38053. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38054. 23), WOLFSSL_FAILURE);
  38055. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  38056. sizeof(wrapCipher)), sizeof(wrapPlain));
  38057. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38058. printf(resultFmt, "passed");
  38059. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  38060. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  38061. printf(testingFmt, "wolfSSL_AES_wrap_key() 256-bit custom iv");
  38062. AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38063. AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  38064. sizeof(key256)), sizeof(wrapCipher));
  38065. printf(resultFmt, "passed");
  38066. printf(testingFmt, "wolfSSL_AES_unwrap_key() 256-bit custom iv");
  38067. AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38068. AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  38069. sizeof(wrapCipher)), sizeof(wrapPlain));
  38070. AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  38071. printf(resultFmt, "passed");
  38072. }
  38073. #endif /* HAVE_AES_KEYWRAP */
  38074. }
  38075. #endif /* WOLFSSL_AES_256 */
  38076. #endif
  38077. return 0;
  38078. }
  38079. static int test_wolfSSL_CRYPTO_cts128(void)
  38080. {
  38081. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
  38082. && defined(HAVE_CTS)
  38083. byte tmp[64]; /* Largest vector size */
  38084. /* Test vectors taken form RFC3962 Appendix B */
  38085. const testVector vects[] = {
  38086. {
  38087. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38088. "\x20",
  38089. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  38090. "\x97",
  38091. 17, 17
  38092. },
  38093. {
  38094. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38095. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  38096. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  38097. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  38098. 31, 31
  38099. },
  38100. {
  38101. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38102. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  38103. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38104. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  38105. 32, 32
  38106. },
  38107. {
  38108. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38109. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38110. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  38111. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38112. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  38113. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  38114. 47, 47
  38115. },
  38116. {
  38117. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38118. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38119. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  38120. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38121. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  38122. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  38123. 48, 48
  38124. },
  38125. {
  38126. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  38127. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  38128. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  38129. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  38130. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  38131. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  38132. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  38133. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  38134. 64, 64
  38135. }
  38136. };
  38137. byte keyBytes[AES_128_KEY_SIZE] = {
  38138. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  38139. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  38140. };
  38141. size_t i;
  38142. XMEMSET(tmp, 0, sizeof(tmp));
  38143. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  38144. AES_KEY encKey;
  38145. AES_KEY decKey;
  38146. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  38147. XMEMSET(iv, 0, sizeof(iv));
  38148. AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
  38149. AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
  38150. AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  38151. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  38152. vects[i].outLen);
  38153. AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  38154. XMEMSET(iv, 0, sizeof(iv));
  38155. AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  38156. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  38157. vects[i].inLen);
  38158. AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  38159. }
  38160. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  38161. return 0;
  38162. }
  38163. #if defined(OPENSSL_ALL)
  38164. #if !defined(NO_ASN)
  38165. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  38166. {
  38167. #if !defined(NO_RSA)
  38168. WOLFSSL_X509* x509;
  38169. WOLFSSL_X509_NAME* subject;
  38170. WOLFSSL_X509_NAME_ENTRY* e;
  38171. WOLFSSL_ASN1_STRING* a;
  38172. FILE* file;
  38173. int idx = 0;
  38174. char targetOutput[16] = "www.wolfssl.com";
  38175. unsigned char* actual_output;
  38176. int len = 0;
  38177. int result = 0;
  38178. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  38179. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  38180. fclose(file);
  38181. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName");
  38182. AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  38183. AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  38184. NID_commonName, -1)), 5);
  38185. AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  38186. AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  38187. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  38188. result = strncmp((const char*)actual_output, targetOutput, len);
  38189. AssertIntEQ(result, 0);
  38190. printf(resultFmt, result == 0 ? passed : failed);
  38191. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
  38192. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
  38193. WOLFSSL_FATAL_ERROR);
  38194. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  38195. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
  38196. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
  38197. WOLFSSL_FATAL_ERROR);
  38198. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  38199. printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
  38200. AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
  38201. WOLFSSL_FATAL_ERROR);
  38202. printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
  38203. wolfSSL_X509_free(x509);
  38204. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38205. #endif
  38206. return 0;
  38207. }
  38208. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  38209. {
  38210. ASN1_STRING* asn1str_test;
  38211. ASN1_STRING* asn1str_answer;
  38212. /* Each character is encoded using 4 bytes */
  38213. char input[] = {
  38214. 0, 0, 0, 'T',
  38215. 0, 0, 0, 'e',
  38216. 0, 0, 0, 's',
  38217. 0, 0, 0, 't',
  38218. };
  38219. char output[] = "Test";
  38220. printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
  38221. AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  38222. AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  38223. AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  38224. AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  38225. AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  38226. AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  38227. ASN1_STRING_free(asn1str_test);
  38228. ASN1_STRING_free(asn1str_answer);
  38229. printf(resultFmt, "passed");
  38230. return 0;
  38231. }
  38232. #endif /* !defined(NO_ASN) */
  38233. static int test_wolfSSL_sk_CIPHER_description(void)
  38234. {
  38235. #if !defined(NO_RSA)
  38236. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38237. int i,j,k;
  38238. int numCiphers = 0;
  38239. const SSL_METHOD *method = NULL;
  38240. const SSL_CIPHER *cipher = NULL;
  38241. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38242. SSL_CTX *ctx = NULL;
  38243. SSL *ssl = NULL;
  38244. char buf[256];
  38245. char test_str[9] = "0000000";
  38246. const char badStr[] = "unknown";
  38247. const char certPath[] = "./certs/client-cert.pem";
  38248. XMEMSET(buf, 0, sizeof(buf));
  38249. printf(testingFmt, "wolfSSL_sk_CIPHER_description");
  38250. AssertNotNull(method = TLSv1_2_client_method());
  38251. AssertNotNull(ctx = SSL_CTX_new(method));
  38252. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38253. SSL_CTX_set_verify_depth(ctx, 4);
  38254. SSL_CTX_set_options(ctx, flags);
  38255. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38256. WOLFSSL_SUCCESS);
  38257. AssertNotNull(ssl = SSL_new(ctx));
  38258. /* SSL_get_ciphers returns a stack of all configured ciphers
  38259. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  38260. */
  38261. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38262. /* loop through the amount of supportedCiphers */
  38263. numCiphers = sk_num(supportedCiphers);
  38264. for (i = 0; i < numCiphers; ++i) {
  38265. /* sk_value increments "sk->data.cipher->cipherOffset".
  38266. * wolfSSL_sk_CIPHER_description sets the description for
  38267. * the cipher based on the provided offset.
  38268. */
  38269. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  38270. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  38271. }
  38272. /* Search cipher description string for "unknown" descriptor */
  38273. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  38274. k = 0;
  38275. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  38276. test_str[k] = badStr[k];
  38277. j++;
  38278. k++;
  38279. }
  38280. }
  38281. /* Fail if test_str == badStr == "unknown" */
  38282. AssertStrNE(test_str,badStr);
  38283. }
  38284. SSL_free(ssl);
  38285. SSL_CTX_free(ctx);
  38286. printf(resultFmt, passed);
  38287. #endif
  38288. return 0;
  38289. }
  38290. static int test_wolfSSL_get_ciphers_compat(void)
  38291. {
  38292. #if !defined(NO_RSA)
  38293. const SSL_METHOD *method = NULL;
  38294. const char certPath[] = "./certs/client-cert.pem";
  38295. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  38296. SSL_CTX *ctx = NULL;
  38297. WOLFSSL *ssl = NULL;
  38298. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  38299. printf(testingFmt, "wolfSSL_get_ciphers_compat");
  38300. method = SSLv23_client_method();
  38301. AssertNotNull(method);
  38302. ctx = SSL_CTX_new(method);
  38303. AssertNotNull(ctx);
  38304. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  38305. SSL_CTX_set_verify_depth(ctx, 4);
  38306. SSL_CTX_set_options(ctx, flags);
  38307. AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  38308. WOLFSSL_SUCCESS);
  38309. AssertNotNull(ssl = SSL_new(ctx));
  38310. /* Test Bad NULL input */
  38311. AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
  38312. /* Test for Good input */
  38313. AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  38314. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  38315. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  38316. SSL_free(ssl);
  38317. SSL_CTX_free(ctx);
  38318. printf(resultFmt, passed);
  38319. #endif
  38320. return 0;
  38321. }
  38322. static int test_wolfSSL_X509_PUBKEY_get(void)
  38323. {
  38324. WOLFSSL_X509_PUBKEY pubkey;
  38325. WOLFSSL_X509_PUBKEY* key;
  38326. WOLFSSL_EVP_PKEY evpkey ;
  38327. WOLFSSL_EVP_PKEY* evpPkey;
  38328. WOLFSSL_EVP_PKEY* retEvpPkey;
  38329. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  38330. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  38331. key = &pubkey;
  38332. evpPkey = &evpkey;
  38333. evpPkey->type = WOLFSSL_SUCCESS;
  38334. key->pkey = evpPkey;
  38335. printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
  38336. AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38337. AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  38338. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  38339. key->pkey = NULL;
  38340. AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  38341. printf(resultFmt,retEvpPkey == NULL ? passed : failed);
  38342. return 0;
  38343. }
  38344. static int test_wolfSSL_EC_KEY_dup(void)
  38345. {
  38346. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
  38347. defined(OPENSSL_EXTRA_X509_SMALL))
  38348. WOLFSSL_EC_KEY* ecKey;
  38349. WOLFSSL_EC_KEY* dupKey;
  38350. ecc_key* srcKey;
  38351. ecc_key* destKey;
  38352. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  38353. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38354. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38355. /* Valid cases */
  38356. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38357. AssertIntEQ(EC_KEY_check_key(dupKey), 1);
  38358. /* Compare pubkey */
  38359. srcKey = (ecc_key*)ecKey->internal;
  38360. destKey = (ecc_key*)dupKey->internal;
  38361. AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  38362. /* compare EC_GROUP */
  38363. AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  38364. /* compare EC_POINT */
  38365. AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  38366. dupKey->pub_key, NULL), MP_EQ);
  38367. /* compare BIGNUM */
  38368. AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  38369. wolfSSL_EC_KEY_free(dupKey);
  38370. /* Invalid cases */
  38371. /* NULL key */
  38372. AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  38373. /* NULL ecc_key */
  38374. wc_ecc_free((ecc_key*)ecKey->internal);
  38375. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  38376. ecKey->internal = NULL; /* Set ecc_key to NULL */
  38377. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38378. wolfSSL_EC_KEY_free(ecKey);
  38379. wolfSSL_EC_KEY_free(dupKey);
  38380. /* NULL Group */
  38381. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38382. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38383. wolfSSL_EC_GROUP_free(ecKey->group);
  38384. ecKey->group = NULL; /* Set group to NULL */
  38385. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38386. wolfSSL_EC_KEY_free(ecKey);
  38387. wolfSSL_EC_KEY_free(dupKey);
  38388. /* NULL public key */
  38389. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38390. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38391. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  38392. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  38393. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38394. wolfSSL_EC_POINT_free(ecKey->pub_key);
  38395. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  38396. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38397. wolfSSL_EC_KEY_free(ecKey);
  38398. wolfSSL_EC_KEY_free(dupKey);
  38399. /* NULL private key */
  38400. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38401. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38402. wolfSSL_BN_free(ecKey->priv_key);
  38403. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  38404. AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38405. wolfSSL_EC_KEY_free(ecKey);
  38406. wolfSSL_EC_KEY_free(dupKey);
  38407. /* Test EC_KEY_up_ref */
  38408. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38409. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  38410. AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  38411. AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  38412. /* reference count doesn't follow duplicate */
  38413. AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  38414. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  38415. AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  38416. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  38417. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  38418. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  38419. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  38420. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  38421. printf(resultFmt, passed);
  38422. #endif
  38423. return 0;
  38424. }
  38425. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  38426. {
  38427. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  38428. DSA *dsa = NULL;
  38429. DSA *setDsa = NULL;
  38430. EVP_PKEY *pkey = NULL;
  38431. EVP_PKEY *set1Pkey = NULL;
  38432. SHA_CTX sha;
  38433. byte signature[DSA_SIG_SIZE];
  38434. byte hash[WC_SHA_DIGEST_SIZE];
  38435. word32 bytes;
  38436. int answer;
  38437. #ifdef USE_CERT_BUFFERS_1024
  38438. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  38439. int dsaKeySz = sizeof_dsa_key_der_1024;
  38440. byte tmp[ONEK_BUF];
  38441. XMEMSET(tmp, 0, sizeof(tmp));
  38442. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38443. bytes = dsaKeySz;
  38444. #elif defined(USE_CERT_BUFFERS_2048)
  38445. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  38446. int dsaKeySz = sizeof_dsa_key_der_2048;
  38447. byte tmp[TWOK_BUF];
  38448. XMEMSET(tmp, 0, sizeof(tmp));
  38449. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  38450. bytes = dsaKeySz;
  38451. #else
  38452. byte tmp[TWOK_BUF];
  38453. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  38454. int dsaKeySz;
  38455. XMEMSET(tmp, 0, sizeof(tmp));
  38456. XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
  38457. if (fp == XBADFILE) {
  38458. return WOLFSSL_BAD_FILE;
  38459. }
  38460. dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
  38461. XFCLOSE(fp);
  38462. #endif /* END USE_CERT_BUFFERS_1024 */
  38463. printf(testingFmt,
  38464. "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
  38465. /* Create hash to later Sign and Verify */
  38466. AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  38467. AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  38468. AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  38469. /* Initialize pkey with der format dsa key */
  38470. AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
  38471. &dsaKeyDer ,(long)dsaKeySz));
  38472. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  38473. /* Should Fail: NULL argument */
  38474. AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
  38475. AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
  38476. /* Should Pass: Initialized pkey argument */
  38477. AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  38478. AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  38479. #ifdef USE_CERT_BUFFERS_1024
  38480. AssertIntEQ(DSA_bits(dsa), 1024);
  38481. #else
  38482. AssertIntEQ(DSA_bits(dsa), 2048);
  38483. #endif
  38484. /* Sign */
  38485. AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  38486. /* Verify. */
  38487. AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  38488. WOLFSSL_SUCCESS);
  38489. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  38490. /* Should Fail: set1Pkey not initialized */
  38491. AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38492. /* Initialize set1Pkey */
  38493. set1Pkey = EVP_PKEY_new();
  38494. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  38495. AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  38496. WOLFSSL_SUCCESS);
  38497. /* Should Pass: set dsa into set1Pkey */
  38498. AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  38499. printf(resultFmt, passed);
  38500. DSA_free(dsa);
  38501. DSA_free(setDsa);
  38502. EVP_PKEY_free(pkey);
  38503. EVP_PKEY_free(set1Pkey);
  38504. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  38505. return 0;
  38506. } /* END test_EVP_PKEY_set1_get1_DSA */
  38507. static int test_wolfSSL_DSA_SIG(void)
  38508. {
  38509. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  38510. !defined(HAVE_FIPS)
  38511. DSA *dsa = NULL;
  38512. DSA *dsa2 = NULL;
  38513. DSA_SIG *sig = NULL;
  38514. const BIGNUM *p = NULL;
  38515. const BIGNUM *q = NULL;
  38516. const BIGNUM *g = NULL;
  38517. const BIGNUM *pub = NULL;
  38518. const BIGNUM *priv = NULL;
  38519. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  38520. printf(testingFmt, "wolfSSL_DSA_SIG");
  38521. AssertNotNull(dsa = DSA_generate_parameters(2048,
  38522. NULL, 0, NULL, NULL, NULL, NULL));
  38523. DSA_free(dsa);
  38524. AssertNotNull(dsa = DSA_new());
  38525. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  38526. NULL, 0, NULL, NULL, NULL), 1);
  38527. AssertIntEQ(DSA_generate_key(dsa), 1);
  38528. DSA_get0_pqg(dsa, &p, &q, &g);
  38529. DSA_get0_key(dsa, &pub, &priv);
  38530. AssertNotNull(p = BN_dup(p));
  38531. AssertNotNull(q = BN_dup(q));
  38532. AssertNotNull(g = BN_dup(g));
  38533. AssertNotNull(pub = BN_dup(pub));
  38534. AssertNotNull(priv = BN_dup(priv));
  38535. AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  38536. AssertNotNull(dsa2 = DSA_new());
  38537. AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
  38538. AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
  38539. AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  38540. printf(resultFmt, passed);
  38541. DSA_free(dsa);
  38542. DSA_free(dsa2);
  38543. DSA_SIG_free(sig);
  38544. #endif
  38545. return 0;
  38546. }
  38547. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  38548. {
  38549. #ifdef HAVE_ECC
  38550. WOLFSSL_EC_KEY *ecKey = NULL;
  38551. WOLFSSL_EC_KEY *ecGet1 = NULL;
  38552. EVP_PKEY *pkey = NULL;
  38553. printf(testingFmt,
  38554. "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
  38555. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38556. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38557. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  38558. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  38559. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  38560. /* Should fail since ecKey is empty */
  38561. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  38562. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38563. AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  38564. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  38565. AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  38566. AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  38567. wolfSSL_EC_KEY_free(ecKey);
  38568. wolfSSL_EC_KEY_free(ecGet1);
  38569. EVP_PKEY_free(pkey);
  38570. /* PASSED */
  38571. printf(resultFmt, passed);
  38572. #endif /* HAVE_ECC */
  38573. return 0;
  38574. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  38575. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  38576. {
  38577. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  38578. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  38579. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  38580. DH *dh = NULL;
  38581. DH *setDh = NULL;
  38582. EVP_PKEY *pkey = NULL;
  38583. FILE* f = NULL;
  38584. unsigned char buf[4096];
  38585. const unsigned char* pt = buf;
  38586. const char* dh2048 = "./certs/dh2048.der";
  38587. long len = 0;
  38588. int code = -1;
  38589. printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
  38590. XMEMSET(buf, 0, sizeof(buf));
  38591. f = XFOPEN(dh2048, "rb");
  38592. AssertTrue(f != XBADFILE);
  38593. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  38594. XFCLOSE(f);
  38595. /* Load dh2048.der into DH with internal format */
  38596. AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  38597. AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  38598. AssertIntEQ(code, 0);
  38599. code = -1;
  38600. pkey = wolfSSL_EVP_PKEY_new();
  38601. /* Set DH into PKEY */
  38602. AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  38603. /* Get DH from PKEY */
  38604. AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  38605. AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  38606. AssertIntEQ(code, 0);
  38607. EVP_PKEY_free(pkey);
  38608. DH_free(setDh);
  38609. DH_free(dh);
  38610. printf(resultFmt, passed);
  38611. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  38612. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  38613. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  38614. return 0;
  38615. } /* END test_EVP_PKEY_set1_get1_DH */
  38616. static int test_wolfSSL_CTX_ctrl(void)
  38617. {
  38618. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38619. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  38620. char caFile[] = "./certs/client-ca.pem";
  38621. char clientFile[] = "./certs/client-cert.pem";
  38622. SSL_CTX* ctx;
  38623. X509* x509 = NULL;
  38624. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38625. byte buf[6000];
  38626. char file[] = "./certs/dsaparams.pem";
  38627. XFILE f;
  38628. int bytes;
  38629. BIO* bio;
  38630. DSA* dsa;
  38631. DH* dh;
  38632. #endif
  38633. #ifdef HAVE_ECC
  38634. WOLFSSL_EC_KEY* ecKey;
  38635. #endif
  38636. printf(testingFmt, "wolfSSL_CTX_ctrl");
  38637. AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  38638. x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
  38639. AssertNotNull(x509);
  38640. AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  38641. x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
  38642. AssertNotNull(x509);
  38643. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38644. /* Initialize DH */
  38645. f = XFOPEN(file, "rb");
  38646. AssertTrue((f != XBADFILE));
  38647. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  38648. XFCLOSE(f);
  38649. bio = BIO_new_mem_buf((void*)buf, bytes);
  38650. AssertNotNull(bio);
  38651. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  38652. AssertNotNull(dsa);
  38653. dh = wolfSSL_DSA_dup_DH(dsa);
  38654. AssertNotNull(dh);
  38655. #endif
  38656. #ifdef HAVE_ECC
  38657. /* Initialize WOLFSSL_EC_KEY */
  38658. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38659. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
  38660. #endif
  38661. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  38662. /* additional test of getting EVP_PKEY key size from X509
  38663. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  38664. * allowed with user RSA */
  38665. {
  38666. EVP_PKEY* pkey;
  38667. #if defined(HAVE_ECC)
  38668. X509* ecX509;
  38669. #endif /* HAVE_ECC */
  38670. AssertNotNull(pkey = X509_get_pubkey(x509));
  38671. /* current RSA key is 2048 bit (256 bytes) */
  38672. AssertIntEQ(EVP_PKEY_size(pkey), 256);
  38673. EVP_PKEY_free(pkey);
  38674. #if defined(HAVE_ECC)
  38675. #if defined(USE_CERT_BUFFERS_256)
  38676. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  38677. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  38678. SSL_FILETYPE_ASN1));
  38679. #else
  38680. AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  38681. cliEccCertFile, SSL_FILETYPE_PEM));
  38682. #endif
  38683. AssertNotNull(pkey = X509_get_pubkey(ecX509));
  38684. /* current ECC key is 256 bit (32 bytes) */
  38685. AssertIntEQ(EVP_PKEY_size(pkey), 32);
  38686. X509_free(ecX509);
  38687. EVP_PKEY_free(pkey);
  38688. #endif /* HAVE_ECC */
  38689. }
  38690. #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
  38691. /* Tests should fail with passed in NULL pointer */
  38692. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
  38693. SSL_FAILURE);
  38694. #if !defined(NO_DH) && !defined(NO_DSA)
  38695. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
  38696. SSL_FAILURE);
  38697. #endif
  38698. #ifdef HAVE_ECC
  38699. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
  38700. SSL_FAILURE);
  38701. #endif
  38702. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  38703. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  38704. */
  38705. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
  38706. SSL_SUCCESS);
  38707. /* Test with SSL_CTRL_OPTIONS
  38708. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  38709. */
  38710. AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
  38711. == SSL_OP_NO_TLSv1);
  38712. AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  38713. /* Test with SSL_CTRL_SET_TMP_DH
  38714. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  38715. */
  38716. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  38717. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
  38718. SSL_SUCCESS);
  38719. #endif
  38720. /* Test with SSL_CTRL_SET_TMP_ECDH
  38721. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  38722. */
  38723. #ifdef HAVE_ECC
  38724. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
  38725. SSL_SUCCESS);
  38726. #endif
  38727. #ifdef WOLFSSL_ENCRYPTED_KEYS
  38728. AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
  38729. AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  38730. #endif
  38731. /* Test for min/max proto */
  38732. #ifndef WOLFSSL_NO_TLS12
  38733. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38734. 0, NULL), SSL_SUCCESS);
  38735. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  38736. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38737. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  38738. #endif
  38739. #ifdef WOLFSSL_TLS13
  38740. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38741. 0, NULL), SSL_SUCCESS);
  38742. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38743. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  38744. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  38745. #ifndef WOLFSSL_NO_TLS12
  38746. AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  38747. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  38748. AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  38749. #endif
  38750. #endif
  38751. /* Cleanup and Pass */
  38752. #if !defined(NO_DH) && !defined(NO_DSA)
  38753. #ifndef NO_BIO
  38754. BIO_free(bio);
  38755. DSA_free(dsa);
  38756. DH_free(dh);
  38757. #endif
  38758. #endif
  38759. #ifdef HAVE_ECC
  38760. wolfSSL_EC_KEY_free(ecKey);
  38761. #endif
  38762. SSL_CTX_free(ctx);
  38763. printf(resultFmt, passed);
  38764. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  38765. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  38766. return 0;
  38767. }
  38768. static int test_wolfSSL_EVP_PKEY_assign(void)
  38769. {
  38770. int type;
  38771. WOLFSSL_EVP_PKEY* pkey;
  38772. #ifndef NO_RSA
  38773. WOLFSSL_RSA* rsa;
  38774. #endif
  38775. #ifndef NO_DSA
  38776. WOLFSSL_DSA* dsa;
  38777. #endif
  38778. #ifdef HAVE_ECC
  38779. WOLFSSL_EC_KEY* ecKey;
  38780. #endif
  38781. (void)pkey;
  38782. printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
  38783. #ifndef NO_RSA
  38784. type = EVP_PKEY_RSA;
  38785. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38786. AssertNotNull(rsa = wolfSSL_RSA_new());
  38787. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
  38788. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38789. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
  38790. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
  38791. wolfSSL_EVP_PKEY_free(pkey);
  38792. #endif /* NO_RSA */
  38793. #ifndef NO_DSA
  38794. type = EVP_PKEY_DSA;
  38795. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38796. AssertNotNull(dsa = wolfSSL_DSA_new());
  38797. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
  38798. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38799. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
  38800. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
  38801. wolfSSL_EVP_PKEY_free(pkey);
  38802. #endif /* NO_DSA */
  38803. #ifdef HAVE_ECC
  38804. type = EVP_PKEY_EC;
  38805. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38806. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  38807. AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
  38808. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
  38809. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
  38810. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
  38811. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  38812. AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
  38813. wolfSSL_EVP_PKEY_free(pkey);
  38814. #endif /* HAVE_ECC */
  38815. (void)type;
  38816. printf(resultFmt, passed);
  38817. return 0;
  38818. }
  38819. static int test_wolfSSL_EVP_PKEY_base_id(void)
  38820. {
  38821. WOLFSSL_EVP_PKEY* pkey;
  38822. printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
  38823. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38824. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  38825. AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  38826. EVP_PKEY_free(pkey);
  38827. printf(resultFmt, passed);
  38828. return 0;
  38829. }
  38830. static int test_wolfSSL_EVP_PKEY_id(void)
  38831. {
  38832. WOLFSSL_EVP_PKEY* pkey;
  38833. printf(testingFmt, "wolfSSL_EVP_PKEY_id");
  38834. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38835. AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  38836. AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  38837. EVP_PKEY_free(pkey);
  38838. printf(resultFmt, passed);
  38839. return 0;
  38840. }
  38841. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  38842. {
  38843. #if defined(OPENSSL_ALL) && \
  38844. !defined(NO_ECC_SECP) && \
  38845. /* This last bit is taken from ecc.c. It is the condition that
  38846. * defines ECC256 */ \
  38847. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  38848. ECC_MIN_KEY_SZ <= 256)
  38849. EVP_PKEY_CTX* ctx;
  38850. EVP_PKEY* pkey = NULL;
  38851. printf(testingFmt, "wolfSSL_EVP_PKEY_paramgen");
  38852. /* Test error conditions. */
  38853. AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  38854. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38855. AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  38856. #ifndef NO_RSA
  38857. EVP_PKEY_CTX_free(ctx);
  38858. /* Parameter generation for RSA not supported yet. */
  38859. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  38860. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  38861. #endif
  38862. #ifdef HAVE_ECC
  38863. EVP_PKEY_CTX_free(ctx);
  38864. AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  38865. AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  38866. AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  38867. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  38868. AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  38869. AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  38870. WOLFSSL_SUCCESS);
  38871. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38872. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38873. #endif
  38874. EVP_PKEY_CTX_free(ctx);
  38875. EVP_PKEY_free(pkey);
  38876. printf(resultFmt, passed);
  38877. #endif
  38878. return 0;
  38879. }
  38880. static int test_wolfSSL_EVP_PKEY_keygen(void)
  38881. {
  38882. WOLFSSL_EVP_PKEY* pkey = NULL;
  38883. EVP_PKEY_CTX* ctx = NULL;
  38884. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38885. WOLFSSL_EVP_PKEY* params = NULL;
  38886. DH* dh = NULL;
  38887. const BIGNUM* pubkey = NULL;
  38888. const BIGNUM* privkey = NULL;
  38889. ASN1_INTEGER* asn1int = NULL;
  38890. unsigned int length = 0;
  38891. byte* derBuffer = NULL;
  38892. #endif
  38893. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
  38894. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38895. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38896. /* Bad cases */
  38897. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  38898. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  38899. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  38900. /* Good case */
  38901. AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  38902. EVP_PKEY_CTX_free(ctx);
  38903. EVP_PKEY_free(pkey);
  38904. pkey = NULL;
  38905. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  38906. /* Test DH keygen */
  38907. {
  38908. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  38909. AssertNotNull(dh = DH_get_2048_256());
  38910. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  38911. AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  38912. AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38913. AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  38914. DH_free(dh);
  38915. EVP_PKEY_CTX_free(ctx);
  38916. EVP_PKEY_free(params);
  38917. /* try exporting generated key to DER, to verify */
  38918. AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
  38919. DH_get0_key(dh, &pubkey, &privkey);
  38920. AssertNotNull(pubkey);
  38921. AssertNotNull(privkey);
  38922. AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  38923. AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  38924. ASN1_INTEGER_free(asn1int);
  38925. DH_free(dh);
  38926. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  38927. EVP_PKEY_free(pkey);
  38928. }
  38929. #endif
  38930. printf(resultFmt, passed);
  38931. return 0;
  38932. }
  38933. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  38934. {
  38935. WOLFSSL_EVP_PKEY* pkey;
  38936. EVP_PKEY_CTX *ctx;
  38937. printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
  38938. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38939. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  38940. AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  38941. EVP_PKEY_CTX_free(ctx);
  38942. EVP_PKEY_free(pkey);
  38943. printf(resultFmt, passed);
  38944. return 0;
  38945. }
  38946. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  38947. {
  38948. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  38949. WOLFSSL_EVP_PKEY* pkey;
  38950. printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
  38951. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  38952. AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  38953. EVP_PKEY_free(pkey);
  38954. printf(resultFmt, passed);
  38955. #endif
  38956. return 0;
  38957. }
  38958. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  38959. {
  38960. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  38961. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  38962. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  38963. !defined(NO_FILESYSTEM)
  38964. WOLFSSL_EVP_PKEY* params = NULL;
  38965. WOLFSSL_EVP_PKEY* copy = NULL;
  38966. DH* dh = NULL;
  38967. BIGNUM* p1;
  38968. BIGNUM* g1;
  38969. BIGNUM* q1;
  38970. BIGNUM* p2;
  38971. BIGNUM* g2;
  38972. BIGNUM* q2;
  38973. printf(testingFmt, "wolfSSL_EVP_PKEY_copy_parameters");
  38974. /* create DH with DH_get_2048_256 params */
  38975. AssertNotNull(params = wolfSSL_EVP_PKEY_new());
  38976. AssertNotNull(dh = DH_get_2048_256());
  38977. AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  38978. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  38979. (const BIGNUM**)&q1,
  38980. (const BIGNUM**)&g1);
  38981. DH_free(dh);
  38982. /* create DH with random generated DH params */
  38983. AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
  38984. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  38985. AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  38986. DH_free(dh);
  38987. AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  38988. AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
  38989. AssertNotNull(dh->p);
  38990. AssertNotNull(dh->g);
  38991. AssertNotNull(dh->q);
  38992. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  38993. (const BIGNUM**)&q2,
  38994. (const BIGNUM**)&g2);
  38995. AssertIntEQ(BN_cmp(p1, p2), 0);
  38996. AssertIntEQ(BN_cmp(q1, q2), 0);
  38997. AssertIntEQ(BN_cmp(g1, g2), 0);
  38998. DH_free(dh);
  38999. EVP_PKEY_free(copy);
  39000. EVP_PKEY_free(params);
  39001. printf(resultFmt, passed);
  39002. #endif
  39003. return 0;
  39004. }
  39005. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  39006. {
  39007. WOLFSSL_EVP_PKEY* pkey;
  39008. EVP_PKEY_CTX *ctx;
  39009. int bits = 2048;
  39010. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
  39011. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39012. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39013. AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  39014. WOLFSSL_SUCCESS);
  39015. EVP_PKEY_CTX_free(ctx);
  39016. EVP_PKEY_free(pkey);
  39017. printf(resultFmt, passed);
  39018. return 0;
  39019. }
  39020. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  39021. {
  39022. /* This is large enough to be used for all key sizes */
  39023. byte key[AES_256_KEY_SIZE] = {0};
  39024. byte iv[AES_BLOCK_SIZE] = {0};
  39025. int i, enumlen;
  39026. EVP_CIPHER_CTX *ctx;
  39027. const EVP_CIPHER *init;
  39028. int enumArray[] = {
  39029. #ifdef HAVE_AES_CBC
  39030. NID_aes_128_cbc,
  39031. #endif
  39032. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39033. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39034. #ifdef HAVE_AESGCM
  39035. NID_aes_128_gcm,
  39036. #endif
  39037. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39038. #ifdef WOLFSSL_AES_COUNTER
  39039. NID_aes_128_ctr,
  39040. #endif
  39041. #ifndef NO_DES3
  39042. NID_des_cbc,
  39043. NID_des_ede3_cbc,
  39044. #endif
  39045. };
  39046. int iv_lengths[] = {
  39047. #ifdef HAVE_AES_CBC
  39048. AES_BLOCK_SIZE,
  39049. #endif
  39050. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39051. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39052. #ifdef HAVE_AESGCM
  39053. GCM_NONCE_MID_SZ,
  39054. #endif
  39055. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39056. #ifdef WOLFSSL_AES_COUNTER
  39057. AES_BLOCK_SIZE,
  39058. #endif
  39059. #ifndef NO_DES3
  39060. DES_BLOCK_SIZE,
  39061. DES_BLOCK_SIZE,
  39062. #endif
  39063. };
  39064. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
  39065. enumlen = (sizeof(enumArray)/sizeof(int));
  39066. for(i = 0; i < enumlen; i++)
  39067. {
  39068. ctx = EVP_CIPHER_CTX_new();
  39069. init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
  39070. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39071. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39072. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  39073. EVP_CIPHER_CTX_free(ctx);
  39074. }
  39075. printf(resultFmt, passed);
  39076. return 0;
  39077. }
  39078. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  39079. {
  39080. #if !defined(NO_DES3)
  39081. byte key[AES_256_KEY_SIZE] = {0};
  39082. byte iv[AES_BLOCK_SIZE] = {0};
  39083. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39084. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39085. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
  39086. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39087. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39088. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
  39089. EVP_CIPHER_CTX_free(ctx);
  39090. printf(resultFmt, passed);
  39091. #endif
  39092. return 0;
  39093. }
  39094. static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
  39095. {
  39096. #if !defined(NO_DES3)
  39097. byte key[AES_256_KEY_SIZE] = {0};
  39098. byte iv[AES_BLOCK_SIZE] = {0};
  39099. int keylen;
  39100. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39101. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39102. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
  39103. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39104. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39105. keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39106. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
  39107. WOLFSSL_SUCCESS);
  39108. EVP_CIPHER_CTX_free(ctx);
  39109. printf(resultFmt, passed);
  39110. #endif
  39111. return 0;
  39112. }
  39113. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  39114. {
  39115. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  39116. byte key[DES3_KEY_SIZE] = {0};
  39117. byte iv[DES_BLOCK_SIZE] = {0};
  39118. int ivLen, keyLen;
  39119. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  39120. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  39121. printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
  39122. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39123. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39124. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  39125. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  39126. /* Bad cases */
  39127. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
  39128. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
  39129. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  39130. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  39131. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
  39132. /* Good case */
  39133. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  39134. EVP_CIPHER_CTX_free(ctx);
  39135. printf(resultFmt, passed);
  39136. #endif
  39137. return 0;
  39138. }
  39139. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  39140. {
  39141. WOLFSSL_ENGINE* e = NULL;
  39142. int id = 0;
  39143. EVP_PKEY_CTX *ctx;
  39144. printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
  39145. AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  39146. EVP_PKEY_CTX_free(ctx);
  39147. printf(resultFmt, passed);
  39148. return 0;
  39149. }
  39150. static int test_wolfSSL_EVP_rc4(void)
  39151. {
  39152. #if !defined(NO_RC4)
  39153. printf(testingFmt, "wolfSSL_EVP_rc4");
  39154. AssertNotNull(wolfSSL_EVP_rc4());
  39155. printf(resultFmt, passed);
  39156. #endif
  39157. return 0;
  39158. }
  39159. static int test_wolfSSL_EVP_enc_null(void)
  39160. {
  39161. printf(testingFmt, "wolfSSL_EVP_enc_null");
  39162. AssertNotNull(wolfSSL_EVP_enc_null());
  39163. printf(resultFmt, passed);
  39164. return 0;
  39165. }
  39166. static int test_wolfSSL_EVP_rc2_cbc(void)
  39167. {
  39168. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  39169. printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
  39170. AssertNull(wolfSSL_EVP_rc2_cbc());
  39171. printf(resultFmt, passed);
  39172. #endif
  39173. return 0;
  39174. }
  39175. static int test_wolfSSL_EVP_mdc2(void)
  39176. {
  39177. #if !defined(NO_WOLFSSL_STUB)
  39178. printf(testingFmt, "wolfSSL_EVP_mdc2");
  39179. AssertNull(wolfSSL_EVP_mdc2());
  39180. printf(resultFmt, passed);
  39181. #endif
  39182. return 0;
  39183. }
  39184. static int test_wolfSSL_EVP_md4(void)
  39185. {
  39186. #if !defined(NO_MD4)
  39187. printf(testingFmt, "wolfSSL_EVP_md4");
  39188. AssertNotNull(wolfSSL_EVP_md4());
  39189. printf(resultFmt, passed);
  39190. #endif
  39191. return 0;
  39192. }
  39193. static int test_wolfSSL_EVP_aes_256_gcm(void)
  39194. {
  39195. printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
  39196. AssertNotNull(wolfSSL_EVP_aes_256_gcm());
  39197. printf(resultFmt, passed);
  39198. return 0;
  39199. }
  39200. static int test_wolfSSL_EVP_aes_192_gcm(void)
  39201. {
  39202. printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
  39203. AssertNotNull(wolfSSL_EVP_aes_192_gcm());
  39204. printf(resultFmt, passed);
  39205. return 0;
  39206. }
  39207. static int test_wolfSSL_EVP_ripemd160(void)
  39208. {
  39209. #if !defined(NO_WOLFSSL_STUB)
  39210. printf(testingFmt, "wolfSSL_EVP_ripemd160");
  39211. AssertNull(wolfSSL_EVP_ripemd160());
  39212. printf(resultFmt, passed);
  39213. #endif
  39214. return 0;
  39215. }
  39216. static int test_wolfSSL_EVP_get_digestbynid(void)
  39217. {
  39218. printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
  39219. #ifndef NO_MD5
  39220. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  39221. #endif
  39222. AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  39223. AssertNull(wolfSSL_EVP_get_digestbynid(0));
  39224. printf(resultFmt, passed);
  39225. return 0;
  39226. }
  39227. static int test_wolfSSL_EVP_MD_nid(void)
  39228. {
  39229. printf(testingFmt, "wolfSSL_EVP_MD_nid");
  39230. #ifndef NO_MD5
  39231. AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  39232. #endif
  39233. #ifndef NO_SHA
  39234. AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  39235. #endif
  39236. #ifndef NO_SHA256
  39237. AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  39238. #endif
  39239. AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
  39240. printf(resultFmt, passed);
  39241. return 0;
  39242. }
  39243. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  39244. {
  39245. #if defined(HAVE_ECC)
  39246. WOLFSSL_EVP_PKEY* pkey;
  39247. printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
  39248. AssertNotNull(pkey = EVP_PKEY_new());
  39249. AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
  39250. EVP_PKEY_free(pkey);
  39251. printf(resultFmt, passed);
  39252. #endif
  39253. return 0;
  39254. }
  39255. static int test_wolfSSL_EVP_X_STATE(void)
  39256. {
  39257. #if !defined(NO_DES3) && !defined(NO_RC4)
  39258. byte key[DES3_KEY_SIZE] = {0};
  39259. byte iv[DES_IV_SIZE] = {0};
  39260. EVP_CIPHER_CTX *ctx;
  39261. const EVP_CIPHER *init;
  39262. printf(testingFmt, "wolfSSL_EVP_X_STATE");
  39263. /* Bad test cases */
  39264. ctx = EVP_CIPHER_CTX_new();
  39265. init = EVP_des_ede3_cbc();
  39266. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39267. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39268. AssertNull(wolfSSL_EVP_X_STATE(NULL));
  39269. AssertNull(wolfSSL_EVP_X_STATE(ctx));
  39270. EVP_CIPHER_CTX_free(ctx);
  39271. /* Good test case */
  39272. ctx = EVP_CIPHER_CTX_new();
  39273. init = wolfSSL_EVP_rc4();
  39274. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39275. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39276. AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
  39277. EVP_CIPHER_CTX_free(ctx);
  39278. printf(resultFmt, passed);
  39279. #endif
  39280. return 0;
  39281. }
  39282. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  39283. {
  39284. #if !defined(NO_DES3) && !defined(NO_RC4)
  39285. byte key[DES3_KEY_SIZE] = {0};
  39286. byte iv[DES_IV_SIZE] = {0};
  39287. EVP_CIPHER_CTX *ctx;
  39288. const EVP_CIPHER *init;
  39289. printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
  39290. /* Bad test cases */
  39291. ctx = EVP_CIPHER_CTX_new();
  39292. init = EVP_des_ede3_cbc();
  39293. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39294. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39295. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  39296. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  39297. EVP_CIPHER_CTX_free(ctx);
  39298. /* Good test case */
  39299. ctx = EVP_CIPHER_CTX_new();
  39300. init = wolfSSL_EVP_rc4();
  39301. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  39302. AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  39303. AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  39304. EVP_CIPHER_CTX_free(ctx);
  39305. printf(resultFmt, passed);
  39306. #endif
  39307. return 0;
  39308. }
  39309. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  39310. {
  39311. #ifdef HAVE_AES_CBC
  39312. #ifdef WOLFSSL_AES_128
  39313. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  39314. #endif
  39315. #ifdef WOLFSSL_AES_192
  39316. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  39317. #endif
  39318. #ifdef WOLFSSL_AES_256
  39319. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  39320. #endif
  39321. #endif
  39322. #ifdef HAVE_AESGCM
  39323. #ifdef WOLFSSL_AES_128
  39324. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  39325. #endif
  39326. #ifdef WOLFSSL_AES_192
  39327. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  39328. #endif
  39329. #ifdef WOLFSSL_AES_256
  39330. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  39331. #endif
  39332. #endif
  39333. #ifdef WOLFSSL_AES_COUNTER
  39334. #ifdef WOLFSSL_AES_128
  39335. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  39336. #endif
  39337. #ifdef WOLFSSL_AES_192
  39338. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  39339. #endif
  39340. #ifdef WOLFSSL_AES_256
  39341. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  39342. #endif
  39343. #endif
  39344. #ifdef HAVE_AES_ECB
  39345. #ifdef WOLFSSL_AES_128
  39346. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  39347. #endif
  39348. #ifdef WOLFSSL_AES_192
  39349. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  39350. #endif
  39351. #ifdef WOLFSSL_AES_256
  39352. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  39353. #endif
  39354. #endif
  39355. #ifdef WOLFSSL_AES_OFB
  39356. #ifdef WOLFSSL_AES_128
  39357. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  39358. #endif
  39359. #ifdef WOLFSSL_AES_192
  39360. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  39361. #endif
  39362. #ifdef WOLFSSL_AES_256
  39363. AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  39364. #endif
  39365. #endif
  39366. #ifndef NO_RC4
  39367. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  39368. #endif
  39369. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39370. AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  39371. #endif
  39372. return 0;
  39373. }
  39374. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  39375. {
  39376. int i, enumlen;
  39377. int enumArray[] = {
  39378. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39379. #ifdef WOLFSSL_AES_128
  39380. NID_aes_128_cbc,
  39381. #endif
  39382. #ifdef WOLFSSL_AES_192
  39383. NID_aes_192_cbc,
  39384. #endif
  39385. #ifdef WOLFSSL_AES_256
  39386. NID_aes_256_cbc,
  39387. #endif
  39388. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39389. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39390. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39391. #ifdef HAVE_AESGCM
  39392. #ifdef WOLFSSL_AES_128
  39393. NID_aes_128_gcm,
  39394. #endif
  39395. #ifdef WOLFSSL_AES_192
  39396. NID_aes_192_gcm,
  39397. #endif
  39398. #ifdef WOLFSSL_AES_256
  39399. NID_aes_256_gcm,
  39400. #endif
  39401. #endif /* HAVE_AESGCM */
  39402. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39403. #ifdef WOLFSSL_AES_COUNTER
  39404. #ifdef WOLFSSL_AES_128
  39405. NID_aes_128_ctr,
  39406. #endif
  39407. #ifdef WOLFSSL_AES_192
  39408. NID_aes_192_ctr,
  39409. #endif
  39410. #ifdef WOLFSSL_AES_256
  39411. NID_aes_256_ctr,
  39412. #endif
  39413. #endif
  39414. #ifndef NO_DES3
  39415. NID_des_cbc,
  39416. NID_des_ede3_cbc,
  39417. #endif
  39418. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39419. NID_chacha20_poly1305,
  39420. #endif
  39421. };
  39422. int iv_lengths[] = {
  39423. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  39424. #ifdef WOLFSSL_AES_128
  39425. AES_BLOCK_SIZE,
  39426. #endif
  39427. #ifdef WOLFSSL_AES_192
  39428. AES_BLOCK_SIZE,
  39429. #endif
  39430. #ifdef WOLFSSL_AES_256
  39431. AES_BLOCK_SIZE,
  39432. #endif
  39433. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  39434. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  39435. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39436. #ifdef HAVE_AESGCM
  39437. #ifdef WOLFSSL_AES_128
  39438. GCM_NONCE_MID_SZ,
  39439. #endif
  39440. #ifdef WOLFSSL_AES_192
  39441. GCM_NONCE_MID_SZ,
  39442. #endif
  39443. #ifdef WOLFSSL_AES_256
  39444. GCM_NONCE_MID_SZ,
  39445. #endif
  39446. #endif /* HAVE_AESGCM */
  39447. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  39448. #ifdef WOLFSSL_AES_COUNTER
  39449. #ifdef WOLFSSL_AES_128
  39450. AES_BLOCK_SIZE,
  39451. #endif
  39452. #ifdef WOLFSSL_AES_192
  39453. AES_BLOCK_SIZE,
  39454. #endif
  39455. #ifdef WOLFSSL_AES_256
  39456. AES_BLOCK_SIZE,
  39457. #endif
  39458. #endif
  39459. #ifndef NO_DES3
  39460. DES_BLOCK_SIZE,
  39461. DES_BLOCK_SIZE,
  39462. #endif
  39463. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  39464. CHACHA20_POLY1305_AEAD_IV_SIZE,
  39465. #endif
  39466. };
  39467. printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
  39468. enumlen = (sizeof(enumArray)/sizeof(int));
  39469. for(i = 0; i < enumlen; i++)
  39470. {
  39471. const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
  39472. AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  39473. }
  39474. printf(resultFmt, passed);
  39475. return 0;
  39476. }
  39477. static int test_wolfSSL_EVP_SignInit_ex(void)
  39478. {
  39479. WOLFSSL_EVP_MD_CTX mdCtx;
  39480. WOLFSSL_ENGINE* e = 0;
  39481. const EVP_MD* md;
  39482. md = "SHA256";
  39483. printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
  39484. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39485. AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  39486. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39487. printf(resultFmt, passed);
  39488. return 0;
  39489. }
  39490. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  39491. {
  39492. #if !defined(NO_SHA256)
  39493. WOLFSSL_EVP_MD_CTX mdCtx;
  39494. unsigned int s = 0;
  39495. unsigned char md[WC_SHA256_DIGEST_SIZE];
  39496. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  39497. printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
  39498. /* Bad Case */
  39499. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39500. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39501. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  39502. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  39503. #else
  39504. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39505. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  39506. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39507. #endif
  39508. /* Good Case */
  39509. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  39510. AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
  39511. AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  39512. AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  39513. printf(resultFmt, passed);
  39514. #endif
  39515. return 0;
  39516. }
  39517. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  39518. {
  39519. #if !defined(NO_DH) && \
  39520. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  39521. FILE* f = NULL;
  39522. unsigned char buf[4096];
  39523. const unsigned char* pt = buf;
  39524. const char* params1 = "./certs/dh2048.der";
  39525. long len = 0;
  39526. WOLFSSL_DH* dh = NULL;
  39527. WOLFSSL_EVP_PKEY* pkey;
  39528. XMEMSET(buf, 0, sizeof(buf));
  39529. f = XFOPEN(params1, "rb");
  39530. AssertTrue(f != XBADFILE);
  39531. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39532. XFCLOSE(f);
  39533. printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
  39534. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  39535. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  39536. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39537. /* Bad cases */
  39538. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  39539. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  39540. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  39541. /* Good case */
  39542. AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  39543. EVP_PKEY_free(pkey);
  39544. printf(resultFmt, passed);
  39545. #endif
  39546. return 0;
  39547. }
  39548. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  39549. {
  39550. #if defined(OPENSSL_EXTRA)
  39551. EVP_PKEY* pkey;
  39552. EVP_PKEY_CTX* ctx;
  39553. printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
  39554. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  39555. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39556. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  39557. /* void */
  39558. EVP_PKEY_CTX_free(ctx);
  39559. AssertTrue(1);
  39560. #else
  39561. /* int */
  39562. AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  39563. #endif
  39564. EVP_PKEY_free(pkey);
  39565. printf(resultFmt, passed);
  39566. #endif
  39567. return 0;
  39568. }
  39569. static int test_wolfSSL_EVP_PKEY_param_check(void)
  39570. {
  39571. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  39572. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  39573. DH *dh = NULL;
  39574. DH *setDh = NULL;
  39575. EVP_PKEY *pkey = NULL;
  39576. EVP_PKEY_CTX* ctx = NULL;
  39577. FILE* f = NULL;
  39578. unsigned char buf[512];
  39579. const unsigned char* pt = buf;
  39580. const char* dh2048 = "./certs/dh2048.der";
  39581. long len = 0;
  39582. int code = -1;
  39583. printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
  39584. XMEMSET(buf, 0, sizeof(buf));
  39585. f = XFOPEN(dh2048, "rb");
  39586. AssertTrue(f != XBADFILE);
  39587. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  39588. XFCLOSE(f);
  39589. /* Load dh2048.der into DH with internal format */
  39590. AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  39591. AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  39592. AssertIntEQ(code, 0);
  39593. code = -1;
  39594. pkey = wolfSSL_EVP_PKEY_new();
  39595. /* Set DH into PKEY */
  39596. AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  39597. /* create ctx from pkey */
  39598. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  39599. AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  39600. /* */
  39601. /* TO DO invlaid case */
  39602. /* */
  39603. EVP_PKEY_CTX_free(ctx);
  39604. EVP_PKEY_free(pkey);
  39605. DH_free(setDh);
  39606. DH_free(dh);
  39607. printf(resultFmt, passed);
  39608. #endif
  39609. #endif
  39610. return 0;
  39611. }
  39612. static int test_wolfSSL_EVP_BytesToKey(void)
  39613. {
  39614. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  39615. byte key[AES_BLOCK_SIZE] = {0};
  39616. byte iv[AES_BLOCK_SIZE] = {0};
  39617. int sz = 5;
  39618. int count = 0;
  39619. const EVP_MD* md = "SHA256";
  39620. const EVP_CIPHER *type;
  39621. const unsigned char *salt = (unsigned char *)"salt1234";
  39622. const byte data[] = {
  39623. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  39624. 0x72,0x6c,0x64
  39625. };
  39626. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  39627. printf(testingFmt, "EVP_BytesToKey");
  39628. /* Bad cases */
  39629. AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  39630. 0);
  39631. AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  39632. 16);
  39633. md = "2";
  39634. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39635. WOLFSSL_FAILURE);
  39636. /* Good case */
  39637. md = "SHA256";
  39638. AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  39639. 16);
  39640. printf(resultFmt, passed);
  39641. #endif
  39642. return 0;
  39643. }
  39644. static int test_evp_cipher_aes_gcm(void)
  39645. {
  39646. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  39647. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  39648. (HAVE_FIPS_VERSION >= 2)))
  39649. /*
  39650. * This test checks data at various points in the encrypt/decrypt process
  39651. * against known values produced using the same test with OpenSSL. This
  39652. * interop testing is critical for verifying the correctness of our
  39653. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  39654. * a flow supported by OpenSSL that uses the control command
  39655. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  39656. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  39657. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  39658. * wolfSSL OpenSSH clients because there was a bug in this flow that
  39659. * happened to "cancel out" if both sides of the connection had the bug.
  39660. */
  39661. enum {
  39662. NUM_ENCRYPTIONS = 3,
  39663. AAD_SIZE = 4
  39664. };
  39665. byte plainText1[] = {
  39666. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  39667. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  39668. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  39669. };
  39670. byte plainText2[] = {
  39671. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  39672. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  39673. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  39674. };
  39675. byte plainText3[] = {
  39676. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  39677. 0x5b, 0x57, 0x10
  39678. };
  39679. byte* plainTexts[NUM_ENCRYPTIONS] = {
  39680. plainText1,
  39681. plainText2,
  39682. plainText3
  39683. };
  39684. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  39685. sizeof(plainText1),
  39686. sizeof(plainText2),
  39687. sizeof(plainText3)
  39688. };
  39689. byte aad1[AAD_SIZE] = {
  39690. 0x00, 0x00, 0x00, 0x01
  39691. };
  39692. byte aad2[AAD_SIZE] = {
  39693. 0x00, 0x00, 0x00, 0x10
  39694. };
  39695. byte aad3[AAD_SIZE] = {
  39696. 0x00, 0x00, 0x01, 0x00
  39697. };
  39698. byte* aads[NUM_ENCRYPTIONS] = {
  39699. aad1,
  39700. aad2,
  39701. aad3
  39702. };
  39703. const byte iv[GCM_NONCE_MID_SZ] = {
  39704. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  39705. };
  39706. byte currentIv[GCM_NONCE_MID_SZ];
  39707. const byte key[] = {
  39708. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  39709. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  39710. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  39711. };
  39712. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  39713. {
  39714. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39715. 0xEF
  39716. },
  39717. {
  39718. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39719. 0xF0
  39720. },
  39721. {
  39722. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  39723. 0xF1
  39724. }
  39725. };
  39726. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  39727. {
  39728. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  39729. 0x3D, 0x32, 0x18, 0x34, 0xA9
  39730. },
  39731. {
  39732. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  39733. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  39734. },
  39735. {
  39736. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  39737. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  39738. }
  39739. };
  39740. const byte expCipherText1[] = {
  39741. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  39742. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  39743. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  39744. };
  39745. const byte expCipherText2[] = {
  39746. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  39747. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  39748. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  39749. };
  39750. const byte expCipherText3[] = {
  39751. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  39752. 0x80, 0x5E, 0x6B,
  39753. };
  39754. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  39755. expCipherText1,
  39756. expCipherText2,
  39757. expCipherText3
  39758. };
  39759. byte* cipherText;
  39760. byte* calcPlainText;
  39761. byte tag[AES_BLOCK_SIZE];
  39762. EVP_CIPHER_CTX* encCtx = NULL;
  39763. EVP_CIPHER_CTX* decCtx = NULL;
  39764. int i, j, outl;
  39765. printf(testingFmt, "test_evp_cipher_aes_gcm");
  39766. /****************************************************/
  39767. for (i = 0; i < 3; ++i) {
  39768. AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
  39769. AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
  39770. /* First iteration, set key before IV. */
  39771. if (i == 0) {
  39772. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  39773. SSL_SUCCESS);
  39774. /*
  39775. * The call to EVP_CipherInit below (with NULL key) should clear the
  39776. * gcmIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  39777. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  39778. * behavior.
  39779. */
  39780. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39781. (void*)iv), SSL_SUCCESS);
  39782. AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  39783. SSL_SUCCESS);
  39784. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39785. currentIv), SSL_FAILURE);
  39786. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  39787. SSL_SUCCESS);
  39788. AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  39789. SSL_SUCCESS);
  39790. }
  39791. /* Second iteration, IV before key. */
  39792. else {
  39793. AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  39794. SSL_SUCCESS);
  39795. AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  39796. SSL_SUCCESS);
  39797. AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  39798. SSL_SUCCESS);
  39799. AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  39800. SSL_SUCCESS);
  39801. }
  39802. /*
  39803. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  39804. * been issued first.
  39805. */
  39806. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39807. currentIv), SSL_FAILURE);
  39808. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39809. (void*)iv), SSL_SUCCESS);
  39810. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  39811. (void*)iv), SSL_SUCCESS);
  39812. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  39813. /*************** Encrypt ***************/
  39814. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39815. currentIv), SSL_SUCCESS);
  39816. /* Check current IV against expected. */
  39817. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39818. /* Add AAD. */
  39819. if (i == 2) {
  39820. /* Test streaming API. */
  39821. AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  39822. AAD_SIZE), SSL_SUCCESS);
  39823. }
  39824. else {
  39825. AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  39826. AAD_SIZE);
  39827. }
  39828. AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39829. DYNAMIC_TYPE_TMP_BUFFER));
  39830. /* Encrypt plaintext. */
  39831. if (i == 2){
  39832. AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  39833. plainTexts[j], plainTextSzs[j]),
  39834. SSL_SUCCESS);
  39835. }
  39836. else {
  39837. AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  39838. plainTextSzs[j]), plainTextSzs[j]);
  39839. }
  39840. if (i == 2) {
  39841. AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  39842. SSL_SUCCESS);
  39843. }
  39844. else {
  39845. /*
  39846. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  39847. * akin to calling EVP_CipherFinal.
  39848. */
  39849. AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  39850. }
  39851. /* Check ciphertext against expected. */
  39852. AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  39853. 0);
  39854. /* Get and check tag against expected. */
  39855. AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  39856. sizeof(tag), tag), SSL_SUCCESS);
  39857. AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  39858. /*************** Decrypt ***************/
  39859. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  39860. currentIv), SSL_SUCCESS);
  39861. /* Check current IV against expected. */
  39862. AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  39863. /* Add AAD. */
  39864. if (i == 2) {
  39865. /* Test streaming API. */
  39866. AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  39867. AAD_SIZE), SSL_SUCCESS);
  39868. }
  39869. else {
  39870. AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  39871. AAD_SIZE);
  39872. }
  39873. /* Set expected tag. */
  39874. AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  39875. sizeof(tag), tag), SSL_SUCCESS);
  39876. /* Decrypt ciphertext. */
  39877. AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  39878. DYNAMIC_TYPE_TMP_BUFFER));
  39879. if (i == 2){
  39880. AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  39881. cipherText, plainTextSzs[j]),
  39882. SSL_SUCCESS);
  39883. }
  39884. else {
  39885. /* This first EVP_Cipher call will check the tag, too. */
  39886. AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  39887. plainTextSzs[j]), plainTextSzs[j]);
  39888. }
  39889. if (i == 2) {
  39890. AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  39891. SSL_SUCCESS);
  39892. }
  39893. else {
  39894. AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  39895. }
  39896. /* Check plaintext against expected. */
  39897. AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  39898. 0);
  39899. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39900. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  39901. }
  39902. EVP_CIPHER_CTX_free(encCtx);
  39903. EVP_CIPHER_CTX_free(decCtx);
  39904. }
  39905. printf(resultFmt, passed);
  39906. #endif
  39907. return 0;
  39908. }
  39909. static int test_wolfSSL_OBJ_ln(void)
  39910. {
  39911. const int nid_set[] = {
  39912. NID_commonName,
  39913. NID_serialNumber,
  39914. NID_countryName,
  39915. NID_localityName,
  39916. NID_stateOrProvinceName,
  39917. NID_organizationName,
  39918. NID_organizationalUnitName,
  39919. NID_domainComponent,
  39920. NID_businessCategory,
  39921. NID_jurisdictionCountryName,
  39922. NID_jurisdictionStateOrProvinceName,
  39923. NID_emailAddress
  39924. };
  39925. const char* ln_set[] = {
  39926. "commonName",
  39927. "serialNumber",
  39928. "countryName",
  39929. "localityName",
  39930. "stateOrProvinceName",
  39931. "organizationName",
  39932. "organizationalUnitName",
  39933. "domainComponent",
  39934. "businessCategory",
  39935. "jurisdictionCountryName",
  39936. "jurisdictionStateOrProvinceName",
  39937. "emailAddress",
  39938. };
  39939. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  39940. printf(testingFmt, "wolfSSL_OBJ_ln");
  39941. AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
  39942. #ifdef HAVE_ECC
  39943. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  39944. {
  39945. EC_builtin_curve r[27];
  39946. size_t nCurves = sizeof(r) / sizeof(r[0]);
  39947. nCurves = EC_get_builtin_curves(r,nCurves);
  39948. for (i = 0; i < nCurves; i++) {
  39949. /* skip ECC_CURVE_INVALID */
  39950. if (r[i].nid != ECC_CURVE_INVALID) {
  39951. AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  39952. AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  39953. }
  39954. }
  39955. }
  39956. #endif
  39957. #endif
  39958. for (i = 0; i < maxIdx; i++) {
  39959. AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  39960. AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  39961. }
  39962. printf(resultFmt, passed);
  39963. return 0;
  39964. }
  39965. static int test_wolfSSL_OBJ_sn(void)
  39966. {
  39967. int i = 0, maxIdx = 7;
  39968. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  39969. NID_stateOrProvinceName,NID_organizationName,
  39970. NID_organizationalUnitName,NID_emailAddress};
  39971. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  39972. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  39973. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  39974. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  39975. WOLFSSL_EMAIL_ADDR};
  39976. printf(testingFmt, "wolfSSL_OBJ_sn");
  39977. AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  39978. for (i = 0; i < maxIdx; i++) {
  39979. AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  39980. AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  39981. }
  39982. printf(resultFmt, passed);
  39983. return 0;
  39984. }
  39985. #if !defined(NO_BIO)
  39986. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  39987. {
  39988. return lh_strhash(s[3]);
  39989. }
  39990. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  39991. {
  39992. return XSTRCMP(a[3], b[3]);
  39993. }
  39994. #endif
  39995. static int test_wolfSSL_TXT_DB(void)
  39996. {
  39997. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  39998. BIO *bio;
  39999. TXT_DB *db = NULL;
  40000. const int columns = 6;
  40001. const char *fields[6] = {
  40002. "V",
  40003. "320926161116Z",
  40004. "",
  40005. "12BD",
  40006. "unknown",
  40007. "/CN=rsa doe",
  40008. };
  40009. char** fields_copy;
  40010. printf(testingFmt, "wolfSSL_TXT_DB");
  40011. /* Test read */
  40012. AssertNotNull(bio = BIO_new(BIO_s_file()));
  40013. AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  40014. AssertNotNull(db = TXT_DB_read(bio, columns));
  40015. AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  40016. DYNAMIC_TYPE_OPENSSL));
  40017. XMEMCPY(fields_copy, fields, sizeof(fields));
  40018. AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
  40019. BIO_free(bio);
  40020. /* Test write */
  40021. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  40022. AssertIntEQ(TXT_DB_write(bio, db), 1484);
  40023. BIO_free(bio);
  40024. /* Test index */
  40025. AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  40026. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  40027. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40028. fields[3] = "12DA";
  40029. AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40030. fields[3] = "FFFF";
  40031. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40032. fields[3] = "";
  40033. AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  40034. TXT_DB_free(db);
  40035. printf(resultFmt, passed);
  40036. #endif
  40037. return 0;
  40038. }
  40039. static int test_wolfSSL_NCONF(void)
  40040. {
  40041. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  40042. const char* confFile = "./tests/NCONF_test.cnf";
  40043. CONF* conf = NULL;
  40044. long eline = 0;
  40045. long num = 0;
  40046. printf(testingFmt, "wolfSSL_NCONF");
  40047. AssertNotNull(conf = NCONF_new(NULL));
  40048. AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
  40049. AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  40050. AssertIntEQ(num, 1234);
  40051. AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  40052. AssertIntEQ(num, 4321);
  40053. AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  40054. AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  40055. "./test-dir/file1");
  40056. AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  40057. "./test-dir/file1:./test-dir/file2:./section1:file2");
  40058. NCONF_free(conf);
  40059. printf(resultFmt, passed);
  40060. #endif
  40061. return 0;
  40062. }
  40063. #endif /* OPENSSL_ALL */
  40064. static int test_wolfSSL_EC_KEY_set_group(void)
  40065. {
  40066. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  40067. defined(OPENSSL_EXTRA)
  40068. EC_KEY *key = NULL;
  40069. EC_GROUP *group = NULL;
  40070. const EC_GROUP *group2 = NULL;
  40071. printf(testingFmt, "wolfSSL_EC_KEY_dup()");
  40072. AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  40073. AssertNotNull(key = EC_KEY_new());
  40074. AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  40075. AssertNotNull(group2 = EC_KEY_get0_group(key));
  40076. AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  40077. EC_GROUP_free(group);
  40078. EC_KEY_free(key);
  40079. printf(resultFmt, passed);
  40080. #endif
  40081. return 0;
  40082. }
  40083. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  40084. {
  40085. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  40086. BIO* bio;
  40087. EC_KEY* key;
  40088. printf(testingFmt, "test_wolfSSL_EC_KEY_set_conv_form");
  40089. /* Error condition: NULL key. */
  40090. AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
  40091. AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  40092. AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  40093. /* Conversion form defaults to uncompressed. */
  40094. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  40095. #ifdef HAVE_COMP_KEY
  40096. /* Explicitly set to compressed. */
  40097. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  40098. AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  40099. #endif
  40100. BIO_free(bio);
  40101. EC_KEY_free(key);
  40102. printf(resultFmt, passed);
  40103. #endif
  40104. return 0;
  40105. }
  40106. static int test_wolfSSL_EC_KEY_print_fp(void)
  40107. {
  40108. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  40109. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256 && \
  40110. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  40111. !defined(NO_STDIO_FILESYSTEM)
  40112. EC_KEY* key = NULL;
  40113. printf(testingFmt, "test_wolfSSL_EC_KEY_print_fp");
  40114. /* Bad file pointer. */
  40115. AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  40116. /* NULL key. */
  40117. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, NULL, 0), WOLFSSL_FAILURE);
  40118. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  40119. /* Negative indent. */
  40120. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, -1), WOLFSSL_FAILURE);
  40121. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  40122. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  40123. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  40124. wolfSSL_EC_KEY_free(key);
  40125. AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  40126. NID_X9_62_prime256v1)));
  40127. AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  40128. AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
  40129. wolfSSL_EC_KEY_free(key);
  40130. printf(resultFmt, passed);
  40131. #endif
  40132. return 0;
  40133. }
  40134. static int test_wolfSSL_X509V3_EXT_get(void) {
  40135. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40136. FILE* f;
  40137. int numOfExt =0;
  40138. int extNid = 0;
  40139. int i = 0;
  40140. WOLFSSL_X509* x509;
  40141. WOLFSSL_X509_EXTENSION* ext;
  40142. const WOLFSSL_v3_ext_method* method;
  40143. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40144. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40145. fclose(f);
  40146. printf(testingFmt, "wolfSSL_X509V3_EXT_get() return struct and nid test");
  40147. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40148. for (i = 0; i < numOfExt; i++) {
  40149. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40150. AssertIntNE((extNid = ext->obj->nid), NID_undef);
  40151. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40152. AssertIntEQ(method->ext_nid, extNid);
  40153. }
  40154. printf(resultFmt, "passed");
  40155. printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
  40156. AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
  40157. printf(resultFmt, "passed");
  40158. wolfSSL_X509_free(x509);
  40159. #endif
  40160. return 0;
  40161. }
  40162. static int test_wolfSSL_X509V3_EXT_nconf(void)
  40163. {
  40164. #ifdef OPENSSL_ALL
  40165. const char *ext_names[] = {
  40166. "subjectKeyIdentifier",
  40167. "authorityKeyIdentifier",
  40168. "subjectAltName",
  40169. "keyUsage",
  40170. };
  40171. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  40172. int ext_nids[] = {
  40173. NID_subject_key_identifier,
  40174. NID_authority_key_identifier,
  40175. NID_subject_alt_name,
  40176. NID_key_usage,
  40177. };
  40178. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  40179. const char *ext_values[] = {
  40180. "hash",
  40181. "hash",
  40182. "DNS:example.com, IP:127.0.0.1",
  40183. "digitalSignature,keyEncipherment,dataEncipherment",
  40184. };
  40185. size_t i;
  40186. X509_EXTENSION* ext;
  40187. X509* x509 = X509_new();
  40188. printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
  40189. for (i = 0; i < ext_names_count; i++) {
  40190. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40191. AssertNotNull(ext);
  40192. X509_EXTENSION_free(ext);
  40193. }
  40194. for (i = 0; i < ext_nids_count; i++) {
  40195. ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
  40196. AssertNotNull(ext);
  40197. X509_EXTENSION_free(ext);
  40198. }
  40199. /* Test adding extension to X509 */
  40200. for (i = 0; i < ext_nids_count; i++) {
  40201. ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
  40202. AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  40203. X509_EXTENSION_free(ext);
  40204. }
  40205. X509_free(x509);
  40206. printf(resultFmt, "passed");
  40207. #endif
  40208. return 0;
  40209. }
  40210. static int test_wolfSSL_X509V3_EXT(void) {
  40211. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40212. FILE* f;
  40213. int numOfExt = 0, nid = 0, i = 0, expected, actual;
  40214. char* str;
  40215. unsigned char* data;
  40216. const WOLFSSL_v3_ext_method* method;
  40217. WOLFSSL_X509* x509;
  40218. WOLFSSL_X509_EXTENSION* ext;
  40219. WOLFSSL_X509_EXTENSION* ext2;
  40220. WOLFSSL_ASN1_OBJECT *obj, *adObj;
  40221. WOLFSSL_ASN1_STRING* asn1str;
  40222. WOLFSSL_AUTHORITY_KEYID* aKeyId;
  40223. WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
  40224. WOLFSSL_BASIC_CONSTRAINTS* bc;
  40225. WOLFSSL_ACCESS_DESCRIPTION* ad;
  40226. WOLFSSL_GENERAL_NAME* gn;
  40227. printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
  40228. /* Check NULL argument */
  40229. AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
  40230. /* Using OCSP cert with X509V3 extensions */
  40231. AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
  40232. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40233. fclose(f);
  40234. AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  40235. /* Basic Constraints */
  40236. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40237. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40238. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  40239. AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  40240. AssertIntEQ(bc->ca, 1);
  40241. AssertNull(bc->pathlen);
  40242. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  40243. i++;
  40244. /* Subject Key Identifier */
  40245. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40246. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40247. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  40248. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40249. AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  40250. asn1str));
  40251. X509_EXTENSION_free(ext2);
  40252. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40253. AssertNotNull(method->i2s);
  40254. AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  40255. wolfSSL_ASN1_STRING_free(asn1str);
  40256. actual = strcmp(str,
  40257. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40258. AssertIntEQ(actual, 0);
  40259. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40260. i++;
  40261. /* Authority Key Identifier */
  40262. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40263. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40264. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  40265. AssertNotNull(aKeyId =
  40266. (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
  40267. AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  40268. AssertNotNull(asn1str = aKeyId->keyid);
  40269. AssertNotNull(str =
  40270. wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
  40271. actual = strcmp(str,
  40272. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  40273. AssertIntEQ(actual, 0);
  40274. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40275. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  40276. i++;
  40277. /* Key Usage */
  40278. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40279. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40280. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  40281. AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  40282. #if defined(WOLFSSL_QT)
  40283. AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  40284. #else
  40285. AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  40286. #endif
  40287. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  40288. #ifdef BIG_ENDIAN_ORDER
  40289. actual = data[1];
  40290. #else
  40291. actual = data[0];
  40292. #endif
  40293. AssertIntEQ(actual, expected);
  40294. wolfSSL_ASN1_STRING_free(asn1str);
  40295. #if 1
  40296. i++;
  40297. /* Authority Info Access */
  40298. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  40299. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  40300. AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  40301. AssertNotNull(aia =
  40302. (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
  40303. #if defined(WOLFSSL_QT)
  40304. AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40305. #else
  40306. AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  40307. #endif
  40308. /* URI entry is an ACCESS_DESCRIPTION type */
  40309. #if defined(WOLFSSL_QT)
  40310. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  40311. #else
  40312. AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  40313. #endif
  40314. AssertNotNull(adObj = ad->method);
  40315. /* Make sure nid is OCSP */
  40316. AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  40317. /* GENERAL_NAME stores URI as an ASN1_STRING */
  40318. AssertNotNull(gn = ad->location);
  40319. AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  40320. AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
  40321. AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  40322. #if defined(WOLFSSL_QT)
  40323. str = (char*)ASN1_STRING_get0_data(asn1str);
  40324. #else
  40325. str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
  40326. #endif
  40327. actual = strcmp(str, "http://127.0.0.1:22220");
  40328. AssertIntEQ(actual, 0);
  40329. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  40330. #else
  40331. (void) aia; (void) ad; (void) adObj; (void) gn;
  40332. #endif
  40333. wolfSSL_X509_free(x509);
  40334. printf(resultFmt, "passed");
  40335. #endif
  40336. return 0;
  40337. }
  40338. static int test_wolfSSL_X509_get_extension_flags(void)
  40339. {
  40340. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40341. XFILE f;
  40342. X509* x509;
  40343. unsigned int extFlags;
  40344. unsigned int keyUsageFlags;
  40345. unsigned int extKeyUsageFlags;
  40346. printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
  40347. /* client-int-cert.pem has the following extension flags. */
  40348. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  40349. /* and the following key usage flags. */
  40350. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40351. | KU_NON_REPUDIATION
  40352. | KU_KEY_ENCIPHERMENT;
  40353. /* and the following extended key usage flags. */
  40354. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  40355. f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
  40356. AssertTrue(f != XBADFILE);
  40357. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40358. XFCLOSE(f);
  40359. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40360. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40361. AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  40362. X509_free(x509);
  40363. /* client-cert-ext.pem has the following extension flags. */
  40364. extFlags = EXFLAG_KUSAGE;
  40365. /* and the following key usage flags. */
  40366. keyUsageFlags = KU_DIGITAL_SIGNATURE
  40367. | KU_KEY_CERT_SIGN
  40368. | KU_CRL_SIGN;
  40369. AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
  40370. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40371. XFCLOSE(f);
  40372. AssertIntEQ(X509_get_extension_flags(x509), extFlags);
  40373. AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  40374. X509_free(x509);
  40375. printf(resultFmt, passed);
  40376. #endif /* OPENSSL_ALL */
  40377. return 0;
  40378. }
  40379. static int test_wolfSSL_X509_get_ext(void){
  40380. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40381. int ret = 0;
  40382. FILE* f;
  40383. WOLFSSL_X509* x509;
  40384. WOLFSSL_X509_EXTENSION* foundExtension;
  40385. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40386. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40387. fclose(f);
  40388. AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  40389. printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
  40390. AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  40391. printf(resultFmt, "passed");
  40392. printf(testingFmt, "wolfSSL_X509_get_ext() valid x509, idx out of bounds");
  40393. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  40394. AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  40395. printf(resultFmt, "passed");
  40396. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, idx out of bounds");
  40397. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  40398. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  40399. printf(resultFmt, "passed");
  40400. printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
  40401. AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  40402. printf(resultFmt, "passed");
  40403. wolfSSL_X509_free(x509);
  40404. #endif
  40405. return 0;
  40406. }
  40407. static int test_wolfSSL_X509_get_ext_by_NID(void)
  40408. {
  40409. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40410. int rc;
  40411. FILE* f;
  40412. WOLFSSL_X509* x509;
  40413. ASN1_OBJECT* obj = NULL;
  40414. AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
  40415. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40416. fclose(f);
  40417. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40418. AssertIntGE(rc, 0);
  40419. /* Start search from last location (should fail) */
  40420. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
  40421. AssertIntGE(rc, -1);
  40422. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
  40423. AssertIntGE(rc, -1);
  40424. rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
  40425. AssertIntEQ(rc, -1);
  40426. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
  40427. AssertIntEQ(rc, -1);
  40428. /* NID_ext_key_usage, check also its nid and oid */
  40429. rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
  40430. AssertIntGT(rc, -1);
  40431. AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
  40432. AssertIntEQ(obj->nid, NID_ext_key_usage);
  40433. AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
  40434. wolfSSL_X509_free(x509);
  40435. #endif
  40436. return 0;
  40437. }
  40438. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  40439. {
  40440. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40441. int rc;
  40442. XFILE f;
  40443. WOLFSSL_X509* x509;
  40444. WOLFSSL_X509_EXTENSION* ext;
  40445. WOLFSSL_ASN1_STRING* sanString;
  40446. byte* sanDer;
  40447. const byte expectedDer[] = {
  40448. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  40449. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  40450. printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
  40451. f = XFOPEN("./certs/server-cert.pem", "rb");
  40452. AssertTrue(f != XBADFILE);
  40453. AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  40454. fclose(f);
  40455. rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
  40456. AssertIntNE(rc, -1);
  40457. AssertNotNull(ext = X509_get_ext(x509, rc));
  40458. AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
  40459. AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  40460. AssertNotNull(sanDer = ASN1_STRING_data(sanString));
  40461. AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  40462. X509_free(x509);
  40463. printf(resultFmt, passed);
  40464. #endif
  40465. return 0;
  40466. }
  40467. static int test_wolfSSL_X509_EXTENSION_new(void)
  40468. {
  40469. #if defined (OPENSSL_ALL)
  40470. WOLFSSL_X509_EXTENSION* ext;
  40471. AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
  40472. AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  40473. ext->obj->nid = WOLFSSL_SUCCESS;
  40474. AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
  40475. wolfSSL_X509_EXTENSION_free(ext);
  40476. #endif
  40477. return 0;
  40478. }
  40479. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  40480. {
  40481. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40482. WOLFSSL_X509* x509;
  40483. WOLFSSL_X509_EXTENSION* ext;
  40484. WOLFSSL_ASN1_OBJECT* o;
  40485. FILE* file;
  40486. int nid = 0;
  40487. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40488. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40489. fclose(file);
  40490. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() testing ext idx 0");
  40491. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40492. AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  40493. AssertIntEQ(o->nid, 128);
  40494. nid = o->nid;
  40495. printf(resultFmt, nid == 128 ? passed : failed);
  40496. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
  40497. AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  40498. printf(resultFmt, passed);
  40499. wolfSSL_X509_free(x509);
  40500. #endif
  40501. return 0;
  40502. }
  40503. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  40504. {
  40505. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40506. WOLFSSL_X509* x509;
  40507. WOLFSSL_X509_EXTENSION* ext;
  40508. WOLFSSL_ASN1_STRING* str;
  40509. FILE* file;
  40510. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
  40511. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40512. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40513. fclose(file);
  40514. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40515. AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  40516. printf(resultFmt, passed);
  40517. wolfSSL_X509_free(x509);
  40518. #endif
  40519. return 0;
  40520. }
  40521. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  40522. {
  40523. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  40524. WOLFSSL_X509* x509;
  40525. WOLFSSL_X509_EXTENSION* ext;
  40526. FILE* file;
  40527. int crit;
  40528. printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
  40529. AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  40530. AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  40531. fclose(file);
  40532. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  40533. crit = wolfSSL_X509_EXTENSION_get_critical(ext);
  40534. AssertIntEQ(crit, 0);
  40535. printf(resultFmt, passed);
  40536. wolfSSL_X509_free(x509);
  40537. #endif
  40538. return 0;
  40539. }
  40540. static int test_wolfSSL_X509V3_EXT_print(void)
  40541. {
  40542. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  40543. !defined(NO_RSA)
  40544. printf(testingFmt, "wolfSSL_X509V3_EXT_print");
  40545. {
  40546. FILE* f;
  40547. WOLFSSL_X509* x509;
  40548. X509_EXTENSION * ext = NULL;
  40549. int loc;
  40550. BIO *bio = NULL;
  40551. AssertNotNull(f = fopen(svrCertFile, "rb"));
  40552. AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  40553. fclose(f);
  40554. AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  40555. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
  40556. AssertIntGT(loc, -1);
  40557. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40558. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40559. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
  40560. AssertIntGT(loc, -1);
  40561. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40562. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40563. loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
  40564. AssertIntGT(loc, -1);
  40565. AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  40566. AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  40567. wolfSSL_BIO_free(bio);
  40568. wolfSSL_X509_free(x509);
  40569. }
  40570. {
  40571. X509 *x509;
  40572. BIO *bio;
  40573. X509_EXTENSION *ext;
  40574. unsigned int i;
  40575. unsigned int idx;
  40576. /* Some NIDs to test with */
  40577. int nids[] = {
  40578. /* NID_key_usage, currently X509_get_ext returns this as a bit
  40579. * string, which messes up X509V3_EXT_print */
  40580. /* NID_ext_key_usage, */
  40581. NID_subject_alt_name,
  40582. };
  40583. int* n;
  40584. AssertNotNull(bio = BIO_new_fp(stdout, BIO_NOCLOSE));
  40585. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  40586. WOLFSSL_FILETYPE_PEM));
  40587. printf("\nPrinting extension values:\n");
  40588. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  40589. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  40590. * update the index */
  40591. AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  40592. AssertNotNull(ext = X509_get_ext(x509, idx));
  40593. AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  40594. printf("\n");
  40595. }
  40596. BIO_free(bio);
  40597. X509_free(x509);
  40598. }
  40599. printf(resultFmt, passed);
  40600. #endif
  40601. return 0;
  40602. }
  40603. static int test_wolfSSL_X509_cmp(void)
  40604. {
  40605. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  40606. FILE* file1;
  40607. FILE* file2;
  40608. WOLFSSL_X509* cert1;
  40609. WOLFSSL_X509* cert2;
  40610. int ret = 0;
  40611. AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
  40612. AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
  40613. AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  40614. AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  40615. fclose(file1);
  40616. fclose(file2);
  40617. printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
  40618. ret = wolfSSL_X509_cmp(cert1, cert1);
  40619. AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  40620. printf(resultFmt, ret == 0 ? passed : failed);
  40621. fflush(stdout);
  40622. printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
  40623. ret = wolfSSL_X509_cmp(cert1, cert2);
  40624. AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  40625. printf(resultFmt, ret == -1 ? passed : failed);
  40626. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
  40627. ret = wolfSSL_X509_cmp(NULL, cert2);
  40628. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  40629. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40630. printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
  40631. ret = wolfSSL_X509_cmp(cert1, NULL);
  40632. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  40633. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40634. printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
  40635. ret = wolfSSL_X509_cmp(NULL, NULL);
  40636. AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  40637. printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
  40638. wolfSSL_X509_free(cert1);
  40639. wolfSSL_X509_free(cert2);
  40640. #endif
  40641. return 0;
  40642. }
  40643. static int test_wolfSSL_PKEY_up_ref(void)
  40644. {
  40645. #if defined(OPENSSL_ALL)
  40646. EVP_PKEY* pkey;
  40647. printf(testingFmt, "wolfSSL_PKEY_up_ref()");
  40648. pkey = EVP_PKEY_new();
  40649. AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
  40650. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40651. EVP_PKEY_free(pkey);
  40652. AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
  40653. EVP_PKEY_free(pkey);
  40654. EVP_PKEY_free(pkey);
  40655. printf(resultFmt, "passed");
  40656. #endif
  40657. return 0;
  40658. }
  40659. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  40660. {
  40661. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  40662. EVP_PKEY* pkey;
  40663. const unsigned char* p;
  40664. unsigned char* der = NULL;
  40665. int derLen;
  40666. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
  40667. p = client_keypub_der_2048;
  40668. /* Check that key can be successfully decoded. */
  40669. AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  40670. sizeof_client_keypub_der_2048));
  40671. /* Check that key can be successfully encoded. */
  40672. AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  40673. /* Ensure that the encoded version matches the original. */
  40674. AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
  40675. AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  40676. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40677. EVP_PKEY_free(pkey);
  40678. printf(resultFmt, passed);
  40679. #endif
  40680. return 0;
  40681. }
  40682. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  40683. {
  40684. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  40685. DSA* dsa;
  40686. char file[] = "./certs/dsaparams.der";
  40687. XFILE f;
  40688. int derInLen;
  40689. byte* derIn;
  40690. int derOutLen;
  40691. byte* derOut = NULL;
  40692. printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
  40693. f = XFOPEN(file, "rb");
  40694. AssertTrue(f != XBADFILE);
  40695. AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  40696. derInLen = (int)XFTELL(f);
  40697. XREWIND(f);
  40698. AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
  40699. DYNAMIC_TYPE_TMP_BUFFER));
  40700. AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
  40701. XFCLOSE(f);
  40702. /* Check that params can be successfully decoded. */
  40703. AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
  40704. /* Check that params can be successfully encoded. */
  40705. AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  40706. /* Ensure that the encoded version matches the original. */
  40707. AssertIntEQ(derInLen, derOutLen);
  40708. AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  40709. XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  40710. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  40711. DSA_free(dsa);
  40712. printf(resultFmt, passed);
  40713. #endif
  40714. return 0;
  40715. }
  40716. static int test_wolfSSL_i2d_PrivateKey(void)
  40717. {
  40718. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
  40719. printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
  40720. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  40721. {
  40722. EVP_PKEY* pkey;
  40723. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  40724. unsigned char buf[FOURK_BUF];
  40725. unsigned char* pt = NULL;
  40726. int bufSz;
  40727. AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  40728. (long)sizeof_server_key_der_2048));
  40729. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  40730. pt = buf;
  40731. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  40732. AssertIntNE((pt - buf), 0);
  40733. AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  40734. EVP_PKEY_free(pkey);
  40735. }
  40736. #endif
  40737. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  40738. {
  40739. EVP_PKEY* pkey;
  40740. const unsigned char* client_key =
  40741. (const unsigned char*)ecc_clikey_der_256;
  40742. unsigned char buf[FOURK_BUF];
  40743. unsigned char* pt = NULL;
  40744. int bufSz;
  40745. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  40746. sizeof_ecc_clikey_der_256)));
  40747. AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  40748. pt = buf;
  40749. AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  40750. AssertIntNE((pt - buf), 0);
  40751. AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  40752. EVP_PKEY_free(pkey);
  40753. }
  40754. #endif
  40755. printf(resultFmt, "passed");
  40756. #endif
  40757. return 0;
  40758. }
  40759. static int test_wolfSSL_OCSP_id_get0_info(void)
  40760. {
  40761. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
  40762. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  40763. X509* cert;
  40764. X509* issuer;
  40765. OCSP_CERTID* id;
  40766. OCSP_CERTID* id2;
  40767. ASN1_STRING* name = NULL;
  40768. ASN1_OBJECT* pmd = NULL;
  40769. ASN1_STRING* keyHash = NULL;
  40770. ASN1_INTEGER* serial = NULL;
  40771. ASN1_INTEGER* x509Int;
  40772. printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
  40773. AssertNotNull(cert =
  40774. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
  40775. AssertNotNull(issuer =
  40776. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  40777. id = OCSP_cert_to_id(NULL, cert, issuer);
  40778. AssertNotNull(id);
  40779. id2 = OCSP_cert_to_id(NULL, cert, issuer);
  40780. AssertNotNull(id2);
  40781. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  40782. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  40783. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  40784. AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  40785. AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  40786. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  40787. AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  40788. AssertNotNull(serial);
  40789. /* compare serial number to one in cert, should be equal */
  40790. x509Int = X509_get_serialNumber(cert);
  40791. AssertNotNull(x509Int);
  40792. AssertIntEQ(x509Int->length, serial->length);
  40793. AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  40794. /* test OCSP_id_cmp */
  40795. AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
  40796. AssertIntNE(OCSP_id_cmp(id, NULL), 0);
  40797. AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
  40798. AssertIntEQ(OCSP_id_cmp(id, id2), 0);
  40799. id->issuerHash[0] = ~id->issuerHash[0];
  40800. AssertIntNE(OCSP_id_cmp(id, id2), 0);
  40801. OCSP_CERTID_free(id);
  40802. OCSP_CERTID_free(id2);
  40803. X509_free(cert); /* free's x509Int */
  40804. X509_free(issuer);
  40805. printf(resultFmt, "passed");
  40806. #endif
  40807. return 0;
  40808. }
  40809. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  40810. {
  40811. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40812. WOLFSSL_OCSP_CERTID certId;
  40813. byte* targetBuffer;
  40814. byte* beginTargetBuffer;
  40815. /* OCSP CertID bytes taken from PCAP */
  40816. byte rawCertId[] = {
  40817. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40818. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40819. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40820. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40821. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40822. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40823. 0xcf, 0xbc, 0x91
  40824. };
  40825. int ret, i;
  40826. printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
  40827. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  40828. certId.rawCertId = rawCertId;
  40829. certId.rawCertIdSize = sizeof(rawCertId);
  40830. targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40831. beginTargetBuffer = targetBuffer;
  40832. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40833. /* If target buffer is not null, function increments targetBuffer to point
  40834. just past the end of the encoded data. */
  40835. AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
  40836. /* Function returns the size of the encoded data. */
  40837. AssertIntEQ(ret, sizeof(rawCertId));
  40838. for (i = 0; i < ret; ++i)
  40839. {
  40840. AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
  40841. }
  40842. XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40843. targetBuffer = NULL;
  40844. ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
  40845. /* If target buffer is null, function allocates memory for a buffer and
  40846. copies the encoded data into it. targetBuffer then points to the start of
  40847. this newly allocate buffer. */
  40848. AssertIntEQ(ret, sizeof(rawCertId));
  40849. for (i = 0; i < ret; ++i)
  40850. {
  40851. AssertIntEQ(targetBuffer[i], rawCertId[i]);
  40852. }
  40853. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  40854. printf(resultFmt, passed);
  40855. #endif
  40856. return 0;
  40857. }
  40858. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  40859. {
  40860. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  40861. WOLFSSL_OCSP_CERTID* certId;
  40862. WOLFSSL_OCSP_CERTID* certIdBad;
  40863. const unsigned char* rawCertIdPtr;
  40864. const unsigned char rawCertId[] = {
  40865. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  40866. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  40867. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  40868. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  40869. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  40870. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  40871. 0xcf, 0xbc, 0x91
  40872. };
  40873. rawCertIdPtr = &rawCertId[0];
  40874. printf(testingFmt, "wolfSSL_d2i_OCSP_CERTID()");
  40875. /* If the cert ID is NULL the function should allocate it and copy the
  40876. * data to it. */
  40877. certId = NULL;
  40878. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  40879. AssertNotNull(certId);
  40880. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  40881. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  40882. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  40883. /* If the cert ID is not NULL the function will just copy the data to it. */
  40884. certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  40885. DYNAMIC_TYPE_TMP_BUFFER);
  40886. AssertNotNull(certId);
  40887. XMEMSET(certId, 0, sizeof(*certId));
  40888. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  40889. rawCertIdPtr = &rawCertId[0];
  40890. certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
  40891. AssertNotNull(certId);
  40892. AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  40893. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  40894. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  40895. /* The below tests should fail when passed bad parameters. NULL should
  40896. * always be returned. */
  40897. certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
  40898. AssertNull(certIdBad);
  40899. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
  40900. AssertNull(certIdBad);
  40901. certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
  40902. AssertNull(certIdBad);
  40903. printf(resultFmt, passed);
  40904. #endif
  40905. return 0;
  40906. }
  40907. static int test_wolfSSL_OCSP_id_cmp(void)
  40908. {
  40909. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40910. OCSP_CERTID id1;
  40911. OCSP_CERTID id2;
  40912. printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
  40913. XMEMSET(&id1, 0, sizeof(id1));
  40914. XMEMSET(&id2, 0, sizeof(id2));
  40915. AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  40916. printf(resultFmt, passed);
  40917. #endif
  40918. return 0;
  40919. }
  40920. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  40921. {
  40922. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40923. WOLFSSL_OCSP_SINGLERESP single;
  40924. const WOLFSSL_OCSP_CERTID* certId;
  40925. XMEMSET(&single, 0, sizeof(single));
  40926. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  40927. printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
  40928. AssertPtrEq(&single, certId);
  40929. printf(resultFmt, passed);
  40930. #endif
  40931. return 0;
  40932. }
  40933. static int test_wolfSSL_OCSP_single_get0_status(void)
  40934. {
  40935. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40936. WOLFSSL_OCSP_SINGLERESP single;
  40937. CertStatus certStatus;
  40938. WOLFSSL_ASN1_TIME* thisDate;
  40939. WOLFSSL_ASN1_TIME* nextDate;
  40940. int ret, i;
  40941. printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
  40942. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40943. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  40944. /* Fill the date fields with some dummy data. */
  40945. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  40946. certStatus.thisDateParsed.data[i] = i;
  40947. certStatus.nextDateParsed.data[i] = i;
  40948. }
  40949. certStatus.status = CERT_GOOD;
  40950. single.status = &certStatus;
  40951. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  40952. &nextDate);
  40953. AssertIntEQ(ret, CERT_GOOD);
  40954. AssertPtrEq(thisDate, &certStatus.thisDateParsed);
  40955. AssertPtrEq(nextDate, &certStatus.nextDateParsed);
  40956. printf(resultFmt, passed);
  40957. #endif
  40958. return 0;
  40959. }
  40960. static int test_wolfSSL_OCSP_resp_count(void)
  40961. {
  40962. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40963. WOLFSSL_OCSP_BASICRESP basicResp;
  40964. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  40965. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  40966. int count;
  40967. printf(testingFmt, "wolfSSL_OCSP_resp_count()");
  40968. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  40969. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40970. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40971. count = wolfSSL_OCSP_resp_count(&basicResp);
  40972. AssertIntEQ(count, 0);
  40973. basicResp.single = &singleRespOne;
  40974. count = wolfSSL_OCSP_resp_count(&basicResp);
  40975. AssertIntEQ(count, 1);
  40976. singleRespOne.next = &singleRespTwo;
  40977. count = wolfSSL_OCSP_resp_count(&basicResp);
  40978. AssertIntEQ(count, 2);
  40979. printf(resultFmt, passed);
  40980. #endif
  40981. return 0;
  40982. }
  40983. static int test_wolfSSL_OCSP_resp_get0(void)
  40984. {
  40985. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  40986. WOLFSSL_OCSP_BASICRESP basicResp;
  40987. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  40988. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  40989. WOLFSSL_OCSP_SINGLERESP* ret;
  40990. printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
  40991. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  40992. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40993. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  40994. basicResp.single = &singleRespOne;
  40995. singleRespOne.next = &singleRespTwo;
  40996. ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
  40997. AssertPtrEq(ret, &singleRespOne);
  40998. ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
  40999. AssertPtrEq(ret, &singleRespTwo);
  41000. printf(resultFmt, passed);
  41001. #endif
  41002. return 0;
  41003. }
  41004. static int test_wolfSSL_EVP_PKEY_derive(void)
  41005. {
  41006. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  41007. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  41008. EVP_PKEY_CTX *ctx;
  41009. unsigned char *skey;
  41010. size_t skeylen;
  41011. EVP_PKEY *pkey, *peerkey;
  41012. const unsigned char* key;
  41013. printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
  41014. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  41015. /* DH */
  41016. key = dh_key_der_2048;
  41017. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41018. sizeof_dh_key_der_2048)));
  41019. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  41020. key = dh_key_der_2048;
  41021. AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  41022. sizeof_dh_key_der_2048)));
  41023. AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  41024. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41025. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41026. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41027. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41028. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41029. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41030. EVP_PKEY_CTX_free(ctx);
  41031. EVP_PKEY_free(peerkey);
  41032. EVP_PKEY_free(pkey);
  41033. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41034. #endif
  41035. #ifdef HAVE_ECC
  41036. /* ECDH */
  41037. key = ecc_clikey_der_256;
  41038. AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  41039. sizeof_ecc_clikey_der_256)));
  41040. key = ecc_clikeypub_der_256;
  41041. AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  41042. sizeof_ecc_clikeypub_der_256)));
  41043. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41044. AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
  41045. AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  41046. AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  41047. AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
  41048. AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  41049. EVP_PKEY_CTX_free(ctx);
  41050. EVP_PKEY_free(peerkey);
  41051. EVP_PKEY_free(pkey);
  41052. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  41053. #endif /* HAVE_ECC */
  41054. printf(resultFmt, "passed");
  41055. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  41056. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  41057. return 0;
  41058. }
  41059. static int test_wolfSSL_EVP_PBE_scrypt(void)
  41060. {
  41061. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  41062. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  41063. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  41064. int ret;
  41065. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  41066. int pwdlen = sizeof(pwd);
  41067. const byte salt[] = {'N','a','C','l'};
  41068. int saltlen = sizeof(salt);
  41069. byte key[80];
  41070. word64 numOvr32 = (word64)INT32_MAX + 1;
  41071. /* expected derived key for N:16, r:1, p:1 */
  41072. const byte expectedKey[] = {
  41073. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  41074. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  41075. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  41076. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  41077. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  41078. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  41079. 0xE7, 0xE9, 0xC0, 0x9A};
  41080. printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
  41081. /* N r p mx key keylen */
  41082. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  41083. AssertIntEQ(ret, 0); /* N must be greater than 1 */
  41084. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  41085. AssertIntEQ(ret, 0); /* N must be power of 2 */
  41086. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  41087. AssertIntEQ(ret, 0); /* r must be greater than 0 */
  41088. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  41089. AssertIntEQ(ret, 0); /* p must be greater than 0 */
  41090. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  41091. AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
  41092. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  41093. AssertIntEQ(ret, 0); /* r must be smaller than 9 */
  41094. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  41095. AssertIntEQ(ret, 1); /* should succeed if key is NULL */
  41096. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  41097. AssertIntEQ(ret, 1); /* should succeed */
  41098. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  41099. key, 64);
  41100. AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  41101. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  41102. key, 64);
  41103. AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  41104. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  41105. AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
  41106. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  41107. AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  41108. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  41109. AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  41110. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  41111. AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  41112. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  41113. AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  41114. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  41115. AssertIntEQ(ret, 1);
  41116. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  41117. AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
  41118. printf(resultFmt, "passed");
  41119. #endif /* !NO_PWDBASED && !NO_SHA256 */
  41120. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  41121. return 0;
  41122. }
  41123. static int test_wolfSSL_EC_get_builtin_curves(void)
  41124. {
  41125. #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
  41126. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41127. EC_builtin_curve* curves = NULL;
  41128. size_t crv_len = 0;
  41129. size_t i = 0;
  41130. printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
  41131. AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  41132. AssertNotNull(curves = (EC_builtin_curve*)
  41133. XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
  41134. DYNAMIC_TYPE_TMP_BUFFER));
  41135. AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  41136. for (i = 0; i < crv_len; i++)
  41137. {
  41138. if (curves[i].comment != NULL)
  41139. AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  41140. }
  41141. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  41142. printf(resultFmt, passed);
  41143. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41144. #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  41145. return 0;
  41146. }
  41147. static int test_no_op_functions(void)
  41148. {
  41149. #if defined(OPENSSL_EXTRA)
  41150. printf(testingFmt, "no_op_functions()");
  41151. /* this makes sure wolfSSL can compile and run these no-op functions */
  41152. SSL_load_error_strings();
  41153. ENGINE_load_builtin_engines();
  41154. OpenSSL_add_all_ciphers();
  41155. AssertIntEQ(CRYPTO_malloc_init(), 0);
  41156. printf(resultFmt, passed);
  41157. #endif
  41158. return 0;
  41159. }
  41160. static int test_wolfSSL_CRYPTO_memcmp(void)
  41161. {
  41162. #ifdef OPENSSL_EXTRA
  41163. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41164. "implementation of TLS/SSL for embedded devices to the cloud.";
  41165. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41166. "implementation of TLS/SSL for embedded devices to the cloud.";
  41167. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  41168. "implementation of TLS/SSL for embedded devices to the cloud!";
  41169. AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  41170. AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  41171. #endif
  41172. return 0;
  41173. }
  41174. /*----------------------------------------------------------------------------*
  41175. | wolfCrypt ASN
  41176. *----------------------------------------------------------------------------*/
  41177. static int test_wc_CreateEncryptedPKCS8Key(void)
  41178. {
  41179. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  41180. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  41181. WC_RNG rng;
  41182. byte* encKey = NULL;
  41183. word32 encKeySz = 0;
  41184. word32 decKeySz = 0;
  41185. const char password[] = "Lorem ipsum dolor sit amet";
  41186. word32 passwordSz = (word32)XSTRLEN(password);
  41187. word32 tradIdx = 0;
  41188. printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
  41189. AssertIntEQ(wc_InitRng(&rng), 0);
  41190. /* Call with NULL for out buffer to get necessary length. */
  41191. AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41192. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  41193. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41194. LENGTH_ONLY_E);
  41195. AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  41196. DYNAMIC_TYPE_TMP_BUFFER));
  41197. /* Call with the allocated out buffer. */
  41198. AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  41199. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  41200. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  41201. 0);
  41202. /* Decrypt the encrypted PKCS8 key we just made. */
  41203. AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  41204. passwordSz)), 0);
  41205. /* encKey now holds the decrypted key (decrypted in place). */
  41206. AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  41207. /* Check that the decrypted key matches the key prior to encryption. */
  41208. AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  41209. sizeof_server_key_der_2048), 0);
  41210. if (encKey != NULL)
  41211. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41212. wc_FreeRng(&rng);
  41213. printf(resultFmt, passed);
  41214. #endif
  41215. return 0;
  41216. }
  41217. static int test_wc_GetPkcs8TraditionalOffset(void)
  41218. {
  41219. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  41220. int length, derSz;
  41221. word32 inOutIdx;
  41222. const char* path = "./certs/server-keyPkcs8.der";
  41223. XFILE file;
  41224. byte der[2048];
  41225. printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
  41226. file = XFOPEN(path, "rb");
  41227. AssertTrue(file != XBADFILE);
  41228. derSz = (int)XFREAD(der, 1, sizeof(der), file);
  41229. XFCLOSE(file);
  41230. /* valid case */
  41231. inOutIdx = 0;
  41232. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41233. AssertIntGT(length, 0);
  41234. /* inOutIdx > sz */
  41235. inOutIdx = 4000;
  41236. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41237. AssertIntEQ(length, BAD_FUNC_ARG);
  41238. /* null input */
  41239. inOutIdx = 0;
  41240. length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
  41241. AssertIntEQ(length, BAD_FUNC_ARG);
  41242. /* invalid input, fill buffer with 1's */
  41243. XMEMSET(der, 1, sizeof(der));
  41244. inOutIdx = 0;
  41245. length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
  41246. AssertIntEQ(length, ASN_PARSE_E);
  41247. printf(resultFmt, passed);
  41248. #endif /* NO_ASN */
  41249. return 0;
  41250. }
  41251. static int test_wc_SetSubjectRaw(void)
  41252. {
  41253. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41254. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41255. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41256. WOLFSSL_X509* x509;
  41257. int peerCertSz;
  41258. const byte* peerCertBuf;
  41259. Cert forgedCert;
  41260. printf(testingFmt, "test_wc_SetSubjectRaw()");
  41261. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41262. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41263. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41264. AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  41265. wolfSSL_FreeX509(x509);
  41266. printf(resultFmt, passed);
  41267. #endif
  41268. return 0;
  41269. }
  41270. static int test_wc_GetSubjectRaw(void)
  41271. {
  41272. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41273. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  41274. Cert cert;
  41275. byte *subjectRaw;
  41276. printf(testingFmt, "test_wc_GetSubjectRaw()");
  41277. AssertIntEQ(0, wc_InitCert(&cert));
  41278. AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  41279. printf(resultFmt, passed);
  41280. #endif
  41281. return 0;
  41282. }
  41283. static int test_wc_SetIssuerRaw(void)
  41284. {
  41285. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41286. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41287. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41288. WOLFSSL_X509* x509;
  41289. int peerCertSz;
  41290. const byte* peerCertBuf;
  41291. Cert forgedCert;
  41292. printf(testingFmt, "test_wc_SetIssuerRaw()");
  41293. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41294. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41295. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41296. AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  41297. wolfSSL_FreeX509(x509);
  41298. printf(resultFmt, passed);
  41299. #endif
  41300. return 0;
  41301. }
  41302. static int test_wc_SetIssueBuffer(void)
  41303. {
  41304. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41305. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  41306. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  41307. WOLFSSL_X509* x509;
  41308. int peerCertSz;
  41309. const byte* peerCertBuf;
  41310. Cert forgedCert;
  41311. printf(testingFmt, "test_wc_SetIssuerBuffer()");
  41312. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
  41313. AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  41314. AssertIntEQ(0, wc_InitCert(&forgedCert));
  41315. AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  41316. wolfSSL_FreeX509(x509);
  41317. printf(resultFmt, passed);
  41318. #endif
  41319. return 0;
  41320. }
  41321. /*
  41322. * Testing wc_SetSubjectKeyId
  41323. */
  41324. static int test_wc_SetSubjectKeyId(void)
  41325. {
  41326. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41327. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41328. Cert cert;
  41329. const char* file = "certs/ecc-client-keyPub.pem";
  41330. printf(testingFmt, "wc_SetSubjectKeyId()");
  41331. AssertIntEQ(0, wc_InitCert(&cert));
  41332. AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  41333. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  41334. AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  41335. printf(resultFmt, passed);
  41336. #endif
  41337. return 0;
  41338. } /* END test_wc_SetSubjectKeyId */
  41339. /*
  41340. * Testing wc_SetSubject
  41341. */
  41342. static int test_wc_SetSubject(void)
  41343. {
  41344. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  41345. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  41346. Cert cert;
  41347. const char* file = "./certs/ca-ecc-cert.pem";
  41348. printf(testingFmt, "wc_SetSubject()");
  41349. AssertIntEQ(0, wc_InitCert(&cert));
  41350. AssertIntEQ(0, wc_SetSubject(&cert, file));
  41351. AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  41352. AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  41353. printf(resultFmt, passed);
  41354. #endif
  41355. return 0;
  41356. } /* END test_wc_SetSubject */
  41357. static int test_CheckCertSignature(void)
  41358. {
  41359. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  41360. WOLFSSL_CERT_MANAGER* cm = NULL;
  41361. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41362. FILE* fp;
  41363. byte cert[4096];
  41364. int certSz;
  41365. #endif
  41366. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  41367. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41368. AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  41369. #ifndef NO_RSA
  41370. #ifdef USE_CERT_BUFFERS_1024
  41371. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  41372. sizeof_server_cert_der_1024, NULL, cm));
  41373. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41374. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  41375. WOLFSSL_FILETYPE_ASN1));
  41376. AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
  41377. sizeof_server_cert_der_1024, NULL, cm));
  41378. #elif defined(USE_CERT_BUFFERS_2048)
  41379. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  41380. sizeof_server_cert_der_2048, NULL, cm));
  41381. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41382. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  41383. WOLFSSL_FILETYPE_ASN1));
  41384. AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
  41385. sizeof_server_cert_der_2048, NULL, cm));
  41386. #endif
  41387. #endif
  41388. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41389. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  41390. sizeof_serv_ecc_der_256, NULL, cm));
  41391. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  41392. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  41393. WOLFSSL_FILETYPE_ASN1));
  41394. AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  41395. NULL, cm));
  41396. #endif
  41397. #if !defined(NO_FILESYSTEM)
  41398. wolfSSL_CertManagerFree(cm);
  41399. AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  41400. #ifndef NO_RSA
  41401. AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
  41402. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41403. XFCLOSE(fp);
  41404. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41405. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41406. "./certs/ca-cert.pem", NULL));
  41407. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41408. #endif
  41409. #ifdef HAVE_ECC
  41410. AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
  41411. AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  41412. XFCLOSE(fp);
  41413. AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  41414. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  41415. "./certs/ca-ecc-cert.pem", NULL));
  41416. AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  41417. #endif
  41418. #endif
  41419. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  41420. (void)fp;
  41421. (void)cert;
  41422. (void)certSz;
  41423. #endif
  41424. wolfSSL_CertManagerFree(cm);
  41425. #endif
  41426. return 0;
  41427. }
  41428. static int test_wc_ParseCert(void)
  41429. {
  41430. #if !defined(NO_CERTS) && !defined(NO_RSA)
  41431. DecodedCert decodedCert;
  41432. const byte* rawCert = client_cert_der_2048;
  41433. const int rawCertSize = sizeof_client_cert_der_2048;
  41434. printf(testingFmt, "wc_ParseCert");
  41435. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  41436. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41437. #ifndef IGNORE_NAME_CONSTRAINTS
  41438. /* check that the subjects emailAddress was not put in the alt name list */
  41439. AssertNotNull(decodedCert.subjectEmail);
  41440. AssertNull(decodedCert.altEmailNames);
  41441. #endif
  41442. wc_FreeDecodedCert(&decodedCert);
  41443. printf(resultFmt, passed);
  41444. #endif
  41445. return 0;
  41446. }
  41447. static int test_MakeCertWithPathLen(void)
  41448. {
  41449. #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  41450. const byte expectedPathLen = 7;
  41451. Cert cert;
  41452. DecodedCert decodedCert;
  41453. byte der[FOURK_BUF];
  41454. int derSize = 0;
  41455. WC_RNG rng;
  41456. ecc_key key;
  41457. printf(testingFmt, "test_MakeCertWithPathLen");
  41458. AssertIntEQ(wc_InitRng(&rng), 0);
  41459. AssertIntEQ(wc_ecc_init(&key), 0);
  41460. AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  41461. AssertIntEQ(wc_InitCert(&cert), 0);
  41462. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  41463. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  41464. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  41465. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  41466. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  41467. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
  41468. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
  41469. cert.selfSigned = 1;
  41470. cert.isCA = 1;
  41471. cert.pathLen = expectedPathLen;
  41472. cert.pathLenSet = 1;
  41473. cert.sigType = CTC_SHA256wECDSA;
  41474. #ifdef WOLFSSL_CERT_EXT
  41475. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  41476. #endif
  41477. AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  41478. derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
  41479. &key, &rng);
  41480. AssertIntGE(derSize, 0);
  41481. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  41482. AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  41483. AssertIntEQ(decodedCert.pathLength, expectedPathLen);
  41484. wc_FreeDecodedCert(&decodedCert);
  41485. AssertIntEQ(wc_ecc_free(&key), 0);
  41486. AssertIntEQ(wc_FreeRng(&rng), 0);
  41487. printf(resultFmt, passed);
  41488. #endif
  41489. return 0;
  41490. }
  41491. /*----------------------------------------------------------------------------*
  41492. | wolfCrypt ECC
  41493. *----------------------------------------------------------------------------*/
  41494. static int test_wc_ecc_get_curve_size_from_name(void)
  41495. {
  41496. #ifdef HAVE_ECC
  41497. int ret;
  41498. printf(testingFmt, "wc_ecc_get_curve_size_from_name");
  41499. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41500. ret = wc_ecc_get_curve_size_from_name("SECP256R1");
  41501. AssertIntEQ(ret, 32);
  41502. #endif
  41503. /* invalid case */
  41504. ret = wc_ecc_get_curve_size_from_name("BADCURVE");
  41505. AssertIntEQ(ret, -1);
  41506. /* NULL input */
  41507. ret = wc_ecc_get_curve_size_from_name(NULL);
  41508. AssertIntEQ(ret, BAD_FUNC_ARG);
  41509. printf(resultFmt, passed);
  41510. #endif /* HAVE_ECC */
  41511. return 0;
  41512. }
  41513. static int test_wc_ecc_get_curve_id_from_name(void)
  41514. {
  41515. #ifdef HAVE_ECC
  41516. int id;
  41517. printf(testingFmt, "wc_ecc_get_curve_id_from_name");
  41518. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41519. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41520. AssertIntEQ(id, ECC_SECP256R1);
  41521. #endif
  41522. /* invalid case */
  41523. id = wc_ecc_get_curve_id_from_name("BADCURVE");
  41524. AssertIntEQ(id, -1);
  41525. /* NULL input */
  41526. id = wc_ecc_get_curve_id_from_name(NULL);
  41527. AssertIntEQ(id, BAD_FUNC_ARG);
  41528. printf(resultFmt, passed);
  41529. #endif /* HAVE_ECC */
  41530. return 0;
  41531. }
  41532. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  41533. !defined(HAVE_SELFTEST) && \
  41534. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  41535. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  41536. {
  41537. int id;
  41538. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41539. int curve_id;
  41540. ecc_key* key;
  41541. const ecc_set_type* params;
  41542. int ret;
  41543. #endif
  41544. WOLFSSL_EC_KEY *ecKey = NULL;
  41545. printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
  41546. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41547. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  41548. AssertIntEQ(id, ECC_SECP256R1);
  41549. ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  41550. AssertNotNull(ecKey);
  41551. ret = EC_KEY_generate_key(ecKey);
  41552. if (ret == 0) {
  41553. /* normal test */
  41554. key = (ecc_key*)ecKey->internal;
  41555. params = key->dp;
  41556. curve_id = wc_ecc_get_curve_id_from_dp_params(params);
  41557. AssertIntEQ(curve_id, id);
  41558. }
  41559. #endif
  41560. /* invalid case, NULL input*/
  41561. id = wc_ecc_get_curve_id_from_dp_params(NULL);
  41562. AssertIntEQ(id, BAD_FUNC_ARG);
  41563. wolfSSL_EC_KEY_free(ecKey);
  41564. printf(resultFmt, passed);
  41565. return 0;
  41566. }
  41567. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  41568. static int test_wc_ecc_get_curve_id_from_params(void)
  41569. {
  41570. #ifdef HAVE_ECC
  41571. int id;
  41572. const byte prime[] =
  41573. {
  41574. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41575. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41576. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41577. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  41578. };
  41579. const byte primeInvalid[] =
  41580. {
  41581. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41582. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41583. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41584. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  41585. };
  41586. const byte Af[] =
  41587. {
  41588. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  41589. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  41590. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  41591. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  41592. };
  41593. const byte Bf[] =
  41594. {
  41595. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  41596. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  41597. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  41598. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  41599. };
  41600. const byte order[] =
  41601. {
  41602. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  41603. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  41604. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  41605. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  41606. };
  41607. const byte Gx[] =
  41608. {
  41609. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  41610. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  41611. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  41612. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  41613. };
  41614. const byte Gy[] =
  41615. {
  41616. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  41617. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  41618. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  41619. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  41620. };
  41621. int cofactor = 1;
  41622. int fieldSize = 256;
  41623. printf(testingFmt, "wc_ecc_get_curve_id_from_params");
  41624. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  41625. id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
  41626. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41627. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41628. AssertIntEQ(id, ECC_SECP256R1);
  41629. #endif
  41630. /* invalid case, fieldSize = 0 */
  41631. id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  41632. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41633. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41634. AssertIntEQ(id, ECC_CURVE_INVALID);
  41635. /* invalid case, NULL prime */
  41636. id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  41637. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41638. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41639. AssertIntEQ(id, BAD_FUNC_ARG);
  41640. /* invalid case, invalid prime */
  41641. id = wc_ecc_get_curve_id_from_params(fieldSize,
  41642. primeInvalid, sizeof(primeInvalid),
  41643. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  41644. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
  41645. AssertIntEQ(id, ECC_CURVE_INVALID);
  41646. printf(resultFmt, passed);
  41647. #endif
  41648. return 0;
  41649. }
  41650. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  41651. {
  41652. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41653. !defined(HAVE_FAST_RSA)
  41654. WOLFSSL_RSA* rsa = NULL;
  41655. WOLFSSL_EVP_PKEY* pkey = NULL;
  41656. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41657. const char* in = "What is easy to do is easy not to do.";
  41658. size_t inlen = XSTRLEN(in);
  41659. size_t outEncLen = 0;
  41660. byte* outEnc = NULL;
  41661. byte* outDec = NULL;
  41662. size_t outDecLen = 0;
  41663. size_t rsaKeySz = 2048/8; /* Bytes */
  41664. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41665. byte* inTmp = NULL;
  41666. byte* outEncTmp = NULL;
  41667. byte* outDecTmp = NULL;
  41668. #endif
  41669. printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
  41670. AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41671. XMEMSET(outEnc, 0, rsaKeySz);
  41672. AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41673. XMEMSET(outDec, 0, rsaKeySz);
  41674. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41675. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41676. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41677. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41678. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41679. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41680. WOLFSSL_SUCCESS);
  41681. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  41682. since ctx uses it.*/
  41683. AssertIntEQ(pkey->references, 2);
  41684. EVP_PKEY_free(pkey);
  41685. AssertIntEQ(pkey->references, 1);
  41686. /* Encrypt data */
  41687. /* Check that we can get the required output buffer length by passing in a
  41688. * NULL output buffer. */
  41689. AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  41690. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41691. AssertIntEQ(rsaKeySz, outEncLen);
  41692. /* Now do the actual encryption. */
  41693. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  41694. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  41695. /* Decrypt data */
  41696. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41697. /* Check that we can get the required output buffer length by passing in a
  41698. * NULL output buffer. */
  41699. AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  41700. WOLFSSL_SUCCESS);
  41701. AssertIntEQ(rsaKeySz, outDecLen);
  41702. /* Now do the actual decryption. */
  41703. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  41704. WOLFSSL_SUCCESS);
  41705. AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  41706. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41707. /* The input length must be the same size as the RSA key.*/
  41708. AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41709. XMEMSET(inTmp, 9, rsaKeySz);
  41710. AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41711. XMEMSET(outEncTmp, 0, rsaKeySz);
  41712. AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41713. XMEMSET(outDecTmp, 0, rsaKeySz);
  41714. AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  41715. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41716. WOLFSSL_SUCCESS);
  41717. AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  41718. WOLFSSL_SUCCESS);
  41719. AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  41720. AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
  41721. WOLFSSL_SUCCESS);
  41722. AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  41723. #endif
  41724. EVP_PKEY_CTX_free(ctx);
  41725. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41726. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41727. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  41728. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41729. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41730. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41731. #endif
  41732. printf(resultFmt, passed);
  41733. #endif
  41734. return 0;
  41735. }
  41736. static int test_wolfSSL_EVP_PKEY_sign_verify(void)
  41737. {
  41738. #if defined(OPENSSL_EXTRA)
  41739. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41740. WOLFSSL_DSA* dsa = NULL;
  41741. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41742. WOLFSSL_EVP_PKEY* pkey = NULL;
  41743. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  41744. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  41745. const char* in = "What is easy to do is easy not to do.";
  41746. size_t inlen = XSTRLEN(in);
  41747. byte hash[SHA256_DIGEST_LENGTH] = {0};
  41748. byte zero[SHA256_DIGEST_LENGTH] = {0};
  41749. SHA256_CTX c;
  41750. byte* sig = NULL;
  41751. byte* sigVerify = NULL;
  41752. size_t siglen;
  41753. size_t siglenOnlyLen;
  41754. size_t keySz = 2048/8; /* Bytes */
  41755. int i;
  41756. int encs[3] = {0};
  41757. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41758. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41759. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41760. encs[0] = EVP_PKEY_RSA;
  41761. #endif
  41762. #endif
  41763. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41764. encs[1] = EVP_PKEY_DSA;
  41765. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41766. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41767. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41768. encs[2] = EVP_PKEY_EC;
  41769. #endif
  41770. #endif
  41771. printf(testingFmt, "wolfSSL_EVP_PKEY_sign_verify()");
  41772. AssertNotNull(sig =
  41773. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41774. AssertNotNull(sigVerify =
  41775. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  41776. for (i = 0; i < 3; i++) {
  41777. if (encs[i] == 0)
  41778. continue;
  41779. siglen = keySz;
  41780. XMEMSET(sig, 0, keySz);
  41781. XMEMSET(sigVerify, 0, keySz);
  41782. /* Generate hash */
  41783. SHA256_Init(&c);
  41784. SHA256_Update(&c, in, inlen);
  41785. SHA256_Final(hash, &c);
  41786. #ifdef WOLFSSL_SMALL_STACK_CACHE
  41787. /* workaround for small stack cache case */
  41788. wc_Sha256Free((wc_Sha256*)&c);
  41789. #endif
  41790. /* Generate key */
  41791. AssertNotNull(pkey = EVP_PKEY_new());
  41792. switch (encs[i]) {
  41793. case EVP_PKEY_RSA:
  41794. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41795. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41796. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41797. {
  41798. WOLFSSL_RSA* rsa = NULL;
  41799. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  41800. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41801. }
  41802. #endif
  41803. #endif
  41804. break;
  41805. case EVP_PKEY_DSA:
  41806. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41807. AssertNotNull(dsa = DSA_new());
  41808. AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  41809. NULL, 0, NULL, NULL, NULL), 1);
  41810. AssertIntEQ(DSA_generate_key(dsa), 1);
  41811. AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  41812. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41813. break;
  41814. case EVP_PKEY_EC:
  41815. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41816. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41817. {
  41818. WOLFSSL_EC_KEY* ecKey = NULL;
  41819. AssertNotNull(ecKey = EC_KEY_new());
  41820. AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
  41821. AssertIntEQ(
  41822. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41823. }
  41824. #endif
  41825. #endif
  41826. break;
  41827. }
  41828. AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  41829. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41830. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41831. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41832. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41833. if (encs[i] == EVP_PKEY_RSA)
  41834. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41835. WOLFSSL_SUCCESS);
  41836. #endif
  41837. #endif
  41838. /* Check returning only length */
  41839. AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  41840. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41841. AssertIntGT(siglenOnlyLen, 0);
  41842. /* Sign data */
  41843. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  41844. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  41845. AssertIntGE(siglenOnlyLen, siglen);
  41846. /* Verify signature */
  41847. AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  41848. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41849. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41850. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41851. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41852. if (encs[i] == EVP_PKEY_RSA)
  41853. AssertIntEQ(
  41854. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  41855. WOLFSSL_SUCCESS);
  41856. #endif
  41857. #endif
  41858. AssertIntEQ(EVP_PKEY_verify(
  41859. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  41860. WOLFSSL_SUCCESS);
  41861. AssertIntEQ(EVP_PKEY_verify(
  41862. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  41863. WOLFSSL_FAILURE);
  41864. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  41865. !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
  41866. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41867. if (encs[i] == EVP_PKEY_RSA) {
  41868. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  41869. /* Try RSA sign/verify with no padding. */
  41870. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41871. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  41872. WOLFSSL_SUCCESS);
  41873. AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41874. siglen), WOLFSSL_SUCCESS);
  41875. AssertIntGE(siglenOnlyLen, siglen);
  41876. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41877. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41878. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  41879. AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41880. siglen), WOLFSSL_SUCCESS);
  41881. #endif
  41882. /* Wrong padding schemes. */
  41883. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41884. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  41885. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41886. AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  41887. siglen), WOLFSSL_SUCCESS);
  41888. AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  41889. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41890. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  41891. AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  41892. siglen), WOLFSSL_SUCCESS);
  41893. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  41894. WOLFSSL_SUCCESS);
  41895. AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  41896. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  41897. }
  41898. #endif
  41899. #endif
  41900. /* error cases */
  41901. siglen = keySz; /* Reset because sig size may vary slightly */
  41902. AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  41903. AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  41904. AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  41905. WOLFSSL_SUCCESS);
  41906. AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  41907. WOLFSSL_SUCCESS);
  41908. EVP_PKEY_free(pkey);
  41909. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  41910. DSA_free(dsa);
  41911. dsa = NULL;
  41912. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  41913. EVP_PKEY_CTX_free(ctx_verify);
  41914. EVP_PKEY_CTX_free(ctx);
  41915. }
  41916. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41917. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  41918. printf(resultFmt, passed);
  41919. #endif /* OPENSSL_EXTRA */
  41920. return 0;
  41921. }
  41922. static int test_EVP_PKEY_rsa(void)
  41923. {
  41924. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  41925. WOLFSSL_RSA* rsa;
  41926. WOLFSSL_EVP_PKEY* pkey;
  41927. AssertNotNull(rsa = wolfSSL_RSA_new());
  41928. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41929. AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  41930. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  41931. AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  41932. AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  41933. wolfSSL_EVP_PKEY_free(pkey);
  41934. printf(resultFmt, passed);
  41935. #endif
  41936. return 0;
  41937. }
  41938. static int test_EVP_PKEY_ec(void)
  41939. {
  41940. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  41941. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  41942. WOLFSSL_EC_KEY* ecKey;
  41943. WOLFSSL_EVP_PKEY* pkey;
  41944. AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
  41945. AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
  41946. AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  41947. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  41948. /* Should fail since ecKey is empty */
  41949. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  41950. AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  41951. AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  41952. wolfSSL_EVP_PKEY_free(pkey);
  41953. printf(resultFmt, passed);
  41954. #endif
  41955. #endif
  41956. return 0;
  41957. }
  41958. static int test_EVP_PKEY_cmp(void)
  41959. {
  41960. #if defined(OPENSSL_EXTRA)
  41961. EVP_PKEY *a, *b;
  41962. const unsigned char *in;
  41963. printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
  41964. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  41965. in = client_key_der_2048;
  41966. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41967. &in, (long)sizeof_client_key_der_2048));
  41968. in = client_key_der_2048;
  41969. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  41970. &in, (long)sizeof_client_key_der_2048));
  41971. /* Test success case RSA */
  41972. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41973. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  41974. #else
  41975. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41976. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41977. EVP_PKEY_free(b);
  41978. EVP_PKEY_free(a);
  41979. #endif
  41980. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41981. in = ecc_clikey_der_256;
  41982. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41983. &in, (long)sizeof_ecc_clikey_der_256));
  41984. in = ecc_clikey_der_256;
  41985. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  41986. &in, (long)sizeof_ecc_clikey_der_256));
  41987. /* Test success case ECC */
  41988. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41989. AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
  41990. #else
  41991. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  41992. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  41993. EVP_PKEY_free(b);
  41994. EVP_PKEY_free(a);
  41995. #endif
  41996. /* Test failure cases */
  41997. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  41998. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  41999. in = client_key_der_2048;
  42000. AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  42001. &in, (long)sizeof_client_key_der_2048));
  42002. in = ecc_clikey_der_256;
  42003. AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  42004. &in, (long)sizeof_ecc_clikey_der_256));
  42005. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42006. AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
  42007. #else
  42008. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42009. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  42010. EVP_PKEY_free(b);
  42011. EVP_PKEY_free(a);
  42012. #endif
  42013. /* invalid or empty failure cases */
  42014. a = EVP_PKEY_new();
  42015. b = EVP_PKEY_new();
  42016. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  42017. AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  42018. AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  42019. AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  42020. #ifdef NO_RSA
  42021. /* Type check will fail since RSA is the default EVP key type */
  42022. AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
  42023. #else
  42024. AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
  42025. #endif
  42026. #else
  42027. AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  42028. AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
  42029. AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
  42030. AssertIntNE(EVP_PKEY_cmp(a, b), 0);
  42031. #endif
  42032. EVP_PKEY_free(b);
  42033. EVP_PKEY_free(a);
  42034. (void)in;
  42035. printf(resultFmt, passed);
  42036. #endif
  42037. return 0;
  42038. }
  42039. static int test_ERR_load_crypto_strings(void)
  42040. {
  42041. #if defined(OPENSSL_ALL)
  42042. ERR_load_crypto_strings();
  42043. printf(resultFmt, passed);
  42044. #endif
  42045. return 0;
  42046. }
  42047. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42048. static void free_x509(X509* x)
  42049. {
  42050. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  42051. }
  42052. #endif
  42053. static int test_sk_X509(void)
  42054. {
  42055. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  42056. {
  42057. STACK_OF(X509)* s;
  42058. AssertNotNull(s = sk_X509_new_null());
  42059. AssertIntEQ(sk_X509_num(s), 0);
  42060. sk_X509_pop_free(s, NULL);
  42061. AssertNotNull(s = sk_X509_new_null());
  42062. AssertIntEQ(sk_X509_num(s), 0);
  42063. sk_X509_pop_free(s, NULL);
  42064. AssertNotNull(s = sk_X509_new_null());
  42065. sk_X509_push(s, (X509*)1);
  42066. AssertIntEQ(sk_X509_num(s), 1);
  42067. AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  42068. sk_X509_push(s, (X509*)2);
  42069. AssertIntEQ(sk_X509_num(s), 2);
  42070. AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  42071. AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  42072. sk_X509_push(s, (X509*)2);
  42073. sk_X509_pop_free(s, free_x509);
  42074. }
  42075. {
  42076. /* Push a list of 10 X509s onto stack, then verify that
  42077. * value(), push(), shift(), and pop() behave as expected. */
  42078. STACK_OF(X509)* s;
  42079. X509* xList[10];
  42080. int i = 0;
  42081. const int len = (sizeof(xList) / sizeof(xList[0]));
  42082. for (i = 0; i < len; ++i)
  42083. AssertNotNull(xList[i] = X509_new());
  42084. /* test push, pop, and free */
  42085. AssertNotNull(s = sk_X509_new_null());
  42086. for (i = 0; i < len; ++i) {
  42087. sk_X509_push(s, xList[i]);
  42088. AssertIntEQ(sk_X509_num(s), i + 1);
  42089. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42090. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42091. }
  42092. /* pop returns and removes last pushed on stack, which is index 0
  42093. * in sk_x509_value */
  42094. for (i = 0; i < len; ++i) {
  42095. X509 * x = sk_X509_value(s, 0);
  42096. X509 * y = sk_X509_pop(s);
  42097. X509 * z = xList[len - 1 - i];
  42098. AssertIntEQ((x == y), 1);
  42099. AssertIntEQ((x == z), 1);
  42100. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42101. }
  42102. sk_free(s);
  42103. /* test push, shift, and free */
  42104. AssertNotNull(s = sk_X509_new_null());
  42105. for (i = 0; i < len; ++i) {
  42106. sk_X509_push(s, xList[i]);
  42107. AssertIntEQ(sk_X509_num(s), i + 1);
  42108. AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  42109. AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  42110. }
  42111. /* shift returns and removes first pushed on stack, which is index i
  42112. * in sk_x509_value() */
  42113. for (i = 0; i < len; ++i) {
  42114. X509 * x = sk_X509_value(s, len - 1 - i);
  42115. X509 * y = sk_X509_shift(s);
  42116. X509 * z = xList[i];
  42117. AssertIntEQ((x == y), 1);
  42118. AssertIntEQ((x == z), 1);
  42119. AssertIntEQ(sk_X509_num(s), len - 1 - i);
  42120. }
  42121. sk_free(s);
  42122. for (i = 0; i < len; ++i)
  42123. X509_free(xList[i]);
  42124. }
  42125. printf(resultFmt, passed);
  42126. #endif
  42127. return 0;
  42128. }
  42129. static int test_sk_X509_CRL(void)
  42130. {
  42131. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  42132. X509_CRL* crl;
  42133. XFILE fp;
  42134. STACK_OF(X509_CRL)* s;
  42135. printf(testingFmt, "test_sk_X509_CRL");
  42136. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  42137. AssertTrue((fp != XBADFILE));
  42138. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  42139. XFCLOSE(fp);
  42140. AssertNotNull(s = sk_X509_CRL_new());
  42141. AssertIntEQ(sk_X509_CRL_num(s), 0);
  42142. AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
  42143. AssertIntEQ(sk_X509_CRL_num(s), 1);
  42144. AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
  42145. sk_X509_CRL_free(s);
  42146. printf(resultFmt, passed);
  42147. #endif
  42148. return 0;
  42149. }
  42150. static int test_X509_get_signature_nid(void)
  42151. {
  42152. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42153. X509* x509;
  42154. AssertIntEQ(X509_get_signature_nid(NULL), 0);
  42155. AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42156. SSL_FILETYPE_PEM));
  42157. AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  42158. X509_free(x509);
  42159. printf(resultFmt, passed);
  42160. #endif
  42161. return 0;
  42162. }
  42163. static int test_X509_REQ(void)
  42164. {
  42165. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  42166. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  42167. X509_NAME* name;
  42168. #ifndef NO_RSA
  42169. X509_NAME* subject;
  42170. #endif
  42171. #if !defined(NO_RSA) || defined(HAVE_ECC)
  42172. X509_REQ* req;
  42173. EVP_PKEY* priv;
  42174. EVP_PKEY* pub;
  42175. unsigned char* der = NULL;
  42176. int len;
  42177. #endif
  42178. #ifndef NO_RSA
  42179. EVP_MD_CTX *mctx = NULL;
  42180. EVP_PKEY_CTX *pkctx = NULL;
  42181. #ifdef USE_CERT_BUFFERS_1024
  42182. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  42183. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  42184. #elif defined(USE_CERT_BUFFERS_2048)
  42185. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  42186. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  42187. #endif
  42188. #endif
  42189. #ifdef HAVE_ECC
  42190. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  42191. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  42192. #endif
  42193. AssertNotNull(name = X509_NAME_new());
  42194. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  42195. (byte*)"wolfssl.com", 11, 0, 1),
  42196. WOLFSSL_SUCCESS);
  42197. AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  42198. (byte*)"support@wolfssl.com", 19, -1,
  42199. 1), WOLFSSL_SUCCESS);
  42200. #ifndef NO_RSA
  42201. AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  42202. (long)sizeof_client_key_der_2048));
  42203. AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  42204. (long)sizeof_client_keypub_der_2048));
  42205. AssertNotNull(req = X509_REQ_new());
  42206. AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  42207. AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  42208. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42209. AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  42210. AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  42211. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42212. AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  42213. AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  42214. AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  42215. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42216. len = i2d_X509_REQ(req, &der);
  42217. DEBUG_WRITE_DER(der, len, "req.der");
  42218. #ifdef USE_CERT_BUFFERS_1024
  42219. AssertIntEQ(len, 381);
  42220. #else
  42221. AssertIntEQ(len, 643);
  42222. #endif
  42223. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42224. der = NULL;
  42225. mctx = EVP_MD_CTX_new();
  42226. AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
  42227. AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  42228. EVP_MD_CTX_free(mctx);
  42229. X509_REQ_free(NULL);
  42230. X509_REQ_free(req);
  42231. /* Test getting the subject from a newly created X509_REQ */
  42232. AssertNotNull(req = X509_REQ_new());
  42233. AssertNotNull(subject = X509_REQ_get_subject_name(req));
  42234. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  42235. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  42236. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  42237. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  42238. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  42239. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  42240. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  42241. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  42242. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  42243. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  42244. AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  42245. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  42246. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42247. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42248. len = i2d_X509_REQ(req, &der);
  42249. DEBUG_WRITE_DER(der, len, "req2.der");
  42250. #ifdef USE_CERT_BUFFERS_1024
  42251. AssertIntEQ(len, 435);
  42252. #else
  42253. AssertIntEQ(len, 696);
  42254. #endif
  42255. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42256. der = NULL;
  42257. EVP_PKEY_free(pub);
  42258. EVP_PKEY_free(priv);
  42259. X509_REQ_free(req);
  42260. #endif
  42261. #ifdef HAVE_ECC
  42262. AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  42263. sizeof_ecc_clikey_der_256));
  42264. AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  42265. sizeof_ecc_clikeypub_der_256));
  42266. AssertNotNull(req = X509_REQ_new());
  42267. AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  42268. AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  42269. AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  42270. /* Signature is random and may be shorter or longer. */
  42271. AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
  42272. AssertIntLE(len, 253);
  42273. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  42274. X509_REQ_free(req);
  42275. EVP_PKEY_free(pub);
  42276. EVP_PKEY_free(priv);
  42277. #ifdef FP_ECC
  42278. wc_ecc_fp_free();
  42279. #endif
  42280. #endif /* HAVE_ECC */
  42281. X509_NAME_free(name);
  42282. printf(resultFmt, passed);
  42283. #endif
  42284. return 0;
  42285. }
  42286. static int test_wolfssl_PKCS7(void)
  42287. {
  42288. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
  42289. PKCS7* pkcs7;
  42290. byte data[FOURK_BUF];
  42291. word32 len = sizeof(data);
  42292. const byte* p = data;
  42293. byte content[] = "Test data to encode.";
  42294. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42295. BIO* bio;
  42296. byte key[sizeof(client_key_der_2048)];
  42297. word32 keySz = (word32)sizeof(key);
  42298. byte* out = NULL;
  42299. #endif
  42300. AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
  42301. (word32)sizeof(content),
  42302. 0, 0, 0, RSA_TYPE)), 0);
  42303. AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  42304. AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  42305. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42306. AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  42307. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  42308. PKCS7_free(pkcs7);
  42309. /* fail case, without PKCS7_NOVERIFY */
  42310. p = data;
  42311. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42312. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42313. 0), WOLFSSL_FAILURE);
  42314. PKCS7_free(pkcs7);
  42315. /* success case, with PKCS7_NOVERIFY */
  42316. p = data;
  42317. AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  42318. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  42319. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  42320. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  42321. /* test i2d */
  42322. XMEMCPY(key, client_key_der_2048, keySz);
  42323. pkcs7->privateKey = key;
  42324. pkcs7->privateKeySz = (word32)sizeof(key);
  42325. pkcs7->encryptOID = RSAk;
  42326. pkcs7->hashOID = SHAh;
  42327. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42328. AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  42329. AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  42330. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42331. BIO_free(bio);
  42332. #endif
  42333. PKCS7_free(NULL);
  42334. PKCS7_free(pkcs7);
  42335. printf(resultFmt, passed);
  42336. #endif
  42337. return 0;
  42338. }
  42339. static int test_wolfSSL_PKCS7_sign(void)
  42340. {
  42341. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  42342. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42343. PKCS7* p7 = NULL;
  42344. PKCS7* p7Ver = NULL;
  42345. byte* out = NULL;
  42346. byte* tmpPtr = NULL;
  42347. int outLen = 0;
  42348. int flags = 0;
  42349. byte data[] = "Test data to encode.";
  42350. const char* cert = "./certs/server-cert.pem";
  42351. const char* key = "./certs/server-key.pem";
  42352. const char* ca = "./certs/ca-cert.pem";
  42353. WOLFSSL_BIO* certBio = NULL;
  42354. WOLFSSL_BIO* keyBio = NULL;
  42355. WOLFSSL_BIO* caBio = NULL;
  42356. WOLFSSL_BIO* inBio = NULL;
  42357. X509* signCert = NULL;
  42358. EVP_PKEY* signKey = NULL;
  42359. X509* caCert = NULL;
  42360. X509_STORE* store = NULL;
  42361. printf(testingFmt, "wolfSSL_PKCS7_sign()");
  42362. /* read signer cert/key into BIO */
  42363. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42364. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42365. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42366. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42367. /* read CA cert into store (for verify) */
  42368. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42369. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42370. AssertNotNull(store = X509_STORE_new());
  42371. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42372. /* data to be signed into BIO */
  42373. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42374. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42375. /* PKCS7_sign, bad args: signer NULL */
  42376. AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  42377. /* PKCS7_sign, bad args: signer key NULL */
  42378. AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  42379. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  42380. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  42381. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  42382. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  42383. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  42384. AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  42385. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  42386. {
  42387. flags = PKCS7_BINARY;
  42388. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42389. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42390. /* verify with d2i_PKCS7 */
  42391. tmpPtr = out;
  42392. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42393. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42394. PKCS7_free(p7Ver);
  42395. /* verify with wc_PKCS7_VerifySignedData */
  42396. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42397. AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  42398. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42399. /* compare the signer found to expected signer */
  42400. AssertIntNE(p7Ver->verifyCertSz, 0);
  42401. tmpPtr = NULL;
  42402. AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  42403. AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  42404. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42405. tmpPtr = NULL;
  42406. wc_PKCS7_Free(p7Ver);
  42407. AssertNotNull(out);
  42408. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42409. out = NULL;
  42410. PKCS7_free(p7);
  42411. }
  42412. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  42413. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  42414. {
  42415. /* re-populate input BIO, may have been consumed */
  42416. BIO_free(inBio);
  42417. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42418. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42419. flags = PKCS7_BINARY | PKCS7_STREAM;
  42420. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42421. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42422. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42423. /* PKCS7_final, bad args: PKCS7 null */
  42424. AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  42425. /* PKCS7_final, bad args: PKCS7 null */
  42426. AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
  42427. tmpPtr = out;
  42428. AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42429. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42430. PKCS7_free(p7Ver);
  42431. AssertNotNull(out);
  42432. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42433. out = NULL;
  42434. PKCS7_free(p7);
  42435. }
  42436. /* TEST SUCCESS: Detached, not streaming, not MIME */
  42437. {
  42438. /* re-populate input BIO, may have been consumed */
  42439. BIO_free(inBio);
  42440. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42441. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42442. flags = PKCS7_BINARY | PKCS7_DETACHED;
  42443. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42444. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42445. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42446. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42447. p7Ver->content = data;
  42448. p7Ver->contentSz = sizeof(data);
  42449. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42450. wc_PKCS7_Free(p7Ver);
  42451. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  42452. * yet support detached content */
  42453. tmpPtr = out;
  42454. AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  42455. PKCS7_free(p7Ver);
  42456. AssertNotNull(out);
  42457. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42458. out = NULL;
  42459. PKCS7_free(p7);
  42460. }
  42461. /* TEST SUCCESS: Detached, streaming, not MIME */
  42462. {
  42463. /* re-populate input BIO, may have been consumed */
  42464. BIO_free(inBio);
  42465. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42466. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42467. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  42468. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42469. AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
  42470. AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  42471. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  42472. AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  42473. p7Ver->content = data;
  42474. p7Ver->contentSz = sizeof(data);
  42475. AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  42476. wc_PKCS7_Free(p7Ver);
  42477. AssertNotNull(out);
  42478. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  42479. PKCS7_free(p7);
  42480. }
  42481. X509_STORE_free(store);
  42482. X509_free(caCert);
  42483. X509_free(signCert);
  42484. EVP_PKEY_free(signKey);
  42485. BIO_free(inBio);
  42486. BIO_free(keyBio);
  42487. BIO_free(certBio);
  42488. BIO_free(caBio);
  42489. printf(resultFmt, passed);
  42490. #endif
  42491. return 0;
  42492. }
  42493. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  42494. {
  42495. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  42496. PKCS7_SIGNED* pkcs7;
  42497. printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
  42498. pkcs7 = PKCS7_SIGNED_new();
  42499. AssertNotNull(pkcs7);
  42500. AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
  42501. PKCS7_SIGNED_free(pkcs7);
  42502. printf(resultFmt, passed);
  42503. #endif
  42504. return 0;
  42505. }
  42506. #ifndef NO_BIO
  42507. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  42508. {
  42509. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  42510. PKCS7* pkcs7 = NULL;
  42511. BIO* bio = NULL;
  42512. const byte* cert_buf = NULL;
  42513. int ret = 0;
  42514. WC_RNG rng;
  42515. const byte data[] = { /* Hello World */
  42516. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  42517. 0x72,0x6c,0x64
  42518. };
  42519. #ifndef NO_RSA
  42520. #if defined(USE_CERT_BUFFERS_2048)
  42521. byte key[sizeof(client_key_der_2048)];
  42522. byte cert[sizeof(client_cert_der_2048)];
  42523. word32 keySz = (word32)sizeof(key);
  42524. word32 certSz = (word32)sizeof(cert);
  42525. XMEMSET(key, 0, keySz);
  42526. XMEMSET(cert, 0, certSz);
  42527. XMEMCPY(key, client_key_der_2048, keySz);
  42528. XMEMCPY(cert, client_cert_der_2048, certSz);
  42529. #elif defined(USE_CERT_BUFFERS_1024)
  42530. byte key[sizeof_client_key_der_1024];
  42531. byte cert[sizeof(sizeof_client_cert_der_1024)];
  42532. word32 keySz = (word32)sizeof(key);
  42533. word32 certSz = (word32)sizeof(cert);
  42534. XMEMSET(key, 0, keySz);
  42535. XMEMSET(cert, 0, certSz);
  42536. XMEMCPY(key, client_key_der_1024, keySz);
  42537. XMEMCPY(cert, client_cert_der_1024, certSz);
  42538. #else
  42539. unsigned char cert[ONEK_BUF];
  42540. unsigned char key[ONEK_BUF];
  42541. XFILE fp;
  42542. int certSz;
  42543. int keySz;
  42544. fp = XFOPEN("./certs/1024/client-cert.der", "rb");
  42545. AssertTrue((fp != XBADFILE));
  42546. certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
  42547. XFCLOSE(fp);
  42548. fp = XFOPEN("./certs/1024/client-key.der", "rb");
  42549. AssertTrue(fp != XBADFILE);
  42550. keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
  42551. XFCLOSE(fp);
  42552. #endif
  42553. #elif defined(HAVE_ECC)
  42554. #if defined(USE_CERT_BUFFERS_256)
  42555. unsigned char cert[sizeof(cliecc_cert_der_256)];
  42556. unsigned char key[sizeof(ecc_clikey_der_256)];
  42557. int certSz = (int)sizeof(cert);
  42558. int keySz = (int)sizeof(key);
  42559. XMEMSET(cert, 0, certSz);
  42560. XMEMSET(key, 0, keySz);
  42561. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  42562. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  42563. #else
  42564. unsigned char cert[ONEK_BUF];
  42565. unsigned char key[ONEK_BUF];
  42566. XFILE fp;
  42567. int certSz, keySz;
  42568. fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
  42569. AssertTrue(fp != XBADFILE);
  42570. certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
  42571. XFCLOSE(fp);
  42572. fp = XFOPEN("./certs/client-ecc-key.der", "rb");
  42573. AssertTrue(fp != XBADFILE);
  42574. keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
  42575. XFCLOSE(fp);
  42576. #endif
  42577. #else
  42578. #error PKCS7 requires ECC or RSA
  42579. #endif
  42580. printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
  42581. AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  42582. /* initialize with DER encoded cert */
  42583. AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  42584. /* init rng */
  42585. AssertIntEQ(wc_InitRng(&rng), 0);
  42586. pkcs7->rng = &rng;
  42587. pkcs7->content = (byte*)data; /* not used for ex */
  42588. pkcs7->contentSz = (word32)sizeof(data);
  42589. pkcs7->contentOID = SIGNED_DATA;
  42590. pkcs7->privateKey = key;
  42591. pkcs7->privateKeySz = (word32)sizeof(key);
  42592. pkcs7->encryptOID = RSAk;
  42593. pkcs7->hashOID = SHAh;
  42594. pkcs7->signedAttribs = NULL;
  42595. pkcs7->signedAttribsSz = 0;
  42596. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  42597. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  42598. AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  42599. /* Read PKCS#7 PEM from BIO */
  42600. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  42601. AssertIntGE(ret, 0);
  42602. BIO_free(bio);
  42603. wc_PKCS7_Free(pkcs7);
  42604. wc_FreeRng(&rng);
  42605. printf(resultFmt, passed);
  42606. #endif
  42607. return 0;
  42608. }
  42609. #ifdef HAVE_SMIME
  42610. static int test_wolfSSL_SMIME_read_PKCS7(void)
  42611. {
  42612. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  42613. !defined(NO_RSA)
  42614. PKCS7* pkcs7 = NULL;
  42615. BIO* bio = NULL;
  42616. BIO* bcont = NULL;
  42617. BIO* out = NULL;
  42618. const byte* outBuf = NULL;
  42619. int outBufLen = 0;
  42620. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  42621. XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
  42622. printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
  42623. /* smime-test.p7s */
  42624. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  42625. AssertNotNull(bio);
  42626. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42627. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42628. AssertNotNull(pkcs7);
  42629. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42630. PKCS7_NOVERIFY), SSL_SUCCESS);
  42631. XFCLOSE(smimeTestFile);
  42632. if (bcont) BIO_free(bcont);
  42633. wolfSSL_PKCS7_free(pkcs7);
  42634. /* smime-test-multipart.p7s */
  42635. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  42636. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42637. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42638. AssertNotNull(pkcs7);
  42639. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42640. PKCS7_NOVERIFY), SSL_SUCCESS);
  42641. XFCLOSE(smimeTestFile);
  42642. if (bcont) BIO_free(bcont);
  42643. wolfSSL_PKCS7_free(pkcs7);
  42644. /* smime-test-multipart-badsig.p7s */
  42645. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  42646. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42647. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42648. AssertNull(pkcs7);
  42649. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42650. PKCS7_NOVERIFY), SSL_FAILURE);
  42651. XFCLOSE(smimeTestFile);
  42652. if (bcont) BIO_free(bcont);
  42653. wolfSSL_PKCS7_free(pkcs7);
  42654. /* smime-test-canon.p7s */
  42655. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42656. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42657. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42658. AssertNotNull(pkcs7);
  42659. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  42660. PKCS7_NOVERIFY), SSL_SUCCESS);
  42661. XFCLOSE(smimeTestFile);
  42662. if (bcont) BIO_free(bcont);
  42663. wolfSSL_PKCS7_free(pkcs7);
  42664. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  42665. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  42666. AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  42667. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  42668. AssertNotNull(pkcs7);
  42669. out = wolfSSL_BIO_new(BIO_s_mem());
  42670. AssertNotNull(out);
  42671. AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  42672. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  42673. AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  42674. /* Content-Type should not show up at beginning of output buffer */
  42675. AssertIntGT(outBufLen, XSTRLEN(contTypeText));
  42676. AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  42677. BIO_free(out);
  42678. BIO_free(bio);
  42679. if (bcont) BIO_free(bcont);
  42680. wolfSSL_PKCS7_free(pkcs7);
  42681. printf(resultFmt, passed);
  42682. #endif
  42683. return 0;
  42684. }
  42685. static int test_wolfSSL_SMIME_write_PKCS7(void)
  42686. {
  42687. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  42688. PKCS7* p7 = NULL;
  42689. PKCS7* p7Ver = NULL;
  42690. int flags = 0;
  42691. byte data[] = "Test data to encode.";
  42692. const char* cert = "./certs/server-cert.pem";
  42693. const char* key = "./certs/server-key.pem";
  42694. const char* ca = "./certs/ca-cert.pem";
  42695. WOLFSSL_BIO* certBio = NULL;
  42696. WOLFSSL_BIO* keyBio = NULL;
  42697. WOLFSSL_BIO* caBio = NULL;
  42698. WOLFSSL_BIO* inBio = NULL;
  42699. WOLFSSL_BIO* outBio = NULL;
  42700. WOLFSSL_BIO* content = NULL;
  42701. X509* signCert = NULL;
  42702. EVP_PKEY* signKey = NULL;
  42703. X509* caCert = NULL;
  42704. X509_STORE* store = NULL;
  42705. printf(testingFmt, "wolfSSL_SMIME_write_PKCS7()");
  42706. /* read signer cert/key into BIO */
  42707. AssertNotNull(certBio = BIO_new_file(cert, "r"));
  42708. AssertNotNull(keyBio = BIO_new_file(key, "r"));
  42709. AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  42710. AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  42711. /* read CA cert into store (for verify) */
  42712. AssertNotNull(caBio = BIO_new_file(ca, "r"));
  42713. AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  42714. AssertNotNull(store = X509_STORE_new());
  42715. AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
  42716. /* generate and verify SMIME: not detached */
  42717. {
  42718. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42719. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42720. flags = PKCS7_STREAM;
  42721. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42722. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42723. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42724. /* bad arg: out NULL */
  42725. AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  42726. /* bad arg: pkcs7 NULL */
  42727. AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  42728. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42729. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42730. BIO_free(content);
  42731. BIO_free(inBio);
  42732. BIO_free(outBio);
  42733. PKCS7_free(p7Ver);
  42734. PKCS7_free(p7);
  42735. }
  42736. /* generate and verify SMIME: not detached, add Content-Type */
  42737. {
  42738. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42739. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42740. flags = PKCS7_STREAM | PKCS7_TEXT;
  42741. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42742. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42743. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42744. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42745. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  42746. BIO_free(content);
  42747. BIO_free(inBio);
  42748. BIO_free(outBio);
  42749. PKCS7_free(p7Ver);
  42750. PKCS7_free(p7);
  42751. }
  42752. /* generate and verify SMIME: detached */
  42753. {
  42754. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42755. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42756. flags = PKCS7_DETACHED | PKCS7_STREAM;
  42757. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42758. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42759. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42760. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42761. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42762. BIO_free(content);
  42763. BIO_free(inBio);
  42764. BIO_free(outBio);
  42765. PKCS7_free(p7Ver);
  42766. PKCS7_free(p7);
  42767. }
  42768. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  42769. {
  42770. AssertNotNull(inBio = BIO_new(BIO_s_mem()));
  42771. AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  42772. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  42773. AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  42774. AssertNotNull(outBio = BIO_new(BIO_s_mem()));
  42775. AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  42776. AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  42777. AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  42778. BIO_free(content);
  42779. BIO_free(inBio);
  42780. BIO_free(outBio);
  42781. PKCS7_free(p7Ver);
  42782. PKCS7_free(p7);
  42783. }
  42784. X509_STORE_free(store);
  42785. X509_free(caCert);
  42786. X509_free(signCert);
  42787. EVP_PKEY_free(signKey);
  42788. BIO_free(keyBio);
  42789. BIO_free(certBio);
  42790. BIO_free(caBio);
  42791. printf(resultFmt, passed);
  42792. #endif
  42793. return 0;
  42794. }
  42795. #endif /* HAVE_SMIME */
  42796. #endif /* !NO_BIO */
  42797. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  42798. returns 0) */
  42799. static int test_X509_STORE_No_SSL_CTX(void)
  42800. {
  42801. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  42802. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  42803. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  42804. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  42805. X509_STORE *store;
  42806. X509_STORE_CTX *storeCtx;
  42807. X509_CRL *crl;
  42808. X509 *ca, *cert;
  42809. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  42810. const char srvCert[] = "./certs/server-cert.pem";
  42811. const char caCert[] = "./certs/ca-cert.pem";
  42812. const char caDir[] = "./certs/crl/hash_pem/";
  42813. XFILE fp;
  42814. X509_LOOKUP *lookup;
  42815. printf(testingFmt, "test_X509_STORE_No_SSL_CTX");
  42816. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42817. /* Set up store with CA */
  42818. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42819. SSL_FILETYPE_PEM)));
  42820. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42821. /* Add CRL lookup directory to store
  42822. NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  42823. of crl.pem */
  42824. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  42825. X509_LOOKUP_hash_dir())));
  42826. AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  42827. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  42828. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  42829. SSL_SUCCESS);
  42830. /* Add CRL to store NOT containing the verified certificate, which
  42831. forces use of the CRL lookup directory */
  42832. fp = XFOPEN(cliCrlPem, "rb");
  42833. AssertTrue((fp != XBADFILE));
  42834. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42835. NULL, NULL));
  42836. XFCLOSE(fp);
  42837. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  42838. /* Create verification context outside of an SSL session */
  42839. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  42840. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  42841. SSL_FILETYPE_PEM)));
  42842. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  42843. /* Perform verification, which should NOT indicate CRL missing due to the
  42844. store CM's X509 store pointer being NULL */
  42845. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  42846. X509_CRL_free(crl);
  42847. X509_STORE_free(store);
  42848. X509_STORE_CTX_free(storeCtx);
  42849. X509_free(cert);
  42850. X509_free(ca);
  42851. printf(resultFmt, passed);
  42852. #endif
  42853. return 0;
  42854. }
  42855. /* Basically the same test as test_X509_STORE_No_SSL_CTX, but with
  42856. * X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  42857. static int test_X509_LOOKUP_add_dir(void)
  42858. {
  42859. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  42860. !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
  42861. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  42862. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  42863. X509_STORE * store;
  42864. X509_STORE_CTX * storeCtx;
  42865. X509_CRL * crl;
  42866. X509 *ca, * cert;
  42867. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  42868. const char srvCert[] = "./certs/server-cert.pem";
  42869. const char caCert[] = "./certs/ca-cert.pem";
  42870. const char caDir[] = "./certs/crl/hash_der/";
  42871. XFILE fp;
  42872. X509_LOOKUP * lookup;
  42873. printf(testingFmt, "test_X509_LOOKUP_add_dir");
  42874. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42875. /* Set up store with CA */
  42876. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42877. SSL_FILETYPE_PEM)));
  42878. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42879. /* Add CRL lookup directory to store.
  42880. Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  42881. of crl.der */
  42882. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  42883. X509_LOOKUP_hash_dir())));
  42884. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  42885. SSL_SUCCESS);
  42886. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  42887. SSL_SUCCESS);
  42888. /* Add CRL to store NOT containing the verified certificate, which
  42889. forces use of the CRL lookup directory */
  42890. fp = XFOPEN(cliCrlPem, "rb");
  42891. AssertTrue((fp != XBADFILE));
  42892. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42893. NULL, NULL));
  42894. XFCLOSE(fp);
  42895. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  42896. /* Create verification context outside of an SSL session */
  42897. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  42898. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  42899. SSL_FILETYPE_PEM)));
  42900. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  42901. /* Perform verification, which should NOT return CRL missing */
  42902. AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  42903. X509_CRL_free(crl);
  42904. X509_STORE_free(store);
  42905. X509_STORE_CTX_free(storeCtx);
  42906. X509_free(cert);
  42907. X509_free(ca);
  42908. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  42909. * We should get CRL_MISSING at the end, because the lookup
  42910. * dir has only ASN1 CRLs. */
  42911. AssertNotNull(store = (X509_STORE *)X509_STORE_new());
  42912. AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  42913. SSL_FILETYPE_PEM)));
  42914. AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  42915. AssertNotNull((lookup = X509_STORE_add_lookup(store,
  42916. X509_LOOKUP_hash_dir())));
  42917. AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  42918. SSL_SUCCESS);
  42919. AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  42920. SSL_SUCCESS);
  42921. fp = XFOPEN(cliCrlPem, "rb");
  42922. AssertTrue((fp != XBADFILE));
  42923. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  42924. NULL, NULL));
  42925. XFCLOSE(fp);
  42926. AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  42927. AssertNotNull((storeCtx = X509_STORE_CTX_new()));
  42928. AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  42929. SSL_FILETYPE_PEM)));
  42930. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  42931. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  42932. in dir containing only ASN1/DER. */
  42933. AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
  42934. X509_CRL_free(crl);
  42935. X509_STORE_free(store);
  42936. X509_STORE_CTX_free(storeCtx);
  42937. X509_free(cert);
  42938. X509_free(ca);
  42939. printf(resultFmt, passed);
  42940. #endif
  42941. return 0;
  42942. }
  42943. /*----------------------------------------------------------------------------*
  42944. | Certificate Failure Checks
  42945. *----------------------------------------------------------------------------*/
  42946. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  42947. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  42948. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  42949. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  42950. int type)
  42951. {
  42952. int ret;
  42953. WOLFSSL_CERT_MANAGER* cm = NULL;
  42954. switch (type) {
  42955. case TESTING_RSA:
  42956. #ifdef NO_RSA
  42957. printf("RSA disabled, skipping test\n");
  42958. return ASN_SIG_CONFIRM_E;
  42959. #else
  42960. break;
  42961. #endif
  42962. case TESTING_ECC:
  42963. #ifndef HAVE_ECC
  42964. printf("ECC disabled, skipping test\n");
  42965. return ASN_SIG_CONFIRM_E;
  42966. #else
  42967. break;
  42968. #endif
  42969. default:
  42970. printf("Bad function argument\n");
  42971. return BAD_FUNC_ARG;
  42972. }
  42973. cm = wolfSSL_CertManagerNew();
  42974. if (cm == NULL) {
  42975. printf("wolfSSL_CertManagerNew failed\n");
  42976. return -1;
  42977. }
  42978. #ifndef NO_FILESYSTEM
  42979. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  42980. if (ret != WOLFSSL_SUCCESS) {
  42981. printf("wolfSSL_CertManagerLoadCA failed\n");
  42982. wolfSSL_CertManagerFree(cm);
  42983. return ret;
  42984. }
  42985. #else
  42986. (void)ca;
  42987. #endif
  42988. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
  42989. /* Let AssertIntEQ handle return code */
  42990. wolfSSL_CertManagerFree(cm);
  42991. return ret;
  42992. }
  42993. #if !defined(NO_FILESYSTEM)
  42994. static int test_RsaSigFailure_cm(void)
  42995. {
  42996. int ret = 0;
  42997. const char* ca_cert = "./certs/ca-cert.pem";
  42998. const char* server_cert = "./certs/server-cert.der";
  42999. byte* cert_buf = NULL;
  43000. size_t cert_sz = 0;
  43001. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43002. if (ret == 0) {
  43003. /* corrupt DER - invert last byte, which is signature */
  43004. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43005. /* test bad cert */
  43006. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
  43007. }
  43008. printf("Signature failure test: RSA: Ret %d\n", ret);
  43009. if (cert_buf)
  43010. free(cert_buf);
  43011. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43012. if (ret == WOLFSSL_FATAL_ERROR) {
  43013. ret = 0;
  43014. }
  43015. #else
  43016. if (ret == ASN_SIG_CONFIRM_E) {
  43017. ret = 0;
  43018. }
  43019. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43020. return ret;
  43021. }
  43022. static int test_EccSigFailure_cm(void)
  43023. {
  43024. int ret = 0;
  43025. /* self-signed ECC cert, so use server cert as CA */
  43026. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  43027. const char* server_cert = "./certs/server-ecc.der";
  43028. byte* cert_buf = NULL;
  43029. size_t cert_sz = 0;
  43030. ret = load_file(server_cert, &cert_buf, &cert_sz);
  43031. if (ret == 0) {
  43032. /* corrupt DER - invert last byte, which is signature */
  43033. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  43034. /* test bad cert */
  43035. ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
  43036. }
  43037. printf("Signature failure test: ECC: Ret %d\n", ret);
  43038. if (cert_buf)
  43039. free(cert_buf);
  43040. #ifdef FP_ECC
  43041. wc_ecc_fp_free();
  43042. #endif
  43043. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  43044. if (ret == WOLFSSL_FATAL_ERROR) {
  43045. ret = 0;
  43046. }
  43047. #else
  43048. if (ret == ASN_SIG_CONFIRM_E) {
  43049. ret = 0;
  43050. }
  43051. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  43052. return ret;
  43053. }
  43054. #endif /* !NO_FILESYSTEM */
  43055. #endif /* NO_CERTS */
  43056. #ifdef WOLFSSL_TLS13
  43057. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  43058. #ifdef WC_SHA384_DIGEST_SIZE
  43059. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  43060. #else
  43061. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  43062. #endif
  43063. #endif
  43064. #ifdef WOLFSSL_EARLY_DATA
  43065. static const char earlyData[] = "Early Data";
  43066. static char earlyDataBuffer[1];
  43067. #endif
  43068. static int test_tls13_apis(void)
  43069. {
  43070. int ret = 0;
  43071. #ifndef WOLFSSL_NO_TLS12
  43072. #ifndef NO_WOLFSSL_CLIENT
  43073. WOLFSSL_CTX* clientTls12Ctx;
  43074. WOLFSSL* clientTls12Ssl;
  43075. #endif
  43076. #ifndef NO_WOLFSSL_SERVER
  43077. WOLFSSL_CTX* serverTls12Ctx;
  43078. WOLFSSL* serverTls12Ssl;
  43079. #endif
  43080. #endif
  43081. #ifndef NO_WOLFSSL_CLIENT
  43082. WOLFSSL_CTX* clientCtx;
  43083. WOLFSSL* clientSsl;
  43084. #endif
  43085. #ifndef NO_WOLFSSL_SERVER
  43086. WOLFSSL_CTX* serverCtx;
  43087. WOLFSSL* serverSsl;
  43088. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43089. const char* ourCert = svrCertFile;
  43090. const char* ourKey = svrKeyFile;
  43091. #endif
  43092. #endif
  43093. int required;
  43094. #ifdef WOLFSSL_EARLY_DATA
  43095. int outSz;
  43096. #endif
  43097. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  43098. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  43099. #ifdef HAVE_PQC
  43100. #ifndef WOLFSSL_WC_KYBER
  43101. WOLFSSL_SABER_LEVEL3
  43102. #else
  43103. WOLFSSL_KYBER_LEVEL1
  43104. #endif
  43105. #else
  43106. WOLFSSL_ECC_SECP256R1
  43107. #endif
  43108. };
  43109. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  43110. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  43111. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  43112. int numGroups = 2;
  43113. #endif
  43114. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  43115. char groupList[] =
  43116. #ifndef NO_ECC_SECP
  43117. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  43118. "P-521:"
  43119. #endif
  43120. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  43121. "P-384:"
  43122. #endif
  43123. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  43124. "P-256"
  43125. #ifdef HAVE_PQC
  43126. #ifndef WOLFSSL_WC_KYBER
  43127. ":P256_SABER_LEVEL1"
  43128. #else
  43129. ":P256_KYBER_LEVEL1"
  43130. #endif
  43131. #endif
  43132. #endif
  43133. #ifdef HAVE_PQC
  43134. ":KYBER_LEVEL1"
  43135. #endif
  43136. "";
  43137. #endif /* !defined(NO_ECC_SECP) */
  43138. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  43139. (void)ret;
  43140. #ifndef WOLFSSL_NO_TLS12
  43141. #ifndef NO_WOLFSSL_CLIENT
  43142. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  43143. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  43144. #endif
  43145. #ifndef NO_WOLFSSL_SERVER
  43146. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  43147. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43148. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  43149. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
  43150. #endif
  43151. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  43152. #endif
  43153. #endif
  43154. #ifndef NO_WOLFSSL_CLIENT
  43155. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  43156. clientSsl = wolfSSL_new(clientCtx);
  43157. #endif
  43158. #ifndef NO_WOLFSSL_SERVER
  43159. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  43160. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  43161. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  43162. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  43163. #endif
  43164. serverSsl = wolfSSL_new(serverCtx);
  43165. #endif
  43166. #ifdef WOLFSSL_SEND_HRR_COOKIE
  43167. AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  43168. #ifndef NO_WOLFSSL_CLIENT
  43169. AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  43170. #endif
  43171. #ifndef NO_WOLFSSL_SERVER
  43172. #ifndef WOLFSSL_NO_TLS12
  43173. AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
  43174. #endif
  43175. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  43176. AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  43177. WOLFSSL_SUCCESS);
  43178. #endif
  43179. #endif
  43180. #ifdef HAVE_SUPPORTED_CURVES
  43181. #ifdef HAVE_ECC
  43182. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43183. #ifndef NO_WOLFSSL_SERVER
  43184. do {
  43185. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  43186. #ifdef WOLFSSL_ASYNC_CRYPT
  43187. if (ret == WC_PENDING_E)
  43188. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  43189. #endif
  43190. } while (ret == WC_PENDING_E);
  43191. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43192. #endif
  43193. #ifndef NO_WOLFSSL_CLIENT
  43194. #ifndef WOLFSSL_NO_TLS12
  43195. do {
  43196. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  43197. #ifdef WOLFSSL_ASYNC_CRYPT
  43198. if (ret == WC_PENDING_E)
  43199. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  43200. #endif
  43201. } while (ret == WC_PENDING_E);
  43202. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43203. #endif
  43204. do {
  43205. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  43206. #ifdef WOLFSSL_ASYNC_CRYPT
  43207. if (ret == WC_PENDING_E)
  43208. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  43209. #endif
  43210. } while (ret == WC_PENDING_E);
  43211. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43212. #endif
  43213. #elif defined(HAVE_CURVE25519)
  43214. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  43215. #ifndef NO_WOLFSSL_SERVER
  43216. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  43217. WOLFSSL_SUCCESS);
  43218. #endif
  43219. #ifndef NO_WOLFSSL_CLIENT
  43220. #ifndef WOLFSSL_NO_TLS12
  43221. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  43222. WOLFSSL_SUCCESS);
  43223. #endif
  43224. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  43225. WOLFSSL_SUCCESS);
  43226. #endif
  43227. #elif defined(HAVE_CURVE448)
  43228. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  43229. #ifndef NO_WOLFSSL_SERVER
  43230. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  43231. WOLFSSL_SUCCESS);
  43232. #endif
  43233. #ifndef NO_WOLFSSL_CLIENT
  43234. #ifndef WOLFSSL_NO_TLS12
  43235. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  43236. WOLFSSL_SUCCESS);
  43237. #endif
  43238. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  43239. WOLFSSL_SUCCESS);
  43240. #endif
  43241. #else
  43242. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
  43243. #ifndef NO_WOLFSSL_CLIENT
  43244. #ifndef WOLFSSL_NO_TLS12
  43245. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  43246. NOT_COMPILED_IN);
  43247. #endif
  43248. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  43249. NOT_COMPILED_IN);
  43250. #endif
  43251. #endif
  43252. #if defined(HAVE_PQC)
  43253. AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  43254. #ifndef NO_WOLFSSL_SERVER
  43255. AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  43256. WOLFSSL_SUCCESS);
  43257. #endif
  43258. #ifndef NO_WOLFSSL_CLIENT
  43259. #ifndef WOLFSSL_NO_TLS12
  43260. AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  43261. BAD_FUNC_ARG);
  43262. #endif
  43263. AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  43264. WOLFSSL_SUCCESS);
  43265. #endif
  43266. #endif
  43267. AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  43268. #ifndef NO_WOLFSSL_SERVER
  43269. AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  43270. #endif
  43271. #ifndef NO_WOLFSSL_CLIENT
  43272. #ifndef WOLFSSL_NO_TLS12
  43273. AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  43274. #endif
  43275. AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  43276. #endif
  43277. #endif /* HAVE_SUPPORTED_CURVES */
  43278. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43279. #ifndef NO_WOLFSSL_CLIENT
  43280. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  43281. #endif
  43282. #ifndef NO_WOLFSSL_SERVER
  43283. #ifndef WOLFSSL_NO_TLS12
  43284. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  43285. #endif
  43286. AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  43287. #endif
  43288. AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  43289. #ifndef NO_WOLFSSL_CLIENT
  43290. AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  43291. #endif
  43292. #ifndef NO_WOLFSSL_SERVER
  43293. #ifndef WOLFSSL_NO_TLS12
  43294. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  43295. #endif
  43296. AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  43297. #endif
  43298. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43299. #ifndef NO_WOLFSSL_CLIENT
  43300. #ifndef WOLFSSL_NO_TLS12
  43301. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  43302. #endif
  43303. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  43304. #endif
  43305. #ifndef NO_WOLFSSL_SERVER
  43306. AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  43307. #endif
  43308. AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  43309. #ifndef NO_WOLFSSL_CLIENT
  43310. #ifndef WOLFSSL_NO_TLS12
  43311. AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  43312. #endif
  43313. AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  43314. #endif
  43315. #ifndef NO_WOLFSSL_SERVER
  43316. AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  43317. #endif
  43318. AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  43319. #ifndef NO_WOLFSSL_CLIENT
  43320. #ifndef WOLFSSL_NO_TLS12
  43321. AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  43322. #endif
  43323. AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  43324. #endif
  43325. #ifndef NO_WOLFSSL_SERVER
  43326. AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  43327. #endif
  43328. AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  43329. AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  43330. #ifndef NO_WOLFSSL_CLIENT
  43331. #ifndef WOLFSSL_NO_TLS12
  43332. AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  43333. BAD_FUNC_ARG);
  43334. #endif
  43335. AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  43336. #endif
  43337. #ifndef NO_WOLFSSL_SERVER
  43338. AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  43339. #endif
  43340. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  43341. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43342. #ifndef NO_WOLFSSL_SERVER
  43343. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  43344. #endif
  43345. #ifndef NO_WOLFSSL_CLIENT
  43346. #ifndef WOLFSSL_NO_TLS12
  43347. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  43348. BAD_FUNC_ARG);
  43349. #endif
  43350. AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  43351. #endif
  43352. AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  43353. #ifndef NO_WOLFSSL_SERVER
  43354. AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  43355. #endif
  43356. #ifndef NO_WOLFSSL_CLIENT
  43357. #ifndef WOLFSSL_NO_TLS12
  43358. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  43359. BAD_FUNC_ARG);
  43360. #endif
  43361. AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  43362. #endif
  43363. AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  43364. #ifndef NO_WOLFSSL_CLIENT
  43365. AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  43366. #endif
  43367. #ifndef NO_WOLFSSL_SERVER
  43368. #ifndef WOLFSSL_NO_TLS12
  43369. AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  43370. BAD_FUNC_ARG);
  43371. #endif
  43372. AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  43373. #endif
  43374. #endif
  43375. #ifdef HAVE_ECC
  43376. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  43377. AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  43378. #ifndef NO_WOLFSSL_SERVER
  43379. AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  43380. #endif
  43381. #ifndef NO_WOLFSSL_CLIENT
  43382. #ifndef WOLFSSL_NO_TLS12
  43383. AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  43384. #endif
  43385. AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  43386. #endif
  43387. #endif
  43388. #ifdef HAVE_SUPPORTED_CURVES
  43389. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43390. #ifndef NO_WOLFSSL_CLIENT
  43391. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  43392. #endif
  43393. AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43394. #ifndef NO_WOLFSSL_CLIENT
  43395. #ifndef WOLFSSL_NO_TLS12
  43396. AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  43397. BAD_FUNC_ARG);
  43398. #endif
  43399. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  43400. WOLFSSL_MAX_GROUP_COUNT + 1),
  43401. BAD_FUNC_ARG);
  43402. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  43403. WOLFSSL_SUCCESS);
  43404. AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  43405. BAD_FUNC_ARG);
  43406. #endif
  43407. #ifndef NO_WOLFSSL_SERVER
  43408. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  43409. WOLFSSL_SUCCESS);
  43410. AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  43411. BAD_FUNC_ARG);
  43412. #endif
  43413. AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  43414. #ifndef NO_WOLFSSL_CLIENT
  43415. AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  43416. #endif
  43417. AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  43418. #ifndef NO_WOLFSSL_CLIENT
  43419. #ifndef WOLFSSL_NO_TLS12
  43420. AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  43421. BAD_FUNC_ARG);
  43422. #endif
  43423. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
  43424. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  43425. AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  43426. WOLFSSL_SUCCESS);
  43427. AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  43428. BAD_FUNC_ARG);
  43429. #endif
  43430. #ifndef NO_WOLFSSL_SERVER
  43431. AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  43432. WOLFSSL_SUCCESS);
  43433. AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  43434. BAD_FUNC_ARG);
  43435. #endif
  43436. #ifdef OPENSSL_EXTRA
  43437. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43438. #ifndef NO_WOLFSSL_CLIENT
  43439. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
  43440. #endif
  43441. AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43442. #ifndef NO_WOLFSSL_CLIENT
  43443. #ifndef WOLFSSL_NO_TLS12
  43444. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  43445. WOLFSSL_FAILURE);
  43446. #endif
  43447. AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  43448. WOLFSSL_SUCCESS);
  43449. #endif
  43450. #ifndef NO_WOLFSSL_SERVER
  43451. AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  43452. WOLFSSL_SUCCESS);
  43453. #endif
  43454. AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  43455. #ifndef NO_WOLFSSL_CLIENT
  43456. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  43457. #endif
  43458. AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  43459. #ifndef NO_WOLFSSL_CLIENT
  43460. #ifndef WOLFSSL_NO_TLS12
  43461. AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  43462. WOLFSSL_FAILURE);
  43463. #endif
  43464. AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  43465. WOLFSSL_SUCCESS);
  43466. #endif
  43467. #ifndef NO_WOLFSSL_SERVER
  43468. AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  43469. WOLFSSL_SUCCESS);
  43470. #endif
  43471. #endif /* OPENSSL_EXTRA */
  43472. #endif /* HAVE_SUPPORTED_CURVES */
  43473. #endif /* HAVE_ECC */
  43474. #ifdef WOLFSSL_EARLY_DATA
  43475. #ifndef OPENSSL_EXTRA
  43476. AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43477. AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43478. #else
  43479. AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43480. AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  43481. #endif
  43482. #ifndef NO_WOLFSSL_CLIENT
  43483. #ifndef OPENSSL_EXTRA
  43484. AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43485. AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43486. #else
  43487. AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  43488. AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  43489. #endif
  43490. #endif
  43491. #ifndef NO_WOLFSSL_SERVER
  43492. #ifndef WOLFSSL_NO_TLS12
  43493. #ifndef OPENSSL_EXTRA
  43494. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43495. BAD_FUNC_ARG);
  43496. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43497. #else
  43498. AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  43499. BAD_FUNC_ARG);
  43500. AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  43501. #endif
  43502. #endif
  43503. #ifndef OPENSSL_EXTRA
  43504. AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  43505. AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  43506. #else
  43507. AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  43508. AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  43509. #endif
  43510. #endif
  43511. #ifndef OPENSSL_EXTRA
  43512. AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43513. AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43514. #else
  43515. AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  43516. AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  43517. #endif
  43518. #ifndef NO_WOLFSSL_CLIENT
  43519. #ifndef OPENSSL_EXTRA
  43520. AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  43521. AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  43522. #else
  43523. AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  43524. AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
  43525. #endif
  43526. #endif
  43527. #ifndef NO_WOLFSSL_SERVER
  43528. #ifndef WOLFSSL_NO_TLS12
  43529. #ifndef OPENSSL_EXTRA
  43530. AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43531. AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43532. #else
  43533. AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  43534. AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  43535. #endif
  43536. #endif
  43537. #ifndef OPENSSL_EXTRA
  43538. AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  43539. AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  43540. #else
  43541. AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  43542. AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
  43543. #endif
  43544. #endif
  43545. AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  43546. &outSz), BAD_FUNC_ARG);
  43547. #ifndef NO_WOLFSSL_CLIENT
  43548. AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  43549. &outSz), BAD_FUNC_ARG);
  43550. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  43551. BAD_FUNC_ARG);
  43552. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  43553. sizeof(earlyData), NULL),
  43554. BAD_FUNC_ARG);
  43555. #endif
  43556. #ifndef NO_WOLFSSL_SERVER
  43557. AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  43558. sizeof(earlyData), &outSz),
  43559. SIDE_ERROR);
  43560. #endif
  43561. #ifndef NO_WOLFSSL_CLIENT
  43562. #ifndef WOLFSSL_NO_TLS12
  43563. AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  43564. sizeof(earlyData), &outSz),
  43565. BAD_FUNC_ARG);
  43566. #endif
  43567. AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  43568. sizeof(earlyData), &outSz),
  43569. WOLFSSL_FATAL_ERROR);
  43570. #endif
  43571. AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  43572. sizeof(earlyDataBuffer), &outSz),
  43573. BAD_FUNC_ARG);
  43574. #ifndef NO_WOLFSSL_SERVER
  43575. AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  43576. sizeof(earlyDataBuffer), &outSz),
  43577. BAD_FUNC_ARG);
  43578. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
  43579. BAD_FUNC_ARG);
  43580. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  43581. sizeof(earlyDataBuffer), NULL),
  43582. BAD_FUNC_ARG);
  43583. #endif
  43584. #ifndef NO_WOLFSSL_CLIENT
  43585. AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  43586. sizeof(earlyDataBuffer), &outSz),
  43587. SIDE_ERROR);
  43588. #endif
  43589. #ifndef NO_WOLFSSL_SERVER
  43590. #ifndef WOLFSSL_NO_TLS12
  43591. AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  43592. sizeof(earlyDataBuffer), &outSz),
  43593. BAD_FUNC_ARG);
  43594. #endif
  43595. AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  43596. sizeof(earlyDataBuffer), &outSz),
  43597. WOLFSSL_FATAL_ERROR);
  43598. #endif
  43599. #endif
  43600. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  43601. AssertIntLT(SSL_get_early_data_status(NULL), 0);
  43602. #endif
  43603. #ifndef NO_WOLFSSL_SERVER
  43604. wolfSSL_free(serverSsl);
  43605. wolfSSL_CTX_free(serverCtx);
  43606. #endif
  43607. #ifndef NO_WOLFSSL_CLIENT
  43608. wolfSSL_free(clientSsl);
  43609. wolfSSL_CTX_free(clientCtx);
  43610. #endif
  43611. #ifndef WOLFSSL_NO_TLS12
  43612. #ifndef NO_WOLFSSL_SERVER
  43613. wolfSSL_free(serverTls12Ssl);
  43614. wolfSSL_CTX_free(serverTls12Ctx);
  43615. #endif
  43616. #ifndef NO_WOLFSSL_CLIENT
  43617. wolfSSL_free(clientTls12Ssl);
  43618. wolfSSL_CTX_free(clientTls12Ctx);
  43619. #endif
  43620. #endif
  43621. return 0;
  43622. }
  43623. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  43624. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  43625. defined(BUILD_TLS_AES_256_GCM_SHA384)
  43626. /* Called when writing. */
  43627. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  43628. {
  43629. (void)ssl;
  43630. (void)buf;
  43631. (void)sz;
  43632. (void)ctx;
  43633. /* Force error return from wolfSSL_accept_TLSv13(). */
  43634. return WANT_WRITE;
  43635. }
  43636. /* Called when reading. */
  43637. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  43638. {
  43639. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  43640. int len = (int)msg->length;
  43641. (void)ssl;
  43642. (void)sz;
  43643. /* Pass back as much of message as will fit in buffer. */
  43644. if (len > sz)
  43645. len = sz;
  43646. XMEMCPY(buf, msg->buffer, len);
  43647. /* Move over returned data. */
  43648. msg->buffer += len;
  43649. msg->length -= len;
  43650. /* Amount actually copied. */
  43651. return len;
  43652. }
  43653. #endif
  43654. static int test_tls13_cipher_suites(void)
  43655. {
  43656. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  43657. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  43658. defined(BUILD_TLS_AES_256_GCM_SHA384)
  43659. WOLFSSL_CTX* ctx;
  43660. WOLFSSL *ssl;
  43661. int i;
  43662. byte clientHello[] = {
  43663. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  43664. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  43665. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  43666. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  43667. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  43668. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  43669. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  43670. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  43671. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43672. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  43673. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  43674. 0x13, 0x01,
  43675. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  43676. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  43677. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  43678. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  43679. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  43680. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  43681. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  43682. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  43683. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  43684. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  43685. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  43686. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  43687. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  43688. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  43689. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  43690. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  43691. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  43692. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  43693. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  43694. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  43695. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  43696. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  43697. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  43698. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  43699. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  43700. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  43701. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  43702. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  43703. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  43704. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  43705. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  43706. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  43707. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  43708. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  43709. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  43710. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  43712. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  43713. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  43714. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  43715. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  43716. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  43717. };
  43718. WOLFSSL_BUFFER_INFO msg;
  43719. /* Offset into ClientHello message data of first cipher suite. */
  43720. const int csOff = 78;
  43721. /* Server cipher list. */
  43722. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  43723. /* Suite list with duplicates. */
  43724. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  43725. "TLS13-AES128-GCM-SHA256:"
  43726. "TLS13-AES256-GCM-SHA384:"
  43727. "TLS13-AES256-GCM-SHA384:"
  43728. "TLS13-AES128-GCM-SHA256";
  43729. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  43730. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  43731. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  43732. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  43733. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  43734. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  43735. #endif
  43736. printf(testingFmt, "test_tls13_cipher_suites");
  43737. /* Set up wolfSSL context. */
  43738. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  43739. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  43740. WOLFSSL_FILETYPE_PEM));
  43741. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  43742. WOLFSSL_FILETYPE_PEM));
  43743. /* Read from 'msg'. */
  43744. wolfSSL_SetIORecv(ctx, CsRecv);
  43745. /* No where to send to - dummy sender. */
  43746. wolfSSL_SetIOSend(ctx, CsSend);
  43747. /* Test cipher suite list with many copies of a cipher suite. */
  43748. AssertNotNull(ssl = wolfSSL_new(ctx));
  43749. msg.buffer = clientHello;
  43750. msg.length = (unsigned int)sizeof(clientHello);
  43751. wolfSSL_SetIOReadCtx(ssl, &msg);
  43752. /* Force server to have as many occurrences of same cipher suite as
  43753. * possible. */
  43754. ssl->suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  43755. for (i = 0; i < ssl->suites->suiteSz; i += 2) {
  43756. ssl->suites->suites[i + 0] = TLS13_BYTE;
  43757. ssl->suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  43758. }
  43759. /* Test multiple occurrences of same cipher suite. */
  43760. wolfSSL_accept_TLSv13(ssl);
  43761. wolfSSL_free(ssl);
  43762. /* Set client order opposite to server order:
  43763. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  43764. clientHello[csOff + 0] = TLS13_BYTE;
  43765. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  43766. clientHello[csOff + 2] = TLS13_BYTE;
  43767. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  43768. /* Test server order negotiation. */
  43769. AssertNotNull(ssl = wolfSSL_new(ctx));
  43770. msg.buffer = clientHello;
  43771. msg.length = (unsigned int)sizeof(clientHello);
  43772. wolfSSL_SetIOReadCtx(ssl, &msg);
  43773. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  43774. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  43775. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  43776. wolfSSL_accept_TLSv13(ssl);
  43777. /* Check refined order - server order. */
  43778. AssertIntEQ(ssl->suites->suiteSz, 4);
  43779. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  43780. AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  43781. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  43782. AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  43783. wolfSSL_free(ssl);
  43784. /* Test client order negotiation. */
  43785. AssertNotNull(ssl = wolfSSL_new(ctx));
  43786. msg.buffer = clientHello;
  43787. msg.length = (unsigned int)sizeof(clientHello);
  43788. wolfSSL_SetIOReadCtx(ssl, &msg);
  43789. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  43790. AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  43791. AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  43792. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  43793. wolfSSL_accept_TLSv13(ssl);
  43794. /* Check refined order - client order. */
  43795. AssertIntEQ(ssl->suites->suiteSz, 4);
  43796. AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  43797. AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  43798. AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  43799. AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  43800. wolfSSL_free(ssl);
  43801. /* Check duplicate detection is working. */
  43802. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  43803. AssertIntEQ(ctx->suites->suiteSz, 4);
  43804. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  43805. AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  43806. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  43807. AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  43808. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  43809. AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  43810. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  43811. AssertIntEQ(ctx->suites->suiteSz, 4);
  43812. AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  43813. AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  43814. AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  43815. AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  43816. #endif
  43817. wolfSSL_CTX_free(ctx);
  43818. printf(resultFmt, passed);
  43819. #endif
  43820. return 0;
  43821. }
  43822. #endif
  43823. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  43824. !defined(NO_OLD_TLS))
  43825. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  43826. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  43827. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  43828. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  43829. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  43830. const unsigned char* priv, unsigned int privSz,
  43831. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  43832. unsigned char* out, unsigned int* outlen,
  43833. void* ctx)
  43834. {
  43835. int result;
  43836. /* Test fail when context associated with WOLFSSL is NULL */
  43837. if (ctx == NULL) {
  43838. return -1;
  43839. }
  43840. (void)ssl;
  43841. /* return 0 on success */
  43842. PRIVATE_KEY_UNLOCK();
  43843. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  43844. PRIVATE_KEY_LOCK();
  43845. return result;
  43846. }
  43847. static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  43848. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  43849. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  43850. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  43851. WOLFSSL_SUCCESS);
  43852. #endif
  43853. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  43854. AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  43855. WOLFSSL_SUCCESS);
  43856. #endif
  43857. }
  43858. static void test_dh_ssl_setup(WOLFSSL* ssl)
  43859. {
  43860. static int dh_test_ctx = 1;
  43861. int ret;
  43862. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  43863. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  43864. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  43865. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  43866. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43867. }
  43868. }
  43869. static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
  43870. {
  43871. int ret;
  43872. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  43873. AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
  43874. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  43875. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  43876. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  43877. }
  43878. }
  43879. #endif
  43880. static int test_DhCallbacks(void)
  43881. {
  43882. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  43883. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
  43884. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  43885. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  43886. WOLFSSL_CTX *ctx;
  43887. WOLFSSL *ssl;
  43888. tcp_ready ready;
  43889. func_args server_args;
  43890. func_args client_args;
  43891. THREAD_TYPE serverThread;
  43892. callback_functions func_cb_client;
  43893. callback_functions func_cb_server;
  43894. int test;
  43895. printf(testingFmt, "test_DhCallbacks");
  43896. #ifndef NO_WOLFSSL_CLIENT
  43897. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  43898. #else
  43899. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  43900. #endif
  43901. AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  43902. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  43903. /* load client ca cert */
  43904. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  43905. WOLFSSL_SUCCESS);
  43906. /* test with NULL arguments */
  43907. wolfSSL_SetDhAgreeCtx(NULL, &test);
  43908. AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
  43909. /* test success case */
  43910. test = 1;
  43911. AssertNotNull(ssl = wolfSSL_new(ctx));
  43912. wolfSSL_SetDhAgreeCtx(ssl, &test);
  43913. AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  43914. wolfSSL_free(ssl);
  43915. wolfSSL_CTX_free(ctx);
  43916. /* test a connection where callback is used */
  43917. #ifdef WOLFSSL_TIRTOS
  43918. fdOpenSession(Task_self());
  43919. #endif
  43920. XMEMSET(&server_args, 0, sizeof(func_args));
  43921. XMEMSET(&client_args, 0, sizeof(func_args));
  43922. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  43923. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  43924. StartTCP();
  43925. InitTcpReady(&ready);
  43926. #if defined(USE_WINDOWS_API)
  43927. /* use RNG to get random port if using windows */
  43928. ready.port = GetRandomPort();
  43929. #endif
  43930. server_args.signal = &ready;
  43931. client_args.signal = &ready;
  43932. server_args.return_code = TEST_FAIL;
  43933. client_args.return_code = TEST_FAIL;
  43934. /* set callbacks to use DH functions */
  43935. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  43936. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  43937. #ifndef WOLFSSL_NO_TLS12
  43938. func_cb_client.method = wolfTLSv1_2_client_method;
  43939. #else
  43940. func_cb_client.method = wolfTLSv1_3_client_method;
  43941. #endif
  43942. client_args.callbacks = &func_cb_client;
  43943. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  43944. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  43945. #ifndef WOLFSSL_NO_TLS12
  43946. func_cb_server.method = wolfTLSv1_2_server_method;
  43947. #else
  43948. func_cb_server.method = wolfTLSv1_3_server_method;
  43949. #endif
  43950. server_args.callbacks = &func_cb_server;
  43951. start_thread(test_server_nofail, &server_args, &serverThread);
  43952. wait_tcp_ready(&server_args);
  43953. test_client_nofail(&client_args, NULL);
  43954. join_thread(serverThread);
  43955. AssertTrue(client_args.return_code);
  43956. AssertTrue(server_args.return_code);
  43957. FreeTcpReady(&ready);
  43958. #ifdef WOLFSSL_TIRTOS
  43959. fdOpenSession(Task_self());
  43960. #endif
  43961. /* now set user ctx to not be 1 so that the callback returns fail case */
  43962. #ifdef WOLFSSL_TIRTOS
  43963. fdOpenSession(Task_self());
  43964. #endif
  43965. XMEMSET(&server_args, 0, sizeof(func_args));
  43966. XMEMSET(&client_args, 0, sizeof(func_args));
  43967. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  43968. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  43969. StartTCP();
  43970. InitTcpReady(&ready);
  43971. #if defined(USE_WINDOWS_API)
  43972. /* use RNG to get random port if using windows */
  43973. ready.port = GetRandomPort();
  43974. #endif
  43975. server_args.signal = &ready;
  43976. client_args.signal = &ready;
  43977. server_args.return_code = TEST_FAIL;
  43978. client_args.return_code = TEST_FAIL;
  43979. /* set callbacks to use DH functions */
  43980. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  43981. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  43982. #ifndef WOLFSSL_NO_TLS12
  43983. func_cb_client.method = wolfTLSv1_2_client_method;
  43984. #else
  43985. func_cb_client.method = wolfTLSv1_3_client_method;
  43986. #endif
  43987. client_args.callbacks = &func_cb_client;
  43988. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  43989. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  43990. #ifndef WOLFSSL_NO_TLS12
  43991. func_cb_server.method = wolfTLSv1_2_server_method;
  43992. #else
  43993. func_cb_server.method = wolfTLSv1_3_server_method;
  43994. #endif
  43995. server_args.callbacks = &func_cb_server;
  43996. start_thread(test_server_nofail, &server_args, &serverThread);
  43997. wait_tcp_ready(&server_args);
  43998. test_client_nofail(&client_args, NULL);
  43999. join_thread(serverThread);
  44000. AssertIntEQ(client_args.return_code, TEST_FAIL);
  44001. AssertIntEQ(server_args.return_code, TEST_FAIL);
  44002. FreeTcpReady(&ready);
  44003. #ifdef WOLFSSL_TIRTOS
  44004. fdOpenSession(Task_self());
  44005. #endif
  44006. printf(resultFmt, passed);
  44007. #endif
  44008. return 0;
  44009. }
  44010. #endif /* HAVE_PK_CALLBACKS */
  44011. #ifdef HAVE_HASHDRBG
  44012. #ifdef TEST_RESEED_INTERVAL
  44013. static int test_wc_RNG_GenerateBlock_Reseed(void)
  44014. {
  44015. int i, ret;
  44016. WC_RNG rng;
  44017. byte key[32];
  44018. ret = wc_InitRng(&rng);
  44019. if (ret == 0) {
  44020. for(i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  44021. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44022. if (ret != 0) {
  44023. break;
  44024. }
  44025. }
  44026. }
  44027. wc_FreeRng(&rng);
  44028. return ret;
  44029. }
  44030. #endif /* TEST_RESEED_INTERVAL */
  44031. static int test_wc_RNG_GenerateBlock(void)
  44032. {
  44033. int i, ret;
  44034. WC_RNG rng;
  44035. byte key[32];
  44036. ret = wc_InitRng(&rng);
  44037. if (ret == 0) {
  44038. for(i = 0; i < 10; i++) {
  44039. ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
  44040. if (ret != 0) {
  44041. break;
  44042. }
  44043. }
  44044. }
  44045. wc_FreeRng(&rng);
  44046. (void)rng; /* for WC_NO_RNG case */
  44047. (void)key;
  44048. return ret;
  44049. }
  44050. #endif
  44051. /*
  44052. * Testing get_rand_digit
  44053. */
  44054. static int test_get_rand_digit(void)
  44055. {
  44056. int ret = 0;
  44057. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  44058. WC_RNG rng;
  44059. mp_digit d;
  44060. printf(testingFmt, "get_rand_digit()");
  44061. ret = wc_InitRng(&rng);
  44062. if (ret == 0) {
  44063. ret = get_rand_digit(&rng, &d);
  44064. }
  44065. if (ret == 0) {
  44066. ret = get_rand_digit(NULL, NULL);
  44067. if (ret == BAD_FUNC_ARG) {
  44068. ret = 0;
  44069. }
  44070. }
  44071. if (ret == 0) {
  44072. ret = get_rand_digit(NULL, &d);
  44073. if (ret == BAD_FUNC_ARG) {
  44074. ret = 0;
  44075. }
  44076. }
  44077. if (ret == 0) {
  44078. ret = get_rand_digit(&rng, NULL);
  44079. if (ret == BAD_FUNC_ARG) {
  44080. ret = 0;
  44081. }
  44082. }
  44083. if (ret == 0) {
  44084. ret = wc_FreeRng(&rng);
  44085. }
  44086. printf(resultFmt, ret == 0 ? passed : failed);
  44087. fflush(stdout);
  44088. #endif
  44089. return ret;
  44090. }/* End test_get_rand_digit*/
  44091. /*
  44092. * Testing get_digit_count
  44093. */
  44094. static int test_get_digit_count(void)
  44095. {
  44096. int ret = 0;
  44097. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  44098. mp_int a;
  44099. printf(testingFmt, "get_digit_count()");
  44100. if (mp_init(&a) != MP_OKAY) {
  44101. ret = -1;
  44102. }
  44103. if (ret == 0) {
  44104. ret = get_digit_count(NULL);
  44105. }
  44106. if (ret == 0) {
  44107. ret = get_digit_count(&a);
  44108. }
  44109. printf(resultFmt, ret == 0 ? passed : failed);
  44110. fflush(stdout);
  44111. mp_clear(&a);
  44112. #endif
  44113. return ret;
  44114. }/* End test_get_digit_count*/
  44115. /*
  44116. * Testing mp_cond_copy
  44117. */
  44118. static int test_mp_cond_copy(void)
  44119. {
  44120. int ret = 0;
  44121. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  44122. defined(WOLFSSL_PUBLIC_MP)
  44123. mp_int a;
  44124. mp_int b;
  44125. int copy = 0;
  44126. printf(testingFmt, "mp_cond_copy()");
  44127. if (mp_init(&a) != MP_OKAY) {
  44128. ret = -1;
  44129. }
  44130. if (ret == 0) {
  44131. if (mp_init(&b) != MP_OKAY) {
  44132. ret = -1;
  44133. }
  44134. }
  44135. if (ret == 0) {
  44136. ret = mp_cond_copy(NULL, copy, NULL);
  44137. if (ret == BAD_FUNC_ARG) {
  44138. ret = 0;
  44139. }
  44140. }
  44141. if (ret == 0) {
  44142. ret = mp_cond_copy(NULL, copy, &b);
  44143. if (ret == BAD_FUNC_ARG) {
  44144. ret = 0;
  44145. }
  44146. }
  44147. if (ret == 0) {
  44148. ret = mp_cond_copy(&a, copy, NULL);
  44149. if (ret == BAD_FUNC_ARG) {
  44150. ret = 0;
  44151. }
  44152. }
  44153. if (ret == 0) {
  44154. ret = mp_cond_copy(&a, copy, &b);
  44155. }
  44156. printf(resultFmt, ret == 0 ? passed : failed);
  44157. fflush(stdout);
  44158. mp_clear(&a);
  44159. mp_clear(&b);
  44160. #endif
  44161. return ret;
  44162. }/* End test_mp_cond_copy*/
  44163. /*
  44164. * Testing mp_rand
  44165. */
  44166. static int test_mp_rand(void)
  44167. {
  44168. int ret = 0;
  44169. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  44170. mp_int a;
  44171. int digits = 1;
  44172. WC_RNG rng;
  44173. printf(testingFmt, "mp_rand()");
  44174. if (mp_init(&a) != MP_OKAY) {
  44175. ret = -1;
  44176. }
  44177. if (ret == 0) {
  44178. ret = wc_InitRng(&rng);
  44179. }
  44180. if (ret == 0) {
  44181. ret = mp_rand(&a, digits, NULL);
  44182. if (ret == MISSING_RNG_E) {
  44183. ret = 0;
  44184. }
  44185. }
  44186. if (ret == 0) {
  44187. ret = mp_rand(NULL, digits, &rng);
  44188. if (ret == BAD_FUNC_ARG) {
  44189. ret = 0;
  44190. }
  44191. }
  44192. if (ret == 0) {
  44193. ret = mp_rand(&a, 0, &rng);
  44194. if (ret == BAD_FUNC_ARG) {
  44195. ret = 0;
  44196. }
  44197. }
  44198. if (ret == 0) {
  44199. ret = mp_rand(&a, digits, &rng);
  44200. }
  44201. printf(resultFmt, ret == 0 ? passed : failed);
  44202. fflush(stdout);
  44203. mp_clear(&a);
  44204. wc_FreeRng(&rng);
  44205. #endif
  44206. return ret;
  44207. }/* End test_mp_rand*/
  44208. /*
  44209. * Testing get_digit
  44210. */
  44211. static int test_get_digit(void)
  44212. {
  44213. int ret = 0;
  44214. #if defined(WOLFSSL_PUBLIC_MP)
  44215. mp_int a;
  44216. int n = 0;
  44217. printf(testingFmt, "get_digit()");
  44218. if (mp_init(&a) != MP_OKAY) {
  44219. ret = -1;
  44220. }
  44221. if (ret == 0) {
  44222. if (get_digit(NULL, n) != 0) { /* Should not hit this */
  44223. ret = -1;
  44224. }
  44225. }
  44226. if (ret == 0) {
  44227. if (get_digit(NULL, n) == 0) { /* Should hit this */
  44228. ret = 0;
  44229. }
  44230. }
  44231. if (ret == 0) {
  44232. if (get_digit(&a, n) != 0) { /* Should not hit this */
  44233. ret = -1;
  44234. }
  44235. }
  44236. if (ret == 0) {
  44237. if (get_digit(&a, n) == 0) { /* Should hit this */
  44238. ret = 0;
  44239. }
  44240. }
  44241. printf(resultFmt, ret == 0 ? passed : failed);
  44242. fflush(stdout);
  44243. mp_clear(&a);
  44244. #endif
  44245. return ret;
  44246. }/* End test_get_digit*/
  44247. /*
  44248. * Testing wc_export_int
  44249. */
  44250. static int test_wc_export_int(void)
  44251. {
  44252. int ret = 0;
  44253. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  44254. defined(WOLFSSL_PUBLIC_MP)
  44255. mp_int mp;
  44256. byte buf[32];
  44257. word32 keySz = (word32)sizeof(buf);
  44258. word32 len = (word32)sizeof(buf);
  44259. printf(testingFmt, "wc_export_int()");
  44260. if (mp_init(&mp) != MP_OKAY) {
  44261. ret = -1;
  44262. }
  44263. if (ret == 0) {
  44264. ret = mp_set(&mp, 1234);
  44265. }
  44266. if (ret == 0) {
  44267. ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44268. if (ret == BAD_FUNC_ARG) {
  44269. ret = 0;
  44270. }
  44271. }
  44272. if (ret == 0) {
  44273. len = sizeof(buf)-1;
  44274. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44275. if (ret == BUFFER_E) {
  44276. ret = 0;
  44277. }
  44278. }
  44279. if (ret == 0) {
  44280. len = sizeof(buf);
  44281. ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
  44282. }
  44283. if (ret == 0) {
  44284. len = 4; /* test input too small */
  44285. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44286. if (ret == BUFFER_E) {
  44287. ret = 0;
  44288. }
  44289. }
  44290. if (ret == 0) {
  44291. len = sizeof(buf);
  44292. ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
  44293. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  44294. if (ret == 0 && len != 5) {
  44295. ret = BAD_FUNC_ARG;
  44296. }
  44297. }
  44298. printf(resultFmt, ret == 0 ? passed : failed);
  44299. fflush(stdout);
  44300. mp_clear(&mp);
  44301. #endif
  44302. return ret;
  44303. }/* End test_wc_export_int*/
  44304. static int test_wc_InitRngNonce(void)
  44305. {
  44306. int ret=0;
  44307. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44308. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44309. WC_RNG rng;
  44310. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44311. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44312. word32 nonceSz = sizeof(nonce);
  44313. printf(testingFmt, "wc_InitRngNonce()");
  44314. if (ret == 0){
  44315. ret = wc_InitRngNonce(&rng, nonce, nonceSz);
  44316. }
  44317. wc_FreeRng(&rng);
  44318. printf(resultFmt, ret == 0 ? passed : failed);
  44319. fflush(stdout);
  44320. #endif
  44321. return ret;
  44322. }/* End test_wc_InitRngNonce*/
  44323. /*
  44324. * Testing wc_InitRngNonce_ex
  44325. */
  44326. static int test_wc_InitRngNonce_ex(void)
  44327. {
  44328. int ret=0;
  44329. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  44330. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
  44331. WC_RNG rng;
  44332. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  44333. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  44334. word32 nonceSz = sizeof(nonce);
  44335. printf(testingFmt, "wc_InitRngNonce_ex()");
  44336. if (ret == 0){
  44337. ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
  44338. }
  44339. wc_FreeRng(&rng);
  44340. printf(resultFmt, ret == 0 ? passed : failed);
  44341. fflush(stdout);
  44342. #endif
  44343. return ret;
  44344. }/*End test_wc_InitRngNonce_ex*/
  44345. static int test_wolfSSL_X509_CRL(void)
  44346. {
  44347. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  44348. X509_CRL *crl;
  44349. char pem[][100] = {
  44350. "./certs/crl/crl.pem",
  44351. "./certs/crl/crl2.pem",
  44352. "./certs/crl/caEccCrl.pem",
  44353. "./certs/crl/eccCliCRL.pem",
  44354. "./certs/crl/eccSrvCRL.pem",
  44355. ""
  44356. };
  44357. #ifndef NO_BIO
  44358. BIO *bio;
  44359. #endif
  44360. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44361. char der[][100] = {
  44362. "./certs/crl/crl.der",
  44363. "./certs/crl/crl2.der",
  44364. ""};
  44365. #endif
  44366. XFILE fp;
  44367. int i;
  44368. printf(testingFmt, "test_wolfSSL_X509_CRL");
  44369. for (i = 0; pem[i][0] != '\0'; i++)
  44370. {
  44371. fp = XFOPEN(pem[i], "rb");
  44372. AssertTrue((fp != XBADFILE));
  44373. AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
  44374. AssertNotNull(crl);
  44375. X509_CRL_free(crl);
  44376. XFCLOSE(fp);
  44377. fp = XFOPEN(pem[i], "rb");
  44378. AssertTrue((fp != XBADFILE));
  44379. AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
  44380. AssertNotNull(crl);
  44381. X509_CRL_free(crl);
  44382. XFCLOSE(fp);
  44383. }
  44384. #ifndef NO_BIO
  44385. for (i = 0; pem[i][0] != '\0'; i++)
  44386. {
  44387. AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
  44388. AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  44389. X509_CRL_free(crl);
  44390. BIO_free(bio);
  44391. }
  44392. #endif
  44393. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  44394. for(i = 0; der[i][0] != '\0'; i++){
  44395. fp = XFOPEN(der[i], "rb");
  44396. AssertTrue((fp != XBADFILE));
  44397. AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  44398. AssertNotNull(crl);
  44399. X509_CRL_free(crl);
  44400. XFCLOSE(fp);
  44401. fp = XFOPEN(der[i], "rb");
  44402. AssertTrue((fp != XBADFILE));
  44403. AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  44404. AssertNotNull(crl);
  44405. X509_CRL_free(crl);
  44406. XFCLOSE(fp);
  44407. }
  44408. #endif
  44409. printf(resultFmt, passed);
  44410. fflush(stdout);
  44411. #endif
  44412. return 0;
  44413. }
  44414. static int test_wolfSSL_X509_load_crl_file(void)
  44415. {
  44416. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44417. !defined(NO_RSA) && !defined(NO_BIO)
  44418. int i;
  44419. char pem[][100] = {
  44420. "./certs/crl/crl.pem",
  44421. "./certs/crl/crl2.pem",
  44422. "./certs/crl/caEccCrl.pem",
  44423. "./certs/crl/eccCliCRL.pem",
  44424. "./certs/crl/eccSrvCRL.pem",
  44425. ""
  44426. };
  44427. char der[][100] = {
  44428. "./certs/crl/crl.der",
  44429. "./certs/crl/crl2.der",
  44430. ""
  44431. };
  44432. WOLFSSL_X509_STORE* store;
  44433. WOLFSSL_X509_LOOKUP* lookup;
  44434. printf(testingFmt, "wolfSSL_X509_load_crl_file");
  44435. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44436. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44437. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44438. X509_FILETYPE_PEM), 1);
  44439. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44440. X509_FILETYPE_PEM), 1);
  44441. if (store) {
  44442. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44443. WOLFSSL_FILETYPE_PEM), 1);
  44444. /* since store hasn't yet known the revoked cert*/
  44445. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44446. WOLFSSL_FILETYPE_PEM), 1);
  44447. }
  44448. for (i = 0; pem[i][0] != '\0'; i++)
  44449. {
  44450. AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
  44451. }
  44452. if (store) {
  44453. /* since store knows crl list */
  44454. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44455. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44456. }
  44457. /* once feeing store */
  44458. X509_STORE_free(store);
  44459. store = NULL;
  44460. AssertNotNull(store = wolfSSL_X509_STORE_new());
  44461. AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  44462. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  44463. X509_FILETYPE_PEM), 1);
  44464. AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  44465. X509_FILETYPE_PEM), 1);
  44466. if (store) {
  44467. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  44468. WOLFSSL_FILETYPE_PEM), 1);
  44469. /* since store hasn't yet known the revoked cert*/
  44470. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44471. WOLFSSL_FILETYPE_PEM), 1);
  44472. }
  44473. for (i = 0; der[i][0] != '\0'; i++)
  44474. {
  44475. AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
  44476. }
  44477. if (store) {
  44478. /* since store knows crl list */
  44479. AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
  44480. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  44481. }
  44482. /* test for incorrect parameter */
  44483. AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  44484. AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  44485. AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  44486. X509_STORE_free(store);
  44487. store = NULL;
  44488. printf(resultFmt, passed);
  44489. #endif
  44490. return 0;
  44491. }
  44492. static int test_wolfSSL_d2i_X509_REQ(void)
  44493. {
  44494. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  44495. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  44496. !defined(WOLFSSL_SP_MATH)
  44497. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
  44498. * generated by libest
  44499. * ./certs/csr.attr.der contains sample attributes
  44500. * ./certs/csr.ext.der contains sample extensions */
  44501. const char* csrFile = "./certs/csr.signed.der";
  44502. const char* csrPopFile = "./certs/csr.attr.der";
  44503. const char* csrExtFile = "./certs/csr.ext.der";
  44504. /* ./certs/csr.dsa.pem is generated using
  44505. * openssl req -newkey dsa:certs/dsaparams.pem \
  44506. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  44507. * -outform PEM
  44508. * with the passphrase "wolfSSL"
  44509. */
  44510. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44511. const char* csrDsaFile = "./certs/csr.dsa.pem";
  44512. XFILE f;
  44513. #endif
  44514. BIO* bio = NULL;
  44515. X509* req = NULL;
  44516. EVP_PKEY *pub_key = NULL;
  44517. {
  44518. AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
  44519. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44520. /*
  44521. * Extract the public key from the CSR
  44522. */
  44523. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44524. /*
  44525. * Verify the signature in the CSR
  44526. */
  44527. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44528. X509_free(req);
  44529. BIO_free(bio);
  44530. EVP_PKEY_free(pub_key);
  44531. }
  44532. {
  44533. #ifdef OPENSSL_ALL
  44534. X509_ATTRIBUTE* attr;
  44535. ASN1_TYPE *at;
  44536. #endif
  44537. AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  44538. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44539. /*
  44540. * Extract the public key from the CSR
  44541. */
  44542. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44543. /*
  44544. * Verify the signature in the CSR
  44545. */
  44546. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44547. #ifdef OPENSSL_ALL
  44548. /*
  44549. * Obtain the challenge password from the CSR
  44550. */
  44551. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  44552. 1);
  44553. AssertNotNull(attr = X509_REQ_get_attr(req, 1));
  44554. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44555. AssertNotNull(at->value.asn1_string);
  44556. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
  44557. AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  44558. #endif
  44559. X509_free(req);
  44560. BIO_free(bio);
  44561. EVP_PKEY_free(pub_key);
  44562. }
  44563. {
  44564. #ifdef OPENSSL_ALL
  44565. X509_ATTRIBUTE* attr;
  44566. ASN1_TYPE *at;
  44567. STACK_OF(X509_EXTENSION) *exts = NULL;
  44568. #endif
  44569. AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  44570. /* This CSR contains an Extension Request attribute so
  44571. * we test extension parsing in a CSR attribute here. */
  44572. AssertNotNull(d2i_X509_REQ_bio(bio, &req));
  44573. /*
  44574. * Extract the public key from the CSR
  44575. */
  44576. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44577. /*
  44578. * Verify the signature in the CSR
  44579. */
  44580. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44581. #ifdef OPENSSL_ALL
  44582. AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
  44583. AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
  44584. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  44585. /*
  44586. * Obtain the challenge password from the CSR
  44587. */
  44588. AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
  44589. 0);
  44590. AssertNotNull(attr = X509_REQ_get_attr(req, 0));
  44591. AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  44592. AssertNotNull(at->value.asn1_string);
  44593. AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  44594. AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  44595. AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  44596. #endif
  44597. X509_free(req);
  44598. BIO_free(bio);
  44599. EVP_PKEY_free(pub_key);
  44600. }
  44601. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  44602. {
  44603. AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  44604. AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  44605. /*
  44606. * Extract the public key from the CSR
  44607. */
  44608. AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
  44609. /*
  44610. * Verify the signature in the CSR
  44611. */
  44612. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44613. X509_free(req);
  44614. BIO_free(bio);
  44615. /* Run the same test, but with a file pointer instead of a BIO.
  44616. * (PEM_read_X509_REQ)*/
  44617. AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  44618. AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  44619. AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
  44620. X509_free(req);
  44621. EVP_PKEY_free(pub_key);
  44622. }
  44623. #endif /* !NO_DSA && !HAVE_SELFTEST */
  44624. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  44625. return 0;
  44626. }
  44627. static int test_wolfSSL_PEM_read_X509(void)
  44628. {
  44629. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  44630. !defined(NO_RSA)
  44631. X509 *x509 = NULL;
  44632. XFILE fp;
  44633. printf(testingFmt, "wolfSSL_PEM_read_X509");
  44634. fp = XFOPEN(svrCertFile, "rb");
  44635. AssertTrue((fp != XBADFILE));
  44636. AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  44637. X509_free(x509);
  44638. XFCLOSE(fp);
  44639. printf(resultFmt, passed);
  44640. #endif
  44641. return 0;
  44642. }
  44643. static int test_wolfSSL_PEM_read(void)
  44644. {
  44645. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44646. const char* filename = "./certs/server-keyEnc.pem";
  44647. XFILE fp;
  44648. char* name = NULL;
  44649. char* header = NULL;
  44650. byte* data = NULL;
  44651. long len;
  44652. EVP_CIPHER_INFO cipher;
  44653. WOLFSSL_BIO* bio;
  44654. byte* fileData;
  44655. size_t fileDataSz;
  44656. byte* out;
  44657. printf(testingFmt, "wolfSSL_PEM_read");
  44658. fp = XFOPEN(filename, "rb");
  44659. AssertTrue((fp != XBADFILE));
  44660. /* Fail cases. */
  44661. AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  44662. AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  44663. AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  44664. AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  44665. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44666. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44667. AssertIntGT(XSTRLEN(header), 0);
  44668. AssertIntGT(len, 0);
  44669. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44670. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  44671. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44672. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44673. DYNAMIC_TYPE_TMP_BUFFER));
  44674. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44675. XFCLOSE(fp);
  44676. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44677. /* Fail cases. */
  44678. AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  44679. AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  44680. AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  44681. AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  44682. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  44683. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  44684. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  44685. /* Fail cases. */
  44686. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  44687. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  44688. AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  44689. #ifndef NO_DES3
  44690. AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  44691. #endif
  44692. /* Fail cases. */
  44693. AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  44694. (void*)"yassl123"), WOLFSSL_FAILURE);
  44695. AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  44696. (void*)"yassl123"), WOLFSSL_FAILURE);
  44697. AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  44698. (void*)"yassl123"), WOLFSSL_FAILURE);
  44699. #if !defined(NO_DES3) && !defined(NO_MD5)
  44700. AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  44701. (void*)"yassl123"), WOLFSSL_SUCCESS);
  44702. #endif
  44703. BIO_free(bio);
  44704. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44705. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44706. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44707. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44708. name = NULL;
  44709. header = NULL;
  44710. data = NULL;
  44711. fp = XFOPEN(svrKeyFile, "rb");
  44712. AssertTrue((fp != XBADFILE));
  44713. AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  44714. AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  44715. AssertIntEQ(XSTRLEN(header), 0);
  44716. AssertIntGT(len, 0);
  44717. AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  44718. AssertIntGT((fileDataSz = XFTELL(fp)), 0);
  44719. AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  44720. AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  44721. DYNAMIC_TYPE_TMP_BUFFER));
  44722. AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  44723. XFCLOSE(fp);
  44724. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  44725. AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  44726. AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  44727. AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  44728. BIO_free(bio);
  44729. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44730. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44731. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44732. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44733. printf(resultFmt, passed);
  44734. #endif
  44735. return 0;
  44736. }
  44737. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  44738. {
  44739. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44740. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44741. const byte iv[12] = { 0 };
  44742. const byte key[16] = { 0 };
  44743. const byte cleartext[16] = { 0 };
  44744. const byte aad[] = {
  44745. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  44746. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  44747. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  44748. 0x4f
  44749. };
  44750. byte out1Part[16];
  44751. byte outTag1Part[16];
  44752. byte out2Part[16];
  44753. byte outTag2Part[16];
  44754. byte decryptBuf[16];
  44755. int len;
  44756. int tlen;
  44757. EVP_CIPHER_CTX* ctx = NULL;
  44758. printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
  44759. /* ENCRYPT */
  44760. /* Send AAD and data in 1 part */
  44761. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44762. tlen = 0;
  44763. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44764. 1);
  44765. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44766. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  44767. AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  44768. sizeof(cleartext)), 1);
  44769. tlen += len;
  44770. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  44771. tlen += len;
  44772. AssertIntEQ(tlen, sizeof(cleartext));
  44773. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  44774. outTag1Part), 1);
  44775. EVP_CIPHER_CTX_free(ctx);
  44776. /* DECRYPT */
  44777. /* Send AAD and data in 1 part */
  44778. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44779. tlen = 0;
  44780. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44781. 1);
  44782. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44783. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  44784. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  44785. sizeof(cleartext)), 1);
  44786. tlen += len;
  44787. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  44788. outTag1Part), 1);
  44789. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  44790. tlen += len;
  44791. AssertIntEQ(tlen, sizeof(cleartext));
  44792. EVP_CIPHER_CTX_free(ctx);
  44793. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  44794. /* ENCRYPT */
  44795. /* Send AAD and data in 2 parts */
  44796. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44797. tlen = 0;
  44798. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44799. 1);
  44800. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44801. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  44802. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  44803. 1);
  44804. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  44805. tlen += len;
  44806. AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  44807. sizeof(cleartext) - 1), 1);
  44808. tlen += len;
  44809. AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  44810. tlen += len;
  44811. AssertIntEQ(tlen, sizeof(cleartext));
  44812. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  44813. outTag2Part), 1);
  44814. AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  44815. AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  44816. EVP_CIPHER_CTX_free(ctx);
  44817. /* DECRYPT */
  44818. /* Send AAD and data in 2 parts */
  44819. AssertNotNull(ctx = EVP_CIPHER_CTX_new());
  44820. tlen = 0;
  44821. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  44822. 1);
  44823. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  44824. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  44825. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  44826. 1);
  44827. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  44828. tlen += len;
  44829. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  44830. sizeof(cleartext) - 1), 1);
  44831. tlen += len;
  44832. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  44833. outTag1Part), 1);
  44834. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  44835. tlen += len;
  44836. AssertIntEQ(tlen, sizeof(cleartext));
  44837. AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  44838. /* Test AAD re-use */
  44839. EVP_CIPHER_CTX_free(ctx);
  44840. printf(resultFmt, passed);
  44841. #endif
  44842. return 0;
  44843. }
  44844. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44845. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44846. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  44847. {
  44848. /* Zero length plain text */
  44849. byte key[] = {
  44850. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44851. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44852. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  44853. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  44854. }; /* align */
  44855. byte iv[] = {
  44856. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  44857. }; /* align */
  44858. byte plaintxt[1];
  44859. int ivSz = 12;
  44860. int plaintxtSz = 0;
  44861. unsigned char tag[16];
  44862. unsigned char tag_kat[] =
  44863. {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  44864. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
  44865. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  44866. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  44867. int ciphertxtSz = 0;
  44868. int decryptedtxtSz = 0;
  44869. int len = 0;
  44870. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  44871. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  44872. AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  44873. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44874. AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  44875. plaintxtSz));
  44876. AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  44877. ciphertxtSz += len;
  44878. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  44879. AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  44880. AssertIntEQ(0, ciphertxtSz);
  44881. AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  44882. EVP_CIPHER_CTX_init(de);
  44883. AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  44884. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44885. AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  44886. decryptedtxtSz = len;
  44887. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  44888. AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  44889. decryptedtxtSz += len;
  44890. AssertIntEQ(0, decryptedtxtSz);
  44891. EVP_CIPHER_CTX_free(en);
  44892. EVP_CIPHER_CTX_free(de);
  44893. return 0;
  44894. }
  44895. #endif
  44896. static int test_wolfssl_EVP_aes_gcm(void)
  44897. {
  44898. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  44899. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  44900. /* A 256 bit key, AES_128 will use the first 128 bit*/
  44901. byte *key = (byte*)"01234567890123456789012345678901";
  44902. /* A 128 bit IV */
  44903. byte *iv = (byte*)"0123456789012345";
  44904. int ivSz = AES_BLOCK_SIZE;
  44905. /* Message to be encrypted */
  44906. byte *plaintxt = (byte*)"for things to change you have to change";
  44907. /* Additional non-confidential data */
  44908. byte *aad = (byte*)"Don't spend major time on minor things.";
  44909. unsigned char tag[AES_BLOCK_SIZE] = {0};
  44910. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  44911. int aadSz = (int)XSTRLEN((char*)aad);
  44912. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  44913. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  44914. int ciphertxtSz = 0;
  44915. int decryptedtxtSz = 0;
  44916. int len = 0;
  44917. int i = 0;
  44918. EVP_CIPHER_CTX en[2];
  44919. EVP_CIPHER_CTX de[2];
  44920. printf(testingFmt, "wolfssl_EVP_aes_gcm");
  44921. for (i = 0; i < 2; i++) {
  44922. EVP_CIPHER_CTX_init(&en[i]);
  44923. if (i == 0) {
  44924. /* Default uses 96-bits IV length */
  44925. #ifdef WOLFSSL_AES_128
  44926. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
  44927. #elif defined(WOLFSSL_AES_192)
  44928. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
  44929. #elif defined(WOLFSSL_AES_256)
  44930. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
  44931. #endif
  44932. }
  44933. else {
  44934. #ifdef WOLFSSL_AES_128
  44935. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  44936. #elif defined(WOLFSSL_AES_192)
  44937. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  44938. #elif defined(WOLFSSL_AES_256)
  44939. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  44940. #endif
  44941. /* non-default must to set the IV length first */
  44942. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44943. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  44944. }
  44945. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  44946. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  44947. ciphertxtSz = len;
  44948. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  44949. ciphertxtSz += len;
  44950. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
  44951. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  44952. EVP_CIPHER_CTX_init(&de[i]);
  44953. if (i == 0) {
  44954. /* Default uses 96-bits IV length */
  44955. #ifdef WOLFSSL_AES_128
  44956. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
  44957. #elif defined(WOLFSSL_AES_192)
  44958. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
  44959. #elif defined(WOLFSSL_AES_256)
  44960. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
  44961. #endif
  44962. }
  44963. else {
  44964. #ifdef WOLFSSL_AES_128
  44965. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
  44966. #elif defined(WOLFSSL_AES_192)
  44967. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
  44968. #elif defined(WOLFSSL_AES_256)
  44969. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
  44970. #endif
  44971. /* non-default must to set the IV length first */
  44972. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  44973. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  44974. }
  44975. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  44976. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  44977. decryptedtxtSz = len;
  44978. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  44979. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  44980. decryptedtxtSz += len;
  44981. AssertIntEQ(ciphertxtSz, decryptedtxtSz);
  44982. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  44983. /* modify tag*/
  44984. tag[AES_BLOCK_SIZE-1]+=0xBB;
  44985. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  44986. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
  44987. /* fail due to wrong tag */
  44988. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  44989. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  44990. AssertIntEQ(0, len);
  44991. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  44992. }
  44993. test_wolfssl_EVP_aes_gcm_zeroLen();
  44994. printf(resultFmt, passed);
  44995. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  44996. return 0;
  44997. }
  44998. static int test_wolfssl_EVP_chacha20_poly1305(void)
  44999. {
  45000. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  45001. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  45002. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  45003. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45004. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  45005. byte cipherText[sizeof(plainText)];
  45006. byte decryptedText[sizeof(plainText)];
  45007. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  45008. EVP_CIPHER_CTX* ctx;
  45009. int outSz;
  45010. printf(testingFmt, "test_wolfssl_EVP_chacha20_poly1305");
  45011. /* Encrypt. */
  45012. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45013. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45014. NULL), WOLFSSL_SUCCESS);
  45015. /* Invalid IV length. */
  45016. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45017. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  45018. /* Valid IV length. */
  45019. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45020. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45021. /* Invalid tag length. */
  45022. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45023. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  45024. /* Valid tag length. */
  45025. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45026. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  45027. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45028. AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45029. WOLFSSL_SUCCESS);
  45030. AssertIntEQ(outSz, sizeof(aad));
  45031. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45032. sizeof(plainText)), WOLFSSL_SUCCESS);
  45033. AssertIntEQ(outSz, sizeof(plainText));
  45034. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45035. AssertIntEQ(outSz, 0);
  45036. /* Invalid tag length. */
  45037. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45038. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  45039. /* Valid tag length. */
  45040. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  45041. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45042. EVP_CIPHER_CTX_free(ctx);
  45043. /* Decrypt. */
  45044. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45045. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  45046. NULL), WOLFSSL_SUCCESS);
  45047. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  45048. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  45049. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45050. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  45051. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45052. AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  45053. WOLFSSL_SUCCESS);
  45054. AssertIntEQ(outSz, sizeof(aad));
  45055. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45056. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45057. AssertIntEQ(outSz, sizeof(cipherText));
  45058. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45059. WOLFSSL_SUCCESS);
  45060. AssertIntEQ(outSz, 0);
  45061. EVP_CIPHER_CTX_free(ctx);
  45062. /* Test partial Inits. CipherInit() allow setting of key and iv
  45063. * in separate calls. */
  45064. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45065. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  45066. key, NULL, 1), WOLFSSL_SUCCESS);
  45067. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45068. WOLFSSL_SUCCESS);
  45069. AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  45070. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  45071. AssertIntEQ(outSz, sizeof(aad));
  45072. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45073. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45074. AssertIntEQ(outSz, sizeof(cipherText));
  45075. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45076. WOLFSSL_SUCCESS);
  45077. AssertIntEQ(outSz, 0);
  45078. EVP_CIPHER_CTX_free(ctx);
  45079. printf(resultFmt, passed);
  45080. #endif
  45081. return 0;
  45082. }
  45083. static int test_wolfssl_EVP_chacha20(void)
  45084. {
  45085. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  45086. byte key[CHACHA_MAX_KEY_SZ];
  45087. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  45088. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45089. byte cipherText[sizeof(plainText)];
  45090. byte decryptedText[sizeof(plainText)];
  45091. EVP_CIPHER_CTX* ctx;
  45092. int outSz;
  45093. printf(testingFmt, "test_wolfssl_EVP_chacha20");
  45094. /* Encrypt. */
  45095. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45096. AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45097. NULL), WOLFSSL_SUCCESS);
  45098. /* Any tag length must fail - not an AEAD cipher. */
  45099. AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  45100. 16, NULL), WOLFSSL_FAILURE);
  45101. AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45102. AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  45103. sizeof(plainText)), WOLFSSL_SUCCESS);
  45104. AssertIntEQ(outSz, sizeof(plainText));
  45105. AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  45106. AssertIntEQ(outSz, 0);
  45107. EVP_CIPHER_CTX_free(ctx);
  45108. /* Decrypt. */
  45109. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45110. AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  45111. NULL), WOLFSSL_SUCCESS);
  45112. AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  45113. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45114. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45115. AssertIntEQ(outSz, sizeof(cipherText));
  45116. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45117. WOLFSSL_SUCCESS);
  45118. AssertIntEQ(outSz, 0);
  45119. EVP_CIPHER_CTX_free(ctx);
  45120. /* Test partial Inits. CipherInit() allow setting of key and iv
  45121. * in separate calls. */
  45122. AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
  45123. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  45124. key, NULL, 1), WOLFSSL_SUCCESS);
  45125. AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  45126. WOLFSSL_SUCCESS);
  45127. AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  45128. sizeof(cipherText)), WOLFSSL_SUCCESS);
  45129. AssertIntEQ(outSz, sizeof(cipherText));
  45130. AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  45131. WOLFSSL_SUCCESS);
  45132. AssertIntEQ(outSz, 0);
  45133. EVP_CIPHER_CTX_free(ctx);
  45134. printf(resultFmt, passed);
  45135. #endif
  45136. return 0;
  45137. }
  45138. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  45139. {
  45140. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  45141. EVP_PKEY_CTX* ctx;
  45142. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  45143. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  45144. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  45145. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  45146. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  45147. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  45148. byte outKey[34];
  45149. size_t outKeySz = sizeof(outKey);
  45150. /* These expected outputs were gathered by running the same test below using
  45151. * OpenSSL. */
  45152. const byte extractAndExpand[] = {
  45153. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  45154. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  45155. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  45156. };
  45157. const byte extractOnly[] = {
  45158. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  45159. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  45160. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  45161. };
  45162. const byte expandOnly[] = {
  45163. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  45164. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  45165. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  45166. };
  45167. const byte extractAndExpandAddInfo[] = {
  45168. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  45169. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  45170. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  45171. };
  45172. printf(testingFmt, "test_wolfSSL_EVP_PKEY_hkdf");
  45173. AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  45174. AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  45175. /* NULL ctx. */
  45176. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  45177. /* NULL md. */
  45178. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  45179. AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  45180. /* NULL ctx. */
  45181. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  45182. WOLFSSL_FAILURE);
  45183. /* NULL salt is ok. */
  45184. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  45185. WOLFSSL_SUCCESS);
  45186. /* Salt length <= 0. */
  45187. /* Length 0 salt is ok. */
  45188. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  45189. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  45190. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  45191. WOLFSSL_SUCCESS);
  45192. /* NULL ctx. */
  45193. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  45194. WOLFSSL_FAILURE);
  45195. /* NULL key. */
  45196. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  45197. WOLFSSL_FAILURE);
  45198. /* Key length <= 0 */
  45199. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  45200. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  45201. AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  45202. WOLFSSL_SUCCESS);
  45203. /* NULL ctx. */
  45204. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  45205. WOLFSSL_FAILURE);
  45206. /* NULL info is ok. */
  45207. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  45208. WOLFSSL_SUCCESS);
  45209. /* Info length <= 0 */
  45210. /* Length 0 info is ok. */
  45211. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  45212. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  45213. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  45214. WOLFSSL_SUCCESS);
  45215. /* NULL ctx. */
  45216. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45217. WOLFSSL_FAILURE);
  45218. /* Extract and expand (default). */
  45219. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45220. AssertIntEQ(outKeySz, sizeof(extractAndExpand));
  45221. AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  45222. /* Extract only. */
  45223. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  45224. WOLFSSL_SUCCESS);
  45225. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45226. AssertIntEQ(outKeySz, sizeof(extractOnly));
  45227. AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  45228. outKeySz = sizeof(outKey);
  45229. /* Expand only. */
  45230. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  45231. WOLFSSL_SUCCESS);
  45232. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45233. AssertIntEQ(outKeySz, sizeof(expandOnly));
  45234. AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  45235. outKeySz = sizeof(outKey);
  45236. /* Extract and expand with appended additional info. */
  45237. AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  45238. WOLFSSL_SUCCESS);
  45239. AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  45240. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  45241. AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  45242. AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  45243. AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  45244. EVP_PKEY_CTX_free(ctx);
  45245. printf(resultFmt, passed);
  45246. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  45247. return 0;
  45248. }
  45249. #ifndef NO_BIO
  45250. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  45251. {
  45252. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45253. BIO* bio;
  45254. X509_INFO* info;
  45255. STACK_OF(X509_INFO)* sk;
  45256. char* subject;
  45257. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45258. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  45259. printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
  45260. AssertNotNull(bio = BIO_new(BIO_s_file()));
  45261. AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
  45262. AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  45263. AssertIntEQ(sk_X509_INFO_num(sk), 2);
  45264. /* using dereference to maintain testing for Apache port*/
  45265. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45266. AssertNotNull(subject =
  45267. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45268. AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  45269. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45270. X509_INFO_free(info);
  45271. AssertNotNull(info = sk_X509_INFO_pop(sk));
  45272. AssertNotNull(subject =
  45273. X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
  45274. AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  45275. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  45276. X509_INFO_free(info);
  45277. AssertNull(info = sk_X509_INFO_pop(sk));
  45278. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  45279. BIO_free(bio);
  45280. printf(resultFmt, passed);
  45281. #endif
  45282. return 0;
  45283. }
  45284. #endif /* !NO_BIO */
  45285. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  45286. {
  45287. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45288. X509 *x509;
  45289. X509_NAME* name;
  45290. int idx = 0;
  45291. X509_NAME_ENTRY *ne;
  45292. ASN1_OBJECT *object = NULL;
  45293. printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
  45294. x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
  45295. AssertNotNull(x509);
  45296. name = X509_get_subject_name(x509);
  45297. idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
  45298. AssertIntGE(idx, 0);
  45299. ne = X509_NAME_get_entry(name, idx);
  45300. AssertNotNull(ne);
  45301. AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
  45302. X509_free(x509);
  45303. printf(resultFmt, passed);
  45304. #endif
  45305. return 0;
  45306. }
  45307. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  45308. {
  45309. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45310. ASN1_INTEGER *a;
  45311. long val;
  45312. int ret;
  45313. printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
  45314. a = ASN1_INTEGER_new();
  45315. val = 0;
  45316. ret = ASN1_INTEGER_set(NULL, val);
  45317. AssertIntEQ(ret, 0);
  45318. ASN1_INTEGER_free(a);
  45319. /* 0 */
  45320. a = ASN1_INTEGER_new();
  45321. val = 0;
  45322. ret = ASN1_INTEGER_set(a, val);
  45323. AssertIntEQ(ret, 1);
  45324. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45325. ASN1_INTEGER_free(a);
  45326. /* 40 */
  45327. a = ASN1_INTEGER_new();
  45328. val = 40;
  45329. ret = ASN1_INTEGER_set(a, val);
  45330. AssertIntEQ(ret, 1);
  45331. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45332. ASN1_INTEGER_free(a);
  45333. /* -40 */
  45334. a = ASN1_INTEGER_new();
  45335. val = -40;
  45336. ret = ASN1_INTEGER_set(a, val);
  45337. AssertIntEQ(ret, 1);
  45338. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45339. ASN1_INTEGER_free(a);
  45340. /* 128 */
  45341. a = ASN1_INTEGER_new();
  45342. val = 128;
  45343. ret = ASN1_INTEGER_set(a, val);
  45344. AssertIntEQ(ret, 1);
  45345. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45346. ASN1_INTEGER_free(a);
  45347. /* -128 */
  45348. a = ASN1_INTEGER_new();
  45349. val = -128;
  45350. ret = ASN1_INTEGER_set(a, val);
  45351. AssertIntEQ(ret, 1);
  45352. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45353. ASN1_INTEGER_free(a);
  45354. /* 200 */
  45355. a = ASN1_INTEGER_new();
  45356. val = 200;
  45357. ret = ASN1_INTEGER_set(a, val);
  45358. AssertIntEQ(ret, 1);
  45359. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45360. ASN1_INTEGER_free(a);
  45361. /* int max (2147483647) */
  45362. a = ASN1_INTEGER_new();
  45363. val = 2147483647;
  45364. ret = ASN1_INTEGER_set(a, val);
  45365. AssertIntEQ(ret, 1);
  45366. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45367. ASN1_INTEGER_free(a);
  45368. /* int min (-2147483648) */
  45369. a = ASN1_INTEGER_new();
  45370. val = -2147483647 - 1;
  45371. ret = ASN1_INTEGER_set(a, val);
  45372. AssertIntEQ(ret, 1);
  45373. AssertIntEQ(ASN1_INTEGER_get(a), val);
  45374. ASN1_INTEGER_free(a);
  45375. printf(resultFmt, passed);
  45376. #endif
  45377. return 0;
  45378. }
  45379. #if defined(OPENSSL_EXTRA)
  45380. typedef struct ASN1IntTestVector {
  45381. const byte* der;
  45382. const size_t derSz;
  45383. const long value;
  45384. } ASN1IntTestVector;
  45385. #endif
  45386. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  45387. {
  45388. #if defined(OPENSSL_EXTRA)
  45389. size_t i;
  45390. WOLFSSL_ASN1_INTEGER* a = NULL;
  45391. WOLFSSL_ASN1_INTEGER* b = NULL;
  45392. WOLFSSL_ASN1_INTEGER* c = NULL;
  45393. const byte* p = NULL;
  45394. byte* reEncoded = NULL;
  45395. int reEncodedSz;
  45396. static const byte zeroDer[] = {
  45397. 0x02, 0x01, 0x00
  45398. };
  45399. static const byte oneDer[] = {
  45400. 0x02, 0x01, 0x01
  45401. };
  45402. static const byte negativeDer[] = {
  45403. 0x02, 0x03, 0xC1, 0x16, 0x0D
  45404. };
  45405. static const byte positiveDer[] = {
  45406. 0x02, 0x03, 0x01, 0x00, 0x01
  45407. };
  45408. static const byte primeDer[] = {
  45409. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  45410. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  45411. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  45412. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  45413. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  45414. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  45415. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  45416. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  45417. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  45418. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  45419. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  45420. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  45421. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  45422. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  45423. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  45424. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  45425. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  45426. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  45427. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  45428. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  45429. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  45430. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  45431. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  45432. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  45433. };
  45434. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  45435. static const ASN1IntTestVector testVectors[] = {
  45436. {zeroDer, sizeof(zeroDer), 0},
  45437. {oneDer, sizeof(oneDer), 1},
  45438. {negativeDer, sizeof(negativeDer), -4123123},
  45439. {positiveDer, sizeof(positiveDer), 65537},
  45440. {primeDer, sizeof(primeDer), 0}
  45441. };
  45442. static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
  45443. printf(testingFmt, "test_wolfSSL_d2i_ASN1_INTEGER");
  45444. /* Check d2i error conditions */
  45445. /* NULL pointer to input. */
  45446. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  45447. AssertNull(b);
  45448. /* NULL input. */
  45449. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  45450. AssertNull(b);
  45451. /* 0 length. */
  45452. p = testVectors[0].der;
  45453. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  45454. AssertNull(b);
  45455. /* Negative length. */
  45456. p = testVectors[0].der;
  45457. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  45458. AssertNull(b);
  45459. /* Garbage DER input. */
  45460. p = garbageDer;
  45461. AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  45462. AssertNull(b);
  45463. {
  45464. /* Check i2d error conditions */
  45465. /* NULL input. */
  45466. byte* p2 = NULL;
  45467. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  45468. /* 0 length input data buffer (a->length == 0). */
  45469. AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  45470. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  45471. a->data = NULL;
  45472. /* NULL input data buffer. */
  45473. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  45474. /* Reset a->data. */
  45475. a->data = a->intData;
  45476. /* Set a to valid value. */
  45477. AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  45478. /* NULL output buffer. */
  45479. AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  45480. wolfSSL_ASN1_INTEGER_free(a);
  45481. }
  45482. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  45483. p = testVectors[i].der;
  45484. a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
  45485. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  45486. if (testVectors[i].derSz <= sizeof(long)) {
  45487. c = wolfSSL_ASN1_INTEGER_new();
  45488. wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
  45489. AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  45490. wolfSSL_ASN1_INTEGER_free(c);
  45491. }
  45492. /* Convert to DER without a pre-allocated output buffer. */
  45493. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  45494. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  45495. AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  45496. /* Convert to DER with a pre-allocated output buffer. In this case, the
  45497. * output buffer pointer should be incremented just past the end of the
  45498. * encoded data. */
  45499. p = reEncoded;
  45500. AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  45501. AssertIntEQ(reEncodedSz, testVectors[i].derSz);
  45502. AssertPtrEq(p, reEncoded - reEncodedSz);
  45503. AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
  45504. XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
  45505. reEncoded = NULL;
  45506. wolfSSL_ASN1_INTEGER_free(a);
  45507. }
  45508. printf(resultFmt, passed);
  45509. #endif /* OPENSSL_EXTRA */
  45510. return 0;
  45511. }
  45512. static int test_wolfSSL_X509_STORE_get1_certs(void)
  45513. {
  45514. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  45515. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45516. X509_STORE_CTX *storeCtx;
  45517. X509_STORE *store;
  45518. X509 *caX509;
  45519. X509 *svrX509;
  45520. X509_NAME *subject;
  45521. WOLF_STACK_OF(WOLFSSL_X509) *certs;
  45522. printf(testingFmt, "wolfSSL_X509_STORE_get1_certs()");
  45523. AssertNotNull(caX509 =
  45524. X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
  45525. AssertNotNull((svrX509 =
  45526. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  45527. AssertNotNull(storeCtx = X509_STORE_CTX_new());
  45528. AssertNotNull(store = X509_STORE_new());
  45529. AssertNotNull(subject = X509_get_subject_name(caX509));
  45530. /* Errors */
  45531. AssertNull(X509_STORE_get1_certs(storeCtx, subject));
  45532. AssertNull(X509_STORE_get1_certs(NULL, subject));
  45533. AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
  45534. AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  45535. AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
  45536. /* Should find the cert */
  45537. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  45538. AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
  45539. sk_X509_pop_free(certs, NULL);
  45540. /* Should not find the cert */
  45541. AssertNotNull(subject = X509_get_subject_name(svrX509));
  45542. AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  45543. AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
  45544. sk_X509_pop_free(certs, NULL);
  45545. X509_STORE_free(store);
  45546. X509_STORE_CTX_free(storeCtx);
  45547. X509_free(svrX509);
  45548. X509_free(caX509);
  45549. printf(resultFmt, passed);
  45550. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  45551. return 0;
  45552. }
  45553. /* Testing code used in dpp.c in hostap */
  45554. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45555. typedef struct {
  45556. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  45557. * as an OID identifying the curve */
  45558. X509_ALGOR *alg;
  45559. /* Compressed format public key per ANSI X9.63 */
  45560. ASN1_BIT_STRING *pub_key;
  45561. } DPP_BOOTSTRAPPING_KEY;
  45562. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  45563. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  45564. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  45565. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  45566. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  45567. #endif
  45568. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  45569. {
  45570. /* Testing code used in dpp.c in hostap */
  45571. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45572. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45573. EC_KEY *eckey;
  45574. EVP_PKEY *key;
  45575. size_t len;
  45576. unsigned char *der = NULL;
  45577. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  45578. const unsigned char *in = ecc_clikey_der_256;
  45579. const EC_GROUP *group;
  45580. const EC_POINT *point;
  45581. int nid;
  45582. AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  45583. AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  45584. (long)sizeof_ecc_clikey_der_256));
  45585. AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  45586. AssertNotNull(group = EC_KEY_get0_group(eckey));
  45587. AssertNotNull(point = EC_KEY_get0_public_key(eckey));
  45588. nid = EC_GROUP_get_curve_name(group);
  45589. AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
  45590. V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
  45591. #ifdef HAVE_COMP_KEY
  45592. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  45593. NULL, 0, NULL)), 0);
  45594. #else
  45595. AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  45596. NULL, 0, NULL)), 0);
  45597. #endif
  45598. AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  45599. #ifdef HAVE_COMP_KEY
  45600. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  45601. der, len, NULL), len);
  45602. #else
  45603. AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  45604. der, len, NULL), len);
  45605. #endif
  45606. bootstrap->pub_key->data = der;
  45607. bootstrap->pub_key->length = (int)len;
  45608. /* Not actually used */
  45609. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  45610. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  45611. der = NULL;
  45612. AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  45613. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  45614. EVP_PKEY_free(key);
  45615. EC_KEY_free(eckey);
  45616. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  45617. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  45618. #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
  45619. return 0;
  45620. }
  45621. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  45622. {
  45623. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  45624. ASN1_INTEGER *a;
  45625. unsigned char *pp,*tpp;
  45626. int ret;
  45627. printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
  45628. a = wolfSSL_ASN1_INTEGER_new();
  45629. /* 40 */
  45630. a->intData[0] = ASN_INTEGER;
  45631. a->intData[1] = 1;
  45632. a->intData[2] = 40;
  45633. ret = i2c_ASN1_INTEGER(a, NULL);
  45634. AssertIntEQ(ret, 1);
  45635. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45636. DYNAMIC_TYPE_TMP_BUFFER));
  45637. tpp = pp;
  45638. XMEMSET(pp, 0, ret + 1);
  45639. i2c_ASN1_INTEGER(a, &pp);
  45640. pp--;
  45641. AssertIntEQ(*pp, 40);
  45642. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45643. /* 128 */
  45644. a->intData[0] = ASN_INTEGER;
  45645. a->intData[1] = 1;
  45646. a->intData[2] = 128;
  45647. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45648. AssertIntEQ(ret, 2);
  45649. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45650. DYNAMIC_TYPE_TMP_BUFFER));
  45651. tpp = pp;
  45652. XMEMSET(pp, 0, ret + 1);
  45653. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45654. pp--;
  45655. AssertIntEQ(*(pp--), 128);
  45656. AssertIntEQ(*pp, 0);
  45657. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45658. /* -40 */
  45659. a->intData[0] = ASN_INTEGER;
  45660. a->intData[1] = 1;
  45661. a->intData[2] = 40;
  45662. a->negative = 1;
  45663. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45664. AssertIntEQ(ret, 1);
  45665. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45666. DYNAMIC_TYPE_TMP_BUFFER));
  45667. tpp = pp;
  45668. XMEMSET(pp, 0, ret + 1);
  45669. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45670. pp--;
  45671. AssertIntEQ(*pp, 216);
  45672. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45673. /* -128 */
  45674. a->intData[0] = ASN_INTEGER;
  45675. a->intData[1] = 1;
  45676. a->intData[2] = 128;
  45677. a->negative = 1;
  45678. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45679. AssertIntEQ(ret, 1);
  45680. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45681. DYNAMIC_TYPE_TMP_BUFFER));
  45682. tpp = pp;
  45683. XMEMSET(pp, 0, ret + 1);
  45684. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45685. pp--;
  45686. AssertIntEQ(*pp, 128);
  45687. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45688. /* -200 */
  45689. a->intData[0] = ASN_INTEGER;
  45690. a->intData[1] = 1;
  45691. a->intData[2] = 200;
  45692. a->negative = 1;
  45693. ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
  45694. AssertIntEQ(ret, 2);
  45695. AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  45696. DYNAMIC_TYPE_TMP_BUFFER));
  45697. tpp = pp;
  45698. XMEMSET(pp, 0, ret + 1);
  45699. wolfSSL_i2c_ASN1_INTEGER(a, &pp);
  45700. pp--;
  45701. AssertIntEQ(*(pp--), 56);
  45702. AssertIntEQ(*pp, 255);
  45703. XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45704. wolfSSL_ASN1_INTEGER_free(a);
  45705. printf(resultFmt, passed);
  45706. #endif /* OPENSSL_EXTRA && !NO_ASN */
  45707. return 0;
  45708. }
  45709. #ifndef NO_INLINE
  45710. #define WOLFSSL_MISC_INCLUDED
  45711. #include <wolfcrypt/src/misc.c>
  45712. #else
  45713. #include <wolfssl/wolfcrypt/misc.h>
  45714. #endif
  45715. static int test_ForceZero(void)
  45716. {
  45717. unsigned char data[32];
  45718. unsigned int i, j, len;
  45719. /* Test case with 0 length */
  45720. ForceZero(data, 0);
  45721. /* Test ForceZero */
  45722. for (i = 0; i < sizeof(data); i++) {
  45723. for (len = 1; len < sizeof(data) - i; len++) {
  45724. for (j = 0; j < sizeof(data); j++)
  45725. data[j] = j + 1;
  45726. ForceZero(data + i, len);
  45727. for (j = 0; j < sizeof(data); j++) {
  45728. if (j < i || j >= i + len) {
  45729. if (data[j] == 0x00)
  45730. return -10200;
  45731. }
  45732. else if (data[j] != 0x00)
  45733. return -10201;
  45734. }
  45735. }
  45736. }
  45737. return 0;
  45738. }
  45739. #ifndef NO_BIO
  45740. static int test_wolfSSL_X509_print(void)
  45741. {
  45742. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  45743. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
  45744. X509 *x509;
  45745. BIO *bio;
  45746. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  45747. const X509_ALGOR *cert_sig_alg;
  45748. #endif
  45749. printf(testingFmt, "wolfSSL_X509_print");
  45750. x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
  45751. AssertNotNull(x509);
  45752. /* print to memory */
  45753. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45754. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  45755. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  45756. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  45757. /* Will print IP address subject alt name. */
  45758. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  45759. #else
  45760. /* Will print IP address subject alt name. */
  45761. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  45762. #endif
  45763. #else
  45764. AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  45765. #endif
  45766. BIO_free(bio);
  45767. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  45768. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  45769. /* Print signature */
  45770. AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  45771. AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  45772. #endif
  45773. /* print to stdout */
  45774. #if !defined(NO_WOLFSSL_DIR)
  45775. AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  45776. #endif
  45777. /* print again */
  45778. AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
  45779. X509_free(x509);
  45780. BIO_free(bio);
  45781. printf(resultFmt, passed);
  45782. #endif
  45783. return 0;
  45784. }
  45785. static int test_wolfSSL_X509_CRL_print(void)
  45786. {
  45787. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  45788. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  45789. X509_CRL* crl;
  45790. BIO *bio;
  45791. XFILE fp;
  45792. printf(testingFmt, "test_X509_CRL_print");
  45793. fp = XFOPEN("./certs/crl/crl.pem", "rb");
  45794. AssertTrue((fp != XBADFILE));
  45795. AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  45796. NULL, NULL));
  45797. XFCLOSE(fp);
  45798. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  45799. AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  45800. X509_CRL_free(crl);
  45801. BIO_free(bio);
  45802. printf(resultFmt, passed);
  45803. #endif
  45804. return 0;
  45805. }
  45806. static int test_wolfSSL_BIO_get_len(void)
  45807. {
  45808. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  45809. BIO *bio = NULL;
  45810. const char txt[] = "Some example text to push to the BIO.";
  45811. printf(testingFmt, "wolfSSL_BIO_get_len");
  45812. AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  45813. AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  45814. AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  45815. AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  45816. BIO_free(bio);
  45817. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  45818. AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  45819. BIO_free(bio);
  45820. printf(resultFmt, passed);
  45821. #endif
  45822. return 0;
  45823. }
  45824. static int test_wolfSSL_ASN1_STRING_print(void){
  45825. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
  45826. ASN1_STRING* asnStr = NULL;
  45827. const char HELLO_DATA[]= \
  45828. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  45829. #define MAX_UNPRINTABLE_CHAR 32
  45830. #define MAX_BUF 255
  45831. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  45832. unsigned char expected[sizeof(unprintableData)+1];
  45833. unsigned char rbuf[MAX_BUF];
  45834. BIO *bio;
  45835. int p_len, i;
  45836. printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
  45837. /* setup */
  45838. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  45839. unprintableData[i] = HELLO_DATA[i];
  45840. expected[i] = HELLO_DATA[i];
  45841. }
  45842. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  45843. unprintableData[sizeof(HELLO_DATA)+i] = i;
  45844. if (i == (int)'\n' || i == (int)'\r')
  45845. expected[sizeof(HELLO_DATA)+i] = i;
  45846. else
  45847. expected[sizeof(HELLO_DATA)+i] = '.';
  45848. }
  45849. unprintableData[sizeof(unprintableData)-1] = '\0';
  45850. expected[sizeof(expected)-1] = '\0';
  45851. XMEMSET(rbuf, 0, MAX_BUF);
  45852. bio = BIO_new(BIO_s_mem());
  45853. BIO_set_write_buf_size(bio, MAX_BUF);
  45854. asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
  45855. ASN1_STRING_set(asnStr,(const void*)unprintableData,
  45856. (int)sizeof(unprintableData));
  45857. /* test */
  45858. p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
  45859. AssertIntEQ(p_len, 46);
  45860. BIO_read(bio, (void*)rbuf, 46);
  45861. AssertStrEQ((char*)rbuf, (const char*)expected);
  45862. BIO_free(bio);
  45863. ASN1_STRING_free(asnStr);
  45864. printf(resultFmt, passed);
  45865. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
  45866. return 0;
  45867. }
  45868. #endif /* !NO_BIO */
  45869. static int test_wolfSSL_ASN1_get_object(void)
  45870. {
  45871. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45872. const unsigned char* derBuf = cliecc_cert_der_256;
  45873. int len = sizeof_cliecc_cert_der_256;
  45874. long asnLen = 0;
  45875. int tag = 0, cls = 0;
  45876. ASN1_OBJECT *a;
  45877. printf(testingFmt, "wolfSSL_ASN1_get_object()");
  45878. /* Read a couple TLV triplets and make sure they match the expected values */
  45879. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  45880. AssertIntEQ(asnLen, 862);
  45881. AssertIntEQ(tag, 0x10);
  45882. AssertIntEQ(cls, 0);
  45883. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45884. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45885. AssertIntEQ(asnLen, 772);
  45886. AssertIntEQ(tag, 0x10);
  45887. AssertIntEQ(cls, 0);
  45888. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45889. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45890. AssertIntEQ(asnLen, 3);
  45891. AssertIntEQ(tag, 0);
  45892. AssertIntEQ(cls, 0x80);
  45893. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45894. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45895. AssertIntEQ(asnLen, 1);
  45896. AssertIntEQ(tag, 0x2);
  45897. AssertIntEQ(cls, 0);
  45898. derBuf += asnLen;
  45899. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45900. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45901. AssertIntEQ(asnLen, 20);
  45902. AssertIntEQ(tag, 0x2);
  45903. AssertIntEQ(cls, 0);
  45904. derBuf += asnLen;
  45905. AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  45906. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  45907. AssertIntEQ(asnLen, 10);
  45908. AssertIntEQ(tag, 0x10);
  45909. AssertIntEQ(cls, 0);
  45910. /* Read an ASN OBJECT */
  45911. AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  45912. ASN1_OBJECT_free(a);
  45913. printf(resultFmt, passed);
  45914. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  45915. return 0;
  45916. }
  45917. static int test_wolfSSL_RSA(void)
  45918. {
  45919. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
  45920. defined(WOLFSSL_KEY_GEN)
  45921. RSA* rsa;
  45922. const BIGNUM *n;
  45923. const BIGNUM *e;
  45924. const BIGNUM *d;
  45925. const BIGNUM *p;
  45926. const BIGNUM *q;
  45927. const BIGNUM *dmp1;
  45928. const BIGNUM *dmq1;
  45929. const BIGNUM *iqmp;
  45930. printf(testingFmt, "wolfSSL_RSA()");
  45931. AssertNotNull(rsa = RSA_new());
  45932. AssertIntEQ(RSA_size(NULL), 0);
  45933. AssertIntEQ(RSA_size(rsa), 0);
  45934. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  45935. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  45936. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  45937. #ifdef WOLFSSL_RSA_KEY_CHECK
  45938. AssertIntEQ(RSA_check_key(rsa), 0);
  45939. #endif
  45940. RSA_free(rsa);
  45941. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  45942. AssertIntEQ(RSA_size(rsa), 256);
  45943. #ifdef WOLFSSL_RSA_KEY_CHECK
  45944. AssertIntEQ(RSA_check_key(NULL), 0);
  45945. AssertIntEQ(RSA_check_key(rsa), 1);
  45946. #endif
  45947. /* sanity check */
  45948. AssertIntEQ(RSA_bits(NULL), 0);
  45949. /* key */
  45950. AssertIntEQ(RSA_bits(rsa), 2048);
  45951. RSA_get0_key(rsa, &n, &e, &d);
  45952. AssertPtrEq(rsa->n, n);
  45953. AssertPtrEq(rsa->e, e);
  45954. AssertPtrEq(rsa->d, d);
  45955. AssertNotNull(n = BN_new());
  45956. AssertNotNull(e = BN_new());
  45957. AssertNotNull(d = BN_new());
  45958. AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  45959. AssertPtrEq(rsa->n, n);
  45960. AssertPtrEq(rsa->e, e);
  45961. AssertPtrEq(rsa->d, d);
  45962. AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  45963. AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  45964. /* crt_params */
  45965. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  45966. AssertPtrEq(rsa->dmp1, dmp1);
  45967. AssertPtrEq(rsa->dmq1, dmq1);
  45968. AssertPtrEq(rsa->iqmp, iqmp);
  45969. AssertNotNull(dmp1 = BN_new());
  45970. AssertNotNull(dmq1 = BN_new());
  45971. AssertNotNull(iqmp = BN_new());
  45972. AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  45973. (BIGNUM*)iqmp), 1);
  45974. AssertPtrEq(rsa->dmp1, dmp1);
  45975. AssertPtrEq(rsa->dmq1, dmq1);
  45976. AssertPtrEq(rsa->iqmp, iqmp);
  45977. AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  45978. AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  45979. (BIGNUM*)iqmp), 0);
  45980. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  45981. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  45982. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  45983. AssertNull(dmp1);
  45984. AssertNull(dmq1);
  45985. AssertNull(iqmp);
  45986. /* factors */
  45987. RSA_get0_factors(rsa, NULL, NULL);
  45988. RSA_get0_factors(rsa, &p, &q);
  45989. AssertPtrEq(rsa->p, p);
  45990. AssertPtrEq(rsa->q, q);
  45991. AssertNotNull(p = BN_new());
  45992. AssertNotNull(q = BN_new());
  45993. AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  45994. AssertPtrEq(rsa->p, p);
  45995. AssertPtrEq(rsa->q, q);
  45996. AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  45997. AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  45998. RSA_get0_factors(NULL, NULL, NULL);
  45999. RSA_get0_factors(NULL, &p, &q);
  46000. AssertNull(p);
  46001. AssertNull(q);
  46002. AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  46003. AssertIntEQ(RSA_bits(rsa), 21);
  46004. RSA_free(rsa);
  46005. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  46006. AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  46007. AssertIntEQ(RSA_size(rsa), 384);
  46008. AssertIntEQ(RSA_bits(rsa), 3072);
  46009. RSA_free(rsa);
  46010. #endif
  46011. /* remove for now with odd key size until adjusting rsa key size check with
  46012. wc_MakeRsaKey()
  46013. AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  46014. RSA_free(rsa);
  46015. */
  46016. AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
  46017. AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  46018. AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  46019. AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
  46020. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  46021. {
  46022. byte buff[FOURK_BUF];
  46023. byte der[FOURK_BUF];
  46024. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  46025. XFILE f;
  46026. int bytes;
  46027. /* test loading encrypted RSA private pem w/o password */
  46028. f = XFOPEN(PrivKeyPemFile, "rb");
  46029. AssertTrue((f != XBADFILE));
  46030. bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
  46031. XFCLOSE(f);
  46032. XMEMSET(der, 0, sizeof(der));
  46033. /* test that error value is returned with no password */
  46034. AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
  46035. }
  46036. #endif
  46037. printf(resultFmt, passed);
  46038. #endif
  46039. return 0;
  46040. }
  46041. static int test_wolfSSL_RSA_DER(void)
  46042. {
  46043. #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46044. !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
  46045. RSA *rsa;
  46046. int i;
  46047. const unsigned char *buff = NULL;
  46048. unsigned char *newBuff = NULL;
  46049. struct tbl_s
  46050. {
  46051. const unsigned char *der;
  46052. int sz;
  46053. } tbl[] = {
  46054. #ifdef USE_CERT_BUFFERS_1024
  46055. {client_key_der_1024, sizeof_client_key_der_1024},
  46056. {server_key_der_1024, sizeof_server_key_der_1024},
  46057. #endif
  46058. #ifdef USE_CERT_BUFFERS_2048
  46059. {client_key_der_2048, sizeof_client_key_der_2048},
  46060. {server_key_der_2048, sizeof_server_key_der_2048},
  46061. #endif
  46062. {NULL, 0}
  46063. };
  46064. /* Public Key DER */
  46065. struct tbl_s pub[] = {
  46066. #ifdef USE_CERT_BUFFERS_1024
  46067. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  46068. #endif
  46069. #ifdef USE_CERT_BUFFERS_2048
  46070. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  46071. #endif
  46072. {NULL, 0}
  46073. };
  46074. printf(testingFmt, "test_wolfSSL_RSA_DER()");
  46075. AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  46076. buff = pub[0].der;
  46077. AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  46078. AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  46079. buff = tbl[0].der;
  46080. AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  46081. AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  46082. rsa = RSA_new();
  46083. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  46084. RSA_free(rsa);
  46085. for (i = 0; tbl[i].der != NULL; i++)
  46086. {
  46087. /* Passing in pointer results in pointer moving. */
  46088. buff = tbl[i].der;
  46089. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  46090. AssertNotNull(rsa);
  46091. RSA_free(rsa);
  46092. }
  46093. for (i = 0; tbl[i].der != NULL; i++)
  46094. {
  46095. /* Passing in pointer results in pointer moving. */
  46096. buff = tbl[i].der;
  46097. AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  46098. AssertNotNull(rsa);
  46099. RSA_free(rsa);
  46100. }
  46101. for (i = 0; pub[i].der != NULL; i++)
  46102. {
  46103. buff = pub[i].der;
  46104. AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  46105. AssertNotNull(rsa);
  46106. AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  46107. newBuff = NULL;
  46108. AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  46109. AssertNotNull(newBuff);
  46110. AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  46111. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46112. RSA_free(rsa);
  46113. }
  46114. printf(resultFmt, passed);
  46115. #endif
  46116. return 0;
  46117. }
  46118. static int test_wolfSSL_RSA_print(void)
  46119. {
  46120. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  46121. !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46122. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  46123. BIO *bio;
  46124. WOLFSSL_RSA* rsa = NULL;
  46125. printf(testingFmt, "wolfSSL_RSA_print");
  46126. AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
  46127. AssertNotNull(rsa = RSA_new());
  46128. AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
  46129. AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  46130. AssertIntEQ(RSA_print(bio, NULL, 0), -1);
  46131. AssertIntEQ(RSA_print_fp(stdout, NULL, 0), 0);
  46132. /* Some very large number of indent spaces. */
  46133. AssertIntEQ(RSA_print(bio, rsa, 128), -1);
  46134. /* RSA is empty. */
  46135. AssertIntEQ(RSA_print(bio, rsa, 0), 0);
  46136. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 0);
  46137. RSA_free(rsa);
  46138. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46139. AssertIntEQ(RSA_print(bio, rsa, 0), 1);
  46140. AssertIntEQ(RSA_print(bio, rsa, 4), 1);
  46141. AssertIntEQ(RSA_print(bio, rsa, -1), 1);
  46142. AssertIntEQ(RSA_print_fp(stdout, rsa, 0), 1);
  46143. AssertIntEQ(RSA_print_fp(stdout, rsa, 4), 1);
  46144. AssertIntEQ(RSA_print_fp(stdout, rsa, -1), 1);
  46145. BIO_free(bio);
  46146. RSA_free(rsa);
  46147. printf(resultFmt, passed);
  46148. #endif
  46149. return 0;
  46150. }
  46151. #ifndef NO_RSA
  46152. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  46153. {
  46154. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46155. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46156. RSA *rsa;
  46157. const unsigned char *derBuf = client_key_der_2048;
  46158. unsigned char em[256] = {0}; /* len = 2048/8 */
  46159. /* Random data simulating a hash */
  46160. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  46161. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46162. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46163. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46164. };
  46165. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46166. AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  46167. RSA_PSS_SALTLEN_DIGEST), 0);
  46168. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  46169. RSA_PSS_SALTLEN_DIGEST), 0);
  46170. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  46171. RSA_PSS_SALTLEN_DIGEST), 0);
  46172. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  46173. RSA_PSS_SALTLEN_DIGEST), 0);
  46174. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  46175. AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  46176. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46177. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  46178. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46179. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  46180. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46181. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  46182. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46183. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46184. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  46185. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  46186. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46187. RSA_PSS_SALTLEN_DIGEST), 1);
  46188. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46189. RSA_PSS_SALTLEN_DIGEST), 1);
  46190. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46191. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46192. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46193. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  46194. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  46195. RSA_PSS_SALTLEN_MAX), 1);
  46196. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  46197. RSA_PSS_SALTLEN_MAX), 1);
  46198. AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  46199. AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  46200. RSA_free(rsa);
  46201. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  46202. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46203. return 0;
  46204. }
  46205. #endif
  46206. static int test_wolfSSL_RSA_sign_sha3(void)
  46207. {
  46208. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  46209. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  46210. RSA *rsa;
  46211. const unsigned char *derBuf = client_key_der_2048;
  46212. unsigned char sigRet[256] = {0};
  46213. unsigned int sigLen = sizeof(sigRet);
  46214. /* Random data simulating a hash */
  46215. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  46216. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  46217. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  46218. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  46219. };
  46220. printf(testingFmt, "wolfSSL_RSA_sign_sha3");
  46221. AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  46222. AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
  46223. &sigLen, rsa), 1);
  46224. RSA_free(rsa);
  46225. printf(resultFmt, passed);
  46226. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  46227. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  46228. return 0;
  46229. }
  46230. static int test_wolfSSL_RSA_get0_key(void)
  46231. {
  46232. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
  46233. RSA *rsa = NULL;
  46234. const BIGNUM* n = NULL;
  46235. const BIGNUM* e = NULL;
  46236. const BIGNUM* d = NULL;
  46237. const unsigned char* der;
  46238. int derSz;
  46239. #ifdef USE_CERT_BUFFERS_1024
  46240. der = client_key_der_1024;
  46241. derSz = sizeof_client_key_der_1024;
  46242. #elif defined(USE_CERT_BUFFERS_2048)
  46243. der = client_key_der_2048;
  46244. derSz = sizeof_client_key_der_2048;
  46245. #else
  46246. der = NULL;
  46247. derSz = 0;
  46248. #endif
  46249. printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
  46250. if (der != NULL) {
  46251. RSA_get0_key(NULL, NULL, NULL, NULL);
  46252. RSA_get0_key(rsa, NULL, NULL, NULL);
  46253. RSA_get0_key(NULL, &n, &e, &d);
  46254. AssertNull(n);
  46255. AssertNull(e);
  46256. AssertNull(d);
  46257. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  46258. AssertNotNull(rsa);
  46259. RSA_get0_key(rsa, NULL, NULL, NULL);
  46260. RSA_get0_key(rsa, &n, NULL, NULL);
  46261. AssertNotNull(n);
  46262. RSA_get0_key(rsa, NULL, &e, NULL);
  46263. AssertNotNull(e);
  46264. RSA_get0_key(rsa, NULL, NULL, &d);
  46265. AssertNotNull(d);
  46266. RSA_get0_key(rsa, &n, &e, &d);
  46267. AssertNotNull(n);
  46268. AssertNotNull(e);
  46269. AssertNotNull(d);
  46270. RSA_free(rsa);
  46271. }
  46272. printf(resultFmt, passed);
  46273. #endif
  46274. return 0;
  46275. }
  46276. static int test_wolfSSL_RSA_meth(void)
  46277. {
  46278. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46279. RSA *rsa;
  46280. RSA_METHOD *rsa_meth;
  46281. printf(testingFmt, "test_wolfSSL_RSA_meth");
  46282. #ifdef WOLFSSL_KEY_GEN
  46283. AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46284. RSA_free(rsa);
  46285. #else
  46286. AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46287. #endif
  46288. AssertNotNull(RSA_get_default_method());
  46289. wolfSSL_RSA_meth_free(NULL);
  46290. AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
  46291. AssertNotNull(rsa_meth =
  46292. RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
  46293. #ifndef NO_WOLFSSL_STUB
  46294. AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  46295. AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  46296. AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  46297. AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  46298. AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  46299. AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  46300. AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  46301. #endif
  46302. AssertIntEQ(RSA_flags(NULL), 0);
  46303. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46304. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  46305. AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  46306. AssertNotNull(rsa = RSA_new());
  46307. /* No method set. */
  46308. AssertIntEQ(RSA_flags(rsa), 0);
  46309. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46310. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46311. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46312. AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  46313. AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  46314. AssertNull(RSA_get_method(NULL));
  46315. AssertPtrEq(RSA_get_method(rsa), rsa_meth);
  46316. AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  46317. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46318. AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46319. AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  46320. RSA_METHOD_FLAG_NO_CHECK);
  46321. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  46322. AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  46323. AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  46324. /* rsa_meth is freed here */
  46325. RSA_free(rsa);
  46326. printf(resultFmt, passed);
  46327. #endif
  46328. return 0;
  46329. }
  46330. static int test_wolfSSL_RSA_verify(void)
  46331. {
  46332. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
  46333. !defined(NO_FILESYSTEM)
  46334. #ifndef NO_BIO
  46335. XFILE fp;
  46336. RSA *pKey, *pubKey;
  46337. X509 *cert;
  46338. const char *text = "Hello wolfSSL !";
  46339. unsigned char hash[SHA256_DIGEST_LENGTH];
  46340. unsigned char signature[2048/8];
  46341. unsigned int signatureLength;
  46342. byte *buf;
  46343. BIO *bio;
  46344. SHA256_CTX c;
  46345. EVP_PKEY *evpPkey, *evpPubkey;
  46346. size_t sz;
  46347. printf(testingFmt, "wolfSSL_RSA_verify");
  46348. /* generate hash */
  46349. SHA256_Init(&c);
  46350. SHA256_Update(&c, text, strlen(text));
  46351. SHA256_Final(hash, &c);
  46352. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46353. /* workaround for small stack cache case */
  46354. wc_Sha256Free((wc_Sha256*)&c);
  46355. #endif
  46356. /* read privete key file */
  46357. fp = XFOPEN(svrKeyFile, "rb");
  46358. AssertTrue((fp != XBADFILE));
  46359. AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
  46360. sz = XFTELL(fp);
  46361. XREWIND(fp);
  46362. AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  46363. AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
  46364. XFCLOSE(fp);
  46365. /* read private key and sign hash data */
  46366. AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  46367. AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  46368. AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  46369. AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  46370. signature, &signatureLength, pKey), SSL_SUCCESS);
  46371. /* read public key and verify signed data */
  46372. fp = XFOPEN(svrCertFile,"rb");
  46373. AssertTrue((fp != XBADFILE));
  46374. cert = PEM_read_X509(fp, 0, 0, 0 );
  46375. XFCLOSE(fp);
  46376. evpPubkey = X509_get_pubkey(cert);
  46377. pubKey = EVP_PKEY_get1_RSA(evpPubkey);
  46378. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  46379. signatureLength, pubKey), SSL_SUCCESS);
  46380. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  46381. signatureLength, NULL), SSL_FAILURE);
  46382. AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  46383. signatureLength, pubKey), SSL_FAILURE);
  46384. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  46385. signatureLength, pubKey), SSL_FAILURE);
  46386. AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  46387. signatureLength, NULL), SSL_FAILURE);
  46388. RSA_free(pKey);
  46389. EVP_PKEY_free(evpPkey);
  46390. RSA_free(pubKey);
  46391. EVP_PKEY_free(evpPubkey);
  46392. X509_free(cert);
  46393. BIO_free(bio);
  46394. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  46395. printf(resultFmt, passed);
  46396. #endif
  46397. #endif
  46398. return 0;
  46399. }
  46400. static int test_wolfSSL_RSA_sign(void)
  46401. {
  46402. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46403. RSA *rsa;
  46404. unsigned char hash[SHA256_DIGEST_LENGTH];
  46405. #ifdef USE_CERT_BUFFERS_1024
  46406. const unsigned char* privDer = client_key_der_1024;
  46407. size_t privDerSz = sizeof_client_key_der_1024;
  46408. const unsigned char* pubDer = client_keypub_der_1024;
  46409. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46410. unsigned char signature[1024/8];
  46411. #else
  46412. const unsigned char* privDer = client_key_der_2048;
  46413. size_t privDerSz = sizeof_client_key_der_2048;
  46414. const unsigned char* pubDer = client_keypub_der_2048;
  46415. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46416. unsigned char signature[2048/8];
  46417. #endif
  46418. unsigned int signatureLen;
  46419. const unsigned char* der;
  46420. printf(testingFmt, "wolfSSL_RSA_sign");
  46421. XMEMSET(hash, 0, sizeof(hash));
  46422. der = privDer;
  46423. rsa = NULL;
  46424. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46425. AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  46426. AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  46427. &signatureLen, rsa), 0);
  46428. AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  46429. &signatureLen, rsa), 0);
  46430. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  46431. &signatureLen, rsa), 0);
  46432. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46433. NULL, rsa), 0);
  46434. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46435. &signatureLen, NULL), 0);
  46436. AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  46437. &signatureLen, rsa), 1);
  46438. RSA_free(rsa);
  46439. der = pubDer;
  46440. rsa = NULL;
  46441. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46442. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  46443. signatureLen, rsa), 1);
  46444. RSA_free(rsa);
  46445. printf(resultFmt, passed);
  46446. #endif
  46447. return 0;
  46448. }
  46449. static int test_wolfSSL_RSA_sign_ex(void)
  46450. {
  46451. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46452. RSA *rsa;
  46453. unsigned char hash[SHA256_DIGEST_LENGTH];
  46454. #ifdef USE_CERT_BUFFERS_1024
  46455. const unsigned char* privDer = client_key_der_1024;
  46456. size_t privDerSz = sizeof_client_key_der_1024;
  46457. const unsigned char* pubDer = client_keypub_der_1024;
  46458. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46459. unsigned char signature[1024/8];
  46460. #else
  46461. const unsigned char* privDer = client_key_der_2048;
  46462. size_t privDerSz = sizeof_client_key_der_2048;
  46463. const unsigned char* pubDer = client_keypub_der_2048;
  46464. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46465. unsigned char signature[2048/8];
  46466. #endif
  46467. unsigned int signatureLen;
  46468. const unsigned char* der;
  46469. unsigned char encodedHash[51];
  46470. unsigned int encodedHashLen;
  46471. const unsigned char expEncHash[] = {
  46472. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  46473. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  46474. 0x00, 0x04, 0x20,
  46475. /* Hash data */
  46476. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46477. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46478. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46479. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  46480. };
  46481. printf(testingFmt, "wolfSSL_RSA_sign_ex");
  46482. XMEMSET(hash, 0, sizeof(hash));
  46483. AssertNotNull(rsa = wolfSSL_RSA_new());
  46484. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46485. &signatureLen, rsa, 1), 0);
  46486. wolfSSL_RSA_free(rsa);
  46487. der = privDer;
  46488. rsa = NULL;
  46489. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46490. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  46491. -1), 0);
  46492. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  46493. signature, &signatureLen, rsa, 1), 0);
  46494. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  46495. &signatureLen, rsa, 1), 0);
  46496. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  46497. &signatureLen, rsa, 1), 0);
  46498. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46499. NULL, rsa, 1), 0);
  46500. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46501. &signatureLen, NULL, 1), 0);
  46502. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46503. &signatureLen, rsa, -1), 0);
  46504. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  46505. &signatureLen, rsa, 0), 0);
  46506. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  46507. &signatureLen, rsa, 0), 0);
  46508. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46509. NULL, rsa, 0), 0);
  46510. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  46511. &signatureLen, rsa, 1), 1);
  46512. /* Test returning encoded hash. */
  46513. AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  46514. &encodedHashLen, rsa, 0), 1);
  46515. AssertIntEQ(encodedHashLen, sizeof(expEncHash));
  46516. AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  46517. RSA_free(rsa);
  46518. der = pubDer;
  46519. rsa = NULL;
  46520. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46521. AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  46522. signatureLen, rsa), 1);
  46523. RSA_free(rsa);
  46524. printf(resultFmt, passed);
  46525. #endif
  46526. return 0;
  46527. }
  46528. static int test_wolfSSL_RSA_public_decrypt(void)
  46529. {
  46530. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46531. RSA *rsa;
  46532. unsigned char msg[SHA256_DIGEST_LENGTH];
  46533. #ifdef USE_CERT_BUFFERS_1024
  46534. const unsigned char* pubDer = client_keypub_der_1024;
  46535. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46536. unsigned char decMsg[1024/8];
  46537. const unsigned char encMsg[] = {
  46538. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  46539. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  46540. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  46541. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  46542. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  46543. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  46544. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  46545. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  46546. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  46547. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  46548. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  46549. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  46550. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  46551. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  46552. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  46553. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  46554. };
  46555. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46556. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46557. defined(WC_RSA_NO_PADDING)
  46558. const unsigned char encMsgNoPad[] = {
  46559. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  46560. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  46561. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  46562. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  46563. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  46564. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  46565. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  46566. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  46567. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  46568. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  46569. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  46570. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  46571. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  46572. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  46573. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  46574. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  46575. };
  46576. #endif
  46577. #else
  46578. const unsigned char* pubDer = client_keypub_der_2048;
  46579. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46580. unsigned char decMsg[2048/8];
  46581. const unsigned char encMsg[] = {
  46582. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  46583. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  46584. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  46585. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  46586. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  46587. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  46588. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  46589. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  46590. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  46591. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  46592. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  46593. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  46594. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  46595. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  46596. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  46597. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  46598. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  46599. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  46600. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  46601. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  46602. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  46603. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  46604. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  46605. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  46606. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  46607. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  46608. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  46609. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  46610. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  46611. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  46612. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  46613. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  46614. };
  46615. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46616. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46617. defined(WC_RSA_NO_PADDING)
  46618. const unsigned char encMsgNoPad[] = {
  46619. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  46620. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  46621. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  46622. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  46623. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  46624. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  46625. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  46626. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  46627. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  46628. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  46629. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  46630. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  46631. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  46632. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  46633. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  46634. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  46635. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  46636. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  46637. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  46638. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  46639. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  46640. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  46641. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  46642. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  46643. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  46644. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  46645. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  46646. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  46647. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  46648. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  46649. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  46650. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  46651. };
  46652. #endif
  46653. #endif
  46654. const unsigned char* der;
  46655. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46656. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46657. defined(WC_RSA_NO_PADDING)
  46658. int i;
  46659. #endif
  46660. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  46661. XMEMSET(msg, 0, sizeof(msg));
  46662. der = pubDer;
  46663. rsa = NULL;
  46664. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46665. AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  46666. AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  46667. RSA_PKCS1_PADDING), -1);
  46668. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  46669. RSA_PKCS1_PADDING), -1);
  46670. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  46671. RSA_PKCS1_PADDING), -1);
  46672. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  46673. RSA_PKCS1_PADDING), -1);
  46674. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  46675. RSA_PKCS1_PSS_PADDING), -1);
  46676. AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  46677. RSA_PKCS1_PADDING), 32);
  46678. AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  46679. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  46680. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  46681. defined(WC_RSA_NO_PADDING)
  46682. AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  46683. rsa, RSA_NO_PADDING), sizeof(decMsg));
  46684. /* Zeros before actual data. */
  46685. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  46686. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  46687. }
  46688. /* Check actual data. */
  46689. XMEMSET(msg, 0x01, sizeof(msg));
  46690. AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  46691. #endif
  46692. RSA_free(rsa);
  46693. printf(resultFmt, passed);
  46694. #endif
  46695. return 0;
  46696. }
  46697. static int test_wolfSSL_RSA_private_encrypt(void)
  46698. {
  46699. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46700. RSA *rsa;
  46701. unsigned char msg[SHA256_DIGEST_LENGTH];
  46702. #ifdef USE_CERT_BUFFERS_1024
  46703. const unsigned char* privDer = client_key_der_1024;
  46704. size_t privDerSz = sizeof_client_key_der_1024;
  46705. unsigned char encMsg[1024/8];
  46706. const unsigned char expEncMsg[] = {
  46707. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  46708. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  46709. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  46710. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  46711. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  46712. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  46713. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  46714. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  46715. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  46716. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  46717. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  46718. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  46719. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  46720. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  46721. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  46722. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  46723. };
  46724. #ifdef WC_RSA_NO_PADDING
  46725. const unsigned char expEncMsgNoPad[] = {
  46726. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  46727. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  46728. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  46729. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  46730. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  46731. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  46732. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  46733. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  46734. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  46735. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  46736. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  46737. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  46738. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  46739. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  46740. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  46741. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  46742. };
  46743. #endif
  46744. #else
  46745. const unsigned char* privDer = client_key_der_2048;
  46746. size_t privDerSz = sizeof_client_key_der_2048;
  46747. unsigned char encMsg[2048/8];
  46748. const unsigned char expEncMsg[] = {
  46749. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  46750. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  46751. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  46752. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  46753. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  46754. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  46755. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  46756. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  46757. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  46758. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  46759. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  46760. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  46761. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  46762. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  46763. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  46764. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  46765. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  46766. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  46767. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  46768. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  46769. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  46770. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  46771. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  46772. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  46773. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  46774. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  46775. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  46776. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  46777. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  46778. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  46779. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  46780. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  46781. };
  46782. #ifdef WC_RSA_NO_PADDING
  46783. const unsigned char expEncMsgNoPad[] = {
  46784. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  46785. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  46786. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  46787. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  46788. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  46789. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  46790. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  46791. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  46792. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  46793. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  46794. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  46795. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  46796. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  46797. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  46798. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  46799. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  46800. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  46801. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  46802. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  46803. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  46804. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  46805. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  46806. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  46807. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  46808. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  46809. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  46810. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  46811. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  46812. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  46813. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  46814. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  46815. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  46816. };
  46817. #endif
  46818. #endif
  46819. const unsigned char* der;
  46820. printf(testingFmt, "wolfSSL_RSA_private_encrypt");
  46821. XMEMSET(msg, 0x00, sizeof(msg));
  46822. der = privDer;
  46823. rsa = NULL;
  46824. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46825. AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  46826. AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  46827. -1);
  46828. AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  46829. RSA_PKCS1_PADDING), -1);
  46830. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  46831. RSA_PKCS1_PADDING), -1);
  46832. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  46833. RSA_PKCS1_PADDING), -1);
  46834. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46835. RSA_PKCS1_PSS_PADDING), -1);
  46836. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46837. RSA_PKCS1_PADDING), sizeof(encMsg));
  46838. AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  46839. #ifdef WC_RSA_NO_PADDING
  46840. /* Non-zero message. */
  46841. XMEMSET(msg, 0x01, sizeof(msg));
  46842. AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  46843. RSA_NO_PADDING), sizeof(encMsg));
  46844. AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  46845. #endif
  46846. RSA_free(rsa);
  46847. printf(resultFmt, passed);
  46848. #endif
  46849. return 0;
  46850. }
  46851. static int test_wolfSSL_RSA_public_encrypt(void)
  46852. {
  46853. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46854. RSA* rsa;
  46855. const unsigned char msg[2048/8] = { 0 };
  46856. unsigned char encMsg[2048/8];
  46857. printf(testingFmt, "wolfSSL_RSA_public_decrypt");
  46858. AssertNotNull(rsa = RSA_new());
  46859. AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  46860. RSA_PKCS1_PADDING), -1);
  46861. AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  46862. RSA_PKCS1_PADDING), -1);
  46863. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  46864. RSA_PKCS1_PADDING), -1);
  46865. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  46866. RSA_PKCS1_PADDING), -1);
  46867. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  46868. RSA_PKCS1_PSS_PADDING), -1);
  46869. /* Empty RSA key. */
  46870. AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  46871. RSA_PKCS1_PADDING), -1);
  46872. RSA_free(rsa);
  46873. printf(resultFmt, passed);
  46874. #endif
  46875. return 0;
  46876. }
  46877. static int test_wolfSSL_RSA_private_decrypt(void)
  46878. {
  46879. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
  46880. RSA* rsa;
  46881. unsigned char msg[2048/8];
  46882. const unsigned char encMsg[2048/8] = { 0 };
  46883. printf(testingFmt, "wolfSSL_RSA_private_decrypt");
  46884. AssertNotNull(rsa = RSA_new());
  46885. AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  46886. RSA_PKCS1_PADDING), -1);
  46887. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  46888. RSA_PKCS1_PADDING), -1);
  46889. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  46890. RSA_PKCS1_PADDING), -1);
  46891. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  46892. RSA_PKCS1_PADDING), -1);
  46893. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  46894. RSA_PKCS1_PSS_PADDING), -1);
  46895. /* Empty RSA key. */
  46896. AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  46897. RSA_PKCS1_PADDING), -1);
  46898. RSA_free(rsa);
  46899. printf(resultFmt, passed);
  46900. #endif
  46901. return 0;
  46902. }
  46903. static int test_wolfSSL_RSA_GenAdd(void)
  46904. {
  46905. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46906. RSA *rsa;
  46907. #ifdef USE_CERT_BUFFERS_1024
  46908. const unsigned char* privDer = client_key_der_1024;
  46909. size_t privDerSz = sizeof_client_key_der_1024;
  46910. const unsigned char* pubDer = client_keypub_der_1024;
  46911. size_t pubDerSz = sizeof_client_keypub_der_1024;
  46912. #else
  46913. const unsigned char* privDer = client_key_der_2048;
  46914. size_t privDerSz = sizeof_client_key_der_2048;
  46915. const unsigned char* pubDer = client_keypub_der_2048;
  46916. size_t pubDerSz = sizeof_client_keypub_der_2048;
  46917. #endif
  46918. const unsigned char* der;
  46919. printf(testingFmt, "wolfSSL_RSA_GenAdd");
  46920. der = privDer;
  46921. rsa = NULL;
  46922. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46923. AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  46924. #ifndef RSA_LOW_MEM
  46925. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  46926. #else
  46927. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  46928. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  46929. #endif
  46930. RSA_free(rsa);
  46931. der = pubDer;
  46932. rsa = NULL;
  46933. AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  46934. /* Need private values. */
  46935. AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  46936. RSA_free(rsa);
  46937. printf(resultFmt, passed);
  46938. #endif
  46939. return 0;
  46940. }
  46941. static int test_wolfSSL_RSA_blinding_on(void)
  46942. {
  46943. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  46944. RSA *rsa;
  46945. WOLFSSL_BN_CTX *bnCtx;
  46946. #ifdef USE_CERT_BUFFERS_1024
  46947. const unsigned char* privDer = client_key_der_1024;
  46948. size_t privDerSz = sizeof_client_key_der_1024;
  46949. #else
  46950. const unsigned char* privDer = client_key_der_2048;
  46951. size_t privDerSz = sizeof_client_key_der_2048;
  46952. #endif
  46953. const unsigned char* der;
  46954. printf(testingFmt, "wolfSSL_RSA_blinding_on");
  46955. der = privDer;
  46956. rsa = NULL;
  46957. AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  46958. AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
  46959. /* Does nothing so all parameters are valid. */
  46960. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  46961. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  46962. AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  46963. AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  46964. wolfSSL_BN_CTX_free(bnCtx);
  46965. RSA_free(rsa);
  46966. printf(resultFmt, passed);
  46967. #endif
  46968. return 0;
  46969. }
  46970. static int test_wolfSSL_RSA_ex_data(void)
  46971. {
  46972. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  46973. RSA* rsa;
  46974. unsigned char data[1];
  46975. printf(testingFmt, "wolfSSL_RSA_ex_data");
  46976. rsa = RSA_new();
  46977. AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  46978. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  46979. #ifdef MAX_EX_DATA
  46980. AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  46981. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  46982. #endif
  46983. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  46984. AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  46985. #ifdef HAVE_EX_DATA
  46986. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  46987. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  46988. AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  46989. #else
  46990. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  46991. AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  46992. AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  46993. #endif
  46994. RSA_free(rsa);
  46995. printf(resultFmt, passed);
  46996. #endif /* !NO_RSA && OPENSSL_EXTRA */
  46997. return 0;
  46998. }
  46999. static int test_wolfSSL_RSA_LoadDer(void)
  47000. {
  47001. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  47002. defined(OPENSSL_EXTRA_X509_SMALL))
  47003. RSA *rsa;
  47004. #ifdef USE_CERT_BUFFERS_1024
  47005. const unsigned char* privDer = client_key_der_1024;
  47006. size_t privDerSz = sizeof_client_key_der_1024;
  47007. #else
  47008. const unsigned char* privDer = client_key_der_2048;
  47009. size_t privDerSz = sizeof_client_key_der_2048;
  47010. #endif
  47011. printf(testingFmt, "wolfSSL_RSA_LoadDer");
  47012. AssertNotNull(rsa = RSA_new());
  47013. AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  47014. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  47015. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  47016. AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  47017. RSA_free(rsa);
  47018. printf(resultFmt, passed);
  47019. #endif
  47020. return 0;
  47021. }
  47022. /* Local API. */
  47023. static int test_wolfSSL_RSA_To_Der(void)
  47024. {
  47025. #ifdef WOLFSSL_TEST_STATIC_BUILD
  47026. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  47027. defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47028. RSA* rsa;
  47029. #ifdef USE_CERT_BUFFERS_1024
  47030. const unsigned char* privDer = client_key_der_1024;
  47031. size_t privDerSz = sizeof_client_key_der_1024;
  47032. const unsigned char* pubDer = client_keypub_der_1024;
  47033. size_t pubDerSz = sizeof_client_keypub_der_1024;
  47034. unsigned char out[sizeof(client_key_der_1024)];
  47035. #else
  47036. const unsigned char* privDer = client_key_der_2048;
  47037. size_t privDerSz = sizeof_client_key_der_2048;
  47038. const unsigned char* pubDer = client_keypub_der_2048;
  47039. size_t pubDerSz = sizeof_client_keypub_der_2048;
  47040. unsigned char out[sizeof(client_key_der_2048)];
  47041. #endif
  47042. const unsigned char* der;
  47043. unsigned char* outDer = NULL;
  47044. printf(testingFmt, "wolfSSL_RSA_To_Der");
  47045. der = privDer;
  47046. rsa = NULL;
  47047. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47048. AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47049. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  47050. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  47051. outDer = out;
  47052. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47053. AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  47054. outDer = NULL;
  47055. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  47056. AssertNotNull(outDer);
  47057. AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  47058. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47059. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  47060. outDer = out;
  47061. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  47062. AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  47063. RSA_free(rsa);
  47064. AssertNotNull(rsa = RSA_new());
  47065. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47066. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  47067. RSA_free(rsa);
  47068. der = pubDer;
  47069. rsa = NULL;
  47070. AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  47071. AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  47072. RSA_free(rsa);
  47073. printf(resultFmt, passed);
  47074. #endif
  47075. #endif
  47076. return 0;
  47077. }
  47078. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  47079. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  47080. {
  47081. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  47082. XFILE file;
  47083. const char* fname = "./certs/server-keyPub.pem";
  47084. RSA *rsa;
  47085. printf(testingFmt, "wolfSSL_PEM_read_RSAPublicKey");
  47086. AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  47087. file = XFOPEN(fname, "rb");
  47088. AssertTrue((file != XBADFILE));
  47089. AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  47090. AssertIntEQ(RSA_size(rsa), 256);
  47091. RSA_free(rsa);
  47092. XFCLOSE(file);
  47093. printf(resultFmt, passed);
  47094. #endif
  47095. return 0;
  47096. }
  47097. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  47098. {
  47099. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47100. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47101. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  47102. RSA* rsa;
  47103. #ifdef USE_CERT_BUFFERS_1024
  47104. const unsigned char* privDer = client_key_der_1024;
  47105. size_t privDerSz = sizeof_client_key_der_1024;
  47106. #else
  47107. const unsigned char* privDer = client_key_der_2048;
  47108. size_t privDerSz = sizeof_client_key_der_2048;
  47109. #endif
  47110. const unsigned char* der;
  47111. #ifndef NO_AES
  47112. unsigned char passwd[] = "password";
  47113. #endif
  47114. printf(testingFmt, "wolfSSL_PEM_write_RSAPrivateKey");
  47115. AssertNotNull(rsa = RSA_new());
  47116. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  47117. NULL, NULL), 0);
  47118. RSA_free(rsa);
  47119. der = privDer;
  47120. rsa = NULL;
  47121. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47122. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  47123. NULL, NULL), 0);
  47124. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, NULL, NULL, NULL, 0,
  47125. NULL, NULL), 0);
  47126. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
  47127. NULL, NULL), 1);
  47128. #ifndef NO_AES
  47129. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  47130. NULL, 0, NULL, NULL), 1);
  47131. AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
  47132. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  47133. #endif
  47134. RSA_free(rsa);
  47135. printf(resultFmt, passed);
  47136. #endif
  47137. return 0;
  47138. }
  47139. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  47140. {
  47141. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  47142. !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
  47143. defined(WOLFSSL_DER_TO_PEM))
  47144. RSA* rsa;
  47145. #ifdef USE_CERT_BUFFERS_1024
  47146. const unsigned char* privDer = client_key_der_1024;
  47147. size_t privDerSz = sizeof_client_key_der_1024;
  47148. #else
  47149. const unsigned char* privDer = client_key_der_2048;
  47150. size_t privDerSz = sizeof_client_key_der_2048;
  47151. #endif
  47152. const unsigned char* der;
  47153. #ifndef NO_AES
  47154. unsigned char passwd[] = "password";
  47155. #endif
  47156. unsigned char* pem;
  47157. int plen;
  47158. printf(testingFmt, "wolfSSL_PEM_write_mem_RSAPrivateKey");
  47159. AssertNotNull(rsa = RSA_new());
  47160. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47161. &plen), 0);
  47162. RSA_free(rsa);
  47163. der = privDer;
  47164. rsa = NULL;
  47165. AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  47166. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  47167. &plen), 0);
  47168. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  47169. &plen), 0);
  47170. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47171. NULL), 0);
  47172. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  47173. &plen), 1);
  47174. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47175. #ifndef NO_AES
  47176. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47177. NULL, 0, &pem, &plen), 1);
  47178. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47179. AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  47180. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  47181. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  47182. #endif
  47183. RSA_free(rsa);
  47184. printf(resultFmt, passed);
  47185. #endif
  47186. return 0;
  47187. }
  47188. static int test_wolfSSL_DH(void)
  47189. {
  47190. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47191. DH *dh = NULL;
  47192. BIGNUM* p;
  47193. BIGNUM* q;
  47194. BIGNUM* g;
  47195. BIGNUM* pub;
  47196. BIGNUM* priv;
  47197. #if defined(OPENSSL_ALL)
  47198. #if !defined(HAVE_FIPS) || \
  47199. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  47200. FILE* f = NULL;
  47201. unsigned char buf[268];
  47202. const unsigned char* pt = buf;
  47203. long len = 0;
  47204. dh = NULL;
  47205. XMEMSET(buf, 0, sizeof(buf));
  47206. /* Test 2048 bit parameters */
  47207. f = XFOPEN("./certs/dh2048.der", "rb");
  47208. AssertTrue(f != XBADFILE);
  47209. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47210. XFCLOSE(f);
  47211. AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
  47212. AssertNotNull(dh->p);
  47213. AssertNotNull(dh->g);
  47214. AssertTrue(pt == buf);
  47215. AssertIntEQ(DH_generate_key(dh), 1);
  47216. AssertIntEQ(DH_generate_key(dh), 1);
  47217. AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  47218. AssertNotNull(pub = BN_new());
  47219. AssertIntEQ(BN_set_word(pub, 1), 1);
  47220. AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  47221. AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  47222. AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  47223. AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
  47224. AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
  47225. AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
  47226. AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
  47227. BN_free(pub);
  47228. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47229. (const BIGNUM**)&q,
  47230. (const BIGNUM**)&g);
  47231. AssertPtrEq(p, dh->p);
  47232. AssertPtrEq(q, dh->q);
  47233. AssertPtrEq(g, dh->g);
  47234. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47235. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  47236. AssertPtrEq(pub, dh->pub_key);
  47237. AssertPtrEq(priv, dh->priv_key);
  47238. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  47239. AssertPtrEq(pub, dh->pub_key);
  47240. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  47241. AssertPtrEq(priv, dh->priv_key);
  47242. AssertNotNull(pub = BN_new());
  47243. AssertNotNull(priv = BN_new());
  47244. AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
  47245. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47246. AssertNotNull(pub = BN_new());
  47247. AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
  47248. AssertNotNull(priv = BN_new());
  47249. AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
  47250. AssertPtrEq(pub, dh->pub_key);
  47251. AssertPtrEq(priv, dh->priv_key);
  47252. DH_free(dh);
  47253. AssertNotNull(dh = DH_new());
  47254. AssertNotNull(p = BN_new());
  47255. AssertIntEQ(BN_set_word(p, 1), 1);
  47256. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47257. AssertNotNull(pub = BN_new());
  47258. AssertNotNull(priv = BN_new());
  47259. AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
  47260. AssertIntEQ(DH_compute_key(buf, p, dh), -1);
  47261. BN_free(p);
  47262. DH_free(dh);
  47263. #ifdef WOLFSSL_KEY_GEN
  47264. AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  47265. AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  47266. #endif
  47267. DH_free(dh);
  47268. #endif
  47269. #endif
  47270. (void)dh;
  47271. (void)p;
  47272. (void)q;
  47273. (void)g;
  47274. (void)pub;
  47275. (void)priv;
  47276. printf(testingFmt, "test_wolfSSL_DH");
  47277. dh = wolfSSL_DH_new();
  47278. AssertNotNull(dh);
  47279. /* invalid parameters test */
  47280. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  47281. (const BIGNUM**)&q,
  47282. (const BIGNUM**)&g);
  47283. DH_get0_pqg(dh, NULL,
  47284. (const BIGNUM**)&q,
  47285. (const BIGNUM**)&g);
  47286. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  47287. DH_get0_pqg(dh, NULL, NULL, NULL);
  47288. AssertTrue(1);
  47289. DH_get0_pqg(dh, (const BIGNUM**)&p,
  47290. (const BIGNUM**)&q,
  47291. (const BIGNUM**)&g);
  47292. AssertPtrEq(p, NULL);
  47293. AssertPtrEq(q, NULL);
  47294. AssertPtrEq(g, NULL);
  47295. DH_free(dh);
  47296. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  47297. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  47298. #if defined(OPENSSL_ALL) || \
  47299. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47300. dh = wolfSSL_DH_new();
  47301. AssertNotNull(dh);
  47302. p = wolfSSL_BN_new();
  47303. AssertNotNull(p);
  47304. AssertIntEQ(BN_set_word(p, 11), 1);
  47305. g = wolfSSL_BN_new();
  47306. AssertNotNull(g);
  47307. AssertIntEQ(BN_set_word(g, 2), 1);
  47308. q = wolfSSL_BN_new();
  47309. AssertNotNull(q);
  47310. AssertIntEQ(BN_set_word(q, 5), 1);
  47311. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  47312. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  47313. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  47314. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  47315. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  47316. AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  47317. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  47318. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  47319. /* Don't need q. */
  47320. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47321. /* Setting again will free the p and g. */
  47322. wolfSSL_BN_free(q);
  47323. DH_free(dh);
  47324. dh = wolfSSL_DH_new();
  47325. AssertNotNull(dh);
  47326. p = wolfSSL_BN_new();
  47327. AssertNotNull(p);
  47328. AssertIntEQ(BN_set_word(p, 11), 1);
  47329. g = wolfSSL_BN_new();
  47330. AssertNotNull(g);
  47331. AssertIntEQ(BN_set_word(g, 2), 1);
  47332. q = wolfSSL_BN_new();
  47333. AssertNotNull(q);
  47334. AssertIntEQ(BN_set_word(q, 5), 1);
  47335. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  47336. /* p, q and g are now owned by dh - don't free. */
  47337. p = wolfSSL_BN_new();
  47338. AssertNotNull(p);
  47339. AssertIntEQ(BN_set_word(p, 11), 1);
  47340. g = wolfSSL_BN_new();
  47341. AssertNotNull(g);
  47342. AssertIntEQ(BN_set_word(g, 2), 1);
  47343. q = wolfSSL_BN_new();
  47344. AssertNotNull(q);
  47345. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  47346. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  47347. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  47348. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  47349. /* p, q and g are now owned by dh - don't free. */
  47350. DH_free(dh);
  47351. AssertIntEQ(DH_generate_key(NULL), 0);
  47352. AssertNotNull(dh = DH_new());
  47353. AssertIntEQ(DH_generate_key(dh), 0);
  47354. p = wolfSSL_BN_new();
  47355. AssertNotNull(p);
  47356. AssertIntEQ(BN_set_word(p, 0), 1);
  47357. g = wolfSSL_BN_new();
  47358. AssertNotNull(g);
  47359. AssertIntEQ(BN_set_word(g, 2), 1);
  47360. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47361. AssertIntEQ(DH_generate_key(dh), 0);
  47362. DH_free(dh);
  47363. #endif
  47364. #endif
  47365. /* Test DH_up_ref() */
  47366. dh = wolfSSL_DH_new();
  47367. AssertNotNull(dh);
  47368. AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  47369. AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  47370. DH_free(dh); /* decrease ref count */
  47371. DH_free(dh); /* free WOLFSSL_DH */
  47372. AssertNull((dh = DH_new_by_nid(NID_sha1)));
  47373. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  47374. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  47375. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  47376. #ifdef HAVE_FFDHE_2048
  47377. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47378. DH_free(dh);
  47379. #endif
  47380. #ifdef HAVE_FFDHE_3072
  47381. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  47382. DH_free(dh);
  47383. #endif
  47384. #ifdef HAVE_FFDHE_4096
  47385. AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  47386. DH_free(dh);
  47387. #endif
  47388. #else
  47389. AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  47390. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  47391. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  47392. AssertIntEQ(wolfSSL_DH_size(NULL), -1);
  47393. printf(resultFmt, passed);
  47394. #endif /* OPENSSL_EXTRA && !NO_DH */
  47395. return 0;
  47396. }
  47397. static int test_wolfSSL_DH_dup(void)
  47398. {
  47399. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  47400. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  47401. defined(OPENSSL_EXTRA)
  47402. DH *dh;
  47403. DH *dhDup;
  47404. WOLFSSL_BIGNUM* p;
  47405. WOLFSSL_BIGNUM* g;
  47406. AssertNotNull(p = wolfSSL_BN_new());
  47407. AssertNotNull(g = wolfSSL_BN_new());
  47408. AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  47409. AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  47410. dhDup = wolfSSL_DH_dup(NULL);
  47411. AssertNull(dhDup);
  47412. dh = wolfSSL_DH_new();
  47413. AssertNotNull(dh);
  47414. dhDup = wolfSSL_DH_dup(dh);
  47415. AssertNull(dhDup);
  47416. #if defined(OPENSSL_ALL) || \
  47417. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  47418. AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  47419. dhDup = wolfSSL_DH_dup(dh);
  47420. AssertNotNull(dhDup);
  47421. wolfSSL_DH_free(dhDup);
  47422. #endif
  47423. wolfSSL_DH_free(dh);
  47424. printf(resultFmt, passed);
  47425. #endif
  47426. #endif
  47427. return 0;
  47428. }
  47429. static int test_wolfSSL_DH_check(void)
  47430. {
  47431. #ifdef OPENSSL_ALL
  47432. #ifndef NO_DH
  47433. #ifndef NO_BIO
  47434. #ifndef NO_DSA
  47435. byte buf[6000];
  47436. char file[] = "./certs/dsaparams.pem";
  47437. XFILE f;
  47438. int bytes;
  47439. BIO* bio;
  47440. DSA* dsa;
  47441. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  47442. static const byte dh2048[] = {
  47443. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  47444. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  47445. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  47446. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  47447. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  47448. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  47449. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  47450. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  47451. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  47452. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  47453. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  47454. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  47455. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  47456. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  47457. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  47458. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  47459. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  47460. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  47461. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  47462. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  47463. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  47464. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  47465. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  47466. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  47467. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  47468. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  47469. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  47470. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  47471. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  47472. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  47473. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  47474. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  47475. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  47476. 0x93, 0x02, 0x01, 0x02
  47477. };
  47478. const byte* params;
  47479. #endif
  47480. DH* dh = NULL;
  47481. WOLFSSL_BIGNUM* p;
  47482. WOLFSSL_BIGNUM* g;
  47483. WOLFSSL_BIGNUM* pTmp = NULL;
  47484. WOLFSSL_BIGNUM* gTmp = NULL;
  47485. int codes = -1;
  47486. printf(testingFmt, "wolfSSL_DH_check");
  47487. #ifndef NO_DSA
  47488. /* Initialize DH */
  47489. f = XFOPEN(file, "rb");
  47490. AssertTrue((f != XBADFILE));
  47491. bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
  47492. XFCLOSE(f);
  47493. bio = BIO_new_mem_buf((void*)buf, bytes);
  47494. AssertNotNull(bio);
  47495. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  47496. AssertNotNull(dsa);
  47497. dh = wolfSSL_DSA_dup_DH(dsa);
  47498. AssertNotNull(dh);
  47499. BIO_free(bio);
  47500. DSA_free(dsa);
  47501. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  47502. params = dh2048;
  47503. dh = wolfSSL_d2i_DHparams(NULL, &params, (long)sizeof(dh2048));
  47504. AssertNotNull(dh);
  47505. #else
  47506. dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
  47507. AssertNotNull(dh);
  47508. #endif
  47509. /* Test assumed to be valid dh.
  47510. * Should return WOLFSSL_SUCCESS
  47511. * codes should be 0
  47512. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  47513. */
  47514. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47515. AssertIntEQ(codes, 0);
  47516. /* Test NULL dh: expected BAD_FUNC_ARG */
  47517. AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  47518. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  47519. pTmp = dh->p;
  47520. dh->p = NULL;
  47521. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47522. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47523. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  47524. /* set dh->p back to normal so it wont fail on next tests */
  47525. dh->p = pTmp;
  47526. pTmp = NULL;
  47527. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  47528. gTmp = dh->g;
  47529. dh->g = NULL;
  47530. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47531. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47532. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  47533. dh->g = gTmp;
  47534. gTmp = NULL;
  47535. /* Cleanup */
  47536. DH_free(dh);
  47537. dh = DH_new();
  47538. AssertNotNull(dh);
  47539. /* Check empty DH. */
  47540. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47541. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47542. AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  47543. /* Check non-prime valued p. */
  47544. AssertNotNull(p = BN_new());
  47545. AssertIntEQ(BN_set_word(p, 4), 1);
  47546. AssertNotNull(g = BN_new());
  47547. AssertIntEQ(BN_set_word(g, 2), 1);
  47548. AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  47549. AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  47550. AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  47551. AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  47552. DH_free(dh);
  47553. printf(resultFmt, passed);
  47554. #endif
  47555. #endif /* !NO_DH && !NO_DSA */
  47556. #endif
  47557. return 0;
  47558. }
  47559. static int test_wolfSSL_DH_prime(void)
  47560. {
  47561. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47562. WOLFSSL_BIGNUM* bn;
  47563. #if WOLFSSL_MAX_BN_BITS >= 768
  47564. WOLFSSL_BIGNUM* bn2;
  47565. #endif
  47566. bn = wolfSSL_DH_768_prime(NULL);
  47567. #if WOLFSSL_MAX_BN_BITS >= 768
  47568. AssertNotNull(bn);
  47569. bn2 = wolfSSL_DH_768_prime(bn);
  47570. AssertNotNull(bn2);
  47571. AssertTrue(bn == bn2);
  47572. wolfSSL_BN_free(bn);
  47573. #else
  47574. AssertNull(bn);
  47575. #endif
  47576. bn = wolfSSL_DH_1024_prime(NULL);
  47577. #if WOLFSSL_MAX_BN_BITS >= 1024
  47578. AssertNotNull(bn);
  47579. wolfSSL_BN_free(bn);
  47580. #else
  47581. AssertNull(bn);
  47582. #endif
  47583. bn = wolfSSL_DH_2048_prime(NULL);
  47584. #if WOLFSSL_MAX_BN_BITS >= 2048
  47585. AssertNotNull(bn);
  47586. wolfSSL_BN_free(bn);
  47587. #else
  47588. AssertNull(bn);
  47589. #endif
  47590. bn = wolfSSL_DH_3072_prime(NULL);
  47591. #if WOLFSSL_MAX_BN_BITS >= 3072
  47592. AssertNotNull(bn);
  47593. wolfSSL_BN_free(bn);
  47594. #else
  47595. AssertNull(bn);
  47596. #endif
  47597. bn = wolfSSL_DH_4096_prime(NULL);
  47598. #if WOLFSSL_MAX_BN_BITS >= 4096
  47599. AssertNotNull(bn);
  47600. wolfSSL_BN_free(bn);
  47601. #else
  47602. AssertNull(bn);
  47603. #endif
  47604. bn = wolfSSL_DH_6144_prime(NULL);
  47605. #if WOLFSSL_MAX_BN_BITS >= 6144
  47606. AssertNotNull(bn);
  47607. wolfSSL_BN_free(bn);
  47608. #else
  47609. AssertNull(bn);
  47610. #endif
  47611. bn = wolfSSL_DH_8192_prime(NULL);
  47612. #if WOLFSSL_MAX_BN_BITS >= 8192
  47613. AssertNotNull(bn);
  47614. wolfSSL_BN_free(bn);
  47615. #else
  47616. AssertNull(bn);
  47617. #endif
  47618. printf(resultFmt, passed);
  47619. #endif
  47620. return 0;
  47621. }
  47622. static int test_wolfSSL_DH_1536_prime(void)
  47623. {
  47624. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47625. BIGNUM* bn;
  47626. unsigned char bits[200];
  47627. int sz = 192; /* known binary size */
  47628. const byte expected[] = {
  47629. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47630. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  47631. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  47632. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  47633. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  47634. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  47635. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  47636. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  47637. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  47638. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  47639. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  47640. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  47641. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  47642. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  47643. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  47644. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  47645. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  47646. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  47647. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  47648. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  47649. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  47650. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  47651. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  47652. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47653. };
  47654. printf(testingFmt, "wolfSSL_DH_1536_prime()");
  47655. bn = get_rfc3526_prime_1536(NULL);
  47656. AssertNotNull(bn);
  47657. AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  47658. AssertIntEQ(0, XMEMCMP(expected, bits, sz));
  47659. BN_free(bn);
  47660. printf(resultFmt, passed);
  47661. #endif
  47662. return 0;
  47663. }
  47664. static int test_wolfSSL_DH_get_2048_256(void)
  47665. {
  47666. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  47667. WOLFSSL_DH* dh;
  47668. const WOLFSSL_BIGNUM* pBn;
  47669. const WOLFSSL_BIGNUM* gBn;
  47670. const WOLFSSL_BIGNUM* qBn;
  47671. const byte pExpected[] = {
  47672. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  47673. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  47674. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  47675. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  47676. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  47677. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  47678. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  47679. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  47680. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  47681. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  47682. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  47683. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  47684. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  47685. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  47686. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  47687. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  47688. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  47689. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  47690. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  47691. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  47692. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  47693. 0x1E, 0x1A, 0x15, 0x97
  47694. };
  47695. const byte gExpected[] = {
  47696. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  47697. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  47698. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  47699. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  47700. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  47701. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  47702. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  47703. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  47704. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  47705. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  47706. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  47707. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  47708. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  47709. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  47710. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  47711. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  47712. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  47713. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  47714. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  47715. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  47716. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  47717. 0x6C, 0xC4, 0x16, 0x59
  47718. };
  47719. const byte qExpected[] = {
  47720. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  47721. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  47722. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  47723. };
  47724. int pSz;
  47725. int qSz;
  47726. int gSz;
  47727. byte* pReturned;
  47728. byte* qReturned;
  47729. byte* gReturned;
  47730. printf(testingFmt, "wolfSSL_DH_get_2048_256()");
  47731. AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
  47732. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  47733. AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  47734. AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47735. AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  47736. AssertIntEQ(pSz, sizeof(pExpected));
  47737. AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  47738. AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  47739. AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47740. AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  47741. AssertIntEQ(qSz, sizeof(qExpected));
  47742. AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  47743. AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  47744. AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  47745. AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  47746. AssertIntEQ(gSz, sizeof(gExpected));
  47747. AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  47748. wolfSSL_DH_free(dh);
  47749. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47750. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47751. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47752. printf(resultFmt, passed);
  47753. #endif
  47754. return 0;
  47755. }
  47756. static int test_wolfSSL_PEM_write_DHparams(void)
  47757. {
  47758. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  47759. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  47760. DH* dh;
  47761. BIO* bio;
  47762. XFILE fp;
  47763. byte pem[2048];
  47764. int pemSz;
  47765. const char expected[] =
  47766. "-----BEGIN DH PARAMETERS-----\n"
  47767. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  47768. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  47769. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  47770. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  47771. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  47772. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  47773. "-----END DH PARAMETERS-----\n";
  47774. const char badPem[] =
  47775. "-----BEGIN DH PARAMETERS-----\n"
  47776. "-----END DH PARAMETERS-----\n";
  47777. const char emptySeqPem[] =
  47778. "-----BEGIN DH PARAMETERS-----\n"
  47779. "MAA=\n"
  47780. "-----END DH PARAMETERS-----\n";
  47781. printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
  47782. AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
  47783. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  47784. XFCLOSE(fp);
  47785. AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  47786. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47787. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47788. AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  47789. (int)sizeof(badPem));
  47790. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47791. BIO_free(bio);
  47792. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47793. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47794. AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  47795. (int)sizeof(emptySeqPem));
  47796. AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47797. BIO_free(bio);
  47798. AssertNotNull(bio = BIO_new(BIO_s_mem()));
  47799. AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  47800. AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  47801. BIO_free(bio);
  47802. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  47803. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  47804. AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  47805. DH_free(dh);
  47806. dh = wolfSSL_DH_new();
  47807. AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  47808. XFCLOSE(fp);
  47809. wolfSSL_DH_free(dh);
  47810. /* check results */
  47811. XMEMSET(pem, 0, sizeof(pem));
  47812. AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
  47813. AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  47814. AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  47815. XFCLOSE(fp);
  47816. printf(resultFmt, passed);
  47817. #endif
  47818. return 0;
  47819. }
  47820. static int test_wolfSSL_d2i_DHparams(void)
  47821. {
  47822. #ifdef OPENSSL_ALL
  47823. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  47824. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47825. FILE* f = NULL;
  47826. unsigned char buf[4096];
  47827. const unsigned char* pt = buf;
  47828. #ifdef HAVE_FFDHE_2048
  47829. const char* params1 = "./certs/dh2048.der";
  47830. #endif
  47831. #ifdef HAVE_FFDHE_3072
  47832. const char* params2 = "./certs/dh3072.der";
  47833. #endif
  47834. long len = 0;
  47835. WOLFSSL_DH* dh = NULL;
  47836. XMEMSET(buf, 0, sizeof(buf));
  47837. /* Test 2048 bit parameters */
  47838. #ifdef HAVE_FFDHE_2048
  47839. printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
  47840. f = XFOPEN(params1, "rb");
  47841. AssertTrue(f != XBADFILE);
  47842. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47843. XFCLOSE(f);
  47844. /* Valid case */
  47845. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  47846. AssertNotNull(dh->p);
  47847. AssertNotNull(dh->g);
  47848. AssertTrue(pt == buf);
  47849. AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  47850. AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  47851. AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  47852. /* Invalid cases */
  47853. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  47854. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  47855. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  47856. DH_free(dh);
  47857. printf(resultFmt, passed);
  47858. *buf = 0;
  47859. pt = buf;
  47860. #endif /* HAVE_FFDHE_2048 */
  47861. /* Test 3072 bit parameters */
  47862. #ifdef HAVE_FFDHE_3072
  47863. printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
  47864. f = XFOPEN(params2, "rb");
  47865. AssertTrue(f != XBADFILE);
  47866. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47867. XFCLOSE(f);
  47868. /* Valid case */
  47869. AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  47870. AssertNotNull(dh->p);
  47871. AssertNotNull(dh->g);
  47872. AssertTrue(pt != buf);
  47873. AssertIntEQ(DH_generate_key(dh), 1);
  47874. /* Invalid cases */
  47875. AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  47876. AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  47877. DH_free(dh);
  47878. #endif /* HAVE_FFDHE_3072 */
  47879. printf(resultFmt, passed);
  47880. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  47881. #endif /* !NO_DH */
  47882. #endif
  47883. return 0;
  47884. }
  47885. static int test_wolfSSL_DH_LoadDer(void)
  47886. {
  47887. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  47888. defined(OPENSSL_EXTRA)
  47889. static const byte dh2048[] = {
  47890. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  47891. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  47892. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  47893. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  47894. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  47895. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  47896. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  47897. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  47898. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  47899. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  47900. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  47901. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  47902. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  47903. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  47904. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  47905. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  47906. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  47907. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  47908. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  47909. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  47910. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  47911. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  47912. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  47913. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  47914. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  47915. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  47916. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  47917. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  47918. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  47919. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  47920. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  47921. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  47922. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  47923. 0x93, 0x02, 0x01, 0x02
  47924. };
  47925. WOLFSSL_DH* dh;
  47926. dh = wolfSSL_DH_new();
  47927. AssertNotNull(dh);
  47928. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  47929. AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  47930. AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  47931. AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  47932. wolfSSL_DH_free(dh);
  47933. printf(resultFmt, passed);
  47934. #endif
  47935. return 0;
  47936. }
  47937. static int test_wolfSSL_i2d_DHparams(void)
  47938. {
  47939. #ifdef OPENSSL_ALL
  47940. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  47941. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47942. FILE* f;
  47943. unsigned char buf[4096];
  47944. const unsigned char* pt;
  47945. unsigned char* pt2;
  47946. #ifdef HAVE_FFDHE_2048
  47947. const char* params1 = "./certs/dh2048.der";
  47948. #endif
  47949. #ifdef HAVE_FFDHE_3072
  47950. const char* params2 = "./certs/dh3072.der";
  47951. #endif
  47952. long len;
  47953. WOLFSSL_DH* dh;
  47954. /* Test 2048 bit parameters */
  47955. #ifdef HAVE_FFDHE_2048
  47956. pt = buf;
  47957. pt2 = buf;
  47958. printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
  47959. f = XFOPEN(params1, "rb");
  47960. AssertTrue(f != XBADFILE);
  47961. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47962. XFCLOSE(f);
  47963. /* Valid case */
  47964. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  47965. AssertTrue(pt == buf);
  47966. AssertIntEQ(DH_generate_key(dh), 1);
  47967. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  47968. /* Invalid case */
  47969. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  47970. /* Return length only */
  47971. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  47972. DH_free(dh);
  47973. printf(resultFmt, passed);
  47974. *buf = 0;
  47975. #endif
  47976. /* Test 3072 bit parameters */
  47977. #ifdef HAVE_FFDHE_3072
  47978. pt = buf;
  47979. pt2 = buf;
  47980. printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
  47981. f = XFOPEN(params2, "rb");
  47982. AssertTrue(f != XBADFILE);
  47983. len = (long)XFREAD(buf, 1, sizeof(buf), f);
  47984. XFCLOSE(f);
  47985. /* Valid case */
  47986. AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  47987. AssertTrue(pt == buf);
  47988. AssertIntEQ(DH_generate_key(dh), 1);
  47989. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  47990. /* Invalid case */
  47991. AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  47992. /* Return length only */
  47993. AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  47994. DH_free(dh);
  47995. #endif
  47996. dh = DH_new();
  47997. AssertNotNull(dh);
  47998. pt2 = buf;
  47999. AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  48000. DH_free(dh);
  48001. printf(resultFmt, passed);
  48002. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  48003. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  48004. #endif
  48005. return 0;
  48006. }
  48007. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  48008. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48009. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
  48010. {
  48011. X509* x509 = NULL;
  48012. BIGNUM* serial_number = NULL;
  48013. X509_NAME* name = NULL;
  48014. time_t epoch_off = 0;
  48015. ASN1_INTEGER* asn1_serial_number;
  48016. long not_before, not_after;
  48017. AssertNotNull(x509 = X509_new());
  48018. AssertIntNE(X509_set_pubkey(x509, pkey), 0);
  48019. AssertNotNull(serial_number = BN_new());
  48020. AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  48021. AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  48022. AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  48023. /* version 3 */
  48024. AssertIntNE(X509_set_version(x509, 2L), 0);
  48025. AssertNotNull(name = X509_NAME_new());
  48026. AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  48027. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  48028. AssertIntNE(X509_set_subject_name(x509, name), 0);
  48029. AssertIntNE(X509_set_issuer_name(x509, name), 0);
  48030. not_before = (long)wc_Time(NULL);
  48031. not_after = not_before + (365 * 24 * 60 * 60);
  48032. AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
  48033. AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
  48034. AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  48035. BN_free(serial_number);
  48036. X509_NAME_free(name);
  48037. X509_free(x509);
  48038. return 0;
  48039. }
  48040. #endif
  48041. static int test_openssl_generate_key_and_cert(void)
  48042. {
  48043. #if defined(OPENSSL_EXTRA)
  48044. #if !defined(NO_RSA)
  48045. {
  48046. EVP_PKEY* pkey = EVP_PKEY_new();
  48047. int key_length = 2048;
  48048. BIGNUM* exponent = BN_new();
  48049. RSA* rsa = RSA_new();
  48050. AssertNotNull(pkey);
  48051. AssertNotNull(exponent);
  48052. AssertNotNull(rsa);
  48053. AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  48054. #ifndef WOLFSSL_KEY_GEN
  48055. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  48056. #if defined(USE_CERT_BUFFERS_1024)
  48057. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  48058. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  48059. key_length = 1024;
  48060. #elif defined(USE_CERT_BUFFERS_2048)
  48061. AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  48062. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  48063. #else
  48064. RSA_free(rsa);
  48065. rsa = NULL;
  48066. #endif
  48067. #else
  48068. AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  48069. AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  48070. AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  48071. AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  48072. #endif
  48073. if (rsa) {
  48074. AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  48075. BN_free(exponent);
  48076. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  48077. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48078. test_openssl_make_self_signed_certificate(pkey);
  48079. #endif
  48080. }
  48081. EVP_PKEY_free(pkey);
  48082. }
  48083. #endif /* !NO_RSA */
  48084. #ifdef HAVE_ECC
  48085. {
  48086. EVP_PKEY* pkey = EVP_PKEY_new();
  48087. EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
  48088. AssertNotNull(pkey);
  48089. AssertNotNull(ec_key);
  48090. #ifndef NO_WOLFSSL_STUB
  48091. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  48092. #endif
  48093. AssertIntNE(EC_KEY_generate_key(ec_key), 0);
  48094. AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  48095. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  48096. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  48097. test_openssl_make_self_signed_certificate(pkey);
  48098. #endif
  48099. EVP_PKEY_free(pkey);
  48100. }
  48101. #endif /* HAVE_ECC */
  48102. #endif /* OPENSSL_EXTRA */
  48103. return 0;
  48104. }
  48105. static int test_stubs_are_stubs(void)
  48106. {
  48107. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
  48108. WOLFSSL_CTX* ctx = NULL;
  48109. WOLFSSL_CTX* ctxN = NULL;
  48110. #ifndef NO_WOLFSSL_CLIENT
  48111. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  48112. AssertNotNull(ctx);
  48113. #elif !defined(NO_WOLFSSL_SERVER)
  48114. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  48115. AssertNotNull(ctx);
  48116. #else
  48117. return;
  48118. #endif
  48119. #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
  48120. AssertIntEQ((int) x(z), 0)
  48121. /* test logic, all stubs return same result regardless of ctx being NULL
  48122. * as there are no sanity checks, it's just a stub! If at some
  48123. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  48124. * if invalid inputs are supplied. Test calling both
  48125. * with and without valid inputs, if a stub functionality remains unchanged.
  48126. */
  48127. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  48128. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  48129. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  48130. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  48131. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  48132. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  48133. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  48134. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  48135. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  48136. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  48137. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  48138. wolfSSL_CTX_free(ctx);
  48139. ctx = NULL;
  48140. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
  48141. return 0;
  48142. }
  48143. static int test_CONF_modules_xxx(void)
  48144. {
  48145. #if defined(OPENSSL_EXTRA)
  48146. CONF_modules_free();
  48147. AssertTrue(1); /* to confirm previous call gives no harm */
  48148. CONF_modules_unload(0);
  48149. AssertTrue(1);
  48150. CONF_modules_unload(1);
  48151. AssertTrue(1);
  48152. CONF_modules_unload(-1);
  48153. AssertTrue(1);
  48154. #endif /* OPENSSL_EXTRA */
  48155. return 0;
  48156. }
  48157. static int test_CRYPTO_set_dynlock_xxx(void)
  48158. {
  48159. #if defined(OPENSSL_EXTRA)
  48160. printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
  48161. CRYPTO_set_dynlock_create_callback(
  48162. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  48163. CRYPTO_set_dynlock_create_callback(
  48164. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  48165. CRYPTO_set_dynlock_destroy_callback(
  48166. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  48167. CRYPTO_set_dynlock_destroy_callback(
  48168. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  48169. CRYPTO_set_dynlock_lock_callback(
  48170. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  48171. CRYPTO_set_dynlock_lock_callback(
  48172. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  48173. AssertTrue(1); /* to confirm previous call gives no harm */
  48174. printf(resultFmt, passed);
  48175. #endif /* OPENSSL_EXTRA */
  48176. return 0;
  48177. }
  48178. static int test_CRYPTO_THREADID_xxx(void)
  48179. {
  48180. #if defined(OPENSSL_EXTRA)
  48181. printf(testingFmt, "CRYPTO_THREADID_xxx()");
  48182. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  48183. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  48184. AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  48185. printf(resultFmt, passed);
  48186. #endif /* OPENSSL_EXTRA */
  48187. return 0;
  48188. }
  48189. static int test_ENGINE_cleanup(void)
  48190. {
  48191. #if defined(OPENSSL_EXTRA)
  48192. printf(testingFmt, "ENGINE_cleanup()");
  48193. ENGINE_cleanup();
  48194. AssertTrue(1); /* to confirm previous call gives no harm */
  48195. printf(resultFmt, passed);
  48196. #endif /* OPENSSL_EXTRA */
  48197. return 0;
  48198. }
  48199. static int test_wolfSSL_CTX_LoadCRL(void)
  48200. {
  48201. #if defined(HAVE_CRL) && !defined(NO_RSA)
  48202. WOLFSSL_CTX* ctx = NULL;
  48203. WOLFSSL* ssl = NULL;
  48204. const char* badPath = "dummypath";
  48205. const char* validPath = "./certs/crl";
  48206. const char* validFilePath = "./certs/crl/cliCrl.pem";
  48207. const char* issuerCert = "./certs/client-cert.pem";
  48208. int derType = WOLFSSL_FILETYPE_ASN1;
  48209. int pemType = WOLFSSL_FILETYPE_PEM;
  48210. int monitor = WOLFSSL_CRL_MONITOR;
  48211. WOLFSSL_CERT_MANAGER* cm = NULL;
  48212. #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  48213. BAD_FUNC_ARG)
  48214. #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
  48215. WOLFSSL_SUCCESS)
  48216. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  48217. #ifndef NO_WOLFSSL_CLIENT
  48218. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48219. #elif !defined(NO_WOLFSSL_SERVER)
  48220. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48221. #else
  48222. return;
  48223. #endif
  48224. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  48225. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  48226. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  48227. wolfSSL_CTX_free(ctx);
  48228. #ifndef NO_WOLFSSL_CLIENT
  48229. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48230. #elif !defined(NO_WOLFSSL_SERVER)
  48231. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48232. #else
  48233. return;
  48234. #endif
  48235. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  48236. WOLFSSL_SUCCESS);
  48237. AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  48238. wolfSSL_CTX_free(ctx);
  48239. #ifndef NO_WOLFSSL_CLIENT
  48240. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  48241. #elif !defined(NO_WOLFSSL_SERVER)
  48242. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  48243. #else
  48244. return;
  48245. #endif
  48246. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  48247. WOLFSSL_SUCCESS);
  48248. AssertNotNull(ssl = wolfSSL_new(ctx));
  48249. AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  48250. wolfSSL_free(ssl);
  48251. wolfSSL_CTX_free(ctx);
  48252. AssertNotNull(cm = wolfSSL_CertManagerNew());
  48253. AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  48254. WOLFSSL_SUCCESS);
  48255. AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
  48256. wolfSSL_CertManagerFree(cm);
  48257. #endif
  48258. return 0;
  48259. }
  48260. static int test_SetTmpEC_DHE_Sz(void)
  48261. {
  48262. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  48263. WOLFSSL_CTX *ctx;
  48264. WOLFSSL *ssl;
  48265. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  48266. AssertNotNull(ctx);
  48267. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  48268. ssl = wolfSSL_new(ctx);
  48269. AssertNotNull(ssl);
  48270. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  48271. wolfSSL_free(ssl);
  48272. wolfSSL_CTX_free(ctx);
  48273. #endif
  48274. return 0;
  48275. }
  48276. static int test_wolfSSL_CTX_get0_privatekey(void)
  48277. {
  48278. #ifdef OPENSSL_ALL
  48279. WOLFSSL_CTX* ctx = NULL;
  48280. printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
  48281. #ifndef NO_RSA
  48282. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  48283. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48284. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  48285. WOLFSSL_FILETYPE_PEM));
  48286. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48287. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  48288. WOLFSSL_FILETYPE_PEM));
  48289. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  48290. wolfSSL_CTX_free(ctx);
  48291. #endif
  48292. #ifdef HAVE_ECC
  48293. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  48294. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48295. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48296. WOLFSSL_FILETYPE_PEM));
  48297. AssertNull(SSL_CTX_get0_privatekey(ctx));
  48298. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48299. WOLFSSL_FILETYPE_PEM));
  48300. AssertNotNull(SSL_CTX_get0_privatekey(ctx));
  48301. wolfSSL_CTX_free(ctx);
  48302. #endif
  48303. printf(resultFmt, passed);
  48304. #endif
  48305. return 0;
  48306. }
  48307. static int test_wolfSSL_dtls_set_mtu(void)
  48308. {
  48309. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  48310. defined(WOLFSSL_DTLS)
  48311. WOLFSSL_CTX* ctx = NULL;
  48312. WOLFSSL* ssl = NULL;
  48313. const char* testCertFile;
  48314. const char* testKeyFile;
  48315. AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  48316. #ifndef NO_RSA
  48317. testCertFile = svrCertFile;
  48318. testKeyFile = svrKeyFile;
  48319. #elif defined(HAVE_ECC)
  48320. testCertFile = eccCertFile;
  48321. testKeyFile = eccKeyFile;
  48322. #endif
  48323. if (testCertFile != NULL && testKeyFile != NULL) {
  48324. AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  48325. WOLFSSL_FILETYPE_PEM));
  48326. AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  48327. WOLFSSL_FILETYPE_PEM));
  48328. }
  48329. AssertNotNull(ssl = wolfSSL_new(ctx));
  48330. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  48331. AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  48332. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  48333. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  48334. AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  48335. AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  48336. AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  48337. wolfSSL_free(ssl);
  48338. wolfSSL_CTX_free(ctx);
  48339. printf(testingFmt, "wolfSSL_dtls_set_mtu()");
  48340. printf(resultFmt, passed);
  48341. #endif
  48342. return 0;
  48343. }
  48344. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  48345. defined(WOLFSSL_DTLS)
  48346. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  48347. enum HandShakeType hsType, word16 length)
  48348. {
  48349. size_t idx = 0;
  48350. byte* l;
  48351. /* record layer */
  48352. /* handshake type */
  48353. out[idx++] = handshake;
  48354. /* protocol version */
  48355. out[idx++] = 0xfe;
  48356. out[idx++] = 0xfd; /* DTLS 1.2 */
  48357. /* epoch 0 */
  48358. XMEMSET(out + idx, 0, 2);
  48359. idx += 2;
  48360. /* sequence number */
  48361. XMEMSET(out + idx, 0, 6);
  48362. c32toa(seq, out + idx + 2);
  48363. idx += 6;
  48364. /* length in BE */
  48365. if (length)
  48366. c16toa(length, out + idx);
  48367. else
  48368. c16toa(outSz - idx - 2, out + idx);
  48369. idx += 2;
  48370. /* handshake layer */
  48371. /* handshake type */
  48372. out[idx++] = (byte)hsType;
  48373. /* length */
  48374. l = out + idx;
  48375. idx += 3;
  48376. /* message seq */
  48377. c16toa(0, out + idx);
  48378. idx += 2;
  48379. /* frag offset */
  48380. c32to24(0, out + idx);
  48381. idx += 3;
  48382. /* frag length */
  48383. c32to24((word32)outSz - (word32)idx - 3, l);
  48384. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  48385. idx += 3;
  48386. XMEMSET(out + idx, 0, outSz - idx);
  48387. }
  48388. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  48389. {
  48390. byte msg[] = "This is a msg for the client";
  48391. byte reply[40];
  48392. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  48393. reply[sizeof(reply) - 1] = '\0';
  48394. printf("Client message: %s\n", reply);
  48395. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  48396. }
  48397. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  48398. {
  48399. byte ch[50];
  48400. int fd = wolfSSL_get_fd(ssl);
  48401. byte msg[] = "This is a msg for the server";
  48402. byte reply[40];
  48403. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  48404. /* Server should ignore this datagram */
  48405. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  48406. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  48407. /* Server should ignore this datagram */
  48408. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  48409. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  48410. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  48411. reply[sizeof(reply) - 1] = '\0';
  48412. printf("Server response: %s\n", reply);
  48413. }
  48414. static int test_wolfSSL_dtls_plaintext(void)
  48415. {
  48416. callback_functions func_cb_client;
  48417. callback_functions func_cb_server;
  48418. size_t i;
  48419. struct test_params {
  48420. method_provider client_meth;
  48421. method_provider server_meth;
  48422. ssl_callback on_result_server;
  48423. ssl_callback on_result_client;
  48424. } params[] = {
  48425. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48426. test_wolfSSL_dtls_plaintext_server,
  48427. test_wolfSSL_dtls_plaintext_client},
  48428. };
  48429. printf(testingFmt, "test_wolfSSL_dtls_plaintext");
  48430. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  48431. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48432. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48433. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48434. func_cb_server.method = params[i].server_meth;
  48435. func_cb_client.method = params[i].client_meth;
  48436. func_cb_client.on_result = params[i].on_result_client;
  48437. func_cb_server.on_result = params[i].on_result_server;
  48438. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48439. if (!func_cb_client.return_code)
  48440. return -1;
  48441. if (!func_cb_server.return_code)
  48442. return -2;
  48443. }
  48444. printf(resultFmt, passed);
  48445. return 0;
  48446. }
  48447. #else
  48448. static int test_wolfSSL_dtls_plaintext(void) {
  48449. return 0;
  48450. }
  48451. #endif
  48452. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  48453. defined(WOLFSSL_DTLS)
  48454. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  48455. {
  48456. byte b[1100]; /* buffer for the messages to send */
  48457. size_t idx = 0;
  48458. size_t seq_offset = 0;
  48459. size_t msg_offset = 0;
  48460. int i;
  48461. int fd = wolfSSL_get_fd(ssl);
  48462. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  48463. word32 seq_number = 100; /* start high so server definitely reads this */
  48464. word16 msg_number = 50; /* start high so server has to buffer this */
  48465. AssertIntEQ(ret, 1);
  48466. /* Now let's start spamming the peer with fragments it needs to store */
  48467. XMEMSET(b, -1, sizeof(b));
  48468. /* record layer */
  48469. /* handshake type */
  48470. b[idx++] = 22;
  48471. /* protocol version */
  48472. b[idx++] = 0xfe;
  48473. b[idx++] = 0xfd; /* DTLS 1.2 */
  48474. /* epoch 0 */
  48475. XMEMSET(b + idx, 0, 2);
  48476. idx += 2;
  48477. /* sequence number */
  48478. XMEMSET(b + idx, 0, 6);
  48479. seq_offset = idx + 2; /* increment only the low 32 bits */
  48480. idx += 6;
  48481. /* static length in BE */
  48482. c16toa(42, b + idx);
  48483. idx += 2;
  48484. /* handshake layer */
  48485. /* cert type */
  48486. b[idx++] = 11;
  48487. /* length */
  48488. c32to24(1000, b + idx);
  48489. idx += 3;
  48490. /* message seq */
  48491. c16toa(0, b + idx);
  48492. msg_offset = idx;
  48493. idx += 2;
  48494. /* frag offset */
  48495. c32to24(500, b + idx);
  48496. idx += 3;
  48497. /* frag length */
  48498. c32to24(30, b + idx);
  48499. idx += 3;
  48500. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  48501. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  48502. seq_number++, msg_number++, i++) {
  48503. struct timespec delay;
  48504. XMEMSET(&delay, 0, sizeof(delay));
  48505. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  48506. c32toa(seq_number, b + seq_offset);
  48507. c16toa(msg_number, b + msg_offset);
  48508. ret = (int)send(fd, b, 55, 0);
  48509. nanosleep(&delay, NULL);
  48510. }
  48511. }
  48512. #ifdef WOLFSSL_DTLS13
  48513. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  48514. {
  48515. byte b[150]; /* buffer for the messages to send */
  48516. size_t idx = 0;
  48517. size_t msg_offset = 0;
  48518. int fd = wolfSSL_get_fd(ssl);
  48519. word16 msg_number = 10; /* start high so server has to buffer this */
  48520. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  48521. AssertIntEQ(ret, 1);
  48522. /* Now let's start spamming the peer with fragments it needs to store */
  48523. XMEMSET(b, -1, sizeof(b));
  48524. /* handshake type */
  48525. b[idx++] = 11;
  48526. /* length */
  48527. c32to24(10000, b + idx);
  48528. idx += 3;
  48529. /* message_seq */
  48530. msg_offset = idx;
  48531. idx += 2;
  48532. /* fragment_offset */
  48533. c32to24(5000, b + idx);
  48534. idx += 3;
  48535. /* fragment_length */
  48536. c32to24(100, b + idx);
  48537. idx += 3;
  48538. /* fragment contents */
  48539. idx += 100;
  48540. for (; ret > 0; msg_number++) {
  48541. byte sendBuf[150];
  48542. int sendSz = sizeof(sendBuf);
  48543. struct timespec delay;
  48544. XMEMSET(&delay, 0, sizeof(delay));
  48545. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  48546. c16toa(msg_number, b + msg_offset);
  48547. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  48548. (int)idx, handshake, 0, 0, 0);
  48549. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  48550. nanosleep(&delay, NULL);
  48551. }
  48552. }
  48553. #endif
  48554. static int test_wolfSSL_dtls_fragments(void)
  48555. {
  48556. callback_functions func_cb_client;
  48557. callback_functions func_cb_server;
  48558. size_t i;
  48559. struct test_params {
  48560. method_provider client_meth;
  48561. method_provider server_meth;
  48562. ssl_callback spammer;
  48563. } params[] = {
  48564. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48565. test_wolfSSL_dtls12_fragments_spammer},
  48566. #ifdef WOLFSSL_DTLS13
  48567. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  48568. test_wolfSSL_dtls13_fragments_spammer},
  48569. #endif
  48570. };
  48571. printf(testingFmt, "test_wolfSSL_dtls_fragments");
  48572. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  48573. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48574. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48575. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48576. func_cb_server.method = params[i].server_meth;
  48577. func_cb_client.method = params[i].client_meth;
  48578. func_cb_client.ssl_ready = params[i].spammer;
  48579. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48580. AssertFalse(func_cb_client.return_code);
  48581. AssertFalse(func_cb_server.return_code);
  48582. /* The socket should be closed by the server resulting in a
  48583. * socket error */
  48584. AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  48585. /* Check the server returned an error indicating the msg buffer
  48586. * was full */
  48587. AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  48588. }
  48589. printf(resultFmt, passed);
  48590. return 0;
  48591. }
  48592. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  48593. {
  48594. int fd, ret;
  48595. byte alert_msg[] = {
  48596. 0x15, /* alert type */
  48597. 0xfe, 0xfd, /* version */
  48598. 0x00, 0x00, /* epoch */
  48599. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  48600. 0x00, 0x02, /* length */
  48601. 0x02, /* level: fatal */
  48602. 0x46 /* protocol version */
  48603. };
  48604. fd = wolfSSL_get_fd(ssl);
  48605. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  48606. AssertIntGT(ret, 0);
  48607. }
  48608. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  48609. {
  48610. callback_functions client_cbs, server_cbs;
  48611. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48612. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48613. client_cbs.doUdp = server_cbs.doUdp = 1;
  48614. if (version12) {
  48615. client_cbs.method = wolfDTLSv1_2_client_method;
  48616. server_cbs.method = wolfDTLSv1_2_server_method;
  48617. }
  48618. else {
  48619. #ifdef WOLFSSL_DTLS13
  48620. client_cbs.method = wolfDTLSv1_3_client_method;
  48621. server_cbs.method = wolfDTLSv1_3_server_method;
  48622. #else
  48623. return 0;
  48624. #endif /* WOLFSSL_DTLS13 */
  48625. }
  48626. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  48627. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48628. if (!client_cbs.return_code)
  48629. return -1;
  48630. if (!server_cbs.return_code)
  48631. return -1;
  48632. return 0;
  48633. }
  48634. static int test_wolfSSL_ignore_alert_before_cookie(void)
  48635. {
  48636. int ret;
  48637. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  48638. if (ret != 0)
  48639. return ret;
  48640. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  48641. if (ret != 0)
  48642. return ret;
  48643. return 0;
  48644. }
  48645. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  48646. {
  48647. int ret;
  48648. int fd;
  48649. byte bad_msg[] = {
  48650. 0x17, /* app data */
  48651. 0xaa, 0xfd, /* bad version */
  48652. 0x00, 0x01, /* epoch 1 */
  48653. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  48654. 0x00, 0x26, /* length: 38 bytes */
  48655. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  48656. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  48657. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  48658. 0x03, 0x18, 0x72
  48659. };
  48660. fd = wolfSSL_get_fd(ssl);
  48661. AssertIntGE(fd, 0);
  48662. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  48663. AssertIntEQ(ret, sizeof(bad_msg));
  48664. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  48665. AssertIntEQ(ret, sizeof("badrecordtest"));
  48666. }
  48667. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  48668. {
  48669. byte buf[100];
  48670. int ret;
  48671. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  48672. AssertIntGT(ret, 0);
  48673. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  48674. }
  48675. static int _test_wolfSSL_dtls_bad_record(
  48676. method_provider client_method, method_provider server_method)
  48677. {
  48678. callback_functions client_cbs, server_cbs;
  48679. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48680. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48681. client_cbs.doUdp = server_cbs.doUdp = 1;
  48682. client_cbs.method = client_method;
  48683. server_cbs.method = server_method;
  48684. client_cbs.on_result = test_wolfSSL_send_bad_record;
  48685. server_cbs.on_result = test_wolfSSL_read_string;
  48686. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48687. if (!client_cbs.return_code)
  48688. return -1;
  48689. if (!server_cbs.return_code)
  48690. return -1;
  48691. return 0;
  48692. }
  48693. static int test_wolfSSL_dtls_bad_record(void)
  48694. {
  48695. int ret;
  48696. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  48697. wolfDTLSv1_2_server_method);
  48698. if (ret != 0)
  48699. return ret;
  48700. #ifdef WOLFSSL_DTLS13
  48701. return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  48702. wolfDTLSv1_3_server_method);
  48703. #else
  48704. return 0;
  48705. #endif /* WOLFSSL_DTLS13 */
  48706. }
  48707. #else
  48708. static int test_wolfSSL_dtls_fragments(void) {
  48709. return 0;
  48710. }
  48711. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  48712. return 0;
  48713. }
  48714. static int test_wolfSSL_dtls_bad_record(void) {
  48715. return 0;
  48716. }
  48717. #endif
  48718. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS)
  48719. static byte test_AEAD_fail_decryption = 0;
  48720. static byte test_AEAD_seq_num = 0;
  48721. static byte test_AEAD_done = 0;
  48722. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  48723. {
  48724. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  48725. if (ret > 0) {
  48726. if (test_AEAD_fail_decryption) {
  48727. /* Modify the packet to trigger a decryption failure */
  48728. buf[ret/2] ^= 0xFF;
  48729. if (test_AEAD_fail_decryption == 1)
  48730. test_AEAD_fail_decryption = 0;
  48731. }
  48732. }
  48733. (void)ctx;
  48734. return ret;
  48735. }
  48736. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  48737. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  48738. {
  48739. if (sendLimit)
  48740. w64Zero(sendLimit);
  48741. switch (ssl->specs.bulk_cipher_algorithm) {
  48742. case wolfssl_aes_gcm:
  48743. if (sendLimit)
  48744. *sendLimit = AEAD_AES_LIMIT;
  48745. FALL_THROUGH;
  48746. case wolfssl_chacha:
  48747. if (hardLimit)
  48748. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  48749. if (keyUpdateLimit)
  48750. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  48751. break;
  48752. case wolfssl_aes_ccm:
  48753. if (sendLimit)
  48754. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  48755. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  48756. if (hardLimit)
  48757. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  48758. if (keyUpdateLimit)
  48759. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  48760. }
  48761. else {
  48762. if (hardLimit)
  48763. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  48764. if (keyUpdateLimit)
  48765. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  48766. }
  48767. break;
  48768. default:
  48769. fprintf(stderr, "Unrecognized bulk cipher");
  48770. AssertFalse(1);
  48771. break;
  48772. }
  48773. }
  48774. static void test_AEAD_limit_client(WOLFSSL* ssl)
  48775. {
  48776. int ret;
  48777. int i;
  48778. int didReKey = 0;
  48779. char msgBuf[20];
  48780. w64wrapper hardLimit;
  48781. w64wrapper keyUpdateLimit;
  48782. w64wrapper counter;
  48783. w64wrapper sendLimit;
  48784. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  48785. w64Zero(&counter);
  48786. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  48787. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  48788. for (i = 0; i < 10; i++) {
  48789. /* Test some failed decryptions */
  48790. test_AEAD_fail_decryption = 1;
  48791. w64Increment(&counter);
  48792. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48793. /* Should succeed since decryption failures are dropped */
  48794. AssertIntGT(ret, 0);
  48795. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  48796. }
  48797. test_AEAD_fail_decryption = 1;
  48798. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  48799. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  48800. /* 100 read calls should be enough to complete the key update */
  48801. w64Zero(&counter);
  48802. for (i = 0; i < 100; i++) {
  48803. /* Key update should be sent and negotiated */
  48804. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48805. AssertIntGT(ret, 0);
  48806. /* Epoch after one key update is 4 */
  48807. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  48808. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  48809. didReKey = 1;
  48810. break;
  48811. }
  48812. }
  48813. AssertTrue(didReKey);
  48814. if (!w64IsZero(sendLimit)) {
  48815. /* Test the sending limit for AEAD ciphers */
  48816. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  48817. test_AEAD_seq_num = 1;
  48818. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  48819. AssertIntGT(ret, 0);
  48820. didReKey = 0;
  48821. w64Zero(&counter);
  48822. /* 100 read calls should be enough to complete the key update */
  48823. for (i = 0; i < 100; i++) {
  48824. /* Key update should be sent and negotiated */
  48825. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48826. AssertIntGT(ret, 0);
  48827. /* Epoch after another key update is 5 */
  48828. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  48829. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  48830. didReKey = 1;
  48831. break;
  48832. }
  48833. }
  48834. AssertTrue(didReKey);
  48835. }
  48836. test_AEAD_fail_decryption = 2;
  48837. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  48838. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  48839. /* Connection should fail with a DECRYPT_ERROR */
  48840. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48841. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  48842. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  48843. test_AEAD_done = 1;
  48844. }
  48845. int counter = 0;
  48846. static void test_AEAD_limit_server(WOLFSSL* ssl)
  48847. {
  48848. char msgBuf[] = "Sending data";
  48849. int ret = WOLFSSL_SUCCESS;
  48850. w64wrapper sendLimit;
  48851. SOCKET_T fd = wolfSSL_get_fd(ssl);
  48852. struct timespec delay;
  48853. XMEMSET(&delay, 0, sizeof(delay));
  48854. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  48855. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  48856. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  48857. while (!test_AEAD_done && ret > 0) {
  48858. counter++;
  48859. if (test_AEAD_seq_num) {
  48860. /* We need to update the seq number so that we can understand the
  48861. * peer. Otherwise we will incorrectly interpret the seq number. */
  48862. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  48863. AssertNotNull(e);
  48864. e->nextPeerSeqNumber = sendLimit;
  48865. test_AEAD_seq_num = 0;
  48866. }
  48867. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  48868. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  48869. nanosleep(&delay, NULL);
  48870. }
  48871. }
  48872. static int test_wolfSSL_dtls_AEAD_limit(void)
  48873. {
  48874. callback_functions func_cb_client;
  48875. callback_functions func_cb_server;
  48876. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  48877. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  48878. printf(testingFmt, "test_wolfSSL_dtls_AEAD_limit");
  48879. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  48880. func_cb_server.method = wolfDTLSv1_3_server_method;
  48881. func_cb_client.method = wolfDTLSv1_3_client_method;
  48882. func_cb_server.on_result = test_AEAD_limit_server;
  48883. func_cb_client.on_result = test_AEAD_limit_client;
  48884. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  48885. if (!func_cb_client.return_code)
  48886. return -1;
  48887. if (!func_cb_server.return_code)
  48888. return -2;
  48889. printf(resultFmt, passed);
  48890. return 0;
  48891. }
  48892. #else
  48893. static int test_wolfSSL_dtls_AEAD_limit(void)
  48894. {
  48895. return 0;
  48896. }
  48897. #endif
  48898. #if defined(WOLFSSL_DTLS) && \
  48899. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  48900. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  48901. {
  48902. int fd, ret;
  48903. byte ch_msg[] = {
  48904. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  48905. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  48906. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  48907. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  48908. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  48909. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  48910. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  48911. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  48912. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  48913. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  48914. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  48915. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  48916. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  48917. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  48918. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  48919. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  48920. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  48921. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  48922. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  48923. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  48924. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  48925. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  48926. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  48927. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  48928. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  48929. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  48930. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  48931. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  48932. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  48933. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  48934. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  48935. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  48936. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  48937. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  48938. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  48939. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  48940. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  48941. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  48942. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  48943. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  48944. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  48945. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  48946. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  48947. 0x31, 0x68, 0x4c
  48948. };
  48949. fd = wolfSSL_get_fd(ssl);
  48950. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  48951. AssertIntGT(ret, 0);
  48952. /* consume the HRR otherwise handshake will fail */
  48953. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  48954. AssertIntGT(ret, 0);
  48955. }
  48956. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  48957. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  48958. {
  48959. int ret;
  48960. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  48961. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  48962. }
  48963. #endif
  48964. static int test_wolfSSL_dtls_stateless(void)
  48965. {
  48966. callback_functions client_cbs, server_cbs;
  48967. size_t i;
  48968. struct {
  48969. method_provider client_meth;
  48970. method_provider server_meth;
  48971. ssl_callback client_ssl_ready;
  48972. ssl_callback server_ssl_ready;
  48973. } test_params[] = {
  48974. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  48975. test_wolfSSL_dtls_send_ch, NULL},
  48976. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  48977. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  48978. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  48979. #endif
  48980. };
  48981. printf(testingFmt, "test_wolfSSL_dtls_stateless");
  48982. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  48983. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  48984. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  48985. client_cbs.doUdp = server_cbs.doUdp = 1;
  48986. client_cbs.method = test_params[i].client_meth;
  48987. server_cbs.method = test_params[i].server_meth;
  48988. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  48989. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  48990. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  48991. if (!client_cbs.return_code)
  48992. return -1;
  48993. if (!server_cbs.return_code)
  48994. return -1;
  48995. }
  48996. printf(resultFmt, passed);
  48997. return 0;
  48998. }
  48999. #else
  49000. static int test_wolfSSL_dtls_stateless(void)
  49001. {
  49002. return 0;
  49003. }
  49004. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  49005. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  49006. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  49007. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  49008. !defined(WOLFSSL_NO_CLIENT_AUTH))
  49009. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  49010. {
  49011. int ret;
  49012. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  49013. printf("loading cert %s failed\n", certA);
  49014. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  49015. return -1;
  49016. }
  49017. return 0;
  49018. }
  49019. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  49020. {
  49021. int ret;
  49022. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  49023. != WOLFSSL_SUCCESS) {
  49024. printf("could not verify the cert: %s\n", certA);
  49025. printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
  49026. return -1;
  49027. } else {
  49028. printf("successfully verified: %s\n", certA);
  49029. }
  49030. return 0;
  49031. }
  49032. #define LOAD_ONE_CA(a, b, c, d) \
  49033. do { \
  49034. (a) = load_ca_into_cm(c, d); \
  49035. if ((a) != 0) \
  49036. return (b); \
  49037. else \
  49038. (b)--; \
  49039. } while(0)
  49040. #define VERIFY_ONE_CERT(a, b, c, d) \
  49041. do { \
  49042. (a) = verify_cert_with_cm(c, d); \
  49043. if ((a) != 0) \
  49044. return (b); \
  49045. else \
  49046. (b)--; \
  49047. } while(0)
  49048. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  49049. {
  49050. int ret;
  49051. int i = -1;
  49052. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  49053. char chainGArr[9][50] = {"certs/ca-cert.pem",
  49054. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  49055. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  49056. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  49057. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  49058. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  49059. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  49060. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  49061. "certs/test-pathlen/chainG-entity.pem"};
  49062. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  49063. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  49064. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  49065. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  49066. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  49067. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  49068. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  49069. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  49070. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  49071. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  49072. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  49073. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  49074. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  49075. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  49076. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  49077. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  49078. /* test validating the entity twice, should have no effect on pathLen since
  49079. * entity/leaf cert */
  49080. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  49081. return ret;
  49082. }
  49083. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  49084. {
  49085. int ret;
  49086. int i = -1;
  49087. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  49088. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  49089. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  49090. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  49091. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  49092. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  49093. */
  49094. char chainHArr[6][50] = {"certs/ca-cert.pem",
  49095. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  49096. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  49097. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  49098. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  49099. "certs/test-pathlen/chainH-entity.pem"};
  49100. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  49101. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  49102. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  49103. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  49104. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  49105. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  49106. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  49107. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  49108. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  49109. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  49110. return ret;
  49111. }
  49112. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  49113. {
  49114. int ret;
  49115. int i = -1;
  49116. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  49117. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  49118. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  49119. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  49120. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  49121. */
  49122. char chainIArr[5][50] = {"certs/ca-cert.pem",
  49123. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  49124. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  49125. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  49126. "certs/test-pathlen/chainI-entity.pem"};
  49127. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  49128. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  49129. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  49130. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  49131. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  49132. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  49133. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  49134. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  49135. return ret;
  49136. }
  49137. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  49138. {
  49139. int ret;
  49140. int i = -1;
  49141. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  49142. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  49143. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  49144. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  49145. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  49146. */
  49147. char chainJArr[6][50] = {"certs/ca-cert.pem",
  49148. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  49149. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  49150. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  49151. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  49152. "certs/test-pathlen/chainJ-entity.pem"};
  49153. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  49154. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  49155. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  49156. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  49157. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  49158. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  49159. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  49160. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  49161. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  49162. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  49163. return ret;
  49164. }
  49165. static int test_various_pathlen_chains(void)
  49166. {
  49167. int ret;
  49168. WOLFSSL_CERT_MANAGER* cm;
  49169. /* Test chain G (large chain with varying pathLens) */
  49170. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49171. printf("cert manager new failed\n");
  49172. return -1;
  49173. }
  49174. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49175. AssertIntEQ(test_chainG(cm), -1);
  49176. #else
  49177. AssertIntEQ(test_chainG(cm), 0);
  49178. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  49179. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49180. if (ret != WOLFSSL_SUCCESS)
  49181. return -1;
  49182. wolfSSL_CertManagerFree(cm);
  49183. /* end test chain G */
  49184. /* Test chain H (5 chain with same pathLens) */
  49185. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49186. printf("cert manager new failed\n");
  49187. return -1;
  49188. }
  49189. AssertIntLT(test_chainH(cm), 0);
  49190. wolfSSL_CertManagerUnloadCAs(cm);
  49191. wolfSSL_CertManagerFree(cm);
  49192. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49193. printf("cert manager new failed\n");
  49194. return -1;
  49195. }
  49196. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49197. if (ret != WOLFSSL_SUCCESS)
  49198. return -1;
  49199. wolfSSL_CertManagerFree(cm);
  49200. /* end test chain H */
  49201. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  49202. * followed by 2 ICA's, should pass) */
  49203. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49204. printf("cert manager new failed\n");
  49205. return -1;
  49206. }
  49207. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  49208. AssertIntEQ(test_chainI(cm), -1);
  49209. #else
  49210. AssertIntEQ(test_chainI(cm), 0);
  49211. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  49212. wolfSSL_CertManagerUnloadCAs(cm);
  49213. wolfSSL_CertManagerFree(cm);
  49214. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49215. printf("cert manager new failed\n");
  49216. return -1;
  49217. }
  49218. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49219. if (ret != WOLFSSL_SUCCESS)
  49220. return -1;
  49221. wolfSSL_CertManagerFree(cm);
  49222. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  49223. * this time followed by 3 ICA's, should fail */
  49224. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49225. printf("cert manager new failed\n");
  49226. return -1;
  49227. }
  49228. AssertIntLT(test_chainJ(cm), 0);
  49229. wolfSSL_CertManagerUnloadCAs(cm);
  49230. wolfSSL_CertManagerFree(cm);
  49231. if ((cm = wolfSSL_CertManagerNew()) == NULL) {
  49232. printf("cert manager new failed\n");
  49233. return -1;
  49234. }
  49235. ret = wolfSSL_CertManagerUnloadCAs(cm);
  49236. wolfSSL_CertManagerFree(cm);
  49237. if (ret == WOLFSSL_SUCCESS) {
  49238. ret = 0;
  49239. }
  49240. return ret;
  49241. }
  49242. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  49243. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  49244. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  49245. {
  49246. byte ekm[100] = {0};
  49247. (void)ctx;
  49248. /* Succes Cases */
  49249. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49250. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  49251. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49252. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  49253. /* Use some random context */
  49254. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49255. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  49256. /* Failure cases */
  49257. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49258. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  49259. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49260. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  49261. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49262. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  49263. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49264. "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
  49265. AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  49266. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  49267. return 0;
  49268. }
  49269. static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  49270. {
  49271. wolfSSL_KeepArrays(ssl);
  49272. }
  49273. static int test_export_keying_material(void)
  49274. {
  49275. #ifndef SINGLE_THREADED
  49276. tcp_ready ready;
  49277. callback_functions clientCb;
  49278. func_args client_args;
  49279. func_args server_args;
  49280. THREAD_TYPE serverThread;
  49281. XMEMSET(&client_args, 0, sizeof(func_args));
  49282. XMEMSET(&server_args, 0, sizeof(func_args));
  49283. XMEMSET(&clientCb, 0, sizeof(callback_functions));
  49284. #ifdef WOLFSSL_TIRTOS
  49285. fdOpenSession(Task_self());
  49286. #endif
  49287. StartTCP();
  49288. InitTcpReady(&ready);
  49289. #if defined(USE_WINDOWS_API)
  49290. /* use RNG to get random port if using windows */
  49291. ready.port = GetRandomPort();
  49292. #endif
  49293. server_args.signal = &ready;
  49294. client_args.signal = &ready;
  49295. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  49296. client_args.callbacks = &clientCb;
  49297. start_thread(test_server_nofail, &server_args, &serverThread);
  49298. wait_tcp_ready(&server_args);
  49299. test_client_nofail(&client_args, test_export_keying_material_cb);
  49300. join_thread(serverThread);
  49301. AssertTrue(client_args.return_code);
  49302. AssertTrue(server_args.return_code);
  49303. FreeTcpReady(&ready);
  49304. #ifdef WOLFSSL_TIRTOS
  49305. fdOpenSession(Task_self());
  49306. #endif
  49307. #endif /* !SINGLE_THREADED */
  49308. return 0;
  49309. }
  49310. #endif /* HAVE_KEYING_MATERIAL */
  49311. static int test_wolfSSL_THREADID_hash(void)
  49312. {
  49313. int ret = 0;
  49314. unsigned long res;
  49315. #if defined(OPENSSL_EXTRA)
  49316. CRYPTO_THREADID id;
  49317. printf(testingFmt, "wolfSSL_THREADID_hash");
  49318. CRYPTO_THREADID_current(NULL);
  49319. AssertTrue(1);
  49320. res = CRYPTO_THREADID_hash(NULL);
  49321. AssertTrue( res == 0UL);
  49322. XMEMSET(&id, 0, sizeof(id));
  49323. res = CRYPTO_THREADID_hash(&id);
  49324. AssertTrue( res == 0UL);
  49325. printf(resultFmt, passed);
  49326. #endif /* OPENSSL_EXTRA */
  49327. (void)res;
  49328. return ret;
  49329. }
  49330. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  49331. {
  49332. int ret = 0;
  49333. WOLFSSL_CTX* ctx = NULL;
  49334. #if defined(OPENSSL_EXTRA)
  49335. printf(testingFmt, "SSL_CTX_set_ecdh_auto");
  49336. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
  49337. AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
  49338. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
  49339. AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
  49340. printf(resultFmt, passed);
  49341. #endif /* OPENSSL_EXTRA */
  49342. (void)ctx;
  49343. return ret;
  49344. }
  49345. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  49346. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  49347. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  49348. {
  49349. callback_functions* callbacks = NULL;
  49350. WOLFSSL_CTX* ctx = NULL;
  49351. WOLFSSL* ssl = NULL;
  49352. SOCKET_T sfd = 0;
  49353. SOCKET_T cfd = 0;
  49354. word16 port;
  49355. char msg[] = "I hear you fa shizzle!";
  49356. int len = (int) XSTRLEN(msg);
  49357. char input[1024];
  49358. int ret, err;
  49359. if (!args)
  49360. return 0;
  49361. ((func_args*)args)->return_code = TEST_FAIL;
  49362. callbacks = ((func_args*)args)->callbacks;
  49363. ctx = wolfSSL_CTX_new(callbacks->method());
  49364. #if defined(USE_WINDOWS_API)
  49365. port = ((func_args*)args)->signal->port;
  49366. #else
  49367. /* Let tcp_listen assign port */
  49368. port = 0;
  49369. #endif
  49370. #ifdef WOLFSSL_TIRTOS
  49371. fdOpenSession(Task_self());
  49372. #endif
  49373. AssertIntEQ(WOLFSSL_SUCCESS,
  49374. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  49375. AssertIntEQ(WOLFSSL_SUCCESS,
  49376. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  49377. WOLFSSL_FILETYPE_PEM));
  49378. AssertIntEQ(WOLFSSL_SUCCESS,
  49379. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  49380. WOLFSSL_FILETYPE_PEM));
  49381. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  49382. AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  49383. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  49384. #elif !defined(NO_DH)
  49385. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  49386. #endif
  49387. if (callbacks->ctx_ready)
  49388. callbacks->ctx_ready(ctx);
  49389. ssl = wolfSSL_new(ctx);
  49390. AssertNotNull(ssl);
  49391. /* listen and accept */
  49392. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  49393. CloseSocket(sfd);
  49394. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  49395. if (callbacks->ssl_ready)
  49396. callbacks->ssl_ready(ssl);
  49397. do {
  49398. err = 0; /* Reset error */
  49399. ret = wolfSSL_accept(ssl);
  49400. if (ret != WOLFSSL_SUCCESS) {
  49401. err = wolfSSL_get_error(ssl, 0);
  49402. }
  49403. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  49404. if (ret != WOLFSSL_SUCCESS) {
  49405. wolfSSL_free(ssl);
  49406. wolfSSL_CTX_free(ctx);
  49407. CloseSocket(cfd);
  49408. ((func_args*)args)->return_code = TEST_FAIL;
  49409. return 0;
  49410. }
  49411. /* read and write data */
  49412. XMEMSET( input, 0, sizeof(input));
  49413. while (1) {
  49414. ret = wolfSSL_read(ssl, input, sizeof(input));
  49415. if (ret > 0) {
  49416. break;
  49417. }
  49418. else {
  49419. err = wolfSSL_get_error(ssl,ret);
  49420. if (err == WOLFSSL_ERROR_WANT_READ) {
  49421. continue;
  49422. }
  49423. break;
  49424. }
  49425. }
  49426. if (err == WOLFSSL_ERROR_ZERO_RETURN) {
  49427. do {
  49428. ret = wolfSSL_write(ssl, msg, len);
  49429. if (ret > 0) {
  49430. break;
  49431. }
  49432. } while (ret < 0);
  49433. }
  49434. /* bidirectional shutdown */
  49435. while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  49436. continue;
  49437. }
  49438. /* wait for the peer to disconnect the tcp connection */
  49439. do {
  49440. ret = wolfSSL_read(ssl, input, sizeof(input));
  49441. err = wolfSSL_get_error(ssl, ret);
  49442. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  49443. /* detect TCP disconnect */
  49444. AssertIntLE(ret,WOLFSSL_FAILURE);
  49445. AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  49446. ((func_args*)args)->return_code = TEST_SUCCESS;
  49447. wolfSSL_free(ssl);
  49448. wolfSSL_CTX_free(ctx);
  49449. CloseSocket(cfd);
  49450. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  49451. wc_ecc_fp_free(); /* free per thread cache */
  49452. #endif
  49453. return 0;
  49454. }
  49455. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  49456. {
  49457. callback_functions* callbacks = NULL;
  49458. WOLFSSL_CTX* ctx = NULL;
  49459. WOLFSSL* ssl = NULL;
  49460. SOCKET_T sfd = 0;
  49461. char msg[] = "hello wolfssl server!";
  49462. int len = (int) XSTRLEN(msg);
  49463. char input[1024];
  49464. int idx;
  49465. int ret, err;
  49466. if (!args)
  49467. return 0;
  49468. ((func_args*)args)->return_code = TEST_FAIL;
  49469. callbacks = ((func_args*)args)->callbacks;
  49470. ctx = wolfSSL_CTX_new(callbacks->method());
  49471. #ifdef WOLFSSL_TIRTOS
  49472. fdOpenSession(Task_self());
  49473. #endif
  49474. AssertIntEQ(WOLFSSL_SUCCESS,
  49475. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  49476. AssertIntEQ(WOLFSSL_SUCCESS,
  49477. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  49478. WOLFSSL_FILETYPE_PEM));
  49479. AssertIntEQ(WOLFSSL_SUCCESS,
  49480. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  49481. WOLFSSL_FILETYPE_PEM));
  49482. AssertNotNull((ssl = wolfSSL_new(ctx)));
  49483. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  49484. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  49485. do {
  49486. err = 0; /* Reset error */
  49487. ret = wolfSSL_connect(ssl);
  49488. if (ret != WOLFSSL_SUCCESS) {
  49489. err = wolfSSL_get_error(ssl, 0);
  49490. }
  49491. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  49492. AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
  49493. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  49494. input[idx] = 0;
  49495. }
  49496. ret = wolfSSL_shutdown(ssl);
  49497. if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  49498. ret = wolfSSL_shutdown(ssl);
  49499. }
  49500. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  49501. ((func_args*)args)->return_code = TEST_SUCCESS;
  49502. wolfSSL_free(ssl);
  49503. wolfSSL_CTX_free(ctx);
  49504. CloseSocket(sfd);
  49505. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  49506. wc_ecc_fp_free(); /* free per thread cache */
  49507. #endif
  49508. return 0;
  49509. }
  49510. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  49511. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  49512. /* This test is to check wolfSSL_read behaves as same as
  49513. * openSSL when it is called after SSL_shutdown completes.
  49514. */
  49515. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  49516. {
  49517. int ret = 0;
  49518. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  49519. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  49520. tcp_ready ready;
  49521. func_args client_args;
  49522. func_args server_args;
  49523. THREAD_TYPE serverThread;
  49524. THREAD_TYPE clientThread;
  49525. callback_functions server_cbf;
  49526. callback_functions client_cbf;
  49527. printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
  49528. #ifdef WOLFSSL_TIRTOS
  49529. fdOpenSession(Task_self());
  49530. #endif
  49531. StartTCP();
  49532. InitTcpReady(&ready);
  49533. #if defined(USE_WINDOWS_API)
  49534. /* use RNG to get random port if using windows */
  49535. ready.port = GetRandomPort();
  49536. #endif
  49537. XMEMSET(&client_args, 0, sizeof(func_args));
  49538. XMEMSET(&server_args, 0, sizeof(func_args));
  49539. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  49540. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  49541. server_cbf.method = wolfTLSv1_2_server_method;
  49542. client_cbf.method = wolfTLSv1_2_client_method;
  49543. server_args.callbacks = &server_cbf;
  49544. client_args.callbacks = &client_cbf;
  49545. server_args.signal = &ready;
  49546. client_args.signal = &ready;
  49547. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  49548. wait_tcp_ready(&server_args);
  49549. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  49550. join_thread(clientThread);
  49551. join_thread(serverThread);
  49552. AssertTrue(client_args.return_code);
  49553. AssertTrue(server_args.return_code);
  49554. FreeTcpReady(&ready);
  49555. printf(resultFmt, passed);
  49556. #endif
  49557. return ret;
  49558. }
  49559. static int test_wolfSSL_CTX_get_min_proto_version(void)
  49560. {
  49561. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  49562. WOLFSSL_CTX *ctx;
  49563. (void)ctx;
  49564. printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
  49565. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49566. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
  49567. #ifdef WOLFSSL_ALLOW_SSLV3
  49568. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  49569. #else
  49570. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  49571. #endif
  49572. wolfSSL_CTX_free(ctx);
  49573. #ifdef WOLFSSL_ALLOW_TLSV10
  49574. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  49575. #else
  49576. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49577. #endif
  49578. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
  49579. #ifdef WOLFSSL_ALLOW_TLSV10
  49580. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  49581. #else
  49582. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  49583. #endif
  49584. wolfSSL_CTX_free(ctx);
  49585. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  49586. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
  49587. #ifndef NO_OLD_TLS
  49588. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  49589. #else
  49590. AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  49591. #endif
  49592. wolfSSL_CTX_free(ctx);
  49593. #ifndef WOLFSSL_NO_TLS12
  49594. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  49595. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
  49596. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  49597. wolfSSL_CTX_free(ctx);
  49598. #endif
  49599. #ifdef WOLFSSL_TLS13
  49600. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  49601. AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
  49602. AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  49603. wolfSSL_CTX_free(ctx);
  49604. #endif
  49605. printf(resultFmt, passed);
  49606. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  49607. return 0;
  49608. }
  49609. static int test_wolfSSL_security_level(void)
  49610. {
  49611. #if defined(OPENSSL_EXTRA)
  49612. SSL_CTX *ctx;
  49613. printf(testingFmt, "test_wolfSSL_security_level()");
  49614. #ifdef WOLFSSL_TLS13
  49615. #ifdef NO_WOLFSSL_SERVER
  49616. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49617. #else
  49618. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49619. #endif
  49620. SSL_CTX_set_security_level(ctx, 1);
  49621. AssertTrue(1);
  49622. AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
  49623. SSL_CTX_free(ctx);
  49624. #else
  49625. (void)ctx;
  49626. #endif
  49627. printf(resultFmt, passed);
  49628. #endif
  49629. return 0;
  49630. }
  49631. static int test_wolfSSL_SSL_in_init(void)
  49632. {
  49633. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  49634. SSL_CTX* ctx;
  49635. SSL* ssl;
  49636. const char* testCertFile;
  49637. const char* testKeyFile;
  49638. printf(testingFmt, "test_wolfSSL_SSL_in_init()");
  49639. #ifdef WOLFSSL_TLS13
  49640. #ifdef NO_WOLFSSL_SERVER
  49641. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49642. #else
  49643. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49644. #endif
  49645. #ifndef NO_RSA
  49646. testCertFile = svrCertFile;
  49647. testKeyFile = svrKeyFile;
  49648. #elif defined(HAVE_ECC)
  49649. testCertFile = eccCertFile;
  49650. testKeyFile = eccKeyFile;
  49651. #else
  49652. testCertFile = NULL;
  49653. testKeyFile = NULL;
  49654. #endif
  49655. if (testCertFile != NULL && testKeyFile != NULL) {
  49656. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  49657. SSL_FILETYPE_PEM));
  49658. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  49659. SSL_FILETYPE_PEM));
  49660. }
  49661. ssl = SSL_new(ctx);
  49662. AssertNotNull(ssl);
  49663. AssertIntEQ(SSL_in_init(ssl), 1);
  49664. SSL_CTX_free(ctx);
  49665. SSL_free(ssl);
  49666. #else
  49667. (void)ctx;
  49668. (void)ssl;
  49669. (void)testCertFile;
  49670. (void)testKeyFile;
  49671. #endif
  49672. printf(resultFmt, passed);
  49673. #endif
  49674. return 0;
  49675. }
  49676. static int test_wolfSSL_EC_curve(void)
  49677. {
  49678. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  49679. int nid = NID_secp160k1;
  49680. const char* nid_name;
  49681. printf(testingFmt, "test_wolfSSL_EC_curve()");
  49682. AssertNotNull(nid_name = EC_curve_nid2nist(nid));
  49683. AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  49684. AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
  49685. printf(resultFmt, passed);
  49686. #endif
  49687. return 0;
  49688. }
  49689. static int test_wolfSSL_CTX_set_timeout(void)
  49690. {
  49691. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  49692. int timeout;
  49693. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  49694. (void)timeout;
  49695. printf(testingFmt, "test_wolfSSL_CTX_set_timeout()");
  49696. AssertNotNull(ctx);
  49697. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  49698. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  49699. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  49700. */
  49701. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  49702. /* giving 0 as timeout value sets default timeout */
  49703. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  49704. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  49705. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  49706. #else
  49707. AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  49708. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  49709. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  49710. #endif
  49711. wolfSSL_CTX_free(ctx);
  49712. printf(resultFmt, passed);
  49713. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  49714. return 0;
  49715. }
  49716. static int test_wolfSSL_OpenSSL_version(void)
  49717. {
  49718. #if defined(OPENSSL_EXTRA)
  49719. const char* ver;
  49720. printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
  49721. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  49722. AssertNotNull(ver = OpenSSL_version(0));
  49723. #else
  49724. AssertNotNull(ver = OpenSSL_version());
  49725. #endif
  49726. AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  49727. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  49728. printf(resultFmt, passed);
  49729. #endif
  49730. return 0;
  49731. }
  49732. static int test_CONF_CTX_CMDLINE(void)
  49733. {
  49734. #if defined(OPENSSL_ALL)
  49735. SSL_CTX* ctx = NULL;
  49736. SSL_CONF_CTX* cctx = NULL;
  49737. printf(testingFmt, "test_CONF_CTX_CMDLINE");
  49738. AssertNotNull(cctx = SSL_CONF_CTX_new());
  49739. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  49740. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  49741. AssertTrue(1);
  49742. /* set flags */
  49743. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  49744. WOLFSSL_CONF_FLAG_CMDLINE);
  49745. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  49746. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  49747. /* cmd invalid command */
  49748. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  49749. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  49750. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  49751. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  49752. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  49753. /* cmd Certificate and Private Key*/
  49754. {
  49755. #if !defined(NO_CERTS) && !defined(NO_RSA)
  49756. const char* ourCert = svrCertFile;
  49757. const char* ourKey = svrKeyFile;
  49758. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  49759. AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
  49760. WOLFSSL_SUCCESS);
  49761. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  49762. AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  49763. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49764. #endif
  49765. }
  49766. /* cmd curves */
  49767. {
  49768. #if defined(HAVE_ECC)
  49769. const char* curve = "secp256r1";
  49770. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  49771. AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  49772. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49773. #endif
  49774. }
  49775. /* cmd CipherString */
  49776. {
  49777. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  49778. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  49779. AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  49780. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49781. }
  49782. /* cmd DH parameter */
  49783. {
  49784. #if !defined(NO_DH) && !defined(NO_BIO)
  49785. const char* ourdhcert = "./certs/dh2048.pem";
  49786. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
  49787. -3);
  49788. AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
  49789. WOLFSSL_SUCCESS);
  49790. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49791. #endif
  49792. }
  49793. SSL_CTX_free(ctx);
  49794. SSL_CONF_CTX_free(cctx);
  49795. printf(resultFmt, passed);
  49796. #endif /* OPENSSL_EXTRA */
  49797. return 0;
  49798. }
  49799. static int test_CONF_CTX_FILE(void)
  49800. {
  49801. #if defined(OPENSSL_ALL)
  49802. SSL_CTX* ctx = NULL;
  49803. SSL_CONF_CTX* cctx = NULL;
  49804. printf(testingFmt, "test_CONF_CTX_FILE");
  49805. AssertNotNull(cctx = SSL_CONF_CTX_new());
  49806. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  49807. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  49808. AssertTrue(1);
  49809. /* set flags */
  49810. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  49811. WOLFSSL_CONF_FLAG_FILE);
  49812. AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  49813. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  49814. /* sanity check */
  49815. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  49816. AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  49817. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  49818. AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  49819. AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  49820. /* cmd Certificate and Private Key*/
  49821. {
  49822. #if !defined(NO_CERTS) && !defined(NO_RSA)
  49823. const char* ourCert = svrCertFile;
  49824. const char* ourKey = svrKeyFile;
  49825. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  49826. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  49827. AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  49828. WOLFSSL_SUCCESS);
  49829. AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  49830. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49831. #endif
  49832. }
  49833. /* cmd curves */
  49834. {
  49835. #if defined(HAVE_ECC)
  49836. const char* curve = "secp256r1";
  49837. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  49838. AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  49839. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49840. #endif
  49841. }
  49842. /* cmd CipherString */
  49843. {
  49844. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  49845. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  49846. AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
  49847. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49848. }
  49849. /* cmd DH parameter */
  49850. {
  49851. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  49852. const char* ourdhcert = "./certs/dh3072.pem";
  49853. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  49854. AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  49855. WOLFSSL_SUCCESS);
  49856. AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  49857. #endif
  49858. }
  49859. SSL_CTX_free(ctx);
  49860. SSL_CONF_CTX_free(cctx);
  49861. printf(resultFmt, passed);
  49862. #endif /* OPENSSL_EXTRA */
  49863. return 0;
  49864. }
  49865. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  49866. {
  49867. #ifdef HAVE_EX_DATA
  49868. int idx1,idx2;
  49869. printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
  49870. /* test for unsupported class index */
  49871. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  49872. 0,NULL, NULL, NULL, NULL ), -1);
  49873. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  49874. 0,NULL, NULL, NULL, NULL ), -1);
  49875. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  49876. 0,NULL, NULL, NULL, NULL ), -1);
  49877. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  49878. 0,NULL, NULL, NULL, NULL ), -1);
  49879. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  49880. 0,NULL, NULL, NULL, NULL ), -1);
  49881. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  49882. 0,NULL, NULL, NULL, NULL ), -1);
  49883. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  49884. 0,NULL, NULL, NULL, NULL ), -1);
  49885. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  49886. 0,NULL, NULL, NULL, NULL ), -1);
  49887. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  49888. 0,NULL, NULL, NULL, NULL ), -1);
  49889. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  49890. 0,NULL, NULL, NULL, NULL ), -1);
  49891. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  49892. 0,NULL, NULL, NULL, NULL ), -1);
  49893. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  49894. 0,NULL, NULL, NULL, NULL ), -1);
  49895. AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
  49896. /* test for supported class index */
  49897. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  49898. 0,NULL, NULL, NULL, NULL );
  49899. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  49900. 0,NULL, NULL, NULL, NULL );
  49901. AssertIntNE(idx1, -1);
  49902. AssertIntNE(idx2, -1);
  49903. AssertIntNE(idx1, idx2);
  49904. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  49905. 0,NULL, NULL, NULL, NULL );
  49906. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  49907. 0,NULL, NULL, NULL, NULL );
  49908. AssertIntNE(idx1, -1);
  49909. AssertIntNE(idx2, -1);
  49910. AssertIntNE(idx1, idx2);
  49911. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  49912. 0,NULL, NULL, NULL, NULL );
  49913. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  49914. 0,NULL, NULL, NULL, NULL );
  49915. AssertIntNE(idx1, -1);
  49916. AssertIntNE(idx2, -1);
  49917. AssertIntNE(idx1, idx2);
  49918. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  49919. 0,NULL, NULL, NULL, NULL );
  49920. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  49921. 0,NULL, NULL, NULL, NULL );
  49922. AssertIntNE(idx1, -1);
  49923. AssertIntNE(idx2, -1);
  49924. AssertIntNE(idx1, idx2);
  49925. printf(resultFmt, "passed");
  49926. #endif /* HAVE_EX_DATA */
  49927. return 0;
  49928. }
  49929. static int test_wolfSSL_set_psk_use_session_callback(void)
  49930. {
  49931. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  49932. SSL_CTX* ctx;
  49933. SSL* ssl;
  49934. const char* testCertFile;
  49935. const char* testKeyFile;
  49936. printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
  49937. #ifdef WOLFSSL_TLS13
  49938. #ifdef NO_WOLFSSL_SERVER
  49939. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  49940. #else
  49941. AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49942. #endif
  49943. #ifndef NO_RSA
  49944. testCertFile = svrCertFile;
  49945. testKeyFile = svrKeyFile;
  49946. #elif defined(HAVE_ECC)
  49947. testCertFile = eccCertFile;
  49948. testKeyFile = eccKeyFile;
  49949. #else
  49950. testCertFile = NULL;
  49951. testKeyFile = NULL;
  49952. #endif
  49953. if (testCertFile != NULL && testKeyFile != NULL) {
  49954. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  49955. SSL_FILETYPE_PEM));
  49956. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  49957. SSL_FILETYPE_PEM));
  49958. }
  49959. ssl = SSL_new(ctx);
  49960. AssertNotNull(ssl);
  49961. SSL_set_psk_use_session_callback(ssl,
  49962. my_psk_use_session_cb);
  49963. AssertTrue(1);
  49964. SSL_CTX_free(ctx);
  49965. SSL_free(ssl);
  49966. #else
  49967. (void)ctx;
  49968. (void)ssl;
  49969. (void)testCertFile;
  49970. (void)testKeyFile;
  49971. #endif
  49972. printf(resultFmt, passed);
  49973. #endif
  49974. return 0;
  49975. }
  49976. static int test_wolfSSL_ERR_strings(void)
  49977. {
  49978. const char* err1 = "unsupported cipher suite";
  49979. const char* err2 = "wolfSSL PEM routines";
  49980. const char* err = NULL;
  49981. (void)err;
  49982. (void)err1;
  49983. (void)err2;
  49984. #if !defined(NO_ERROR_STRINGS)
  49985. printf(testingFmt, "test_wolfSSL_ERR_strings");
  49986. #if defined(OPENSSL_EXTRA)
  49987. err = ERR_reason_error_string(UNSUPPORTED_SUITE);
  49988. AssertTrue(err != NULL);
  49989. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  49990. err = ERR_func_error_string(UNSUPPORTED_SUITE);
  49991. AssertTrue(err != NULL);
  49992. AssertIntEQ((*err == '\0'), 1);
  49993. err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
  49994. AssertTrue(err != NULL);
  49995. AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  49996. #else
  49997. err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
  49998. AssertTrue(err != NULL);
  49999. AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  50000. err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
  50001. AssertTrue(err != NULL);
  50002. AssertIntEQ((*err == '\0'), 1);
  50003. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  50004. err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
  50005. AssertTrue(err != NULL);
  50006. AssertIntEQ((*err == '\0'), 1);
  50007. #endif
  50008. printf(resultFmt, passed);
  50009. #endif
  50010. return 0;
  50011. }
  50012. static int test_wolfSSL_EVP_shake128(void)
  50013. {
  50014. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  50015. defined(WOLFSSL_SHAKE128)
  50016. printf(testingFmt, "test_wolfSSL_EVP_shake128");
  50017. const EVP_MD* md = NULL;
  50018. md = EVP_shake128();
  50019. AssertTrue(md != NULL);
  50020. AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  50021. printf(resultFmt, passed);
  50022. #endif
  50023. return 0;
  50024. }
  50025. static int test_wolfSSL_EVP_shake256(void)
  50026. {
  50027. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  50028. defined(WOLFSSL_SHAKE256)
  50029. const EVP_MD* md = NULL;
  50030. printf(testingFmt, "test_wolfSSL_EVP_shake256");
  50031. md = EVP_shake256();
  50032. AssertTrue(md != NULL);
  50033. AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  50034. printf(resultFmt, passed);
  50035. #endif
  50036. return 0;
  50037. }
  50038. static int test_EVP_blake2(void)
  50039. {
  50040. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  50041. const EVP_MD* md = NULL;
  50042. (void)md;
  50043. printf(testingFmt, "test_EVP_blake2");
  50044. #if defined(HAVE_BLAKE2)
  50045. md = EVP_blake2b512();
  50046. AssertTrue(md != NULL);
  50047. AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  50048. #endif
  50049. #if defined(HAVE_BLAKE2S)
  50050. md = EVP_blake2s256();
  50051. AssertTrue(md != NULL);
  50052. AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  50053. #endif
  50054. printf(resultFmt, passed);
  50055. #endif
  50056. return 0;
  50057. }
  50058. #if defined(OPENSSL_EXTRA)
  50059. static void list_md_fn(const EVP_MD* m, const char* from,
  50060. const char* to, void* arg)
  50061. {
  50062. const char* mn;
  50063. BIO *bio;
  50064. (void) from;
  50065. (void) to;
  50066. (void) arg;
  50067. (void) mn;
  50068. (void) bio;
  50069. if (!m) {
  50070. /* alias */
  50071. AssertNull(m);
  50072. AssertNotNull(to);
  50073. }
  50074. else {
  50075. AssertNotNull(m);
  50076. AssertNull(to);
  50077. }
  50078. AssertNotNull(from);
  50079. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  50080. mn = EVP_get_digestbyname(from);
  50081. /* print to stdout */
  50082. AssertNotNull(arg);
  50083. bio = BIO_new(BIO_s_file());
  50084. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  50085. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  50086. BIO_free(bio);
  50087. #endif
  50088. }
  50089. #endif
  50090. static int test_EVP_MD_do_all(void)
  50091. {
  50092. #if defined(OPENSSL_EXTRA)
  50093. printf(testingFmt, "test_EVP_MD_do_all");
  50094. EVP_MD_do_all(NULL, stdout);
  50095. /* to confirm previous call gives no harm */
  50096. AssertTrue(1);
  50097. EVP_MD_do_all(list_md_fn, stdout);
  50098. /* to confirm previous call gives no harm */
  50099. AssertTrue(1);
  50100. printf(resultFmt, passed);
  50101. #endif
  50102. return 0;
  50103. }
  50104. #if defined(OPENSSL_EXTRA)
  50105. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  50106. {
  50107. (void)arg;
  50108. (void)nm;
  50109. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  50110. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  50111. /* print to stdout */
  50112. AssertNotNull(arg);
  50113. bio = BIO_new(BIO_s_file());
  50114. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  50115. BIO_printf(bio, "%s\n", mn);
  50116. BIO_free(bio);
  50117. #endif
  50118. }
  50119. #endif
  50120. static int test_OBJ_NAME_do_all(void)
  50121. {
  50122. #if defined(OPENSSL_EXTRA)
  50123. printf(testingFmt, "test_OBJ_NAME_do_all");
  50124. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  50125. /* to confirm previous call gives no harm */
  50126. AssertTrue(1);
  50127. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stdout);
  50128. /* to confirm previous call gives no harm */
  50129. AssertTrue(1);
  50130. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
  50131. AssertTrue(1);
  50132. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
  50133. AssertTrue(1);
  50134. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
  50135. AssertTrue(1);
  50136. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
  50137. AssertTrue(1);
  50138. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
  50139. AssertTrue(1);
  50140. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
  50141. AssertTrue(1);
  50142. OBJ_NAME_do_all(-1, obj_name_t, stdout);
  50143. AssertTrue(1);
  50144. printf(resultFmt, passed);
  50145. #endif
  50146. return 0;
  50147. }
  50148. static int test_SSL_CIPHER_get_xxx(void)
  50149. {
  50150. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  50151. !defined(NO_FILESYSTEM)
  50152. const SSL_CIPHER* cipher = NULL;
  50153. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  50154. int i, numCiphers = 0;
  50155. SSL_CTX* ctx = NULL;
  50156. SSL* ssl = NULL;
  50157. const char* testCertFile;
  50158. const char* testKeyFile;
  50159. char buf[256] = {0};
  50160. const char* cipher_id = NULL;
  50161. int expect_nid1 = NID_undef;
  50162. int expect_nid2 = NID_undef;
  50163. int expect_nid3 = NID_undef;
  50164. int expect_nid4 = NID_undef;
  50165. int expect_nid5 = 0;
  50166. const char* cipher_id2 = NULL;
  50167. int expect_nid21 = NID_undef;
  50168. int expect_nid22 = NID_undef;
  50169. int expect_nid23 = NID_undef;
  50170. int expect_nid24 = NID_undef;
  50171. int expect_nid25 = 0;
  50172. (void)cipher;
  50173. (void)supportedCiphers;
  50174. (void)i;
  50175. (void)numCiphers;
  50176. (void)ctx;
  50177. (void)ssl;
  50178. (void)testCertFile;
  50179. (void)testKeyFile;
  50180. printf(testingFmt, "test_SSL_CIPHER_get_xxx");
  50181. #if defined(WOLFSSL_TLS13)
  50182. cipher_id = "TLS13-AES128-GCM-SHA256";
  50183. expect_nid1 = NID_auth_rsa;
  50184. expect_nid2 = NID_aes_128_gcm;
  50185. expect_nid3 = NID_sha256;
  50186. expect_nid4 = NID_kx_any;
  50187. expect_nid5 = 1;
  50188. #if !defined(WOLFSSL_NO_TLS12)
  50189. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  50190. expect_nid21 = NID_auth_rsa;
  50191. expect_nid22 = NID_aes_256_gcm;
  50192. expect_nid23 = NID_sha384;
  50193. expect_nid24 = NID_kx_ecdhe;
  50194. expect_nid25 = 1;
  50195. #endif
  50196. #endif
  50197. #ifdef NO_WOLFSSL_SERVER
  50198. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  50199. #else
  50200. AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  50201. #endif
  50202. if (cipher_id) {
  50203. #ifndef NO_RSA
  50204. testCertFile = svrCertFile;
  50205. testKeyFile = svrKeyFile;
  50206. #elif defined(HAVE_ECC)
  50207. testCertFile = eccCertFile;
  50208. testKeyFile = eccKeyFile;
  50209. #else
  50210. testCertFile = NULL;
  50211. testKeyFile = NULL;
  50212. #endif
  50213. if (testCertFile != NULL && testKeyFile != NULL) {
  50214. AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  50215. SSL_FILETYPE_PEM));
  50216. AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  50217. SSL_FILETYPE_PEM));
  50218. }
  50219. ssl = SSL_new(ctx);
  50220. AssertNotNull(ssl);
  50221. AssertIntEQ(SSL_in_init(ssl), 1);
  50222. supportedCiphers = SSL_get_ciphers(ssl);
  50223. numCiphers = sk_num(supportedCiphers);
  50224. for (i = 0; i < numCiphers; ++i) {
  50225. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  50226. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  50227. }
  50228. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  50229. break;
  50230. }
  50231. }
  50232. /* test case for */
  50233. if (i != numCiphers) {
  50234. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  50235. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  50236. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  50237. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  50238. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  50239. }
  50240. if (cipher_id2) {
  50241. for (i = 0; i < numCiphers; ++i) {
  50242. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  50243. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  50244. }
  50245. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  50246. break;
  50247. }
  50248. }
  50249. /* test case for */
  50250. if (i != numCiphers) {
  50251. AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  50252. AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  50253. AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  50254. AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  50255. AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  50256. }
  50257. }
  50258. }
  50259. if (ctx)
  50260. SSL_CTX_free(ctx);
  50261. if(ssl)
  50262. SSL_free(ssl);
  50263. printf(resultFmt, passed);
  50264. #endif
  50265. return 0;
  50266. }
  50267. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  50268. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  50269. int* keyFormat)
  50270. {
  50271. int ret;
  50272. byte* key_buf = NULL;
  50273. size_t key_sz = 0;
  50274. EncryptedInfo encInfo;
  50275. XMEMSET(&encInfo, 0, sizeof(encInfo));
  50276. ret = load_file(privKeyFile, &key_buf, &key_sz);
  50277. if (ret == 0) {
  50278. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  50279. NULL, &encInfo, keyFormat);
  50280. }
  50281. if (key_buf != NULL) {
  50282. free(key_buf); key_buf = NULL;
  50283. }
  50284. (void)encInfo; /* not used in this test */
  50285. #ifdef DEBUG_WOLFSSL
  50286. printf("%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  50287. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  50288. (*pDer)->length);
  50289. #endif
  50290. return ret;
  50291. }
  50292. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  50293. {
  50294. int ret = CRYPTOCB_UNAVAILABLE;
  50295. const char* privKeyFile = (const char*)ctx;
  50296. DerBuffer* pDer = NULL;
  50297. int keyFormat = 0;
  50298. if (info->algo_type == WC_ALGO_TYPE_PK) {
  50299. #ifdef DEBUG_WOLFSSL
  50300. printf("test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  50301. #endif
  50302. #ifndef NO_RSA
  50303. if (info->pk.type == WC_PK_TYPE_RSA) {
  50304. switch (info->pk.rsa.type) {
  50305. case RSA_PUBLIC_ENCRYPT:
  50306. case RSA_PUBLIC_DECRYPT:
  50307. /* perform software based RSA public op */
  50308. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  50309. break;
  50310. case RSA_PRIVATE_ENCRYPT:
  50311. case RSA_PRIVATE_DECRYPT:
  50312. {
  50313. RsaKey key;
  50314. /* perform software based RSA private op */
  50315. #ifdef DEBUG_WOLFSSL
  50316. printf("test_CryptoCb_Func: RSA Priv\n");
  50317. #endif
  50318. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  50319. &keyFormat);
  50320. if (ret != 0) {
  50321. return ret;
  50322. }
  50323. ret = wc_InitRsaKey(&key, HEAP_HINT);
  50324. if (ret == 0) {
  50325. word32 keyIdx = 0;
  50326. /* load RSA private key and perform private transform */
  50327. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  50328. &key, pDer->length);
  50329. if (ret == 0) {
  50330. ret = wc_RsaFunction(
  50331. info->pk.rsa.in, info->pk.rsa.inLen,
  50332. info->pk.rsa.out, info->pk.rsa.outLen,
  50333. info->pk.rsa.type, &key, info->pk.rsa.rng);
  50334. }
  50335. else {
  50336. /* if decode fails, then fall-back to software based crypto */
  50337. printf("test_CryptoCb_Func: RSA private key decode "
  50338. "failed %d, falling back to software\n", ret);
  50339. ret = CRYPTOCB_UNAVAILABLE;
  50340. }
  50341. wc_FreeRsaKey(&key);
  50342. }
  50343. wc_FreeDer(&pDer); pDer = NULL;
  50344. break;
  50345. }
  50346. }
  50347. #ifdef DEBUG_WOLFSSL
  50348. printf("test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  50349. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  50350. #endif
  50351. }
  50352. #endif /* !NO_RSA */
  50353. #ifdef HAVE_ECC
  50354. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  50355. /* mark this key as ephemeral */
  50356. if (info->pk.eckg.key != NULL) {
  50357. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  50358. sizeof(info->pk.eckg.key->label));
  50359. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  50360. }
  50361. }
  50362. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  50363. ecc_key key;
  50364. /* perform software based ECC sign */
  50365. #ifdef DEBUG_WOLFSSL
  50366. printf("test_CryptoCb_Func: ECC Sign\n");
  50367. #endif
  50368. if (info->pk.eccsign.key != NULL &&
  50369. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  50370. /* this is an empheral key */
  50371. #ifdef DEBUG_WOLFSSL
  50372. printf("test_CryptoCb_Func: skipping signing op on ephemeral key\n");
  50373. #endif
  50374. return CRYPTOCB_UNAVAILABLE;
  50375. }
  50376. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  50377. if (ret != 0) {
  50378. return ret;
  50379. }
  50380. ret = wc_ecc_init(&key);
  50381. if (ret == 0) {
  50382. word32 keyIdx = 0;
  50383. /* load ECC private key and perform private transform */
  50384. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  50385. &key, pDer->length);
  50386. if (ret == 0) {
  50387. ret = wc_ecc_sign_hash(
  50388. info->pk.eccsign.in, info->pk.eccsign.inlen,
  50389. info->pk.eccsign.out, info->pk.eccsign.outlen,
  50390. info->pk.eccsign.rng, &key);
  50391. }
  50392. else {
  50393. /* if decode fails, then fall-back to software based crypto */
  50394. printf("test_CryptoCb_Func: ECC private key decode "
  50395. "failed %d, falling back to software\n", ret);
  50396. ret = CRYPTOCB_UNAVAILABLE;
  50397. }
  50398. wc_ecc_free(&key);
  50399. }
  50400. wc_FreeDer(&pDer); pDer = NULL;
  50401. #ifdef DEBUG_WOLFSSL
  50402. printf("test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  50403. ret, *info->pk.eccsign.outlen);
  50404. #endif
  50405. }
  50406. #endif /* HAVE_ECC */
  50407. #ifdef HAVE_ED25519
  50408. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  50409. ed25519_key key;
  50410. /* perform software based ED25519 sign */
  50411. #ifdef DEBUG_WOLFSSL
  50412. printf("test_CryptoCb_Func: ED25519 Sign\n");
  50413. #endif
  50414. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  50415. if (ret != 0) {
  50416. return ret;
  50417. }
  50418. ret = wc_ed25519_init(&key);
  50419. if (ret == 0) {
  50420. word32 keyIdx = 0;
  50421. /* load ED25519 private key and perform private transform */
  50422. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  50423. &key, pDer->length);
  50424. if (ret == 0) {
  50425. /* calculate public key */
  50426. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  50427. if (ret == 0) {
  50428. key.pubKeySet = 1;
  50429. ret = wc_ed25519_sign_msg_ex(
  50430. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  50431. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  50432. &key, info->pk.ed25519sign.type,
  50433. info->pk.ed25519sign.context,
  50434. info->pk.ed25519sign.contextLen);
  50435. }
  50436. }
  50437. else {
  50438. /* if decode fails, then fall-back to software based crypto */
  50439. printf("test_CryptoCb_Func: ED25519 private key decode "
  50440. "failed %d, falling back to software\n", ret);
  50441. ret = CRYPTOCB_UNAVAILABLE;
  50442. }
  50443. wc_ed25519_free(&key);
  50444. }
  50445. wc_FreeDer(&pDer); pDer = NULL;
  50446. #ifdef DEBUG_WOLFSSL
  50447. printf("test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  50448. ret, *info->pk.ed25519sign.outLen);
  50449. #endif
  50450. }
  50451. #endif /* HAVE_ED25519 */
  50452. }
  50453. (void)thisDevId;
  50454. (void)keyFormat;
  50455. return ret;
  50456. }
  50457. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  50458. static void test_wc_CryptoCb_TLS(int tlsVer,
  50459. const char* cliCaPemFile, const char* cliCertPemFile,
  50460. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  50461. const char* svrCaPemFile, const char* svrCertPemFile,
  50462. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  50463. {
  50464. callback_functions client_cbf;
  50465. callback_functions server_cbf;
  50466. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  50467. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  50468. if (tlsVer == WOLFSSL_TLSV1_3) {
  50469. #ifdef WOLFSSL_TLS13
  50470. server_cbf.method = wolfTLSv1_3_server_method;
  50471. client_cbf.method = wolfTLSv1_3_client_method;
  50472. #endif
  50473. }
  50474. else if (tlsVer == WOLFSSL_TLSV1_2) {
  50475. #ifndef WOLFSSL_NO_TLS12
  50476. server_cbf.method = wolfTLSv1_2_server_method;
  50477. client_cbf.method = wolfTLSv1_2_client_method;
  50478. #endif
  50479. }
  50480. else if (tlsVer == WOLFSSL_TLSV1_1) {
  50481. #ifndef NO_OLD_TLS
  50482. server_cbf.method = wolfTLSv1_1_server_method;
  50483. client_cbf.method = wolfTLSv1_1_client_method;
  50484. #endif
  50485. }
  50486. else if (tlsVer == WOLFSSL_TLSV1) {
  50487. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  50488. server_cbf.method = wolfTLSv1_server_method;
  50489. client_cbf.method = wolfTLSv1_client_method;
  50490. #endif
  50491. }
  50492. else if (tlsVer == WOLFSSL_SSLV3) {
  50493. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  50494. defined(WOLFSSL_STATIC_RSA)
  50495. server_cbf.method = wolfSSLv3_server_method;
  50496. client_cbf.method = wolfSSLv3_client_method;
  50497. #endif
  50498. }
  50499. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  50500. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  50501. server_cbf.method = wolfDTLSv1_2_server_method;
  50502. client_cbf.method = wolfDTLSv1_2_client_method;
  50503. #endif
  50504. }
  50505. else if (tlsVer == WOLFSSL_DTLSV1) {
  50506. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  50507. server_cbf.method = wolfDTLSv1_server_method;
  50508. client_cbf.method = wolfDTLSv1_client_method;
  50509. #endif
  50510. }
  50511. if (server_cbf.method == NULL) {
  50512. /* not enabled */
  50513. return;
  50514. }
  50515. /* Setup the keys for the TLS test */
  50516. client_cbf.certPemFile = cliCertPemFile;
  50517. client_cbf.keyPemFile = cliPubKeyPemFile;
  50518. client_cbf.caPemFile = cliCaPemFile;
  50519. server_cbf.certPemFile = svrCertPemFile;
  50520. server_cbf.keyPemFile = svrPubKeyPemFile;
  50521. server_cbf.caPemFile = svrCaPemFile;
  50522. /* Setup a crypto callback with pointer to private key file for testing */
  50523. client_cbf.devId = 1;
  50524. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  50525. (void*)cliPrivKeyPemFile);
  50526. server_cbf.devId = 2;
  50527. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  50528. (void*)svrPrivKeyPemFile);
  50529. /* Perform TLS server and client test */
  50530. /* First test is at WOLFSSL_CTX level */
  50531. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50532. /* Check for success */
  50533. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50534. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50535. /* Second test is a WOLFSSL object level */
  50536. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  50537. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50538. /* Check for success */
  50539. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50540. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50541. /* Un register the devId's */
  50542. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  50543. client_cbf.devId = INVALID_DEVID;
  50544. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  50545. server_cbf.devId = INVALID_DEVID;
  50546. }
  50547. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  50548. static int test_wc_CryptoCb(void)
  50549. {
  50550. #ifdef WOLF_CRYPTO_CB
  50551. /* TODO: Add crypto callback API tests */
  50552. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50553. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  50554. int tlsVer;
  50555. #endif
  50556. #ifndef NO_RSA
  50557. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50558. test_wc_CryptoCb_TLS(tlsVer,
  50559. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  50560. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
  50561. }
  50562. #endif
  50563. #ifdef HAVE_ECC
  50564. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50565. test_wc_CryptoCb_TLS(tlsVer,
  50566. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  50567. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
  50568. }
  50569. #endif
  50570. #ifdef HAVE_ED25519
  50571. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  50572. if (tlsVer == WOLFSSL_DTLSV1) continue;
  50573. test_wc_CryptoCb_TLS(tlsVer,
  50574. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  50575. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
  50576. }
  50577. #endif
  50578. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  50579. #endif /* WOLF_CRYPTO_CB */
  50580. return 0;
  50581. }
  50582. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  50583. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  50584. static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  50585. const char* cliCaPemFile, const char* cliCertPemFile,
  50586. const char* cliPrivKeyPemFile,
  50587. const char* svrCaPemFile, const char* svrCertPemFile,
  50588. const char* svrPrivKeyPemFile,
  50589. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  50590. {
  50591. callback_functions client_cbf;
  50592. callback_functions server_cbf;
  50593. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  50594. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  50595. if (tlsVer == WOLFSSL_TLSV1_3) {
  50596. #ifdef WOLFSSL_TLS13
  50597. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  50598. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  50599. #endif
  50600. }
  50601. else if (tlsVer == WOLFSSL_TLSV1_2) {
  50602. #ifndef WOLFSSL_NO_TLS12
  50603. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  50604. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  50605. #endif
  50606. }
  50607. else if (tlsVer == WOLFSSL_TLSV1_1) {
  50608. #ifndef NO_OLD_TLS
  50609. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  50610. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  50611. #endif
  50612. }
  50613. else if (tlsVer == WOLFSSL_TLSV1) {
  50614. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  50615. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  50616. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  50617. #endif
  50618. }
  50619. else if (tlsVer == WOLFSSL_SSLV3) {
  50620. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  50621. defined(WOLFSSL_STATIC_RSA)
  50622. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  50623. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  50624. #endif
  50625. }
  50626. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  50627. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  50628. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  50629. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  50630. #endif
  50631. }
  50632. else if (tlsVer == WOLFSSL_DTLSV1) {
  50633. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  50634. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  50635. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  50636. #endif
  50637. }
  50638. if (server_cbf.method_ex == NULL) {
  50639. /* not enabled */
  50640. return;
  50641. }
  50642. /* Setup the keys for the TLS test */
  50643. client_cbf.certPemFile = cliCertPemFile;
  50644. client_cbf.keyPemFile = cliPrivKeyPemFile;
  50645. client_cbf.caPemFile = cliCaPemFile;
  50646. server_cbf.certPemFile = svrCertPemFile;
  50647. server_cbf.keyPemFile = svrPrivKeyPemFile;
  50648. server_cbf.caPemFile = svrCaPemFile;
  50649. client_cbf.mem = cliMem;
  50650. client_cbf.memSz = cliMemSz;
  50651. server_cbf.mem = svrMem;
  50652. server_cbf.memSz = svrMemSz;
  50653. client_cbf.devId = INVALID_DEVID;
  50654. server_cbf.devId = INVALID_DEVID;
  50655. /* Perform TLS server and client test */
  50656. /* First test is at WOLFSSL_CTX level */
  50657. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50658. /* Check for success */
  50659. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50660. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50661. /* Second test is a WOLFSSL object level */
  50662. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  50663. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  50664. /* Check for success */
  50665. AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
  50666. AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
  50667. }
  50668. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  50669. #ifdef WOLFSSL_STATIC_MEMORY
  50670. #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
  50671. defined(SESSION_CERTS)
  50672. #ifdef OPENSSL_EXTRA
  50673. #define TEST_TLS_STATIC_MEMSZ (400000)
  50674. #else
  50675. #define TEST_TLS_STATIC_MEMSZ (320000)
  50676. #endif
  50677. #else
  50678. #define TEST_TLS_STATIC_MEMSZ (80000)
  50679. #endif
  50680. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  50681. {
  50682. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  50683. WOLFSSL_MEM_STATS mem_stats;
  50684. WOLFSSL_MEM_CONN_STATS ssl_stats;
  50685. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  50686. AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  50687. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50688. AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  50689. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  50690. #endif
  50691. AssertNotNull((ssl1 = wolfSSL_new(ctx)));
  50692. AssertNotNull((ssl2 = wolfSSL_new(ctx)));
  50693. /* this should fail because kMaxCtxClients == 2 */
  50694. AssertNull((ssl3 = wolfSSL_new(ctx)));
  50695. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  50696. #ifdef DEBUG_WOLFSSL
  50697. wolfSSL_PrintStatsConn(&ssl_stats);
  50698. #endif
  50699. (void)ssl_stats;
  50700. }
  50701. /* display collected statistics */
  50702. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  50703. #ifdef DEBUG_WOLFSSL
  50704. wolfSSL_PrintStats(&mem_stats);
  50705. #endif
  50706. (void)mem_stats;
  50707. }
  50708. wolfSSL_free(ssl1);
  50709. wolfSSL_free(ssl2);
  50710. return 0;
  50711. }
  50712. #endif /* WOLFSSL_STATIC_MEMORY */
  50713. static int test_wolfSSL_CTX_StaticMemory(void)
  50714. {
  50715. #ifdef WOLFSSL_STATIC_MEMORY
  50716. wolfSSL_method_func method_func;
  50717. WOLFSSL_CTX* ctx;
  50718. const int kMaxCtxClients = 2;
  50719. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50720. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  50721. int tlsVer;
  50722. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  50723. #endif
  50724. #endif
  50725. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  50726. printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
  50727. #ifndef NO_WOLFSSL_SERVER
  50728. #ifndef WOLFSSL_NO_TLS12
  50729. method_func = wolfTLSv1_2_server_method_ex;
  50730. #else
  50731. method_func = wolfTLSv1_3_server_method_ex;
  50732. #endif
  50733. #else
  50734. #ifndef WOLFSSL_NO_TLS12
  50735. method_func = wolfTLSv1_2_client_method_ex;
  50736. #else
  50737. method_func = wolfTLSv1_3_client_method_ex;
  50738. #endif
  50739. #endif
  50740. /* Test creating CTX directly from static memory pool */
  50741. ctx = NULL;
  50742. AssertIntEQ(wolfSSL_CTX_load_static_memory(
  50743. &ctx, method_func, svrMem, sizeof(svrMem),
  50744. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  50745. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  50746. wolfSSL_CTX_free(ctx);
  50747. ctx = NULL;
  50748. /* Test for heap allocated CTX, then assigning static pool to it */
  50749. AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  50750. AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
  50751. NULL, svrMem, sizeof(svrMem),
  50752. 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  50753. test_wolfSSL_CTX_StaticMemory_SSL(ctx);
  50754. wolfSSL_CTX_free(ctx);
  50755. /* TLS Level Tests using static memory */
  50756. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  50757. #ifndef NO_RSA
  50758. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50759. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50760. svrCertFile, cliCertFile, cliKeyFile,
  50761. cliCertFile, svrCertFile, svrKeyFile,
  50762. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50763. }
  50764. #endif
  50765. #ifdef HAVE_ECC
  50766. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  50767. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50768. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  50769. cliEccCertFile, eccCertFile, eccKeyFile,
  50770. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50771. }
  50772. #endif
  50773. #ifdef HAVE_ED25519
  50774. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  50775. if (tlsVer == WOLFSSL_DTLSV1) continue;
  50776. test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  50777. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  50778. cliEdCertFile, edCertFile, edKeyFile,
  50779. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
  50780. }
  50781. #endif
  50782. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  50783. printf(resultFmt, passed);
  50784. #endif
  50785. return 0;
  50786. }
  50787. static int test_openssl_FIPS_drbg(void)
  50788. {
  50789. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  50790. DRBG_CTX* dctx;
  50791. byte data1[32], data2[32], zeroData[32];
  50792. byte testSeed[16];
  50793. size_t dlen = sizeof(data1);
  50794. int i;
  50795. XMEMSET(data1, 0, dlen);
  50796. XMEMSET(data2, 0, dlen);
  50797. XMEMSET(zeroData, 0, sizeof(zeroData));
  50798. for (i=0; i<(int)sizeof(testSeed); i++) {
  50799. testSeed[i] = (byte)i;
  50800. }
  50801. printf(testingFmt, "test_openssl_FIPS_drbg()");
  50802. AssertNotNull(dctx = FIPS_get_default_drbg());
  50803. AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  50804. AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  50805. WOLFSSL_SUCCESS);
  50806. AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  50807. AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  50808. WOLFSSL_SUCCESS);
  50809. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  50810. AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  50811. WOLFSSL_SUCCESS);
  50812. AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  50813. WOLFSSL_SUCCESS);
  50814. AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  50815. AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
  50816. AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  50817. printf(resultFmt, passed);
  50818. #endif
  50819. return 0;
  50820. }
  50821. static int test_wolfSSL_FIPS_mode(void)
  50822. {
  50823. #if defined(OPENSSL_ALL)
  50824. printf(testingFmt, "test_wolfSSL_FIPS_mode()");
  50825. #ifdef HAVE_FIPS
  50826. AssertIntEQ(wolfSSL_FIPS_mode(), 1);
  50827. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  50828. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  50829. #else
  50830. AssertIntEQ(wolfSSL_FIPS_mode(), 0);
  50831. AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  50832. AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  50833. #endif
  50834. printf(resultFmt, passed);
  50835. #endif
  50836. return 0;
  50837. }
  50838. #ifdef WOLFSSL_DTLS
  50839. /* Prints out the current window */
  50840. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  50841. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  50842. int i;
  50843. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  50844. word32 b = w[i];
  50845. int j;
  50846. /* Prints out a 32 bit binary number in big endian order */
  50847. for (j = 0; j < 32; j++, b <<= 1) {
  50848. if (b & (((word32)1) << 31))
  50849. printf("1");
  50850. else
  50851. printf("0");
  50852. }
  50853. printf(" ");
  50854. }
  50855. printf("cur_hi %u cur_lo %u\n", h, l);
  50856. #else
  50857. (void)h;
  50858. (void)l;
  50859. (void)w;
  50860. #endif
  50861. }
  50862. /* a - cur_hi
  50863. * b - cur_lo
  50864. * c - next_hi
  50865. * d - next_lo
  50866. * e - window
  50867. * f - expected next_hi
  50868. * g - expected next_lo
  50869. * h - expected window[1]
  50870. * i - expected window[0]
  50871. */
  50872. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  50873. wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
  50874. DUW_TEST_print_window_binary((a), (b), (e)); \
  50875. AssertIntEQ((c), (f)); \
  50876. AssertIntEQ((d), (g)); \
  50877. AssertIntEQ((e)[1], (h)); \
  50878. AssertIntEQ((e)[0], (i)); \
  50879. } while (0)
  50880. static int test_wolfSSL_DtlsUpdateWindow(void)
  50881. {
  50882. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  50883. word32 next_lo = 0;
  50884. word16 next_hi = 0;
  50885. printf(testingFmt, "wolfSSL_DtlsUpdateWindow()");
  50886. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  50887. printf("\n");
  50888. #endif
  50889. XMEMSET(window, 0, sizeof window);
  50890. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  50891. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  50892. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  50893. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  50894. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  50895. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  50896. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  50897. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  50898. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  50899. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  50900. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  50901. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  50902. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  50903. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  50904. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  50905. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  50906. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  50907. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  50908. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  50909. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  50910. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  50911. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  50912. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  50913. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  50914. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  50915. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  50916. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  50917. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  50918. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  50919. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  50920. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  50921. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  50922. printf(resultFmt, passed);
  50923. fflush(stdout);
  50924. return 0;
  50925. }
  50926. #endif /* WOLFSSL_DTLS */
  50927. #ifdef WOLFSSL_DTLS
  50928. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  50929. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  50930. {
  50931. DtlsMsg* cur;
  50932. static byte msg[100];
  50933. static byte msgInit = 0;
  50934. if (!msgInit) {
  50935. int i;
  50936. for (i = 0; i < 100; i++)
  50937. msg[i] = i + 1;
  50938. msgInit = 1;
  50939. }
  50940. /* Sanitize test parameters */
  50941. if (len > sizeof(msg))
  50942. return -1;
  50943. if (f_offset + f_len > sizeof(msg))
  50944. return -1;
  50945. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  50946. if (ssl->dtls_rx_msg_list == NULL)
  50947. return -100;
  50948. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  50949. return -200;
  50950. if (cur->fragBucketListCount != f_count)
  50951. return -300;
  50952. if (cur->ready != ready)
  50953. return -400;
  50954. if (cur->bytesReceived != bytesReceived)
  50955. return -500;
  50956. if (ready) {
  50957. if (cur->fragBucketList != NULL)
  50958. return -600;
  50959. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  50960. return -700;
  50961. }
  50962. else {
  50963. DtlsFragBucket* fb;
  50964. if (cur->fragBucketList == NULL)
  50965. return -800;
  50966. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  50967. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  50968. return -900;
  50969. }
  50970. }
  50971. return 0;
  50972. }
  50973. static void DFB_TEST_RESET(WOLFSSL* ssl)
  50974. {
  50975. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  50976. ssl->dtls_rx_msg_list = NULL;
  50977. ssl->dtls_rx_msg_list_sz = 0;
  50978. }
  50979. static int test_wolfSSL_DTLS_fragment_buckets(void)
  50980. {
  50981. WOLFSSL ssl[1];
  50982. printf(testingFmt, "wolfSSL_DTLS_fragment_buckets()");
  50983. XMEMSET(ssl, 0, sizeof(*ssl));
  50984. AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  50985. AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  50986. AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  50987. AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  50988. AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  50989. AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  50990. /* Test all permutations of 3 regions */
  50991. /* 1 2 3 */
  50992. AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  50993. AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  50994. AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  50995. /* 1 3 2 */
  50996. AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  50997. AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  50998. AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  50999. /* 2 1 3 */
  51000. AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  51001. AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  51002. AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  51003. /* 2 3 1 */
  51004. AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  51005. AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  51006. AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  51007. /* 3 1 2 */
  51008. AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  51009. AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  51010. AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  51011. /* 3 2 1 */
  51012. AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  51013. AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  51014. AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  51015. /* Test overlapping regions */
  51016. AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  51017. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  51018. AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  51019. AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  51020. AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  51021. /* Test overlapping multiple regions */
  51022. AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51023. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  51024. AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  51025. AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  51026. AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  51027. AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  51028. AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  51029. AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  51030. AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  51031. AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  51032. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  51033. AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  51034. AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  51035. AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  51036. DFB_TEST_RESET(ssl);
  51037. printf(resultFmt, passed);
  51038. return 0;
  51039. }
  51040. #endif
  51041. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  51042. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51043. !defined(NO_OLD_TLS)
  51044. static int test_WOLFSSL_dtls_version_alert(void)
  51045. {
  51046. struct test_memio_ctx test_ctx;
  51047. WOLFSSL_CTX *ctx_c, *ctx_s;
  51048. WOLFSSL *ssl_c, *ssl_s;
  51049. int ret;
  51050. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51051. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51052. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
  51053. if (ret != 0)
  51054. return -1;
  51055. /* client hello */
  51056. ret = wolfSSL_connect(ssl_c);
  51057. if (ret == 0 || ssl_c->error != WANT_READ )
  51058. return -2;
  51059. /* hrr */
  51060. ret = wolfSSL_accept(ssl_s);
  51061. if (ret == 0 || ssl_s->error != WANT_READ )
  51062. return -3;
  51063. /* client hello 1 */
  51064. ret = wolfSSL_connect(ssl_c);
  51065. if (ret == 0 || ssl_c->error != WANT_READ )
  51066. return -4;
  51067. /* server hello */
  51068. ret = wolfSSL_accept(ssl_s);
  51069. if (ret == 0 || ssl_s->error != WANT_READ )
  51070. return -5;
  51071. /* should fail */
  51072. ret = wolfSSL_connect(ssl_c);
  51073. if (ret == 0 || ssl_c->error != VERSION_ERROR)
  51074. return -6;
  51075. /* shuould fail */
  51076. ret = wolfSSL_accept(ssl_s);
  51077. if (ret == 0 ||
  51078. (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
  51079. return -7;
  51080. wolfSSL_free(ssl_c);
  51081. wolfSSL_free(ssl_s);
  51082. wolfSSL_CTX_free(ctx_c);
  51083. wolfSSL_CTX_free(ctx_s);
  51084. return 0;
  51085. }
  51086. #else
  51087. static int test_WOLFSSL_dtls_version_alert(void)
  51088. {
  51089. return 0;
  51090. }
  51091. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  51092. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  51093. * !defined(NO_OLD_TLS)
  51094. */
  51095. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  51096. && defined(WOLFSSL_TLS13) && \
  51097. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  51098. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  51099. {
  51100. struct test_memio_ctx *test_ctx;
  51101. byte buf[2048];
  51102. int idx, sz;
  51103. word32 tmp;
  51104. int ret;
  51105. idx = 5; /* space for record header */
  51106. buf[idx] = session_ticket; /* type */
  51107. idx++;
  51108. tmp = OPAQUE32_LEN +
  51109. OPAQUE32_LEN +
  51110. OPAQUE8_LEN + nonceLength +
  51111. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  51112. c32to24(tmp, buf + idx);
  51113. idx += OPAQUE24_LEN;
  51114. c32toa((word32)12345, buf+idx); /* lifetime */
  51115. idx += OPAQUE32_LEN;
  51116. c32toa((word32)12345, buf+idx); /* add */
  51117. idx += OPAQUE32_LEN;
  51118. buf[idx] = nonceLength; /* nonce length */
  51119. idx++;
  51120. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  51121. idx += nonceLength;
  51122. tmp = 1; /* ticket len */
  51123. c16toa((word16)tmp, buf+idx);
  51124. idx += 2;
  51125. buf[idx] = 0xFF; /* ticket */
  51126. idx++;
  51127. tmp = 0; /* ext len */
  51128. c16toa((word16)tmp, buf+idx);
  51129. idx += 2;
  51130. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  51131. handshake, 0, 0, 0);
  51132. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  51133. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  51134. return !(ret == sz);
  51135. }
  51136. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  51137. {
  51138. int i;
  51139. if (sess == NULL)
  51140. return -1;
  51141. if (sess->ticketNonce.len != len)
  51142. return -1;
  51143. for (i = 0; i < len; i++)
  51144. if (sess->ticketNonce.data[i] != len)
  51145. return -1;
  51146. return 0;
  51147. }
  51148. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  51149. {
  51150. char *buf[1024];
  51151. int ret;
  51152. ret = send_new_session_ticket(ssl_s, len, len);
  51153. if (ret != 0)
  51154. return -1;
  51155. ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
  51156. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  51157. return -1;
  51158. return test_ticket_nonce_check(ssl_c->session, len);
  51159. }
  51160. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  51161. {
  51162. WOLFSSL_SESSION *sess, *cached;
  51163. WOLFSSL_CTX *ctx;
  51164. int ret;
  51165. ctx = ssl_c->ctx;
  51166. ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
  51167. if (ret != 0)
  51168. return -1;
  51169. sess = wolfSSL_get1_session(ssl_c);
  51170. if (sess == NULL)
  51171. return -1;
  51172. ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  51173. NULL, ssl_c->options.side, 1,NULL);
  51174. if (ret != 0)
  51175. return -1;
  51176. cached = wolfSSL_SESSION_new();
  51177. if (cached == NULL)
  51178. return -1;
  51179. ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
  51180. if (ret != WOLFSSL_SUCCESS)
  51181. return -1;
  51182. ret = test_ticket_nonce_check(cached, len);
  51183. if (ret != 0)
  51184. return -1;
  51185. wolfSSL_SESSION_free(cached);
  51186. wolfSSL_SESSION_free(sess);
  51187. return 0;
  51188. }
  51189. static int test_ticket_nonce_malloc(void)
  51190. {
  51191. struct test_memio_ctx test_ctx;
  51192. WOLFSSL_CTX *ctx_c, *ctx_s;
  51193. byte small, medium, big;
  51194. WOLFSSL *ssl_c, *ssl_s;
  51195. int ret;
  51196. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  51197. ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  51198. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
  51199. if (ret != 0)
  51200. return -1;
  51201. /* will send ticket manually */
  51202. wolfSSL_no_ticket_TLSv13(ssl_s);
  51203. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  51204. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  51205. while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
  51206. ret = wolfSSL_connect(ssl_c);
  51207. if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
  51208. return -2;
  51209. ret = wolfSSL_accept(ssl_s);
  51210. if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
  51211. return -3;
  51212. }
  51213. small = TLS13_TICKET_NONCE_STATIC_SZ;
  51214. medium = small + 20 <= 255 ? small + 20 : 255;
  51215. big = medium + 20 <= 255 ? small + 20 : 255;
  51216. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  51217. return -1;
  51218. if (ssl_c->session->ticketNonce.data !=
  51219. ssl_c->session->ticketNonce.dataStatic)
  51220. return -1;
  51221. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  51222. return -1;
  51223. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
  51224. return -1;
  51225. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
  51226. return -5;
  51227. if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
  51228. return -6;
  51229. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  51230. return -1;
  51231. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  51232. return -1;
  51233. if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
  51234. return -1;
  51235. if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
  51236. return -1;
  51237. if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
  51238. return -1;
  51239. wolfSSL_free(ssl_c);
  51240. wolfSSL_free(ssl_s);
  51241. wolfSSL_CTX_free(ctx_c);
  51242. wolfSSL_CTX_free(ctx_s);
  51243. return 0;
  51244. }
  51245. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  51246. /*----------------------------------------------------------------------------*
  51247. | Main
  51248. *----------------------------------------------------------------------------*/
  51249. typedef int (*TEST_FUNC)(void);
  51250. typedef struct {
  51251. const char *name;
  51252. TEST_FUNC func;
  51253. } TEST_CASE;
  51254. #define TEST_DECL(func) { #func, func }
  51255. TEST_CASE testCases[] = {
  51256. TEST_DECL(test_fileAccess),
  51257. TEST_DECL(test_wolfSSL_Init),
  51258. TEST_DECL(test_wolfSSL_Method_Allocators),
  51259. #ifndef NO_WOLFSSL_SERVER
  51260. TEST_DECL(test_wolfSSL_CTX_new),
  51261. #endif
  51262. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  51263. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  51264. TEST_DECL(test_for_double_Free),
  51265. #endif
  51266. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  51267. TEST_DECL(test_wolfSSL_get_finished),
  51268. TEST_DECL(test_wolfSSL_CTX_add_session),
  51269. #endif
  51270. TEST_DECL(test_SSL_CIPHER_get_xxx),
  51271. TEST_DECL(test_wolfSSL_ERR_strings),
  51272. TEST_DECL(test_wolfSSL_EVP_shake128),
  51273. TEST_DECL(test_wolfSSL_EVP_shake256),
  51274. TEST_DECL(test_EVP_blake2),
  51275. TEST_DECL(test_EVP_MD_do_all),
  51276. TEST_DECL(test_OBJ_NAME_do_all),
  51277. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  51278. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  51279. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  51280. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  51281. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  51282. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  51283. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  51284. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  51285. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  51286. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  51287. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  51288. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  51289. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  51290. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  51291. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  51292. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  51293. TEST_DECL(test_wolfSSL_FPKI),
  51294. TEST_DECL(test_wolfSSL_CertRsaPss),
  51295. TEST_DECL(test_wolfSSL_CertManagerCRL),
  51296. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  51297. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  51298. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  51299. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  51300. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  51301. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  51302. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  51303. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  51304. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  51305. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  51306. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  51307. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  51308. TEST_DECL(test_server_wolfSSL_new),
  51309. TEST_DECL(test_client_wolfSSL_new),
  51310. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  51311. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  51312. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  51313. TEST_DECL(test_SetTmpEC_DHE_Sz),
  51314. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  51315. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  51316. TEST_DECL(test_wolfSSL_dtls_plaintext),
  51317. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  51318. defined(HAVE_IO_TESTS_DEPENDENCIES)
  51319. TEST_DECL(test_wolfSSL_read_write),
  51320. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
  51321. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  51322. #endif
  51323. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
  51324. TEST_DECL(test_wolfSSL_dtls_export),
  51325. TEST_DECL(test_wolfSSL_tls_export),
  51326. #endif
  51327. TEST_DECL(test_wolfSSL_SetMinVersion),
  51328. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  51329. /* TLS extensions tests */
  51330. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  51331. TEST_DECL(test_wolfSSL_UseSNI),
  51332. #endif
  51333. TEST_DECL(test_wolfSSL_UseTrustedCA),
  51334. TEST_DECL(test_wolfSSL_UseMaxFragment),
  51335. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  51336. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  51337. TEST_DECL(test_wolfSSL_UseALPN),
  51338. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  51339. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  51340. /* X509 tests */
  51341. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  51342. TEST_DECL(test_wolfSSL_PKCS12),
  51343. TEST_DECL(test_wolfSSL_no_password_cb),
  51344. TEST_DECL(test_wolfSSL_PKCS8),
  51345. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  51346. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  51347. TEST_DECL(test_wolfSSL_PKCS5),
  51348. TEST_DECL(test_wolfSSL_URI),
  51349. TEST_DECL(test_wolfSSL_TBS),
  51350. TEST_DECL(test_wolfSSL_X509_verify),
  51351. TEST_DECL(test_wolfSSL_X509_TLS_version),
  51352. TEST_DECL(test_wc_PemToDer),
  51353. TEST_DECL(test_wc_AllocDer),
  51354. TEST_DECL(test_wc_CertPemToDer),
  51355. TEST_DECL(test_wc_PubKeyPemToDer),
  51356. TEST_DECL(test_wc_PemPubKeyToDer),
  51357. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  51358. TEST_DECL(test_wc_CheckCertSigPubKey),
  51359. /* OCSP Stapling */
  51360. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  51361. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  51362. /* Multicast */
  51363. TEST_DECL(test_wolfSSL_mcast),
  51364. /* compatibility tests */
  51365. TEST_DECL(test_wolfSSL_lhash),
  51366. TEST_DECL(test_wolfSSL_X509_NAME),
  51367. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  51368. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  51369. #ifndef NO_BIO
  51370. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  51371. TEST_DECL(test_wolfSSL_X509_INFO),
  51372. #endif
  51373. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  51374. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  51375. TEST_DECL(test_wolfSSL_X509_check_host),
  51376. TEST_DECL(test_wolfSSL_X509_check_email),
  51377. TEST_DECL(test_wolfSSL_DES),
  51378. TEST_DECL(test_wolfSSL_certs),
  51379. TEST_DECL(test_wolfSSL_X509_check_private_key),
  51380. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  51381. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  51382. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  51383. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  51384. TEST_DECL(test_wolfSSL_private_keys),
  51385. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  51386. #ifndef NO_BIO
  51387. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  51388. #endif
  51389. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  51390. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  51391. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  51392. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  51393. #ifndef NO_BIO
  51394. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  51395. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  51396. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  51397. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  51398. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  51399. #endif
  51400. TEST_DECL(test_DSA_do_sign_verify),
  51401. TEST_DECL(test_wolfSSL_tmp_dh),
  51402. TEST_DECL(test_wolfSSL_ctrl),
  51403. TEST_DECL(test_wolfSSL_EVP_MD_size),
  51404. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  51405. TEST_DECL(test_wolfSSL_EVP_Digest),
  51406. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  51407. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  51408. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  51409. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  51410. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  51411. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  51412. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  51413. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  51414. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  51415. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  51416. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  51417. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  51418. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  51419. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  51420. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  51421. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  51422. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  51423. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  51424. #endif
  51425. #ifndef NO_BIO
  51426. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  51427. TEST_DECL(test_wolfSSL_GetLoggingCb),
  51428. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  51429. TEST_DECL(test_wc_ERR_remove_state),
  51430. TEST_DECL(test_wc_ERR_print_errors_fp),
  51431. #endif
  51432. TEST_DECL(test_wolfSSL_set_options),
  51433. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  51434. TEST_DECL(test_wolfSSL_set1_curves_list),
  51435. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  51436. TEST_DECL(test_wolfSSL_PKCS7_certs),
  51437. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  51438. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  51439. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  51440. TEST_DECL(test_wolfSSL_msgCb),
  51441. TEST_DECL(test_wolfSSL_either_side),
  51442. TEST_DECL(test_wolfSSL_DTLS_either_side),
  51443. TEST_DECL(test_wolfSSL_dtls_fragments),
  51444. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  51445. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  51446. TEST_DECL(test_wolfSSL_dtls_bad_record),
  51447. TEST_DECL(test_wolfSSL_dtls_stateless),
  51448. TEST_DECL(test_generate_cookie),
  51449. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  51450. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  51451. TEST_DECL(test_wolfSSL_X509_Name_canon),
  51452. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  51453. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  51454. TEST_DECL(test_wolfSSL_X509_NID),
  51455. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  51456. TEST_DECL(test_wolfSSL_get0_param),
  51457. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  51458. TEST_DECL(test_wolfSSL_set1_host),
  51459. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  51460. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  51461. TEST_DECL(test_wolfSSL_X509_STORE),
  51462. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  51463. TEST_DECL(test_X509_STORE_get0_objects),
  51464. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  51465. TEST_DECL(test_wolfSSL_BN),
  51466. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  51467. #ifndef NO_BIO
  51468. TEST_DECL(test_wolfSSL_PEM_read_bio),
  51469. TEST_DECL(test_wolfSSL_BIO),
  51470. #endif
  51471. TEST_DECL(test_wolfSSL_ASN1_STRING),
  51472. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  51473. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  51474. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  51475. TEST_DECL(test_wolfSSL_X509),
  51476. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  51477. TEST_DECL(test_wolfSSL_X509_sign),
  51478. TEST_DECL(test_wolfSSL_X509_sign2),
  51479. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  51480. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  51481. #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51482. TEST_DECL(test_wolfSSL_check_domain),
  51483. #endif
  51484. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  51485. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  51486. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  51487. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  51488. TEST_DECL(test_wolfSSL_RAND),
  51489. TEST_DECL(test_wolfSSL_BUF),
  51490. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  51491. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  51492. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  51493. TEST_DECL(test_wolfSSL_X509_cmp_time),
  51494. TEST_DECL(test_wolfSSL_X509_time_adj),
  51495. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  51496. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  51497. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  51498. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  51499. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  51500. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  51501. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  51502. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  51503. TEST_DECL(test_wolfSSL_Tls13_postauth),
  51504. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  51505. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  51506. TEST_DECL(test_wolfSSL_THREADID_hash),
  51507. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  51508. TEST_DECL(test_wolfSSL_RAND_bytes),
  51509. TEST_DECL(test_wolfSSL_BN_rand),
  51510. TEST_DECL(test_wolfSSL_pseudo_rand),
  51511. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  51512. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  51513. TEST_DECL(test_error_queue_per_thread),
  51514. TEST_DECL(test_wolfSSL_ERR_put_error),
  51515. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  51516. #ifndef NO_BIO
  51517. TEST_DECL(test_wolfSSL_ERR_print_errors),
  51518. #endif
  51519. TEST_DECL(test_wolfSSL_HMAC),
  51520. TEST_DECL(test_wolfSSL_CMAC),
  51521. TEST_DECL(test_wolfSSL_OBJ),
  51522. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  51523. TEST_DECL(test_wolfSSL_OBJ_cmp),
  51524. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  51525. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  51526. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  51527. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  51528. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  51529. TEST_DECL(test_wolfSSL_X509_set_name),
  51530. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  51531. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  51532. TEST_DECL(test_wolfSSL_X509_set_version),
  51533. #ifndef NO_BIO
  51534. TEST_DECL(test_wolfSSL_BIO_gets),
  51535. TEST_DECL(test_wolfSSL_BIO_puts),
  51536. TEST_DECL(test_wolfSSL_BIO_dump),
  51537. TEST_DECL(test_wolfSSL_BIO_should_retry),
  51538. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  51539. TEST_DECL(test_wolfSSL_BIO_write),
  51540. TEST_DECL(test_wolfSSL_BIO_connect),
  51541. TEST_DECL(test_wolfSSL_BIO_accept),
  51542. TEST_DECL(test_wolfSSL_BIO_printf),
  51543. TEST_DECL(test_wolfSSL_BIO_f_md),
  51544. TEST_DECL(test_wolfSSL_BIO_up_ref),
  51545. TEST_DECL(test_wolfSSL_BIO_tls),
  51546. #endif
  51547. TEST_DECL(test_wolfSSL_cert_cb),
  51548. TEST_DECL(test_wolfSSL_SESSION),
  51549. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  51550. TEST_DECL(test_wolfSSL_ticket_keys),
  51551. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  51552. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  51553. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  51554. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  51555. TEST_DECL(test_wolfSSL_MD4),
  51556. TEST_DECL(test_wolfSSL_verify_mode),
  51557. TEST_DECL(test_wolfSSL_verify_depth),
  51558. TEST_DECL(test_wolfSSL_HMAC_CTX),
  51559. TEST_DECL(test_wolfSSL_msg_callback),
  51560. TEST_DECL(test_wolfSSL_SHA),
  51561. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  51562. TEST_DECL(test_wolfSSL_MD5),
  51563. TEST_DECL(test_wolfSSL_MD5_Transform),
  51564. TEST_DECL(test_wolfSSL_SHA_Transform),
  51565. TEST_DECL(test_wolfSSL_SHA256),
  51566. TEST_DECL(test_wolfSSL_SHA256_Transform),
  51567. TEST_DECL(test_wolfSSL_SHA224),
  51568. TEST_DECL(test_wolfSSL_SHA512_Transform),
  51569. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  51570. TEST_DECL(test_wolfSSL_X509_CRL),
  51571. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  51572. TEST_DECL(test_wolfSSL_PEM_read_X509),
  51573. TEST_DECL(test_wolfSSL_PEM_read),
  51574. #ifndef NO_BIO
  51575. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  51576. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  51577. #endif
  51578. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  51579. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  51580. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  51581. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  51582. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  51583. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  51584. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  51585. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  51586. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  51587. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  51588. TEST_DECL(test_wolfSSL_X509_check_ca),
  51589. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  51590. TEST_DECL(test_wolfSSL_make_cert),
  51591. TEST_DECL(test_wolfSSL_DES_ncbc),
  51592. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  51593. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  51594. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  51595. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  51596. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  51597. TEST_DECL(test_wolfssl_EVP_chacha20),
  51598. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  51599. TEST_DECL(test_wolfSSL_PKEY_up_ref),
  51600. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  51601. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  51602. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  51603. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  51604. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  51605. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  51606. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  51607. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  51608. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  51609. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  51610. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  51611. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  51612. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  51613. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  51614. TEST_DECL(test_CONF_modules_xxx),
  51615. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  51616. TEST_DECL(test_CRYPTO_THREADID_xxx),
  51617. TEST_DECL(test_ENGINE_cleanup),
  51618. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  51619. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  51620. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  51621. #ifdef OPENSSL_ALL
  51622. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  51623. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  51624. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  51625. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  51626. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  51627. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  51628. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  51629. TEST_DECL(test_wolfSSL_DSA_SIG),
  51630. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  51631. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  51632. TEST_DECL(test_wolfSSL_CTX_ctrl),
  51633. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  51634. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  51635. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  51636. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  51637. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  51638. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  51639. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  51640. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  51641. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  51642. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  51643. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  51644. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
  51645. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  51646. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  51647. TEST_DECL(test_wolfSSL_EVP_rc4),
  51648. TEST_DECL(test_wolfSSL_EVP_enc_null),
  51649. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  51650. TEST_DECL(test_wolfSSL_EVP_mdc2),
  51651. TEST_DECL(test_wolfSSL_EVP_md4),
  51652. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  51653. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  51654. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  51655. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  51656. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  51657. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  51658. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  51659. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  51660. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  51661. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  51662. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  51663. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  51664. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  51665. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  51666. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  51667. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  51668. TEST_DECL(test_evp_cipher_aes_gcm),
  51669. TEST_DECL(test_wolfSSL_OBJ_ln),
  51670. TEST_DECL(test_wolfSSL_OBJ_sn),
  51671. TEST_DECL(test_wolfSSL_TXT_DB),
  51672. TEST_DECL(test_wolfSSL_NCONF),
  51673. #endif /* OPENSSL_ALL */
  51674. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  51675. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  51676. #ifndef NO_BIO
  51677. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  51678. #endif /* !NO_BIO */
  51679. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  51680. TEST_DECL(test_wolfSSL_X509_CA_num),
  51681. TEST_DECL(test_wolfSSL_X509_get_version),
  51682. #ifndef NO_BIO
  51683. TEST_DECL(test_wolfSSL_X509_print),
  51684. TEST_DECL(test_wolfSSL_X509_CRL_print),
  51685. TEST_DECL(test_wolfSSL_BIO_get_len),
  51686. #endif
  51687. TEST_DECL(test_wolfSSL_RSA),
  51688. TEST_DECL(test_wolfSSL_RSA_DER),
  51689. TEST_DECL(test_wolfSSL_RSA_print),
  51690. #ifndef NO_RSA
  51691. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  51692. #endif
  51693. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  51694. TEST_DECL(test_wolfSSL_RSA_get0_key),
  51695. TEST_DECL(test_wolfSSL_RSA_meth),
  51696. TEST_DECL(test_wolfSSL_RSA_verify),
  51697. TEST_DECL(test_wolfSSL_RSA_sign),
  51698. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  51699. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  51700. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  51701. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  51702. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  51703. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  51704. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  51705. TEST_DECL(test_wolfSSL_RSA_ex_data),
  51706. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  51707. TEST_DECL(test_wolfSSL_RSA_To_Der),
  51708. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  51709. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  51710. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  51711. TEST_DECL(test_wolfSSL_DH),
  51712. TEST_DECL(test_wolfSSL_DH_dup),
  51713. TEST_DECL(test_wolfSSL_DH_check),
  51714. TEST_DECL(test_wolfSSL_DH_prime),
  51715. TEST_DECL(test_wolfSSL_DH_1536_prime),
  51716. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  51717. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  51718. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  51719. TEST_DECL(test_wolfSSL_d2i_DHparams),
  51720. TEST_DECL(test_wolfSSL_DH_LoadDer),
  51721. TEST_DECL(test_wolfSSL_i2d_DHparams),
  51722. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  51723. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  51724. TEST_DECL(test_wolfSSL_X509V3_EXT),
  51725. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  51726. TEST_DECL(test_wolfSSL_X509_get_ext),
  51727. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  51728. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  51729. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  51730. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  51731. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  51732. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  51733. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  51734. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  51735. TEST_DECL(test_wolfSSL_X509_cmp),
  51736. #ifndef NO_BIO
  51737. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  51738. #endif
  51739. TEST_DECL(test_wolfSSL_ASN1_get_object),
  51740. TEST_DECL(test_openssl_generate_key_and_cert),
  51741. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  51742. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  51743. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  51744. /* test the no op functions for compatibility */
  51745. TEST_DECL(test_no_op_functions),
  51746. /* OpenSSL EVP_PKEY API tests */
  51747. TEST_DECL(test_EVP_PKEY_rsa),
  51748. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  51749. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
  51750. TEST_DECL(test_EVP_PKEY_ec),
  51751. TEST_DECL(test_EVP_PKEY_cmp),
  51752. /* OpenSSL error API tests */
  51753. TEST_DECL(test_ERR_load_crypto_strings),
  51754. /* OpenSSL sk_X509 API test */
  51755. TEST_DECL(test_sk_X509),
  51756. /* OpenSSL sk_X509_CRL API test */
  51757. TEST_DECL(test_sk_X509_CRL),
  51758. /* OpenSSL X509 API test */
  51759. TEST_DECL(test_X509_get_signature_nid),
  51760. /* OpenSSL X509 REQ API test */
  51761. TEST_DECL(test_X509_REQ),
  51762. /* OpenSSL PKCS7 API test */
  51763. TEST_DECL(test_wolfssl_PKCS7),
  51764. TEST_DECL(test_wolfSSL_PKCS7_sign),
  51765. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  51766. #ifndef NO_BIO
  51767. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  51768. #ifdef HAVE_SMIME
  51769. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  51770. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  51771. #endif /* HAVE_SMIME */
  51772. #endif /* !NO_BIO */
  51773. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  51774. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  51775. TEST_DECL(test_X509_LOOKUP_add_dir),
  51776. /* wolfCrypt ASN tests */
  51777. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  51778. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  51779. TEST_DECL(test_wc_SetSubjectRaw),
  51780. TEST_DECL(test_wc_GetSubjectRaw),
  51781. TEST_DECL(test_wc_SetIssuerRaw),
  51782. TEST_DECL(test_wc_SetIssueBuffer),
  51783. TEST_DECL(test_wc_SetSubjectKeyId),
  51784. TEST_DECL(test_wc_SetSubject),
  51785. TEST_DECL(test_CheckCertSignature),
  51786. TEST_DECL(test_wc_ParseCert),
  51787. TEST_DECL(test_MakeCertWithPathLen),
  51788. /* wolfCrypt ECC tests */
  51789. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  51790. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  51791. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  51792. #ifdef WOLFSSL_TLS13
  51793. /* TLS v1.3 API tests */
  51794. TEST_DECL(test_tls13_apis),
  51795. TEST_DECL(test_tls13_cipher_suites),
  51796. #endif
  51797. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  51798. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  51799. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  51800. /* Bad certificate signature tests */
  51801. TEST_DECL(test_EccSigFailure_cm),
  51802. TEST_DECL(test_RsaSigFailure_cm),
  51803. #endif /* NO_CERTS */
  51804. #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
  51805. !defined(NO_OLD_TLS))
  51806. TEST_DECL(test_DhCallbacks),
  51807. #endif
  51808. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  51809. TEST_DECL(test_export_keying_material),
  51810. #endif
  51811. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  51812. TEST_DECL(test_wolfSSL_security_level),
  51813. TEST_DECL(test_wolfSSL_SSL_in_init),
  51814. TEST_DECL(test_wolfSSL_EC_curve),
  51815. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  51816. TEST_DECL(test_wolfSSL_OpenSSL_version),
  51817. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  51818. TEST_DECL(test_CONF_CTX_FILE),
  51819. TEST_DECL(test_CONF_CTX_CMDLINE),
  51820. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  51821. /* wolfcrypt */
  51822. TEST_DECL(test_wolfCrypt_Init),
  51823. TEST_DECL(test_wc_InitMd5),
  51824. TEST_DECL(test_wc_Md5Update),
  51825. TEST_DECL(test_wc_Md5Final),
  51826. TEST_DECL(test_wc_InitSha),
  51827. TEST_DECL(test_wc_ShaUpdate),
  51828. TEST_DECL(test_wc_ShaFinal),
  51829. TEST_DECL(test_wc_InitSha256),
  51830. TEST_DECL(test_wc_Sha256Update),
  51831. TEST_DECL(test_wc_Sha256Final),
  51832. TEST_DECL(test_wc_Sha256FinalRaw),
  51833. TEST_DECL(test_wc_Sha256GetFlags),
  51834. TEST_DECL(test_wc_Sha256Free),
  51835. TEST_DECL(test_wc_Sha256GetHash),
  51836. TEST_DECL(test_wc_Sha256Copy),
  51837. TEST_DECL(test_wc_InitSha512),
  51838. TEST_DECL(test_wc_Sha512Update),
  51839. TEST_DECL(test_wc_Sha512Final),
  51840. TEST_DECL(test_wc_Sha512GetFlags),
  51841. TEST_DECL(test_wc_Sha512FinalRaw),
  51842. TEST_DECL(test_wc_Sha512Free),
  51843. TEST_DECL(test_wc_Sha512GetHash),
  51844. TEST_DECL(test_wc_Sha512Copy),
  51845. TEST_DECL(test_wc_InitSha512_224),
  51846. TEST_DECL(test_wc_Sha512_224Update),
  51847. TEST_DECL(test_wc_Sha512_224Final),
  51848. TEST_DECL(test_wc_Sha512_224GetFlags),
  51849. TEST_DECL(test_wc_Sha512_224FinalRaw),
  51850. TEST_DECL(test_wc_Sha512_224Free),
  51851. TEST_DECL(test_wc_Sha512_224GetHash),
  51852. TEST_DECL(test_wc_Sha512_224Copy),
  51853. TEST_DECL(test_wc_InitSha512_256),
  51854. TEST_DECL(test_wc_Sha512_256Update),
  51855. TEST_DECL(test_wc_Sha512_256Final),
  51856. TEST_DECL(test_wc_Sha512_256GetFlags),
  51857. TEST_DECL(test_wc_Sha512_256FinalRaw),
  51858. TEST_DECL(test_wc_Sha512_256Free),
  51859. TEST_DECL(test_wc_Sha512_256GetHash),
  51860. TEST_DECL(test_wc_Sha512_256Copy),
  51861. TEST_DECL(test_wc_InitSha384),
  51862. TEST_DECL(test_wc_Sha384Update),
  51863. TEST_DECL(test_wc_Sha384Final),
  51864. TEST_DECL(test_wc_Sha384GetFlags),
  51865. TEST_DECL(test_wc_Sha384FinalRaw),
  51866. TEST_DECL(test_wc_Sha384Free),
  51867. TEST_DECL(test_wc_Sha384GetHash),
  51868. TEST_DECL(test_wc_Sha384Copy),
  51869. TEST_DECL(test_wc_InitSha224),
  51870. TEST_DECL(test_wc_Sha224Update),
  51871. TEST_DECL(test_wc_Sha224Final),
  51872. TEST_DECL(test_wc_Sha224SetFlags),
  51873. TEST_DECL(test_wc_Sha224GetFlags),
  51874. TEST_DECL(test_wc_Sha224Free),
  51875. TEST_DECL(test_wc_Sha224GetHash),
  51876. TEST_DECL(test_wc_Sha224Copy),
  51877. TEST_DECL(test_wc_InitBlake2b),
  51878. TEST_DECL(test_wc_InitBlake2b_WithKey),
  51879. TEST_DECL(test_wc_InitBlake2s_WithKey),
  51880. TEST_DECL(test_wc_InitRipeMd),
  51881. TEST_DECL(test_wc_RipeMdUpdate),
  51882. TEST_DECL(test_wc_RipeMdFinal),
  51883. TEST_DECL(test_wc_InitSha3),
  51884. TEST_DECL(testing_wc_Sha3_Update),
  51885. TEST_DECL(test_wc_Sha3_224_Final),
  51886. TEST_DECL(test_wc_Sha3_256_Final),
  51887. TEST_DECL(test_wc_Sha3_384_Final),
  51888. TEST_DECL(test_wc_Sha3_512_Final),
  51889. TEST_DECL(test_wc_Sha3_224_Copy),
  51890. TEST_DECL(test_wc_Sha3_256_Copy),
  51891. TEST_DECL(test_wc_Sha3_384_Copy),
  51892. TEST_DECL(test_wc_Sha3_512_Copy),
  51893. TEST_DECL(test_wc_Sha3_GetFlags),
  51894. TEST_DECL(test_wc_InitShake256),
  51895. TEST_DECL(testing_wc_Shake256_Update),
  51896. TEST_DECL(test_wc_Shake256_Final),
  51897. TEST_DECL(test_wc_Shake256_Copy),
  51898. TEST_DECL(test_wc_Shake256Hash),
  51899. TEST_DECL(test_wc_Md5HmacSetKey),
  51900. TEST_DECL(test_wc_Md5HmacUpdate),
  51901. TEST_DECL(test_wc_Md5HmacFinal),
  51902. TEST_DECL(test_wc_ShaHmacSetKey),
  51903. TEST_DECL(test_wc_ShaHmacUpdate),
  51904. TEST_DECL(test_wc_ShaHmacFinal),
  51905. TEST_DECL(test_wc_Sha224HmacSetKey),
  51906. TEST_DECL(test_wc_Sha224HmacUpdate),
  51907. TEST_DECL(test_wc_Sha224HmacFinal),
  51908. TEST_DECL(test_wc_Sha256HmacSetKey),
  51909. TEST_DECL(test_wc_Sha256HmacUpdate),
  51910. TEST_DECL(test_wc_Sha256HmacFinal),
  51911. TEST_DECL(test_wc_Sha384HmacSetKey),
  51912. TEST_DECL(test_wc_Sha384HmacUpdate),
  51913. TEST_DECL(test_wc_Sha384HmacFinal),
  51914. TEST_DECL(test_wc_HashInit),
  51915. TEST_DECL(test_wc_HashSetFlags),
  51916. TEST_DECL(test_wc_HashGetFlags),
  51917. TEST_DECL(test_wc_InitCmac),
  51918. TEST_DECL(test_wc_CmacUpdate),
  51919. TEST_DECL(test_wc_CmacFinal),
  51920. TEST_DECL(test_wc_AesCmacGenerate),
  51921. TEST_DECL(test_wc_AesGcmStream),
  51922. TEST_DECL(test_wc_Des3_SetIV),
  51923. TEST_DECL(test_wc_Des3_SetKey),
  51924. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  51925. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  51926. TEST_DECL(test_wc_Des3_EcbEncrypt),
  51927. TEST_DECL(test_wc_Chacha_SetKey),
  51928. TEST_DECL(test_wc_Chacha_Process),
  51929. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  51930. TEST_DECL(test_wc_Poly1305SetKey),
  51931. TEST_DECL(test_wc_CamelliaSetKey),
  51932. TEST_DECL(test_wc_CamelliaSetIV),
  51933. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  51934. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  51935. TEST_DECL(test_wc_Arc4SetKey),
  51936. TEST_DECL(test_wc_Arc4Process),
  51937. TEST_DECL(test_wc_Rc2SetKey),
  51938. TEST_DECL(test_wc_Rc2SetIV),
  51939. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  51940. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  51941. TEST_DECL(test_wc_AesSetKey),
  51942. TEST_DECL(test_wc_AesSetIV),
  51943. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  51944. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  51945. TEST_DECL(test_wc_AesGcmSetKey),
  51946. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  51947. TEST_DECL(test_wc_GmacSetKey),
  51948. TEST_DECL(test_wc_GmacUpdate),
  51949. TEST_DECL(test_wc_InitRsaKey),
  51950. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  51951. TEST_DECL(test_wc_RsaPublicKeyDecode),
  51952. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  51953. TEST_DECL(test_wc_MakeRsaKey),
  51954. TEST_DECL(test_wc_SetKeyUsage),
  51955. TEST_DECL(test_wc_CheckProbablePrime),
  51956. TEST_DECL(test_wc_RsaPSS_Verify),
  51957. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  51958. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  51959. TEST_DECL(test_wc_SetMutexCb),
  51960. TEST_DECL(test_wc_LockMutex_ex),
  51961. TEST_DECL(test_wc_RsaKeyToDer),
  51962. TEST_DECL(test_wc_RsaKeyToPublicDer),
  51963. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  51964. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  51965. TEST_DECL(test_wc_RsaEncryptSize),
  51966. TEST_DECL(test_wc_RsaSSL_SignVerify),
  51967. TEST_DECL(test_wc_RsaFlattenPublicKey),
  51968. TEST_DECL(test_RsaDecryptBoundsCheck),
  51969. TEST_DECL(test_wc_AesCcmSetKey),
  51970. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  51971. TEST_DECL(test_wc_InitDsaKey),
  51972. TEST_DECL(test_wc_DsaSignVerify),
  51973. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  51974. TEST_DECL(test_wc_MakeDsaKey),
  51975. TEST_DECL(test_wc_DsaKeyToDer),
  51976. TEST_DECL(test_wc_DsaKeyToPublicDer),
  51977. TEST_DECL(test_wc_DsaImportParamsRaw),
  51978. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  51979. TEST_DECL(test_wc_DsaExportParamsRaw),
  51980. TEST_DECL(test_wc_DsaExportKeyRaw),
  51981. TEST_DECL(test_wc_SignatureGetSize_ecc),
  51982. TEST_DECL(test_wc_SignatureGetSize_rsa),
  51983. /*
  51984. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  51985. * avoid memory leaks.
  51986. */
  51987. TEST_DECL(test_wolfCrypt_Cleanup),
  51988. #ifdef OPENSSL_EXTRA
  51989. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  51990. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  51991. TEST_DECL(test_wolfSSL_EC),
  51992. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  51993. TEST_DECL(test_ECDSA_size_sign),
  51994. TEST_DECL(test_ED25519),
  51995. TEST_DECL(test_ED448),
  51996. TEST_DECL(test_EC_i2d),
  51997. #endif
  51998. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  51999. !defined(HAVE_SELFTEST) && \
  52000. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  52001. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  52002. #endif
  52003. #ifdef HAVE_HASHDRBG
  52004. #ifdef TEST_RESEED_INTERVAL
  52005. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  52006. #endif
  52007. TEST_DECL(test_wc_RNG_GenerateBlock),
  52008. #endif
  52009. TEST_DECL(test_get_rand_digit),
  52010. TEST_DECL(test_get_digit_count),
  52011. TEST_DECL(test_mp_cond_copy),
  52012. TEST_DECL(test_mp_rand),
  52013. TEST_DECL(test_get_digit),
  52014. TEST_DECL(test_wc_export_int),
  52015. TEST_DECL(test_wc_InitRngNonce),
  52016. TEST_DECL(test_wc_InitRngNonce_ex),
  52017. TEST_DECL(test_wc_ed25519_make_key),
  52018. TEST_DECL(test_wc_ed25519_init),
  52019. TEST_DECL(test_wc_ed25519_sign_msg),
  52020. TEST_DECL(test_wc_ed25519_import_public),
  52021. TEST_DECL(test_wc_ed25519_import_private_key),
  52022. TEST_DECL(test_wc_ed25519_export),
  52023. TEST_DECL(test_wc_ed25519_size),
  52024. TEST_DECL(test_wc_ed25519_exportKey),
  52025. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  52026. TEST_DECL(test_wc_curve25519_init),
  52027. TEST_DECL(test_wc_curve25519_size),
  52028. TEST_DECL(test_wc_curve25519_export_key_raw),
  52029. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  52030. TEST_DECL(test_wc_curve25519_make_key),
  52031. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  52032. TEST_DECL(test_wc_curve25519_make_pub),
  52033. TEST_DECL(test_wc_curve25519_export_public_ex),
  52034. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  52035. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  52036. TEST_DECL(test_wc_curve25519_import_private),
  52037. TEST_DECL(test_wc_ed448_make_key),
  52038. TEST_DECL(test_wc_ed448_init),
  52039. TEST_DECL(test_wc_ed448_sign_msg),
  52040. TEST_DECL(test_wc_ed448_import_public),
  52041. TEST_DECL(test_wc_ed448_import_private_key),
  52042. TEST_DECL(test_wc_ed448_export),
  52043. TEST_DECL(test_wc_ed448_size),
  52044. TEST_DECL(test_wc_ed448_exportKey),
  52045. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  52046. TEST_DECL(test_wc_curve448_make_key),
  52047. TEST_DECL(test_wc_curve448_shared_secret_ex),
  52048. TEST_DECL(test_wc_curve448_export_public_ex),
  52049. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  52050. TEST_DECL(test_wc_curve448_export_key_raw),
  52051. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  52052. TEST_DECL(test_wc_curve448_import_private),
  52053. TEST_DECL(test_wc_curve448_init),
  52054. TEST_DECL(test_wc_curve448_size),
  52055. TEST_DECL(test_wc_ecc_make_key),
  52056. TEST_DECL(test_wc_ecc_init),
  52057. TEST_DECL(test_wc_ecc_check_key),
  52058. TEST_DECL(test_wc_ecc_get_generator),
  52059. TEST_DECL(test_wc_ecc_size),
  52060. TEST_DECL(test_wc_ecc_params),
  52061. TEST_DECL(test_wc_ecc_signVerify_hash),
  52062. TEST_DECL(test_wc_ecc_shared_secret),
  52063. TEST_DECL(test_wc_ecc_export_x963),
  52064. TEST_DECL(test_wc_ecc_export_x963_ex),
  52065. TEST_DECL(test_wc_ecc_import_x963),
  52066. TEST_DECL(ecc_import_private_key),
  52067. TEST_DECL(test_wc_ecc_export_private_only),
  52068. TEST_DECL(test_wc_ecc_rs_to_sig),
  52069. TEST_DECL(test_wc_ecc_import_raw),
  52070. TEST_DECL(test_wc_ecc_import_unsigned),
  52071. TEST_DECL(test_wc_ecc_sig_size),
  52072. TEST_DECL(test_wc_ecc_ctx_new),
  52073. TEST_DECL(test_wc_ecc_ctx_reset),
  52074. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  52075. TEST_DECL(test_wc_ecc_ctx_set_info),
  52076. TEST_DECL(test_wc_ecc_encryptDecrypt),
  52077. TEST_DECL(test_wc_ecc_del_point),
  52078. TEST_DECL(test_wc_ecc_pointFns),
  52079. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  52080. TEST_DECL(test_wc_ecc_verify_hash_ex),
  52081. TEST_DECL(test_wc_ecc_mulmod),
  52082. TEST_DECL(test_wc_ecc_is_valid_idx),
  52083. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  52084. TEST_DECL(test_wc_ecc_sig_size_calc),
  52085. TEST_DECL(test_ToTraditional),
  52086. TEST_DECL(test_wc_EccPrivateKeyToDer),
  52087. TEST_DECL(test_wc_DhPublicKeyDecode),
  52088. TEST_DECL(test_wc_Ed25519KeyToDer),
  52089. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  52090. TEST_DECL(test_wc_Ed448KeyToDer),
  52091. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  52092. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  52093. TEST_DECL(test_wc_SetSubjectBuffer),
  52094. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  52095. TEST_DECL(test_wc_PKCS7_New),
  52096. TEST_DECL(test_wc_PKCS7_Init),
  52097. TEST_DECL(test_wc_PKCS7_InitWithCert),
  52098. TEST_DECL(test_wc_PKCS7_EncodeData),
  52099. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  52100. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  52101. TEST_DECL(test_wc_PKCS7_VerifySignedData),
  52102. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  52103. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  52104. TEST_DECL(test_wc_PKCS7_Degenerate),
  52105. TEST_DECL(test_wc_PKCS7_BER),
  52106. TEST_DECL(test_PKCS7_signed_enveloped),
  52107. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  52108. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  52109. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  52110. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  52111. TEST_DECL(test_wc_i2d_PKCS12),
  52112. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  52113. TEST_DECL(test_openssl_FIPS_drbg),
  52114. TEST_DECL(test_wc_CryptoCb),
  52115. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  52116. TEST_DECL(test_wolfSSL_FIPS_mode),
  52117. #ifdef WOLFSSL_DTLS
  52118. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  52119. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  52120. #endif
  52121. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  52122. TEST_DECL(test_ForceZero),
  52123. TEST_DECL(test_wolfSSL_Cleanup),
  52124. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  52125. && defined(WOLFSSL_TLS13) && \
  52126. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  52127. TEST_DECL(test_ticket_nonce_malloc),
  52128. #endif
  52129. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  52130. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  52131. !defined(WOLFSSL_NO_CLIENT_AUTH))
  52132. TEST_DECL(test_various_pathlen_chains),
  52133. #endif
  52134. /* If at some point a stub get implemented this test should fail indicating
  52135. * a need to implement a new test case
  52136. */
  52137. TEST_DECL(test_stubs_are_stubs)
  52138. };
  52139. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  52140. static void TestSetup(void)
  52141. {
  52142. /* Stub, for now. Add common test setup code here. */
  52143. }
  52144. static void TestCleanup(void)
  52145. {
  52146. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  52147. /* Clear any errors added to the error queue during the test run. */
  52148. wolfSSL_ERR_clear_error();
  52149. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  52150. }
  52151. void ApiTest(void)
  52152. {
  52153. int i;
  52154. int ret;
  52155. printf(" Begin API Tests\n");
  52156. fflush(stdout);
  52157. for (i = 0; i < TEST_CASE_CNT; ++i) {
  52158. TestSetup();
  52159. ret = testCases[i].func();
  52160. if (ret != 0) {
  52161. fprintf(stderr, "%s failed.\n", testCases[i].name);
  52162. }
  52163. AssertIntEQ(ret, 0);
  52164. TestCleanup();
  52165. }
  52166. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  52167. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  52168. wc_ecc_fp_free(); /* free per thread cache */
  52169. #endif
  52170. wolfSSL_Cleanup();
  52171. (void)testDevId;
  52172. printf(" End API Tests\n");
  52173. fflush(stdout);
  52174. }